blob: b35a5a85d3b65947b64e1f284f91e9e5b888a45a [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;
375 unsigned int set = 0;
376
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) {
392 set |= 0x55555555;
393 }
394 else if (strcmp(args[cur_arg], "even") == 0) {
395 set |= 0xAAAAAAAA;
396 }
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
410 if (low < 1 || high > 32) {
411 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
412 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200413 return -1;
414 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100415
416 while (low <= high)
417 set |= 1 << (low++ - 1);
418 }
419 else {
420 memprintf(err,
421 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
422 args[0], args[1]);
423 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200424 }
425 cur_arg++;
426 }
427 global.stats_fe->bind_proc = set;
428 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200429 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200430 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200431 return -1;
432 }
433 return 0;
434}
435
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100436/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
437 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100438 * NOTE: Some tools happen to rely on the field position instead of its name,
439 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100440 */
441static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100442{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100443 chunk_appendf(&trash,
444 "# pxname,svname,"
445 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100446 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100447 "bin,bout,"
448 "dreq,dresp,"
449 "ereq,econ,eresp,"
450 "wretr,wredis,"
451 "status,weight,act,bck,"
452 "chkfail,chkdown,lastchg,downtime,qlimit,"
453 "pid,iid,sid,throttle,lbtot,tracked,type,"
454 "rate,rate_lim,rate_max,"
455 "check_status,check_code,check_duration,"
456 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
457 "req_rate,req_rate_max,req_tot,"
458 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100459 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100460 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100461}
462
Simon Hormand9366582011-06-15 15:18:45 +0900463/* print a string of text buffer to <out>. The format is :
464 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
465 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
466 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
467 */
468static int dump_text(struct chunk *out, const char *buf, int bsize)
469{
470 unsigned char c;
471 int ptr = 0;
472
473 while (buf[ptr] && ptr < bsize) {
474 c = buf[ptr];
475 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
476 if (out->len > out->size - 1)
477 break;
478 out->str[out->len++] = c;
479 }
480 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
481 if (out->len > out->size - 2)
482 break;
483 out->str[out->len++] = '\\';
484 switch (c) {
485 case ' ': c = ' '; break;
486 case '\t': c = 't'; break;
487 case '\n': c = 'n'; break;
488 case '\r': c = 'r'; break;
489 case '\e': c = 'e'; break;
490 case '\\': c = '\\'; break;
491 }
492 out->str[out->len++] = c;
493 }
494 else {
495 if (out->len > out->size - 4)
496 break;
497 out->str[out->len++] = '\\';
498 out->str[out->len++] = 'x';
499 out->str[out->len++] = hextab[(c >> 4) & 0xF];
500 out->str[out->len++] = hextab[c & 0xF];
501 }
502 ptr++;
503 }
504
505 return ptr;
506}
507
508/* print a buffer in hexa.
509 * Print stopped if <bsize> is reached, or if no more place in the chunk.
510 */
511static int dump_binary(struct chunk *out, const char *buf, int bsize)
512{
513 unsigned char c;
514 int ptr = 0;
515
516 while (ptr < bsize) {
517 c = buf[ptr];
518
519 if (out->len > out->size - 2)
520 break;
521 out->str[out->len++] = hextab[(c >> 4) & 0xF];
522 out->str[out->len++] = hextab[c & 0xF];
523
524 ptr++;
525 }
526 return ptr;
527}
528
529/* Dump the status of a table to a stream interface's
530 * read buffer. It returns 0 if the output buffer is full
531 * and needs to be called again, otherwise non-zero.
532 */
533static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
534 struct proxy *proxy, struct proxy *target)
535{
Willy Tarreau306f8302013-07-08 15:53:06 +0200536 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900537
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900539 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
540
541 /* any other information should be dumped here */
542
Willy Tarreau290e63a2012-09-20 18:07:14 +0200543 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100544 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900545
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200546 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900547 return 0;
548
549 return 1;
550}
551
552/* Dump the a table entry to a stream interface's
553 * read buffer. It returns 0 if the output buffer is full
554 * and needs to be called again, otherwise non-zero.
555 */
556static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
557 struct proxy *proxy, struct stksess *entry)
558{
559 int dt;
560
Willy Tarreau77804732012-10-29 16:14:26 +0100561 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900562
563 if (proxy->table.type == STKTABLE_TYPE_IP) {
564 char addr[INET_ADDRSTRLEN];
565 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900567 }
568 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
569 char addr[INET6_ADDRSTRLEN];
570 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100571 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900572 }
573 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100574 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900575 }
576 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100577 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900578 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
579 }
580 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100581 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900582 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
583 }
584
Willy Tarreau77804732012-10-29 16:14:26 +0100585 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900586
587 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
588 void *ptr;
589
590 if (proxy->table.data_ofs[dt] == 0)
591 continue;
592 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100593 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900594 else
Willy Tarreau77804732012-10-29 16:14:26 +0100595 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900596
597 ptr = stktable_data_ptr(&proxy->table, entry, dt);
598 switch (stktable_data_types[dt].std_type) {
599 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100600 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900601 break;
602 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100603 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900604 break;
605 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100606 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900607 break;
608 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100609 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900610 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
611 proxy->table.data_arg[dt].u));
612 break;
613 }
614 }
Willy Tarreau77804732012-10-29 16:14:26 +0100615 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900616
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200617 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900618 return 0;
619
620 return 1;
621}
622
Willy Tarreaudec98142012-06-06 23:37:08 +0200623static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900624{
Willy Tarreau306f8302013-07-08 15:53:06 +0200625 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100626 struct appctx *appctx = __objt_appctx(si->end);
627 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900628 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900629 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900630 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200631 long long value;
632 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200633 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200634 void *ptr;
635 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900636
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100637 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900638
639 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100640 appctx->ctx.cli.msg = "Key value expected\n";
641 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900642 return;
643 }
644
Simon Hormanc5b89f62011-06-15 15:18:50 +0900645 switch (px->table.type) {
646 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900647 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100648 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900649 break;
650 case STKTABLE_TYPE_IPV6:
651 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100652 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900653 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900654 case STKTABLE_TYPE_INTEGER:
655 {
656 char *endptr;
657 unsigned long val;
658 errno = 0;
659 val = strtoul(args[4], &endptr, 10);
660 if ((errno == ERANGE && val == ULONG_MAX) ||
661 (errno != 0 && val == 0) || endptr == args[4] ||
662 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100663 appctx->ctx.cli.msg = "Invalid key\n";
664 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900665 return;
666 }
667 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100668 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900669 break;
670 }
671 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900672 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100673 static_table_key->key = args[4];
674 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900675 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900676 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200677 switch (action) {
678 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100679 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 +0200680 break;
681 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100682 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 +0200683 break;
684 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100685 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200686 break;
687 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100688 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900689 return;
690 }
691
692 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200693 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100694 appctx->ctx.cli.msg = stats_permission_denied_msg;
695 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900696 return;
697 }
698
Willy Tarreau07115412012-10-29 21:56:59 +0100699 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900700
Willy Tarreaudec98142012-06-06 23:37:08 +0200701 switch (action) {
702 case STAT_CLI_O_TAB:
703 if (!ts)
704 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100705 chunk_reset(&trash);
706 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900707 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100708 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900709 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200710
711 case STAT_CLI_O_CLR:
712 if (!ts)
713 return;
714 if (ts->ref_cnt) {
715 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100716 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
717 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200718 return;
719 }
720 stksess_kill(&px->table, ts);
721 break;
Simon Horman17bce342011-06-15 15:18:47 +0900722
Willy Tarreau654694e2012-06-07 01:03:16 +0200723 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200724 if (ts)
725 stktable_touch(&px->table, ts, 1);
726 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100727 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200728 if (!ts) {
729 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100730 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
731 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200732 return;
733 }
734 stktable_store(&px->table, ts, 1);
735 }
736
Willy Tarreau47060b62013-08-01 21:11:42 +0200737 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
738 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100739 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
740 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200741 return;
742 }
743
744 data_type = stktable_get_data_type(args[cur_arg] + 5);
745 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100746 appctx->ctx.cli.msg = "Unknown data type\n";
747 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200748 return;
749 }
750
751 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "Data type not stored in this table\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200754 return;
755 }
756
757 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100758 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
759 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200760 return;
761 }
762
763 ptr = stktable_data_ptr(&px->table, ts, data_type);
764
765 switch (stktable_data_types[data_type].std_type) {
766 case STD_T_SINT:
767 stktable_data_cast(ptr, std_t_sint) = value;
768 break;
769 case STD_T_UINT:
770 stktable_data_cast(ptr, std_t_uint) = value;
771 break;
772 case STD_T_ULL:
773 stktable_data_cast(ptr, std_t_ull) = value;
774 break;
775 case STD_T_FRQP:
776 /* We set both the current and previous values. That way
777 * the reported frequency is stable during all the period
778 * then slowly fades out. This allows external tools to
779 * push measures without having to update them too often.
780 */
781 frqp = &stktable_data_cast(ptr, std_t_frqp);
782 frqp->curr_tick = now_ms;
783 frqp->prev_ctr = 0;
784 frqp->curr_ctr = value;
785 break;
786 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200787 }
788 break;
789
Willy Tarreaudec98142012-06-06 23:37:08 +0200790 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100791 appctx->ctx.cli.msg = "Unknown action\n";
792 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200793 break;
Simon Horman121f3052011-06-15 15:18:46 +0900794 }
Simon Horman121f3052011-06-15 15:18:46 +0900795}
796
Willy Tarreau654694e2012-06-07 01:03:16 +0200797static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900798{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100799 struct appctx *appctx = __objt_appctx(si->end);
800
Willy Tarreau04b3a192013-04-13 09:41:37 +0200801 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100802 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
803 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200804 return;
805 }
806
Simon Hormand5b9fd92011-06-15 15:18:48 +0900807 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
809 if (appctx->ctx.table.data_type < 0) {
810 appctx->ctx.cli.msg = "Unknown data type\n";
811 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812 return;
813 }
814
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100815 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
816 appctx->ctx.cli.msg = "Data type not stored in this table\n";
817 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900818 return;
819 }
820
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100821 appctx->ctx.table.data_op = get_std_op(args[4]);
822 if (appctx->ctx.table.data_op < 0) {
823 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
824 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900825 return;
826 }
827
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100828 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
829 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
830 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900831 return;
832 }
833}
834
Willy Tarreaudec98142012-06-06 23:37:08 +0200835static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900836{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100837 struct appctx *appctx = __objt_appctx(si->end);
838
839 appctx->ctx.table.data_type = -1;
840 appctx->st2 = STAT_ST_INIT;
841 appctx->ctx.table.target = NULL;
842 appctx->ctx.table.proxy = NULL;
843 appctx->ctx.table.entry = NULL;
844 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900845
846 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100847 appctx->ctx.table.target = find_stktable(args[2]);
848 if (!appctx->ctx.table.target) {
849 appctx->ctx.cli.msg = "No such table\n";
850 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900851 return;
852 }
853 }
854 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200855 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900856 goto err_args;
857 return;
858 }
859
860 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200861 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900862 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200863 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900864 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900865 goto err_args;
866
867 return;
868
869err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200870 switch (action) {
871 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100872 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 +0200873 break;
874 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100875 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 +0200876 break;
877 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100878 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200879 break;
880 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100881 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900882}
883
Willy Tarreau532a4502011-09-07 22:37:44 +0200884/* Expects to find a frontend named <arg> and returns it, otherwise displays various
885 * adequate error messages and returns NULL. This function also expects the session
886 * level to be admin.
887 */
888static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
889{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100890 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200891 struct proxy *px;
892
Willy Tarreau290e63a2012-09-20 18:07:14 +0200893 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = stats_permission_denied_msg;
895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200896 return NULL;
897 }
898
899 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100900 appctx->ctx.cli.msg = "A frontend name is expected.\n";
901 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200902 return NULL;
903 }
904
905 px = findproxy(arg, PR_CAP_FE);
906 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 appctx->ctx.cli.msg = "No such frontend.\n";
908 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200909 return NULL;
910 }
911 return px;
912}
913
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200914/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
915 * and returns the pointer to the server. Otherwise, display adequate error messages
916 * and returns NULL. This function also expects the session level to be admin. Note:
917 * the <arg> is modified to remove the '/'.
918 */
919static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
920{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100921 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200922 struct proxy *px;
923 struct server *sv;
924 char *line;
925
Willy Tarreau290e63a2012-09-20 18:07:14 +0200926 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100927 appctx->ctx.cli.msg = stats_permission_denied_msg;
928 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200929 return NULL;
930 }
931
932 /* split "backend/server" and make <line> point to server */
933 for (line = arg; *line; line++)
934 if (*line == '/') {
935 *line++ = '\0';
936 break;
937 }
938
939 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100940 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
941 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942 return NULL;
943 }
944
945 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100946 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
947 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200948 return NULL;
949 }
950
951 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100952 appctx->ctx.cli.msg = "Proxy is disabled.\n";
953 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200954 return NULL;
955 }
956
957 return sv;
958}
959
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100960/* This function is used with map and acl management. It permits to browse
961 * each reference. The variable <getnext> must contain the current node,
962 * <end> point to the root node and the <flags> permit to filter required
963 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100964 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100965static inline
966struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
967 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100968{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100969 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100970
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100971 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100972
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100973 /* Get next list entry. */
974 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100975
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100976 /* If the entry is the last of the list, return NULL. */
977 if (&ref->list == end)
978 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100979
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100980 /* If the entry match the flag, return it. */
981 if (ref->flags & flags)
982 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100983 }
984}
985
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100986static inline
987struct pat_ref *pat_ref_lookup_ref(const char *reference)
988{
989 int id;
990 char *error;
991
992 /* If the reference starts by a '#', this is numeric id. */
993 if (reference[0] == '#') {
994 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
995 id = strtol(reference + 1, &error, 10);
996 if (*error != '\0')
997 return NULL;
998
999 /* Perform the unique id lookup. */
1000 return pat_ref_lookupid(id);
1001 }
1002
1003 /* Perform the string lookup. */
1004 return pat_ref_lookup(reference);
1005}
1006
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001007/* This function is used with map and acl management. It permits to browse
1008 * each reference.
1009 */
1010static inline
1011struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1012{
1013 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001014 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1015 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001016 return NULL;
1017 return expr;
1018}
1019
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001020/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001021 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001022 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001023 * designating the function which will have to process the request, which can
1024 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001025 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001026static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001027{
Willy Tarreau306f8302013-07-08 15:53:06 +02001028 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001029 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001030 char *args[MAX_STATS_ARGS + 1];
1031 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001032 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001033
1034 while (isspace((unsigned char)*line))
1035 line++;
1036
1037 arg = 0;
1038 args[arg] = line;
1039
1040 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001041 if (*line == '\\') {
1042 line++;
1043 if (*line == '\0')
1044 break;
1045 }
1046 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047 *line++ = '\0';
1048
1049 while (isspace((unsigned char)*line))
1050 line++;
1051
1052 args[++arg] = line;
1053 continue;
1054 }
1055
1056 line++;
1057 }
1058
1059 while (++arg <= MAX_STATS_ARGS)
1060 args[arg] = line;
1061
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001062 /* remove \ */
1063 arg = 0;
1064 while (*args[arg] != '\0') {
1065 j = 0;
1066 for (i=0; args[arg][i] != '\0'; i++) {
1067 if (args[arg][i] == '\\')
1068 continue;
1069 args[arg][j] = args[arg][i];
1070 j++;
1071 }
1072 args[arg][j] = '\0';
1073 arg++;
1074 }
1075
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001076 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001077 if (strcmp(args[0], "show") == 0) {
1078 if (strcmp(args[1], "stat") == 0) {
1079 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001080 appctx->ctx.stats.flags |= STAT_BOUND;
1081 appctx->ctx.stats.iid = atoi(args[2]);
1082 appctx->ctx.stats.type = atoi(args[3]);
1083 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001084 }
1085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001086 appctx->st2 = STAT_ST_INIT;
1087 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001088 }
1089 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001090 appctx->st2 = STAT_ST_INIT;
1091 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001092 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001093 else if (strcmp(args[1], "pools") == 0) {
1094 appctx->st2 = STAT_ST_INIT;
1095 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1096 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001097 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001098 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001099 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001100 appctx->ctx.cli.msg = stats_permission_denied_msg;
1101 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001102 return 1;
1103 }
Willy Tarreau76153662012-11-26 01:16:39 +01001104 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001105 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001106 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001107 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001108 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001109 appctx->ctx.sess.target = NULL;
1110 appctx->ctx.sess.section = 0; /* start with session status */
1111 appctx->ctx.sess.pos = 0;
1112 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001113 }
1114 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001115 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001116 appctx->ctx.cli.msg = stats_permission_denied_msg;
1117 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001118 return 1;
1119 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001120 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001121 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001122 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->ctx.errors.iid = -1;
1124 appctx->ctx.errors.px = NULL;
1125 appctx->st2 = STAT_ST_INIT;
1126 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001127 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001128 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001129 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001130 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001131 else if (strcmp(args[1], "map") == 0 ||
1132 strcmp(args[1], "acl") == 0) {
1133
1134 /* Set ACL or MAP flags. */
1135 if (args[1][0] == 'm')
1136 appctx->ctx.map.display_flags = PAT_REF_MAP;
1137 else
1138 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001139
1140 /* no parameter: display all map avalaible */
1141 if (!*args[2]) {
1142 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001143 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001144 return 1;
1145 }
1146
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001147 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001148 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001149 if (!appctx->ctx.map.ref ||
1150 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1151 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001152 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001153 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001154 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001155 appctx->st0 = STAT_CLI_PRINT;
1156 return 1;
1157 }
1158 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001159 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001160 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001161 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001162 return 0;
1163 }
1164 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001165 else if (strcmp(args[0], "clear") == 0) {
1166 if (strcmp(args[1], "counters") == 0) {
1167 struct proxy *px;
1168 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001169 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001170 int clrall = 0;
1171
1172 if (strcmp(args[2], "all") == 0)
1173 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001174
Willy Tarreau6162db22009-10-10 17:13:00 +02001175 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001176 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1177 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001178 appctx->ctx.cli.msg = stats_permission_denied_msg;
1179 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001180 return 1;
1181 }
1182
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001183 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001184 if (clrall) {
1185 memset(&px->be_counters, 0, sizeof(px->be_counters));
1186 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1187 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001188 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001189 px->be_counters.conn_max = 0;
1190 px->be_counters.p.http.rps_max = 0;
1191 px->be_counters.sps_max = 0;
1192 px->be_counters.cps_max = 0;
1193 px->be_counters.nbpend_max = 0;
1194
1195 px->fe_counters.conn_max = 0;
1196 px->fe_counters.p.http.rps_max = 0;
1197 px->fe_counters.sps_max = 0;
1198 px->fe_counters.cps_max = 0;
1199 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001200 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001201
1202 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001203 if (clrall)
1204 memset(&sv->counters, 0, sizeof(sv->counters));
1205 else {
1206 sv->counters.cur_sess_max = 0;
1207 sv->counters.nbpend_max = 0;
1208 sv->counters.sps_max = 0;
1209 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001210
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001211 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001212 if (li->counters) {
1213 if (clrall)
1214 memset(li->counters, 0, sizeof(*li->counters));
1215 else
1216 li->counters->conn_max = 0;
1217 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001218 }
1219
Willy Tarreau81c25d02011-09-07 15:17:21 +02001220 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001221 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001222 return 1;
1223 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001224 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001225 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001226 /* end of processing */
1227 return 1;
1228 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001229 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1230 /* Set ACL or MAP flags. */
1231 if (args[1][0] == 'm')
1232 appctx->ctx.map.display_flags = PAT_REF_MAP;
1233 else
1234 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001235
1236 /* no parameter */
1237 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001238 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1239 appctx->ctx.cli.msg = "Missing map identifier.\n";
1240 else
1241 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001242 appctx->st0 = STAT_CLI_PRINT;
1243 return 1;
1244 }
1245
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001246 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001247 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001248 if (!appctx->ctx.map.ref ||
1249 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1250 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001251 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001252 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001253 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001254 appctx->st0 = STAT_CLI_PRINT;
1255 return 1;
1256 }
1257
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001258 /* Clear all. */
1259 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001260
1261 /* return response */
1262 appctx->ctx.cli.msg = "Done.\n";
1263 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001264 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001265 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001266 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001267 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001268 return 0;
1269 }
1270 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001271 else if (strcmp(args[0], "get") == 0) {
1272 if (strcmp(args[1], "weight") == 0) {
1273 struct proxy *px;
1274 struct server *sv;
1275
1276 /* split "backend/server" and make <line> point to server */
1277 for (line = args[2]; *line; line++)
1278 if (*line == '/') {
1279 *line++ = '\0';
1280 break;
1281 }
1282
1283 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001284 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1285 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001286 return 1;
1287 }
1288
1289 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001290 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1291 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001292 return 1;
1293 }
1294
1295 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001296 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1297 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001298 return 1;
1299 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001300 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1301 /* Set flags. */
1302 if (args[1][0] == 'm')
1303 appctx->ctx.map.display_flags = PAT_REF_MAP;
1304 else
1305 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001306
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001307 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001308 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001309 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1310 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1311 else
1312 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001313 appctx->st0 = STAT_CLI_PRINT;
1314 return 1;
1315 }
1316
1317 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001318 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001319 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001320 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001321 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001322 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001323 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001324 appctx->st0 = STAT_CLI_PRINT;
1325 return 1;
1326 }
1327
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001328 /* copy input string. The string must be allocated because
1329 * it may be used over multiple iterations. It's released
1330 * at the end and upon abort anyway.
1331 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001332 appctx->ctx.map.chunk.len = strlen(args[3]);
1333 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1334 appctx->ctx.map.chunk.str = strdup(args[3]);
1335 if (!appctx->ctx.map.chunk.str) {
1336 appctx->ctx.cli.msg = "Out of memory error.\n";
1337 appctx->st0 = STAT_CLI_PRINT;
1338 return 1;
1339 }
1340
1341 /* prepare response */
1342 appctx->st2 = STAT_ST_INIT;
1343 appctx->st0 = STAT_CLI_O_MLOOK;
1344 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001345 else { /* not "get weight" */
1346 return 0;
1347 }
1348 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001349 else if (strcmp(args[0], "set") == 0) {
1350 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001351 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001352 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001353
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001354 sv = expect_server_admin(s, si, args[2]);
1355 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001356 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001357
Simon Horman7d09b9a2013-02-12 10:45:51 +09001358 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001359 /*
1360 * The user-weight may now be zero and thus
1361 * the server considered to be draining.
1362 * Update the server's drain state as necessary.
1363 */
1364 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001365 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001366 appctx->ctx.cli.msg = warning;
1367 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001368 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001369 return 1;
1370 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001371 else if (strcmp(args[1], "timeout") == 0) {
1372 if (strcmp(args[2], "cli") == 0) {
1373 unsigned timeout;
1374 const char *res;
1375
1376 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001377 appctx->ctx.cli.msg = "Expects an integer value.\n";
1378 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001379 return 1;
1380 }
1381
1382 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1383 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001384 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1385 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001386 return 1;
1387 }
1388
1389 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1390 return 1;
1391 }
1392 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001393 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1394 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001395 return 1;
1396 }
1397 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001398 else if (strcmp(args[1], "maxconn") == 0) {
1399 if (strcmp(args[2], "frontend") == 0) {
1400 struct proxy *px;
1401 struct listener *l;
1402 int v;
1403
Willy Tarreau532a4502011-09-07 22:37:44 +02001404 px = expect_frontend_admin(s, si, args[3]);
1405 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001406 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001407
1408 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001409 appctx->ctx.cli.msg = "Integer value expected.\n";
1410 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001411 return 1;
1412 }
1413
1414 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001415 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->ctx.cli.msg = "Value out of range.\n";
1417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001418 return 1;
1419 }
1420
1421 /* OK, the value is fine, so we assign it to the proxy and to all of
1422 * its listeners. The blocked ones will be dequeued.
1423 */
1424 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001425 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001426 l->maxconn = v;
1427 if (l->state == LI_FULL)
1428 resume_listener(l);
1429 }
1430
1431 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1432 dequeue_all_listeners(&s->fe->listener_queue);
1433
1434 return 1;
1435 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001436 else if (strcmp(args[2], "global") == 0) {
1437 int v;
1438
Willy Tarreau290e63a2012-09-20 18:07:14 +02001439 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001440 appctx->ctx.cli.msg = stats_permission_denied_msg;
1441 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001442 return 1;
1443 }
1444
1445 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001446 appctx->ctx.cli.msg = "Expects an integer value.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001448 return 1;
1449 }
1450
1451 v = atoi(args[3]);
1452 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001453 appctx->ctx.cli.msg = "Value out of range.\n";
1454 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001455 return 1;
1456 }
1457
1458 /* check for unlimited values */
1459 if (v <= 0)
1460 v = global.hardmaxconn;
1461
1462 global.maxconn = v;
1463
1464 /* Dequeues all of the listeners waiting for a resource */
1465 if (!LIST_ISEMPTY(&global_listener_queue))
1466 dequeue_all_listeners(&global_listener_queue);
1467
1468 return 1;
1469 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001470 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001471 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1472 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001473 return 1;
1474 }
1475 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001476 else if (strcmp(args[1], "rate-limit") == 0) {
1477 if (strcmp(args[2], "connections") == 0) {
1478 if (strcmp(args[3], "global") == 0) {
1479 int v;
1480
Willy Tarreau290e63a2012-09-20 18:07:14 +02001481 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001482 appctx->ctx.cli.msg = stats_permission_denied_msg;
1483 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001484 return 1;
1485 }
1486
1487 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001488 appctx->ctx.cli.msg = "Expects an integer value.\n";
1489 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001490 return 1;
1491 }
1492
1493 v = atoi(args[4]);
1494 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001495 appctx->ctx.cli.msg = "Value out of range.\n";
1496 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001497 return 1;
1498 }
1499
1500 global.cps_lim = v;
1501
1502 /* Dequeues all of the listeners waiting for a resource */
1503 if (!LIST_ISEMPTY(&global_listener_queue))
1504 dequeue_all_listeners(&global_listener_queue);
1505
1506 return 1;
1507 }
1508 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1510 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001511 return 1;
1512 }
1513 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001514 else if (strcmp(args[2], "sessions") == 0) {
1515 if (strcmp(args[3], "global") == 0) {
1516 int v;
1517
1518 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1519 appctx->ctx.cli.msg = stats_permission_denied_msg;
1520 appctx->st0 = STAT_CLI_PRINT;
1521 return 1;
1522 }
1523
1524 if (!*args[4]) {
1525 appctx->ctx.cli.msg = "Expects an integer value.\n";
1526 appctx->st0 = STAT_CLI_PRINT;
1527 return 1;
1528 }
1529
1530 v = atoi(args[4]);
1531 if (v < 0) {
1532 appctx->ctx.cli.msg = "Value out of range.\n";
1533 appctx->st0 = STAT_CLI_PRINT;
1534 return 1;
1535 }
1536
1537 global.sps_lim = v;
1538
1539 /* Dequeues all of the listeners waiting for a resource */
1540 if (!LIST_ISEMPTY(&global_listener_queue))
1541 dequeue_all_listeners(&global_listener_queue);
1542
1543 return 1;
1544 }
1545 else {
1546 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1547 appctx->st0 = STAT_CLI_PRINT;
1548 return 1;
1549 }
1550 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001551#ifdef USE_OPENSSL
1552 else if (strcmp(args[2], "ssl-sessions") == 0) {
1553 if (strcmp(args[3], "global") == 0) {
1554 int v;
1555
1556 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1557 appctx->ctx.cli.msg = stats_permission_denied_msg;
1558 appctx->st0 = STAT_CLI_PRINT;
1559 return 1;
1560 }
1561
1562 if (!*args[4]) {
1563 appctx->ctx.cli.msg = "Expects an integer value.\n";
1564 appctx->st0 = STAT_CLI_PRINT;
1565 return 1;
1566 }
1567
1568 v = atoi(args[4]);
1569 if (v < 0) {
1570 appctx->ctx.cli.msg = "Value out of range.\n";
1571 appctx->st0 = STAT_CLI_PRINT;
1572 return 1;
1573 }
1574
1575 global.ssl_lim = v;
1576
1577 /* Dequeues all of the listeners waiting for a resource */
1578 if (!LIST_ISEMPTY(&global_listener_queue))
1579 dequeue_all_listeners(&global_listener_queue);
1580
1581 return 1;
1582 }
1583 else {
1584 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1585 appctx->st0 = STAT_CLI_PRINT;
1586 return 1;
1587 }
1588 }
1589#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001590 else if (strcmp(args[2], "http-compression") == 0) {
1591 if (strcmp(args[3], "global") == 0) {
1592 int v;
1593
Willy Tarreau85d47f92012-11-21 00:29:50 +01001594 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001595 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1596 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001597 return 1;
1598 }
1599
William Lallemandd85f9172012-11-09 17:05:39 +01001600 v = atoi(args[4]);
1601 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1602 }
1603 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001604 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1605 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001606 return 1;
1607 }
1608 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001609 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001610 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001612 return 1;
1613 }
1614 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001615 else if (strcmp(args[1], "table") == 0) {
1616 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1617 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001618 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001619 char *err;
1620
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001621 /* Set flags. */
1622 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001623
1624 /* Expect three parameters: map name, key and new value. */
1625 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001626 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001627 appctx->st0 = STAT_CLI_PRINT;
1628 return 1;
1629 }
1630
1631 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001632 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001633 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001634 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001635 appctx->st0 = STAT_CLI_PRINT;
1636 return 1;
1637 }
1638
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001639 /* If the entry identifier start with a '#', it is considered as
1640 * pointer id
1641 */
1642 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1643 struct pat_ref_elt *ref;
1644 long long int conv;
1645 char *error;
1646
1647 /* Convert argument to integer value. */
1648 conv = strtoll(&args[3][1], &error, 16);
1649 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001650 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001651 appctx->st0 = STAT_CLI_PRINT;
1652 return 1;
1653 }
1654
1655 /* Convert and check integer to pointer. */
1656 ref = (struct pat_ref_elt *)(long)conv;
1657 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001658 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001659 appctx->st0 = STAT_CLI_PRINT;
1660 return 1;
1661 }
1662
1663 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001664 err = NULL;
1665 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1666 if (err)
1667 memprintf(&err, "%s.\n", err);
1668 appctx->ctx.cli.err = err;
1669 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001670 return 1;
1671 }
1672 }
1673 else {
1674 /* Else, use the entry identifier as pattern
1675 * string, and update the value.
1676 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001677 err = NULL;
1678 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1679 if (err)
1680 memprintf(&err, "%s.\n", err);
1681 appctx->ctx.cli.err = err;
1682 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001683 return 1;
1684 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001685 }
1686
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001687 /* The set is done, send message. */
1688 appctx->ctx.cli.msg = "Done.\n";
1689 appctx->st0 = STAT_CLI_PRINT;
1690 return 1;
1691 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001692 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001693 return 0;
1694 }
1695 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001696 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001697 if (strcmp(args[1], "agent") == 0) {
1698 struct server *sv;
1699
1700 sv = expect_server_admin(s, si, args[2]);
1701 if (!sv)
1702 return 1;
1703
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001704 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1705 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1706 appctx->st0 = STAT_CLI_PRINT;
1707 return 1;
1708 }
1709
1710 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001711 return 1;
1712 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001713 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001714 struct server *sv;
1715
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001716 sv = expect_server_admin(s, si, args[2]);
1717 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001718 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001719
Cyril Bontécd19e512010-01-31 22:34:03 +01001720 if (sv->state & SRV_MAINTAIN) {
1721 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001722 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001723 /* If this server tracks the status of another one,
1724 * we must restore the good status.
1725 */
Willy Tarreau44267702011-10-28 15:35:33 +02001726 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001727 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001728 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001729 } else {
1730 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001731 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001732 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001733 }
1734 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001735 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001736 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001737 }
1738 }
1739
Willy Tarreau532a4502011-09-07 22:37:44 +02001740 return 1;
1741 }
1742 else if (strcmp(args[1], "frontend") == 0) {
1743 struct proxy *px;
1744
1745 px = expect_frontend_admin(s, si, args[2]);
1746 if (!px)
1747 return 1;
1748
1749 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001750 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1751 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001752 return 1;
1753 }
1754
1755 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001756 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1757 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001758 return 1;
1759 }
1760
1761 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001762 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1763 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001764 return 1;
1765 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001766 return 1;
1767 }
1768 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001769 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001771 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001772 }
1773 }
1774 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001775 if (strcmp(args[1], "agent") == 0) {
1776 struct server *sv;
1777
1778 sv = expect_server_admin(s, si, args[2]);
1779 if (!sv)
1780 return 1;
1781
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001782 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001783 return 1;
1784 }
1785 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001786 struct server *sv;
1787
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001788 sv = expect_server_admin(s, si, args[2]);
1789 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001790 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001791
Cyril Bontécd19e512010-01-31 22:34:03 +01001792 if (! (sv->state & SRV_MAINTAIN)) {
1793 /* Not already in maintenance, we can change the server state */
1794 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001795 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001796 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001797 }
1798
Willy Tarreau532a4502011-09-07 22:37:44 +02001799 return 1;
1800 }
1801 else if (strcmp(args[1], "frontend") == 0) {
1802 struct proxy *px;
1803
1804 px = expect_frontend_admin(s, si, args[2]);
1805 if (!px)
1806 return 1;
1807
1808 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001809 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1810 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001811 return 1;
1812 }
1813
1814 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001815 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1816 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001817 return 1;
1818 }
1819
1820 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001821 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1822 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001823 return 1;
1824 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001825 return 1;
1826 }
1827 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001828 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1829 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001830 return 1;
1831 }
1832 }
1833 else if (strcmp(args[0], "shutdown") == 0) {
1834 if (strcmp(args[1], "frontend") == 0) {
1835 struct proxy *px;
1836
1837 px = expect_frontend_admin(s, si, args[2]);
1838 if (!px)
1839 return 1;
1840
1841 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001842 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1843 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001844 return 1;
1845 }
1846
1847 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1848 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1849 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1850 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1851 stop_proxy(px);
1852 return 1;
1853 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001854 else if (strcmp(args[1], "session") == 0) {
1855 struct session *sess, *ptr;
1856
Willy Tarreau290e63a2012-09-20 18:07:14 +02001857 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001858 appctx->ctx.cli.msg = stats_permission_denied_msg;
1859 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001860 return 1;
1861 }
1862
1863 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001864 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1865 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001866 return 1;
1867 }
1868
1869 ptr = (void *)strtoul(args[2], NULL, 0);
1870
1871 /* first, look for the requested session in the session table */
1872 list_for_each_entry(sess, &sessions, list) {
1873 if (sess == ptr)
1874 break;
1875 }
1876
1877 /* do we have the session ? */
1878 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001879 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1880 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001881 return 1;
1882 }
1883
1884 session_shutdown(sess, SN_ERR_KILLED);
1885 return 1;
1886 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001887 else if (strcmp(args[1], "sessions") == 0) {
1888 if (strcmp(args[2], "server") == 0) {
1889 struct server *sv;
1890 struct session *sess, *sess_bck;
1891
1892 sv = expect_server_admin(s, si, args[3]);
1893 if (!sv)
1894 return 1;
1895
1896 /* kill all the session that are on this server */
1897 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1898 if (sess->srv_conn == sv)
1899 session_shutdown(sess, SN_ERR_KILLED);
1900
1901 return 1;
1902 }
1903 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001904 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1905 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001906 return 1;
1907 }
1908 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001909 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001910 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1911 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001912 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001913 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001914 }
1915 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001916 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1917 if (args[1][0] == 'm')
1918 appctx->ctx.map.display_flags = PAT_REF_MAP;
1919 else
1920 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001921
1922 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001923 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1924 if (!*args[2] || !*args[3]) {
1925 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1926 appctx->st0 = STAT_CLI_PRINT;
1927 return 1;
1928 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001929 }
1930
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001931 else {
1932 if (!*args[2] || !*args[3]) {
1933 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1934 appctx->st0 = STAT_CLI_PRINT;
1935 return 1;
1936 }
1937 }
1938
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001939 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001940 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001941 if (!appctx->ctx.map.ref ||
1942 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001943 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001944 appctx->st0 = STAT_CLI_PRINT;
1945 return 1;
1946 }
1947
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001948 /* If the entry identifier start with a '#', it is considered as
1949 * pointer id
1950 */
1951 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1952 struct pat_ref_elt *ref;
1953 long long int conv;
1954 char *error;
1955
1956 /* Convert argument to integer value. */
1957 conv = strtoll(&args[3][1], &error, 16);
1958 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001959 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001960 appctx->st0 = STAT_CLI_PRINT;
1961 return 1;
1962 }
1963
1964 /* Convert and check integer to pointer. */
1965 ref = (struct pat_ref_elt *)(long)conv;
1966 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001967 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001968 appctx->st0 = STAT_CLI_PRINT;
1969 return 1;
1970 }
1971
1972 /* Try to delete the entry. */
1973 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
1974 /* The entry is not found, send message. */
1975 appctx->ctx.cli.msg = "Key not found.\n";
1976 appctx->st0 = STAT_CLI_PRINT;
1977 return 1;
1978 }
1979 }
1980 else {
1981 /* Else, use the entry identifier as pattern
1982 * string and try to delete the entry.
1983 */
1984 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
1985 /* The entry is not found, send message. */
1986 appctx->ctx.cli.msg = "Key not found.\n";
1987 appctx->st0 = STAT_CLI_PRINT;
1988 return 1;
1989 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001990 }
1991
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001992 /* The deletion is done, send message. */
1993 appctx->ctx.cli.msg = "Done.\n";
1994 appctx->st0 = STAT_CLI_PRINT;
1995 return 1;
1996 }
1997 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001998 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001999 appctx->st0 = STAT_CLI_PRINT;
2000 return 1;
2001 }
2002 }
2003 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002004 if (strcmp(args[1], "map") == 0 ||
2005 strcmp(args[1], "acl") == 0) {
2006 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002007 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002008
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002009 /* Set flags. */
2010 if (args[1][0] == 'm')
2011 appctx->ctx.map.display_flags = PAT_REF_MAP;
2012 else
2013 appctx->ctx.map.display_flags = PAT_REF_ACL;
2014
2015 /* If the keywork is "map", we expect three parameters, if it
2016 * is "acl", we expect only two parameters
2017 */
2018 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2019 if (!*args[2] || !*args[3] || !*args[4]) {
2020 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2021 appctx->st0 = STAT_CLI_PRINT;
2022 return 1;
2023 }
2024 }
2025 else {
2026 if (!*args[2] || !*args[3]) {
2027 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2028 appctx->st0 = STAT_CLI_PRINT;
2029 return 1;
2030 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002031 }
2032
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002033 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002034 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002035 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002036 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002037 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002038 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002039 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002040 appctx->st0 = STAT_CLI_PRINT;
2041 return 1;
2042 }
2043
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002044 /* The command "add acl" is prohibited if the reference
2045 * use samples.
2046 */
2047 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2048 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2049 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2050 "You must use the command 'add map' to add values.\n";
2051 appctx->st0 = STAT_CLI_PRINT;
2052 return 1;
2053 }
2054
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002055 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002056 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002057 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002058 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002059 else
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002060 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002061 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002062 if (err)
2063 memprintf(&err, "%s.\n", err);
2064 appctx->ctx.cli.err = err;
2065 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002066 return 1;
2067 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002068
2069 /* The add is done, send message. */
2070 appctx->ctx.cli.msg = "Done.\n";
2071 appctx->st0 = STAT_CLI_PRINT;
2072 return 1;
2073 }
2074 else { /* unknown "del" parameter */
2075 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2076 appctx->st0 = STAT_CLI_PRINT;
2077 return 1;
2078 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002079 }
2080 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002081 return 0;
2082 }
2083 return 1;
2084}
2085
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002086/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002087 * used to processes I/O from/to the stats unix socket. The system relies on a
2088 * state machine handling requests and various responses. We read a request,
2089 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002090 * Then we can read again. The state is stored in appctx->st0 and is one of the
2091 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002092 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002093 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002094static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002095{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002096 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002097 struct channel *req = si->ob;
2098 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002099 int reql;
2100 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002101
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002102 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2103 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002104
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002105 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002106 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002107 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002108 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2109 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002110 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002111 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002112 /* Let's close for real now. We just close the request
2113 * side, the conditions below will complete if needed.
2114 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002115 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002116 break;
2117 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002118 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002119 /* ensure we have some output room left in the event we
2120 * would want to return some info right after parsing.
2121 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002122 if (buffer_almost_full(si->ib->buf)) {
2123 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002124 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002125 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002127 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002128 if (reql <= 0) { /* closed or EOL not found */
2129 if (reql == 0)
2130 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002131 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002132 continue;
2133 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002134
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002135 /* seek for a possible semi-colon. If we find one, we
2136 * replace it with an LF and skip only this part.
2137 */
2138 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002139 if (trash.str[len] == ';') {
2140 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002141 reql = len + 1;
2142 break;
2143 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002144
Willy Tarreau816fc222009-10-04 07:36:58 +02002145 /* now it is time to check that we have a full line,
2146 * remove the trailing \n and possibly \r, then cut the
2147 * line.
2148 */
2149 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002150 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002151 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002152 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002153 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002154
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002155 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002156 len--;
2157
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002158 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002159
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002160 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002161 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002162 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002163 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002164 continue;
2165 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002166 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002167 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002168 else if (strcmp(trash.str, "help") == 0 ||
2169 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002170 appctx->ctx.cli.msg = stats_sock_usage_msg;
2171 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002172 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002173 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002174 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002175 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002176 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002177 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002178 /* if prompt is disabled, print help on empty lines,
2179 * so that the user at least knows how to enable
2180 * prompt and find help.
2181 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002182 appctx->ctx.cli.msg = stats_sock_usage_msg;
2183 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184 }
2185
2186 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002187 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002188 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002189 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002190 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002191 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002192 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002193 continue;
2194 }
2195
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002196 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002197 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002198 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2199 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002200 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002201 case STAT_CLI_PRINT_FREE:
2202 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2203 free(appctx->ctx.cli.err);
2204 appctx->st0 = STAT_CLI_PROMPT;
2205 }
2206 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002207 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002208 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002209 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002210 break;
2211 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002212 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002213 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002214 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002215 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002216 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002217 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002218 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002219 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002220 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002221 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002222 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002223 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002224 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002225 if (stats_table_request(si, appctx->st0))
2226 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002227 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002228 case STAT_CLI_O_PATS:
2229 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002230 appctx->st0 = STAT_CLI_PROMPT;
2231 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002232 case STAT_CLI_O_PAT:
2233 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002234 appctx->st0 = STAT_CLI_PROMPT;
2235 break;
2236 case STAT_CLI_O_MLOOK:
2237 if (stats_map_lookup(si))
2238 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002239 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002240 case STAT_CLI_O_POOLS:
2241 if (stats_dump_pools_to_buffer(si))
2242 appctx->st0 = STAT_CLI_PROMPT;
2243 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002244 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002245 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002246 break;
2247 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002248
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002249 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002250 if (appctx->st0 == STAT_CLI_PROMPT) {
2251 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2252 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002253 }
2254
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002255 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002256 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002257 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002258
2259 /* Now we close the output if one of the writers did so,
2260 * or if we're not in interactive mode and the request
2261 * buffer is empty. This still allows pipelined requests
2262 * to be sent in non-interactive mode.
2263 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002264 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2265 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002266 continue;
2267 }
2268
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002269 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002270 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002271 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002272 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002273
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002274 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002275 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2276 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002277 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002278 * and nothing more to consume. This is comparable to a broken pipe, so
2279 * we forward the close to the request side so that it flows upstream to
2280 * the client.
2281 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002282 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002283 }
2284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002285 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002286 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2287 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2288 /* We have no more processing to do, and nothing more to send, and
2289 * the client side has closed. So we'll forward this state downstream
2290 * on the response buffer.
2291 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002292 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002293 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002294 }
2295
2296 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002297 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002298
2299 /* we don't want to expire timeouts while we're processing requests */
2300 si->ib->rex = TICK_ETERNITY;
2301 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002302
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002303 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002304 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 +02002305 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002306 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 +02002307
2308 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2309 /* check that we have released everything then unregister */
2310 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002311 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002312}
2313
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002314/* This function dumps information onto the stream interface's read buffer.
2315 * It returns 0 as long as it does not complete, non-zero upon completion.
2316 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002317 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002318static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002319{
2320 unsigned int up = (now.tv_sec - start_date.tv_sec);
2321
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002322 chunk_printf(&trash,
2323 "Name: " PRODUCT_NAME "\n"
2324 "Version: " HAPROXY_VERSION "\n"
2325 "Release_date: " HAPROXY_DATE "\n"
2326 "Nbproc: %d\n"
2327 "Process_num: %d\n"
2328 "Pid: %d\n"
2329 "Uptime: %dd %dh%02dm%02ds\n"
2330 "Uptime_sec: %d\n"
2331 "Memmax_MB: %d\n"
2332 "Ulimit-n: %d\n"
2333 "Maxsock: %d\n"
2334 "Maxconn: %d\n"
2335 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002336 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002337 "CumConns: %d\n"
2338 "CumReq: %d\n"
2339#ifdef USE_OPENSSL
2340 "MaxSslConns: %d\n"
2341 "CurrSslConns: %d\n"
2342 "CumSslConns: %d\n"
2343#endif
2344 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002345 "PipesUsed: %d\n"
2346 "PipesFree: %d\n"
2347 "ConnRate: %d\n"
2348 "ConnRateLimit: %d\n"
2349 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002350 "SessRate: %d\n"
2351 "SessRateLimit: %d\n"
2352 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002353#ifdef USE_OPENSSL
2354 "SslRate: %d\n"
2355 "SslRateLimit: %d\n"
2356 "MaxSslRate: %d\n"
2357#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002358 "CompressBpsIn: %u\n"
2359 "CompressBpsOut: %u\n"
2360 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002361#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002362 "ZlibMemUsage: %ld\n"
2363 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002364#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002365 "Tasks: %d\n"
2366 "Run_queue: %d\n"
2367 "Idle_pct: %d\n"
2368 "node: %s\n"
2369 "description: %s\n"
2370 "",
2371 global.nbproc,
2372 relative_pid,
2373 pid,
2374 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2375 up,
2376 global.rlimit_memmax,
2377 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002378 global.maxsock, global.maxconn, global.hardmaxconn,
2379 actconn, totalconn, global.req_count,
2380#ifdef USE_OPENSSL
2381 global.maxsslconn, sslconns, totalsslconns,
2382#endif
2383 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002384 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002385 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002386#ifdef USE_OPENSSL
2387 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2388#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002389 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2390 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002391#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002392 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002393#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002394 nb_tasks_cur, run_queue_cur, idle_pct,
2395 global.node, global.desc ? global.desc : ""
2396 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002397
2398 if (bi_putchk(si->ib, &trash) == -1)
2399 return 0;
2400
2401 return 1;
2402}
2403
Willy Tarreau12833bb2014-01-28 16:49:56 +01002404/* This function dumps memory usage information onto the stream interface's
2405 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2406 * completion. No state is used.
2407 */
2408static int stats_dump_pools_to_buffer(struct stream_interface *si)
2409{
2410 dump_pools_to_trash();
2411 if (bi_putchk(si->ib, &trash) == -1)
2412 return 0;
2413 return 1;
2414}
2415
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002416/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2417 * the state from stream interface <si>. The caller is responsible for clearing
2418 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002419 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002420static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002421{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002422 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002423 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002425 if (!(px->cap & PR_CAP_FE))
2426 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002427
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002428 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002429 return 0;
2430
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002431 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002432 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002433 /* name, queue */
2434 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002435
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002436 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002437 /* Column sub-heading for Enable or Disable server */
2438 chunk_appendf(&trash, "<td></td>");
2439 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002440
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002441 chunk_appendf(&trash,
2442 "<td class=ac>"
2443 "<a name=\"%s/Frontend\"></a>"
2444 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2445 "<td colspan=3></td>"
2446 "",
2447 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002448
Willy Tarreau466c9b52012-12-23 02:25:03 +01002449 chunk_appendf(&trash,
2450 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002451 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002452 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2453 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2454 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002455 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2456 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2457 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002458
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002459 if (px->mode == PR_MODE_HTTP)
2460 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002461 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002462 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002463
2464 chunk_appendf(&trash,
2465 "</table></div></u></td>"
2466 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002467 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002468 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2469 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2470 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002471 U2H(px->fe_counters.sps_max),
2472 U2H(px->fe_counters.cps_max),
2473 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002474
2475 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002476 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002477 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002478 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002479
2480 chunk_appendf(&trash,
2481 "</table></div></u></td>"
2482 /* sessions rate : limit */
2483 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002484 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002485
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002486 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002487 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002488 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002489 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002490 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2491 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002492 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002493 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2494 U2H(px->fe_counters.cum_sess),
2495 U2H(px->fe_counters.cum_conn),
2496 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002497
Willy Tarreau466c9b52012-12-23 02:25:03 +01002498 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002499 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002500 chunk_appendf(&trash,
2501 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2502 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2503 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2504 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2505 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2506 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2507 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2508 "<tr><th>- other responses:</th><td>%s</td></tr>"
2509 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2510 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002511 U2H(px->fe_counters.p.http.cum_req),
2512 U2H(px->fe_counters.p.http.rsp[1]),
2513 U2H(px->fe_counters.p.http.rsp[2]),
2514 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002516 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002517 U2H(px->fe_counters.p.http.rsp[3]),
2518 U2H(px->fe_counters.p.http.rsp[4]),
2519 U2H(px->fe_counters.p.http.rsp[5]),
2520 U2H(px->fe_counters.p.http.rsp[0]),
2521 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002522 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002523
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002524 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002525 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002526 /* sessions: lbtot, lastsess */
2527 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002528 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002529 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002530 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002531 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002532
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002533 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002534 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002535 "<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 +01002536 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002537 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002538 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2539 px->fe_counters.comp_in ?
2540 (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 +01002541 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002542
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002543 chunk_appendf(&trash,
2544 /* denied: req, resp */
2545 "<td>%s</td><td>%s</td>"
2546 /* errors : request, connect, response */
2547 "<td>%s</td><td></td><td></td>"
2548 /* warnings: retries, redispatches */
2549 "<td></td><td></td>"
2550 /* server status : reflect frontend status */
2551 "<td class=ac>%s</td>"
2552 /* rest of server: nothing */
2553 "<td class=ac colspan=8></td></tr>"
2554 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002555 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2556 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002557 px->state == PR_STREADY ? "OPEN" :
2558 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002559 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002560 else { /* CSV mode */
2561 chunk_appendf(&trash,
2562 /* pxid, name, queue cur, queue max, */
2563 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002564 /* sessions : current, max, limit, total */
2565 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002566 /* bytes : in, out */
2567 "%lld,%lld,"
2568 /* denied: req, resp */
2569 "%lld,%lld,"
2570 /* errors : request, connect, response */
2571 "%lld,,,"
2572 /* warnings: retries, redispatches */
2573 ",,"
2574 /* server status : reflect frontend status */
2575 "%s,"
2576 /* rest of server: nothing */
2577 ",,,,,,,,"
2578 /* pid, iid, sid, throttle, lbtot, tracked, type */
2579 "%d,%d,0,,,,%d,"
2580 /* rate, rate_lim, rate_max */
2581 "%u,%u,%u,"
2582 /* check_status, check_code, check_duration */
2583 ",,,",
2584 px->id,
2585 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2586 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2587 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2588 px->fe_counters.failed_req,
2589 px->state == PR_STREADY ? "OPEN" :
2590 px->state == PR_STFULL ? "FULL" : "STOP",
2591 relative_pid, px->uuid, STATS_TYPE_FE,
2592 read_freq_ctr(&px->fe_sess_per_sec),
2593 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002595 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2596 if (px->mode == PR_MODE_HTTP) {
2597 for (i=1; i<6; i++)
2598 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2599 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2600 }
2601 else
2602 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002603
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002604 /* failed health analyses */
2605 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002606
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607 /* requests : req_rate, req_rate_max, req_tot, */
2608 chunk_appendf(&trash, "%u,%u,%lld,",
2609 read_freq_ctr(&px->fe_req_per_sec),
2610 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2611
2612 /* errors: cli_aborts, srv_aborts */
2613 chunk_appendf(&trash, ",,");
2614
2615 /* compression: in, out, bypassed */
2616 chunk_appendf(&trash, "%lld,%lld,%lld,",
2617 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2618
2619 /* compression: comp_rsp */
2620 chunk_appendf(&trash, "%lld,",
2621 px->fe_counters.p.http.comp_rsp);
2622
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002623 /* lastsess */
2624 chunk_appendf(&trash, ",");
2625
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 /* finish with EOL */
2627 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002628 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002629 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002630}
2631
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002632/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2633 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2634 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2635 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002636 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002638{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002639 struct appctx *appctx = __objt_appctx(si->end);
2640
2641 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002642 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002643 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002644 /* Column sub-heading for Enable or Disable server */
2645 chunk_appendf(&trash, "<td></td>");
2646 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002647 chunk_appendf(&trash,
2648 /* frontend name, listener name */
2649 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2650 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2651 "",
2652 px->id, l->name,
2653 (flags & ST_SHLGNDS)?"<u>":"",
2654 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002655
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002656 if (flags & ST_SHLGNDS) {
2657 char str[INET6_ADDRSTRLEN];
2658 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002659
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002660 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002661
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002662 port = get_host_port(&l->addr);
2663 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2664 case AF_INET:
2665 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2666 break;
2667 case AF_INET6:
2668 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2669 break;
2670 case AF_UNIX:
2671 chunk_appendf(&trash, "unix, ");
2672 break;
2673 case -1:
2674 chunk_appendf(&trash, "(%s), ", strerror(errno));
2675 break;
2676 }
Willy Tarreau91861262007-10-17 17:06:05 +02002677
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002678 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002679 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002680 }
Willy Tarreau91861262007-10-17 17:06:05 +02002681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002683 /* queue */
2684 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002685 /* sessions rate: current, max, limit */
2686 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002687 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002689 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002690 /* bytes: in, out */
2691 "<td>%s</td><td>%s</td>"
2692 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002694 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2695 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002696
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002697 chunk_appendf(&trash,
2698 /* denied: req, resp */
2699 "<td>%s</td><td>%s</td>"
2700 /* errors: request, connect, response */
2701 "<td>%s</td><td></td><td></td>"
2702 /* warnings: retries, redispatches */
2703 "<td></td><td></td>"
2704 /* server status: reflect listener status */
2705 "<td class=ac>%s</td>"
2706 /* rest of server: nothing */
2707 "<td class=ac colspan=8></td></tr>"
2708 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002709 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2710 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002711 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2712 }
2713 else { /* CSV mode */
2714 chunk_appendf(&trash,
2715 /* pxid, name, queue cur, queue max, */
2716 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002717 /* sessions: current, max, limit, total */
2718 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002719 /* bytes: in, out */
2720 "%lld,%lld,"
2721 /* denied: req, resp */
2722 "%lld,%lld,"
2723 /* errors: request, connect, response */
2724 "%lld,,,"
2725 /* warnings: retries, redispatches */
2726 ",,"
2727 /* server status: reflect listener status */
2728 "%s,"
2729 /* rest of server: nothing */
2730 ",,,,,,,,"
2731 /* pid, iid, sid, throttle, lbtot, tracked, type */
2732 "%d,%d,%d,,,,%d,"
2733 /* rate, rate_lim, rate_max */
2734 ",,,"
2735 /* check_status, check_code, check_duration */
2736 ",,,"
2737 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2738 ",,,,,,"
2739 /* failed health analyses */
2740 ","
2741 /* requests : req_rate, req_rate_max, req_tot, */
2742 ",,,"
2743 /* errors: cli_aborts, srv_aborts */
2744 ",,"
2745 /* compression: in, out, bypassed, comp_rsp */
2746 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002747 /* lastsess */
2748 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 "\n",
2750 px->id, l->name,
2751 l->nbconn, l->counters->conn_max,
2752 l->maxconn, l->counters->cum_conn,
2753 l->counters->bytes_in, l->counters->bytes_out,
2754 l->counters->denied_req, l->counters->denied_resp,
2755 l->counters->failed_req,
2756 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2757 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2758 }
2759 return 1;
2760}
Willy Tarreau91861262007-10-17 17:06:05 +02002761
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002762/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2763 * from stream interface <si>, stats flags <flags>, and server state <state>.
2764 * The caller is responsible for clearing the trash if needed. Returns non-zero
2765 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002766 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2767 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002768 */
2769static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2770{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002771 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002772 struct server *ref = sv->track ? sv->track : sv;
2773 char str[INET6_ADDRSTRLEN];
2774 struct chunk src;
2775 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002776
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002777 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002778 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002779 "DOWN",
2780 "DN %d/%d &uarr;",
2781 "UP %d/%d &darr;",
2782 "UP",
2783 "NOLB %d/%d &darr;",
2784 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002785 "DRAIN %d/%d &darr;",
2786 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 "<i>no check</i>"
2788 };
Willy Tarreau91861262007-10-17 17:06:05 +02002789
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2791 chunk_appendf(&trash, "<tr class=\"maintain\">");
2792 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002793 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 "<tr class=\"%s%d\">",
2795 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002796
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002797 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002798 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002799 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2800 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002801
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002802 chunk_appendf(&trash,
2803 "<td class=ac><a name=\"%s/%s\"></a>%s"
2804 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2805 "",
2806 px->id, sv->id,
2807 (flags & ST_SHLGNDS) ? "<u>" : "",
2808 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002809
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002811 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002812
2813 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2814 case AF_INET:
2815 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2816 break;
2817 case AF_INET6:
2818 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2819 break;
2820 case AF_UNIX:
2821 chunk_appendf(&trash, "unix, ");
2822 break;
2823 case -1:
2824 chunk_appendf(&trash, "(%s), ", strerror(errno));
2825 break;
2826 default: /* address family not supported */
2827 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002828 }
Willy Tarreau91861262007-10-17 17:06:05 +02002829
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002830 /* id */
2831 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002832
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002833 /* cookie */
2834 if (sv->cookie) {
2835 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002836
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002837 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2838 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002839
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002840 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002841 }
2842
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002843 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002844 }
Willy Tarreau91861262007-10-17 17:06:05 +02002845
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002846 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002847 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002848 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 /* sessions rate : current, max, limit */
2850 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002852 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002853 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2854 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002855
Willy Tarreau466c9b52012-12-23 02:25:03 +01002856
2857 chunk_appendf(&trash,
2858 /* sessions: current, max, limit, total */
2859 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002860 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002861 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2862 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002863 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2864 U2H(sv->counters.cum_sess),
2865 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002866
Willy Tarreau466c9b52012-12-23 02:25:03 +01002867 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2868 if (px->mode == PR_MODE_HTTP) {
2869 unsigned long long tot;
2870 for (tot = i = 0; i < 6; i++)
2871 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002872
Willy Tarreau466c9b52012-12-23 02:25:03 +01002873 chunk_appendf(&trash,
2874 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2875 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2876 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2877 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2878 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2879 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2880 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2881 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002882 U2H(tot),
2883 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2884 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2885 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2886 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2887 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2888 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 +02002889 }
Willy Tarreau91861262007-10-17 17:06:05 +02002890
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002891 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002892 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002893 /* sessions: lbtot, last */
2894 "<td>%s</td><td>%s</td>",
2895 U2H(sv->counters.cum_lbconn),
2896 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002897
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898 chunk_appendf(&trash,
2899 /* bytes : in, out */
2900 "<td>%s</td><td>%s</td>"
2901 /* denied: req, resp */
2902 "<td></td><td>%s</td>"
2903 /* errors : request, connect */
2904 "<td></td><td>%s</td>"
2905 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002906 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002907 /* warnings: retries, redispatches */
2908 "<td>%lld</td><td>%lld</td>"
2909 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002910 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2911 U2H(sv->counters.failed_secu),
2912 U2H(sv->counters.failed_conns),
2913 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002914 sv->counters.cli_aborts,
2915 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 sv->counters.retries, sv->counters.redispatches);
2917
2918 /* status, lest check */
2919 chunk_appendf(&trash, "<td class=ac>");
2920
2921 if (sv->state & SRV_MAINTAIN) {
2922 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2923 chunk_appendf(&trash, "MAINT");
2924 }
2925 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2926 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2927 chunk_appendf(&trash, "MAINT(via)");
2928 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002929 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002930 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2931 chunk_appendf(&trash,
2932 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002933 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2934 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002935 }
Willy Tarreau91861262007-10-17 17:06:05 +02002936
Willy Tarreauff5ae352013-12-11 20:36:34 +01002937 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002938 chunk_appendf(&trash,
2939 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002940 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002941 get_check_status_info(sv->check.status));
2942
2943 if (sv->check.status >= HCHK_STATUS_L57DATA)
2944 chunk_appendf(&trash, "/%d", sv->check.code);
2945
2946 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002947 chunk_appendf(&trash, " in %lums", sv->check.duration);
2948
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002949 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002950 get_check_status_description(sv->check.status));
2951 if (*sv->check.desc) {
2952 chunk_appendf(&trash, ": ");
2953 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2954 chunk_htmlencode(&trash, &src);
2955 }
2956 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002957 }
2958 else
2959 chunk_appendf(&trash, "</td><td>");
2960
2961 chunk_appendf(&trash,
2962 /* weight */
2963 "</td><td class=ac>%d</td>"
2964 /* act, bck */
2965 "<td class=ac>%s</td><td class=ac>%s</td>"
2966 "",
2967 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2968 (sv->state & SRV_BACKUP) ? "-" : "Y",
2969 (sv->state & SRV_BACKUP) ? "Y" : "-");
2970
2971 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002972 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002973 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002974
2975 if (ref->observe)
2976 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2977
2978 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002979 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 "<td>%lld</td><td>%s</td>"
2981 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002982 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2984 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002985 else if (sv != ref) {
2986 if (sv->state & SRV_MAINTAIN)
2987 chunk_appendf(&trash,
2988 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2989 ref->proxy->id, ref->id);
2990 else
2991 chunk_appendf(&trash,
2992 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2993 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2994 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995 else
2996 chunk_appendf(&trash, "<td colspan=3></td>");
2997
2998 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002999 if (sv->state & SRV_WARMINGUP)
3000 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003001 else
3002 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3003 }
3004 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003005 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 "DOWN,",
3007 "DOWN %d/%d,",
3008 "UP %d/%d,",
3009 "UP,",
3010 "NOLB %d/%d,",
3011 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003012 "DRAIN %d/%d,",
3013 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003014 "no check,"
3015 };
3016
3017 chunk_appendf(&trash,
3018 /* pxid, name */
3019 "%s,%s,"
3020 /* queue : current, max */
3021 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003022 /* sessions : current, max, limit, total */
3023 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003024 /* bytes : in, out */
3025 "%lld,%lld,"
3026 /* denied: req, resp */
3027 ",%lld,"
3028 /* errors : request, connect, response */
3029 ",%lld,%lld,"
3030 /* warnings: retries, redispatches */
3031 "%lld,%lld,"
3032 "",
3033 px->id, sv->id,
3034 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003035 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003036 sv->counters.bytes_in, sv->counters.bytes_out,
3037 sv->counters.failed_secu,
3038 sv->counters.failed_conns, sv->counters.failed_resp,
3039 sv->counters.retries, sv->counters.redispatches);
3040
3041 /* status */
3042 if (sv->state & SRV_MAINTAIN)
3043 chunk_appendf(&trash, "MAINT,");
3044 else if (ref != sv && ref->state & SRV_MAINTAIN)
3045 chunk_appendf(&trash, "MAINT(via),");
3046 else
3047 chunk_appendf(&trash,
3048 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09003049 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3050 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003051
3052 chunk_appendf(&trash,
3053 /* weight, active, backup */
3054 "%d,%d,%d,"
3055 "",
3056 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3057 (sv->state & SRV_BACKUP) ? 0 : 1,
3058 (sv->state & SRV_BACKUP) ? 1 : 0);
3059
3060 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003061 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003062 chunk_appendf(&trash,
3063 "%lld,%lld,%d,%d,",
3064 sv->counters.failed_checks, sv->counters.down_trans,
3065 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3066 else
3067 chunk_appendf(&trash, ",,,,");
3068
3069 /* queue limit, pid, iid, sid, */
3070 chunk_appendf(&trash,
3071 "%s,"
3072 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003073 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003074 relative_pid, px->uuid, sv->puid);
3075
3076 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01003077 if (sv->state & SRV_WARMINGUP)
3078 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003079
3080 /* sessions: lbtot */
3081 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3082
3083 /* tracked */
3084 if (sv->track)
3085 chunk_appendf(&trash, "%s/%s,",
3086 sv->track->proxy->id, sv->track->id);
3087 else
3088 chunk_appendf(&trash, ",");
3089
3090 /* type */
3091 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3092
3093 /* rate */
3094 chunk_appendf(&trash, "%u,,%u,",
3095 read_freq_ctr(&sv->sess_per_sec),
3096 sv->counters.sps_max);
3097
Willy Tarreauff5ae352013-12-11 20:36:34 +01003098 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003099 /* check_status */
3100 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3101
3102 /* check_code */
3103 if (sv->check.status >= HCHK_STATUS_L57DATA)
3104 chunk_appendf(&trash, "%u,", sv->check.code);
3105 else
3106 chunk_appendf(&trash, ",");
3107
3108 /* check_duration */
3109 if (sv->check.status >= HCHK_STATUS_CHECKED)
3110 chunk_appendf(&trash, "%lu,", sv->check.duration);
3111 else
3112 chunk_appendf(&trash, ",");
3113
3114 }
3115 else
3116 chunk_appendf(&trash, ",,,");
3117
3118 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3119 if (px->mode == PR_MODE_HTTP) {
3120 for (i=1; i<6; i++)
3121 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3122
3123 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3124 }
3125 else
3126 chunk_appendf(&trash, ",,,,,,");
3127
3128 /* failed health analyses */
3129 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3130
3131 /* requests : req_rate, req_rate_max, req_tot, */
3132 chunk_appendf(&trash, ",,,");
3133
3134 /* errors: cli_aborts, srv_aborts */
3135 chunk_appendf(&trash, "%lld,%lld,",
3136 sv->counters.cli_aborts, sv->counters.srv_aborts);
3137
3138 /* compression: in, out, bypassed, comp_rsp */
3139 chunk_appendf(&trash, ",,,,");
3140
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003141 /* lastsess */
3142 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3143
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003144 /* finish with EOL */
3145 chunk_appendf(&trash, "\n");
3146 }
3147 return 1;
3148}
3149
3150/* Dumps a line for backend <px> to the trash for and uses the state from stream
3151 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3152 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3153 */
3154static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3155{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003156 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003157 struct chunk src;
3158 int i;
3159
3160 if (!(px->cap & PR_CAP_BE))
3161 return 0;
3162
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003163 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003164 return 0;
3165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003166 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003168 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003169 /* Column sub-heading for Enable or Disable server */
3170 chunk_appendf(&trash, "<td></td>");
3171 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003172 chunk_appendf(&trash,
3173 "<td class=ac>"
3174 /* name */
3175 "%s<a name=\"%s/Backend\"></a>"
3176 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3177 "",
3178 (flags & ST_SHLGNDS)?"<u>":"",
3179 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180
3181 if (flags & ST_SHLGNDS) {
3182 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003183 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003184 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3185
3186 /* cookie */
3187 if (px->cookie_name) {
3188 chunk_appendf(&trash, ", cookie: '");
3189 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3190 chunk_htmlencode(&trash, &src);
3191 chunk_appendf(&trash, "'");
3192 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003193 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003194 }
3195
3196 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003197 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003198 /* queue : current, max */
3199 "<td>%s</td><td>%s</td><td></td>"
3200 /* sessions rate : current, max, limit */
3201 "<td>%s</td><td>%s</td><td></td>"
3202 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003203 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003204 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3205 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206
3207 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003208 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003210 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003211 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003213 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3214 U2H(px->be_counters.cum_conn),
3215 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003216
Willy Tarreau466c9b52012-12-23 02:25:03 +01003217 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003219 chunk_appendf(&trash,
3220 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3221 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3222 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3223 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3224 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3225 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3226 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3227 "<tr><th>- other responses:</th><td>%s</td></tr>"
3228 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3229 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003230 U2H(px->be_counters.p.http.cum_req),
3231 U2H(px->be_counters.p.http.rsp[1]),
3232 U2H(px->be_counters.p.http.rsp[2]),
3233 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003234 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003235 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003236 U2H(px->be_counters.p.http.rsp[3]),
3237 U2H(px->be_counters.p.http.rsp[4]),
3238 U2H(px->be_counters.p.http.rsp[5]),
3239 U2H(px->be_counters.p.http.rsp[0]),
3240 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003241 }
3242
3243 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003244 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003245 /* sessions: lbtot, last */
3246 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003247 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003248 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003249 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003250 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003251 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003252 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003255 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003256 "<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 +01003257 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003258 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003259 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3260 px->be_counters.comp_in ?
3261 (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 +01003262 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3263
3264 chunk_appendf(&trash,
3265 /* denied: req, resp */
3266 "<td>%s</td><td>%s</td>"
3267 /* errors : request, connect */
3268 "<td></td><td>%s</td>"
3269 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003270 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003271 /* warnings: retries, redispatches */
3272 "<td>%lld</td><td>%lld</td>"
3273 /* backend status: reflect backend status (up/down): we display UP
3274 * if the backend has known working servers or if it has no server at
3275 * all (eg: for stats). Then we display the total weight, number of
3276 * active and backups. */
3277 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3278 "<td class=ac>%d</td><td class=ac>%d</td>"
3279 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003280 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3281 U2H(px->be_counters.failed_conns),
3282 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283 px->be_counters.cli_aborts,
3284 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003285 px->be_counters.retries, px->be_counters.redispatches,
3286 human_time(now.tv_sec - px->last_change, 1),
3287 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3288 "<font color=\"red\"><b>DOWN</b></font>",
3289 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3290 px->srv_act, px->srv_bck);
3291
3292 chunk_appendf(&trash,
3293 /* rest of backend: nothing, down transitions, total downtime, throttle */
3294 "<td class=ac>&nbsp;</td><td>%d</td>"
3295 "<td>%s</td>"
3296 "<td></td>"
3297 "</tr>",
3298 px->down_trans,
3299 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3300 }
3301 else { /* CSV mode */
3302 chunk_appendf(&trash,
3303 /* pxid, name */
3304 "%s,BACKEND,"
3305 /* queue : current, max */
3306 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003307 /* sessions : current, max, limit, total */
3308 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309 /* bytes : in, out */
3310 "%lld,%lld,"
3311 /* denied: req, resp */
3312 "%lld,%lld,"
3313 /* errors : request, connect, response */
3314 ",%lld,%lld,"
3315 /* warnings: retries, redispatches */
3316 "%lld,%lld,"
3317 /* backend status: reflect backend status (up/down): we display UP
3318 * if the backend has known working servers or if it has no server at
3319 * all (eg: for stats). Then we display the total weight, number of
3320 * active and backups. */
3321 "%s,"
3322 "%d,%d,%d,"
3323 /* rest of backend: nothing, down transitions, last change, total downtime */
3324 ",%d,%d,%d,,"
3325 /* pid, iid, sid, throttle, lbtot, tracked, type */
3326 "%d,%d,0,,%lld,,%d,"
3327 /* rate, rate_lim, rate_max, */
3328 "%u,,%u,"
3329 /* check_status, check_code, check_duration */
3330 ",,,",
3331 px->id,
3332 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3333 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3334 px->be_counters.bytes_in, px->be_counters.bytes_out,
3335 px->be_counters.denied_req, px->be_counters.denied_resp,
3336 px->be_counters.failed_conns, px->be_counters.failed_resp,
3337 px->be_counters.retries, px->be_counters.redispatches,
3338 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3339 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3340 px->srv_act, px->srv_bck,
3341 px->down_trans, (int)(now.tv_sec - px->last_change),
3342 px->srv?be_downtime(px):0,
3343 relative_pid, px->uuid,
3344 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3345 read_freq_ctr(&px->be_sess_per_sec),
3346 px->be_counters.sps_max);
3347
3348 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3349 if (px->mode == PR_MODE_HTTP) {
3350 for (i=1; i<6; i++)
3351 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3352 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3353 }
3354 else
3355 chunk_appendf(&trash, ",,,,,,");
3356
3357 /* failed health analyses */
3358 chunk_appendf(&trash, ",");
3359
3360 /* requests : req_rate, req_rate_max, req_tot, */
3361 chunk_appendf(&trash, ",,,");
3362
3363 /* errors: cli_aborts, srv_aborts */
3364 chunk_appendf(&trash, "%lld,%lld,",
3365 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3366
3367 /* compression: in, out, bypassed */
3368 chunk_appendf(&trash, "%lld,%lld,%lld,",
3369 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3370
3371 /* compression: comp_rsp */
3372 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3373
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003374 /* lastsess */
3375 chunk_appendf(&trash, "%d,", be_lastsession(px));
3376
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003377 /* finish with EOL */
3378 chunk_appendf(&trash, "\n");
3379 }
3380 return 1;
3381}
3382
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003383/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003385 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003386 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003387static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003389 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003390 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3391
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003394
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003395 /* 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 +02003396 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003397 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003398 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003399 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3400 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003401 }
3402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003404 "<form action=\"%s%s%s%s\" method=\"post\">",
3405 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003406 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3407 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003408 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 }
3410
3411 /* print a new table */
3412 chunk_appendf(&trash,
3413 "<table class=\"tbl\" width=\"100%%\">\n"
3414 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003415 "<th class=\"pxname\" width=\"10%%\">");
3416
3417 chunk_appendf(&trash,
3418 "<a name=\"%s\"></a>%s"
3419 "<a class=px href=\"#%s\">%s</a>",
3420 px->id,
3421 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3422 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003423
3424 if (uri->flags & ST_SHLGNDS) {
3425 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003426 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3428 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003429 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430 }
3431
3432 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003433 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003434 "<th class=\"%s\" width=\"90%%\">%s</th>"
3435 "</tr>\n"
3436 "</table>\n"
3437 "<table class=\"tbl\" width=\"100%%\">\n"
3438 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003439 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3440 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3441
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003442 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 /* Column heading for Enable or Disable server */
3444 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003445 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446
3447 chunk_appendf(&trash,
3448 "<th rowspan=2></th>"
3449 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003450 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3452 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3453 "<th colspan=9>Server</th>"
3454 "</tr>\n"
3455 "<tr class=\"titre\">"
3456 "<th>Cur</th><th>Max</th><th>Limit</th>"
3457 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003458 "<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 +01003459 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3460 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3461 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3462 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3463 "<th>Thrtle</th>\n"
3464 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003465}
3466
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003467/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003468 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003469 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003470static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003471{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003472 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 chunk_appendf(&trash, "</table>");
3474
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003475 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 /* close the form used to enable/disable this proxy servers */
3477 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003478 "Choose the action to perform on the checked servers : "
3479 "<select name=action>"
3480 "<option value=\"\"></option>"
3481 "<option value=\"disable\">Disable</option>"
3482 "<option value=\"enable\">Enable</option>"
3483 "<option value=\"stop\">Soft Stop</option>"
3484 "<option value=\"start\">Soft Start</option>"
3485 "<option value=\"shutdown\">Kill Sessions</option>"
3486 "</select>"
3487 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3488 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3489 "</form>",
3490 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003491 }
3492
3493 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494}
Willy Tarreau91861262007-10-17 17:06:05 +02003495
3496/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003497 * Dumps statistics for a proxy. The output is sent to the stream interface's
3498 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3499 * buffer space, or non-zero if everything completed. This function is used
3500 * both by the CLI and the HTTP entry points, and is able to dump the output
3501 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003502 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003503static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003504{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003505 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003506 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003507 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003508 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003509 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003510
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003511 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003512
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003513 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003514 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003515 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003516 if (uri && uri->scope) {
3517 /* we have a limited scope, we have to check the proxy name */
3518 struct stat_scope *scope;
3519 int len;
3520
3521 len = strlen(px->id);
3522 scope = uri->scope;
3523
3524 while (scope) {
3525 /* match exact proxy name */
3526 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3527 break;
3528
3529 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003530 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003531 break;
3532 scope = scope->next;
3533 }
3534
3535 /* proxy name not found : don't dump anything */
3536 if (scope == NULL)
3537 return 1;
3538 }
3539
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003540 /* if the user has requested a limited output and the proxy
3541 * name does not match, skip it.
3542 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003543 if (appctx->ctx.stats.scope_len &&
3544 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 +02003545 return 1;
3546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3548 (appctx->ctx.stats.iid != -1) &&
3549 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003550 return 1;
3551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003552 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003553 /* fall through */
3554
Willy Tarreau295a8372011-03-10 11:25:07 +01003555 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003556 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003557 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003558 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003559 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003560 }
Willy Tarreau91861262007-10-17 17:06:05 +02003561
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003562 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003563 /* fall through */
3564
Willy Tarreau295a8372011-03-10 11:25:07 +01003565 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003566 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003567 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003568 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003569 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003571 appctx->ctx.stats.l = px->conf.listeners.n;
3572 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003573 /* fall through */
3574
Willy Tarreau295a8372011-03-10 11:25:07 +01003575 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003576 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003577 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
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 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003584 if (!l->counters)
3585 continue;
3586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003587 if (appctx->ctx.stats.flags & STAT_BOUND) {
3588 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003589 break;
3590
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003591 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003592 continue;
3593 }
3594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 /* print the frontend */
3596 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3597 if (bi_putchk(rep, &trash) == -1)
3598 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003599 }
3600
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003601 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3602 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003603 /* fall through */
3604
Willy Tarreau295a8372011-03-10 11:25:07 +01003605 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003606 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003607 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003608 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 +02003609
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003610 if (buffer_almost_full(rep->buf)) {
3611 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003612 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003613 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003614
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003615 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003617 if (appctx->ctx.stats.flags & STAT_BOUND) {
3618 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003619 break;
3620
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003621 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003622 continue;
3623 }
3624
Willy Tarreau44267702011-10-28 15:35:33 +02003625 if (sv->track)
3626 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003627 else
3628 svs = sv;
3629
Willy Tarreau91861262007-10-17 17:06:05 +02003630 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003631 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003632 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003633 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003634 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003635 sv_state = 3; /* UP */
3636 else
3637 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003638
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003639 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003640 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003641 else if (svs->state & SRV_GOINGDOWN)
3642 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003643 }
Willy Tarreau91861262007-10-17 17:06:05 +02003644 else
Simon Horman125d0992013-02-24 17:23:38 +09003645 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003646 sv_state = 1; /* going up */
3647 else
3648 sv_state = 0; /* DOWN */
3649
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003650 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003651 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003652 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003653 continue;
3654 }
3655
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003656 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3657 if (bi_putchk(rep, &trash) == -1)
3658 return 0;
3659 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003660
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003661 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003662 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003663
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003664 case STAT_PX_ST_BE:
3665 /* print the backend */
3666 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3667 if (bi_putchk(rep, &trash) == -1)
3668 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003669
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003670 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003671 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003672
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003673 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003674 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003675 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003676 if (bi_putchk(rep, &trash) == -1)
3677 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003678 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003679
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003680 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003681 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003682
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003683 case STAT_PX_ST_FIN:
3684 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003685
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003686 default:
3687 /* unknown state, we should put an abort() here ! */
3688 return 1;
3689 }
3690}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003691
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003692/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3693 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003695static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696{
3697 /* WARNING! This must fit in the first buffer !!! */
3698 chunk_appendf(&trash,
3699 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3700 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3701 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3702 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3703 "<style type=\"text/css\"><!--\n"
3704 "body {"
3705 " font-family: arial, helvetica, sans-serif;"
3706 " font-size: 12px;"
3707 " font-weight: normal;"
3708 " color: black;"
3709 " background: white;"
3710 "}\n"
3711 "th,td {"
3712 " font-size: 10px;"
3713 "}\n"
3714 "h1 {"
3715 " font-size: x-large;"
3716 " margin-bottom: 0.5em;"
3717 "}\n"
3718 "h2 {"
3719 " font-family: helvetica, arial;"
3720 " font-size: x-large;"
3721 " font-weight: bold;"
3722 " font-style: italic;"
3723 " color: #6020a0;"
3724 " margin-top: 0em;"
3725 " margin-bottom: 0em;"
3726 "}\n"
3727 "h3 {"
3728 " font-family: helvetica, arial;"
3729 " font-size: 16px;"
3730 " font-weight: bold;"
3731 " color: #b00040;"
3732 " background: #e8e8d0;"
3733 " margin-top: 0em;"
3734 " margin-bottom: 0em;"
3735 "}\n"
3736 "li {"
3737 " margin-top: 0.25em;"
3738 " margin-right: 2em;"
3739 "}\n"
3740 ".hr {margin-top: 0.25em;"
3741 " border-color: black;"
3742 " border-bottom-style: solid;"
3743 "}\n"
3744 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3745 ".total {background: #20D0D0;color: #ffff80;}\n"
3746 ".frontend {background: #e8e8d0;}\n"
3747 ".socket {background: #d0d0d0;}\n"
3748 ".backend {background: #e8e8d0;}\n"
3749 ".active0 {background: #ff9090;}\n"
3750 ".active1 {background: #ffd020;}\n"
3751 ".active2 {background: #ffffa0;}\n"
3752 ".active3 {background: #c0ffc0;}\n"
3753 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003754 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3755 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3756 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003757 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003758 ".backup0 {background: #ff9090;}\n"
3759 ".backup1 {background: #ff80ff;}\n"
3760 ".backup2 {background: #c060ff;}\n"
3761 ".backup3 {background: #b0d0ff;}\n"
3762 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3763 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003764 ".backup6 {background: #c060ff;}\n"
3765 ".backup7 {background: #cc9900;}\n"
3766 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003767 ".maintain {background: #c07820;}\n"
3768 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3769 "\n"
3770 "a.px:link {color: #ffff40; text-decoration: none;}"
3771 "a.px:visited {color: #ffff40; text-decoration: none;}"
3772 "a.px:hover {color: #ffffff; text-decoration: none;}"
3773 "a.lfsb:link {color: #000000; text-decoration: none;}"
3774 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3775 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3776 "\n"
3777 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3778 "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"
3779 "table.tbl td.ac { text-align: center;}\n"
3780 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3781 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3782 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3783 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3784 "\n"
3785 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3786 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3787 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003788 "table.det { border-collapse: collapse; border-style: none; }\n"
3789 "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 +01003790 "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 +01003791 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003792 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003793 " display:block;\n"
3794 " visibility:hidden;\n"
3795 " z-index:2147483647;\n"
3796 " position:absolute;\n"
3797 " padding:2px 4px 3px;\n"
3798 " background:#f0f060; color:#000000;\n"
3799 " border:1px solid #7040c0;\n"
3800 " white-space:nowrap;\n"
3801 " font-style:normal;font-size:11px;font-weight:normal;\n"
3802 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3803 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3804 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003805 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003806 "-->\n"
3807 "</style></head>\n",
3808 (uri->flags & ST_SHNODE) ? " on " : "",
3809 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3810 );
3811}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003812
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003813/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003814 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003815 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003816 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003817static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003818{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003819 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003820 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003821 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003823 /* WARNING! this has to fit the first packet too.
3824 * We are around 3.5 kB, add adding entries will
3825 * become tricky if we want to support 4kB buffers !
3826 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003827 chunk_appendf(&trash,
3828 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3829 PRODUCT_NAME "%s</a></h1>\n"
3830 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3831 "<hr width=\"100%%\" class=\"hr\">\n"
3832 "<h3>&gt; General process information</h3>\n"
3833 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3834 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3835 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3836 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3837 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3838 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3839 "Running tasks: %d/%d; idle = %d %%<br>\n"
3840 "</td><td align=\"center\" nowrap>\n"
3841 "<table class=\"lgd\"><tr>\n"
3842 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3843 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3844 "</tr><tr>\n"
3845 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3846 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3847 "</tr><tr>\n"
3848 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3849 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3850 "</tr><tr>\n"
3851 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003852 "</tr><tr>\n"
3853 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003854 "</tr><tr>\n"
3855 "<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 -07003856 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003857 "<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 +01003858 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003859 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 "</td>"
3861 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3862 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3863 "",
3864 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3865 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3866 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3867 (uri->flags & ST_SHDESC) ? ": " : "",
3868 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3869 pid, relative_pid, global.nbproc,
3870 up / 86400, (up % 86400) / 3600,
3871 (up % 3600) / 60, (up % 60),
3872 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3873 global.rlimit_memmax ? " MB" : "",
3874 global.rlimit_nofile,
3875 global.maxsock, global.maxconn, global.maxpipes,
3876 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3877 run_queue_cur, nb_tasks_cur, idle_pct
3878 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003879
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003880 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3881 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3882 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003883
3884 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003885 "<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 +02003886 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003887 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3888 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3889 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003890 STAT_SCOPE_TXT_MAXLEN);
3891
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003892 /* 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 +02003893 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003894 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003895 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003896 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3897 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003898 }
3899
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003900 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003901 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003902 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003903 uri->uri_prefix,
3904 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003905 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003906 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003907 else
3908 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003909 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910 uri->uri_prefix,
3911 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003912 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003913 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003914
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003915 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003916 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003917 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003918 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003919 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003920 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003921 "",
3922 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923 else
3924 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003925 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003926 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003927 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003928 ";norefresh",
3929 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003931
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003932 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003933 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003934 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003935 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3936 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003937 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003938
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003939 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003940 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003941 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003942 (uri->refresh > 0) ? ";norefresh" : "",
3943 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003944
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003945 chunk_appendf(&trash,
3946 "</ul></td>"
3947 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3948 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3949 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3950 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3951 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3952 "</ul>"
3953 "</td>"
3954 "</tr></table>\n"
3955 ""
3956 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003957
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003958 if (appctx->ctx.stats.st_code) {
3959 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003960 case STAT_STATUS_DONE:
3961 chunk_appendf(&trash,
3962 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003963 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003964 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003965 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003966 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3967 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003968 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003969 break;
3970 case STAT_STATUS_NONE:
3971 chunk_appendf(&trash,
3972 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003973 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003974 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003975 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003976 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3977 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003978 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003979 break;
3980 case STAT_STATUS_PART:
3981 chunk_appendf(&trash,
3982 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003983 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003984 "Action partially processed.<br>"
3985 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003986 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003987 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3988 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003989 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003990 break;
3991 case STAT_STATUS_ERRP:
3992 chunk_appendf(&trash,
3993 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003994 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 "Action not processed because of invalid parameters."
3996 "<ul>"
3997 "<li>The action is maybe unknown.</li>"
3998 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3999 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4000 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004001 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004002 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4003 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004004 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 break;
4006 case STAT_STATUS_EXCD:
4007 chunk_appendf(&trash,
4008 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004009 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004010 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4011 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004012 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004013 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4014 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004015 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016 break;
4017 case STAT_STATUS_DENY:
4018 chunk_appendf(&trash,
4019 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004020 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004022 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004023 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4024 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004025 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004026 break;
4027 default:
4028 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09004029 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004030 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004031 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004032 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004033 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4034 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004035 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004036 }
4037 chunk_appendf(&trash, "<p>\n");
4038 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004040
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004041/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4042 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004043 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004044static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004045{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004046 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004047}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004048
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004049/* This function dumps statistics onto the stream interface's read buffer in
4050 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004051 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4052 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4053 * and the session must be closed, or -1 in case of any error. This function is
4054 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004055 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004056static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004057{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004058 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004059 struct channel *rep = si->ib;
4060 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004061
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004062 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004063
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004064 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004065 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004066 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004067 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004068
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004069 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004070 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004071 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004072 else
4073 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004074
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004075 if (bi_putchk(rep, &trash) == -1)
4076 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004077
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004078 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004080
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004081 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004082 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004083 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004084 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004085 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004086 }
Willy Tarreau91861262007-10-17 17:06:05 +02004087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004088 appctx->ctx.stats.px = proxy;
4089 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4090 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004091 /* fall through */
4092
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004093 case STAT_ST_LIST:
4094 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004095 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004096 if (buffer_almost_full(rep->buf)) {
4097 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004098 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004099 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004100
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004101 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004102 /* skip the disabled proxies, global frontend and non-networked ones */
4103 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004104 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004107 appctx->ctx.stats.px = px->next;
4108 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004109 }
4110 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004112 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004114
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004115 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004116 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004117 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004118 if (bi_putchk(rep, &trash) == -1)
4119 return 0;
4120 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004121
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004122 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004123 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004124
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004125 case STAT_ST_FIN:
4126 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004128 default:
4129 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004130 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004131 return -1;
4132 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004133}
Willy Tarreauae526782010-03-04 20:34:23 +01004134
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004135/* We reached the stats page through a POST request. The appctx is
4136 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004137 * Parse the posted data and enable/disable servers if necessary.
4138 * Returns 1 if request was parsed or zero if it needs more data.
4139 */
4140static int stats_process_http_post(struct stream_interface *si)
4141{
4142 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004143 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004144
4145 struct proxy *px = NULL;
4146 struct server *sv = NULL;
4147
4148 char key[LINESIZE];
4149 int action = ST_ADM_ACTION_NONE;
4150 int reprocess = 0;
4151
4152 int total_servers = 0;
4153 int altered_servers = 0;
4154
4155 char *first_param, *cur_param, *next_param, *end_params;
4156 char *st_cur_param = NULL;
4157 char *st_next_param = NULL;
4158
4159 struct chunk *temp;
4160 int reql;
4161
4162 temp = get_trash_chunk();
4163 if (temp->size < s->txn.req.body_len) {
4164 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004165 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004166 goto out;
4167 }
4168
4169 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4170 if (reql <= 0) {
4171 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004172 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004173 return 0;
4174 }
4175
4176 first_param = temp->str;
4177 end_params = temp->str + reql;
4178 cur_param = next_param = end_params;
4179 *end_params = '\0';
4180
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004181 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004182
4183 /*
4184 * Parse the parameters in reverse order to only store the last value.
4185 * From the html form, the backend and the action are at the end.
4186 */
4187 while (cur_param > first_param) {
4188 char *value;
4189 int poffset, plen;
4190
4191 cur_param--;
4192
4193 if ((*cur_param == '&') || (cur_param == first_param)) {
4194 reprocess_servers:
4195 /* Parse the key */
4196 poffset = (cur_param != first_param ? 1 : 0);
4197 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4198 if ((plen > 0) && (plen <= sizeof(key))) {
4199 strncpy(key, cur_param + poffset, plen);
4200 key[plen - 1] = '\0';
4201 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004202 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004203 goto out;
4204 }
4205
4206 /* Parse the value */
4207 value = key;
4208 while (*value != '\0' && *value != '=') {
4209 value++;
4210 }
4211 if (*value == '=') {
4212 /* Ok, a value is found, we can mark the end of the key */
4213 *value++ = '\0';
4214 }
4215 if (url_decode(key) < 0 || url_decode(value) < 0)
4216 break;
4217
4218 /* Now we can check the key to see what to do */
4219 if (!px && (strcmp(key, "b") == 0)) {
4220 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4221 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004222 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004223 goto out;
4224 }
4225 }
4226 else if (!action && (strcmp(key, "action") == 0)) {
4227 if (strcmp(value, "disable") == 0) {
4228 action = ST_ADM_ACTION_DISABLE;
4229 }
4230 else if (strcmp(value, "enable") == 0) {
4231 action = ST_ADM_ACTION_ENABLE;
4232 }
4233 else if (strcmp(value, "stop") == 0) {
4234 action = ST_ADM_ACTION_STOP;
4235 }
4236 else if (strcmp(value, "start") == 0) {
4237 action = ST_ADM_ACTION_START;
4238 }
4239 else if (strcmp(value, "shutdown") == 0) {
4240 action = ST_ADM_ACTION_SHUTDOWN;
4241 }
4242 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004243 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004244 goto out;
4245 }
4246 }
4247 else if (strcmp(key, "s") == 0) {
4248 if (!(px && action)) {
4249 /*
4250 * Indicates that we'll need to reprocess the parameters
4251 * as soon as backend and action are known
4252 */
4253 if (!reprocess) {
4254 st_cur_param = cur_param;
4255 st_next_param = next_param;
4256 }
4257 reprocess = 1;
4258 }
4259 else if ((sv = findserver(px, value)) != NULL) {
4260 switch (action) {
4261 case ST_ADM_ACTION_DISABLE:
4262 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4263 /* Not already in maintenance, we can change the server state */
4264 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004265 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004266 set_server_down(&sv->check);
4267 altered_servers++;
4268 total_servers++;
4269 }
4270 break;
4271 case ST_ADM_ACTION_ENABLE:
4272 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004273 /* Already in maintenance, we can change the server state.
4274 * If this server tracks the status of another one,
4275 * we must restore the good status.
4276 */
4277 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4278 set_server_up(&sv->check);
4279 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4280 }
4281 else {
4282 sv->state &= ~SRV_MAINTAIN;
4283 sv->check.state &= ~CHK_ST_PAUSED;
4284 set_server_down(&sv->check);
4285 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004286 altered_servers++;
4287 total_servers++;
4288 }
4289 break;
4290 case ST_ADM_ACTION_STOP:
4291 case ST_ADM_ACTION_START:
4292 if (action == ST_ADM_ACTION_START)
4293 sv->uweight = sv->iweight;
4294 else
4295 sv->uweight = 0;
4296
4297 server_recalc_eweight(sv);
4298 set_server_drain_state(sv);
4299
4300 altered_servers++;
4301 total_servers++;
4302 break;
4303 case ST_ADM_ACTION_SHUTDOWN:
4304 if (px->state != PR_STSTOPPED) {
4305 struct session *sess, *sess_bck;
4306
4307 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4308 if (sess->srv_conn == sv)
4309 session_shutdown(sess, SN_ERR_KILLED);
4310
4311 altered_servers++;
4312 total_servers++;
4313 }
4314 break;
4315 }
4316 } else {
4317 /* the server name is unknown or ambiguous (duplicate names) */
4318 total_servers++;
4319 }
4320 }
4321 if (reprocess && px && action) {
4322 /* Now, we know the backend and the action chosen by the user.
4323 * We can safely restart from the first server parameter
4324 * to reprocess them
4325 */
4326 cur_param = st_cur_param;
4327 next_param = st_next_param;
4328 reprocess = 0;
4329 goto reprocess_servers;
4330 }
4331
4332 next_param = cur_param;
4333 }
4334 }
4335
4336 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004337 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004338 }
4339 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004340 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004341 }
4342 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004343 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004344 }
4345 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004346 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004347 }
4348 out:
4349 return 1;
4350}
4351
4352
4353static int stats_send_http_headers(struct stream_interface *si)
4354{
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
4359 chunk_printf(&trash,
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004360 "HTTP/1.%c 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004361 "Cache-Control: no-cache\r\n"
4362 "Connection: close\r\n"
4363 "Content-Type: %s\r\n",
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004364 (appctx->ctx.stats.flags & STAT_CHUNKED) ? '1' : '0',
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004365 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004366
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004367 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004368 chunk_appendf(&trash, "Refresh: %d\r\n",
4369 uri->refresh);
4370
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004371 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4372
4373 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4374 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4375 else
4376 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004377
4378 s->txn.status = 200;
4379 s->logs.tv_request = now;
4380
4381 if (bi_putchk(si->ib, &trash) == -1)
4382 return 0;
4383
4384 return 1;
4385}
4386
4387static int stats_send_http_redirect(struct stream_interface *si)
4388{
4389 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4390 struct session *s = session_from_task(si->owner);
4391 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004393
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004394 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004395 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004396 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004397 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004398 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4399 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004400 }
4401
4402 /* We don't want to land on the posted stats page because a refresh will
4403 * repost the data. We don't want this to happen on accident so we redirect
4404 * the browse to the stats page with a GET.
4405 */
4406 chunk_printf(&trash,
4407 "HTTP/1.1 303 See Other\r\n"
4408 "Cache-Control: no-cache\r\n"
4409 "Content-Type: text/plain\r\n"
4410 "Connection: close\r\n"
4411 "Location: %s;st=%s%s%s%s\r\n"
4412 "\r\n",
4413 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004414 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4415 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4416 stat_status_codes[appctx->ctx.stats.st_code]) ?
4417 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004418 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004419 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4420 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004421 scope_txt);
4422
4423 s->txn.status = 303;
4424 s->logs.tv_request = now;
4425
4426 if (bi_putchk(si->ib, &trash) == -1)
4427 return 0;
4428
4429 return 1;
4430}
4431
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004432/* This I/O handler runs as an applet embedded in a stream interface. It is
4433 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004434 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004435 * automatically unregisters itself once transfer is complete.
4436 */
4437static void http_stats_io_handler(struct stream_interface *si)
4438{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004440 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004441 struct channel *req = si->ob;
4442 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004443
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004444 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4445 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004446
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004447 /* check that the output is not closed */
4448 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004449 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004450
Willy Tarreau347a35d2013-11-22 17:51:09 +01004451 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004452 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004453 if (stats_send_http_headers(si)) {
4454 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004455 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004456 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004457 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004458 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004459 }
4460
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004461 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004462 unsigned int prev_len = si->ib->buf->i;
4463 unsigned int data_len;
4464 unsigned int last_len;
4465 unsigned int last_fwd;
4466
4467 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4468 /* One difficulty we're facing is that we must prevent
4469 * the input data from being automatically forwarded to
4470 * the output area. For this, we temporarily disable
4471 * forwarding on the channel.
4472 */
4473 last_fwd = si->ib->to_forward;
4474 si->ib->to_forward = 0;
4475 chunk_printf(&trash, "\r\n000000\r\n");
4476 if (bi_putchk(si->ib, &trash) == -1) {
4477 si->ib->to_forward = last_fwd;
4478 goto fail;
4479 }
4480 }
4481
4482 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004483 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004485
4486 last_len = si->ib->buf->i;
4487
4488 /* Now we must either adjust or remove the chunk size. This is
4489 * not easy because the chunk size might wrap at the end of the
4490 * buffer, so we pretend we have nothing in the buffer, we write
4491 * the size, then restore the buffer's contents. Note that we can
4492 * only do that because no forwarding is scheduled on the stats
4493 * applet.
4494 */
4495 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4496 si->ib->total -= (last_len - prev_len);
4497 si->ib->buf->i -= (last_len - prev_len);
4498
4499 if (last_len != data_len) {
4500 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4501 bi_putchk(si->ib, &trash);
4502
4503 si->ib->total += (last_len - data_len);
4504 si->ib->buf->i += (last_len - data_len);
4505 }
4506 /* now re-enable forwarding */
4507 channel_forward(si->ib, last_fwd);
4508 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004509 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004511 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004512 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004513 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004514 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004515 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004516 }
4517
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004518 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004519 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004521 }
4522
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004523 if (appctx->st0 == STAT_HTTP_DONE) {
4524 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4525 chunk_printf(&trash, "\r\n0\r\n\r\n");
4526 if (bi_putchk(si->ib, &trash) == -1)
4527 goto fail;
4528 }
4529 /* eat the whole request */
4530 bo_skip(si->ob, si->ob->buf->o);
4531 res->flags |= CF_READ_NULL;
4532 si_shutr(si);
4533 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004534
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004535 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4536 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004537
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004538 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004539 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4540 si_shutr(si);
4541 res->flags |= CF_READ_NULL;
4542 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004543 }
Willy Tarreau91861262007-10-17 17:06:05 +02004544
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004545 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004546 /* update all other flags and resync with the other side */
4547 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004548
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004549 /* we don't want to expire timeouts while we're processing requests */
4550 si->ib->rex = TICK_ETERNITY;
4551 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004552
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004553 out:
4554 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4555 /* check that we have released everything then unregister */
4556 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004557 }
4558}
4559
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004560
Willy Tarreau909d5172012-11-26 03:04:41 +01004561static inline const char *get_conn_ctrl_name(const struct connection *conn)
4562{
Willy Tarreau3c728722014-01-23 13:50:42 +01004563 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004564 return "NONE";
4565 return conn->ctrl->name;
4566}
4567
4568static inline const char *get_conn_xprt_name(const struct connection *conn)
4569{
4570 static char ptr[17];
4571
Willy Tarreauaad69382014-01-23 14:21:42 +01004572 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004573 return "NONE";
4574
4575 if (conn->xprt == &raw_sock)
4576 return "RAW";
4577
4578#ifdef USE_OPENSSL
4579 if (conn->xprt == &ssl_sock)
4580 return "SSL";
4581#endif
4582 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4583 return ptr;
4584}
4585
4586static inline const char *get_conn_data_name(const struct connection *conn)
4587{
4588 static char ptr[17];
4589
4590 if (!conn->data)
4591 return "NONE";
4592
4593 if (conn->data == &sess_conn_cb)
4594 return "SESS";
4595
4596 if (conn->data == &si_conn_cb)
4597 return "STRM";
4598
4599 if (conn->data == &check_conn_cb)
4600 return "CHCK";
4601
4602 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4603 return ptr;
4604}
4605
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004606/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004607 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004608 * 0 if the output buffer is full and it needs to be called again, otherwise
4609 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004610 */
Willy Tarreau76153662012-11-26 01:16:39 +01004611static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004612{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004613 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004614 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004615 extern const char *monthname[12];
4616 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004617 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004618 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004619
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004620 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004621
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004622 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004623 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004624 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4625 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004626 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004627 appctx->ctx.sess.uid = 0;
4628 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004629 return 1;
4630 }
4631
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004632 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004633 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004634 appctx->ctx.sess.uid = sess->uniq_id;
4635 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004636 /* fall through */
4637
4638 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004639 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004640 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004641 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004642 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004643 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4644 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004645 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004646 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004647
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004648 conn = objt_conn(sess->si[0].end);
4649 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004650 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004651 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004652 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004653 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004654 break;
4655 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004656 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004657 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004658 default:
4659 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004660 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004661 break;
4662 }
4663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004664 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004665 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004666 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004667
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004668 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004669 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004670 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4671 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4672 sess->listener ? sess->listener->luid : 0);
4673
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004674 if (conn)
4675 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004676
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004677 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004678 case AF_INET:
4679 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004680 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004682 break;
4683 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004685 break;
4686 default:
4687 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004688 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004689 break;
4690 }
4691
Willy Tarreau50943332011-09-02 17:33:05 +02004692 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004693 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004694 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004695 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004696 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4697 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004698 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004699
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004700 conn = objt_conn(sess->si[1].end);
4701 if (conn)
4702 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004703
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004704 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004705 case AF_INET:
4706 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004707 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004708 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004709 break;
4710 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004711 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004712 break;
4713 default:
4714 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004715 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004716 break;
4717 }
4718
4719 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004720 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004721 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004722 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4723 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004724 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004726
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004727 if (conn)
4728 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004729
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004730 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004731 case AF_INET:
4732 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004733 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004734 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004735 break;
4736 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004737 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004738 break;
4739 default:
4740 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004741 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004742 break;
4743 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004744
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004745 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004746 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004747 sess->task,
4748 sess->task->state,
4749 sess->task->nice, sess->task->calls,
4750 sess->task->expire ?
4751 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4752 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4753 TICKS_TO_MS(1000)) : "<NEVER>",
4754 task_in_rq(sess->task) ? ", running" : "");
4755
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004756 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004757 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004758 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4759
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004760 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004761 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4762 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4763 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4764
4765 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004766 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004767 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004768 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004769 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004770 obj_type_name(sess->si[0].end),
4771 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004772 sess->si[0].exp ?
4773 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4774 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4775 TICKS_TO_MS(1000)) : "<NEVER>",
4776 sess->si[0].err_type);
4777
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004778 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004779 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004780 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004781 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004782 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004783 obj_type_name(sess->si[1].end),
4784 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004785 sess->si[1].exp ?
4786 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4787 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4788 TICKS_TO_MS(1000)) : "<NEVER>",
4789 sess->si[1].err_type);
4790
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004791 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004792 chunk_appendf(&trash,
4793 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004794 conn,
4795 get_conn_ctrl_name(conn),
4796 get_conn_xprt_name(conn),
4797 get_conn_data_name(conn),
4798 obj_type_name(conn->target),
4799 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004800
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004801 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004802 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004803 conn->flags,
4804 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004805 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004806 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004807 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004808 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004809 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4810 chunk_appendf(&trash,
4811 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4812 tmpctx,
4813 tmpctx->st0,
4814 tmpctx->st1,
4815 tmpctx->st2,
4816 tmpctx->applet->name);
4817 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004818
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004819 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004820 chunk_appendf(&trash,
4821 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004822 conn,
4823 get_conn_ctrl_name(conn),
4824 get_conn_xprt_name(conn),
4825 get_conn_data_name(conn),
4826 obj_type_name(conn->target),
4827 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004828
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004829 chunk_appendf(&trash,
4830 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004831 conn->flags,
4832 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004833 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004834 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004835 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004836 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004837 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4838 chunk_appendf(&trash,
4839 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4840 tmpctx,
4841 tmpctx->st0,
4842 tmpctx->st1,
4843 tmpctx->st2,
4844 tmpctx->applet->name);
4845 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004846
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004847 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004848 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004849 " an_exp=%s",
4850 sess->req,
4851 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004852 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004853 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004854 sess->req->analyse_exp ?
4855 human_time(TICKS_TO_MS(sess->req->analyse_exp - 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 " rex=%s",
4860 sess->req->rex ?
4861 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4862 TICKS_TO_MS(1000)) : "<NEVER>");
4863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004864 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004865 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004866 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004867 sess->req->wex ?
4868 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4869 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004870 sess->req->buf,
4871 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004872 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004873 sess->txn.req.next, sess->req->buf->i,
4874 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004875
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004876 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004877 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004878 " an_exp=%s",
4879 sess->rep,
4880 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004881 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004882 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004883 sess->rep->analyse_exp ?
4884 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4885 TICKS_TO_MS(1000)) : "<NEVER>");
4886
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004887 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004888 " rex=%s",
4889 sess->rep->rex ?
4890 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4891 TICKS_TO_MS(1000)) : "<NEVER>");
4892
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004893 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004894 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004895 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004896 sess->rep->wex ?
4897 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4898 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004899 sess->rep->buf,
4900 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004901 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004902 sess->txn.rsp.next, sess->rep->buf->i,
4903 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004904
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004905 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004906 return 0;
4907
4908 /* use other states to dump the contents */
4909 }
4910 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004911 appctx->ctx.sess.uid = 0;
4912 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004913 return 1;
4914}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004915
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004916static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004917{
4918 struct appctx *appctx = __objt_appctx(si->end);
4919
4920 switch (appctx->st2) {
4921 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004922 /* Display the column headers. If the message cannot be sent,
4923 * quit the fucntion with returning 0. The function is called
4924 * later and restart at the state "STAT_ST_INIT".
4925 */
4926 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01004927 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004928 if (bi_putchk(si->ib, &trash) == -1)
4929 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004930
4931 /* Now, we start the browsing of the references lists.
4932 * Note that the following call to LIST_ELEM return bad pointer. The only
4933 * avalaible field of this pointer is <list>. It is used with the function
4934 * pat_list_get_next() for retruning the first avalaible entry
4935 */
4936 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
4937 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4938 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004939 appctx->st2 = STAT_ST_LIST;
4940 /* fall through */
4941
4942 case STAT_ST_LIST:
4943 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004944 chunk_reset(&trash);
4945
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004946 /* Build messages. If the reference is used by another category than
4947 * the listed categorie, display the information in the massage.
4948 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01004949 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01004950 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
4951 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004952
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004953 if (bi_putchk(si->ib, &trash) == -1) {
4954 /* let's try again later from this session. We add ourselves into
4955 * this session's users so that it can remove us upon termination.
4956 */
4957 return 0;
4958 }
4959
4960 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004961 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4962 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004963 }
4964
4965 appctx->st2 = STAT_ST_FIN;
4966 /* fall through */
4967
4968 default:
4969 appctx->st2 = STAT_ST_FIN;
4970 return 1;
4971 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004972 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004973}
4974
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004975static int stats_map_lookup(struct stream_interface *si)
4976{
4977 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004978 struct sample sample;
4979 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004980 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004981
4982 switch (appctx->st2) {
4983 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004984 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01004985 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004986 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004987 appctx->st2 = STAT_ST_LIST;
4988 /* fall through */
4989
4990 case STAT_ST_LIST:
4991 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004992 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004993 /* initialise chunk to build new message */
4994 chunk_reset(&trash);
4995
4996 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004997 sample.type = SMP_T_STR;
4998 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004999 sample.data.str.len = appctx->ctx.map.chunk.len;
5000 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005001 if (appctx->ctx.map.expr->pat_head->match &&
5002 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005003 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5004 else
5005 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005006
5007 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005008 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5009 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5010 break;
5011 if (match_method >= PAT_MATCH_NUM)
5012 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5013 else
5014 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005015
5016 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005017 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005018 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5019 chunk_appendf(&trash, ", found=no");
5020 else
5021 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005022 }
5023
5024 /* Display match and match info */
5025 else {
5026 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005027 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5028 chunk_appendf(&trash, ", found=yes");
5029 else
5030 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005031
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005032 /* display index mode */
5033 if (pat->flags & PAT_F_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005034 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005035 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005036 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005037
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005038 /* case sensitive */
5039 if (pat->flags & PAT_F_IGNORE_CASE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005040 chunk_appendf(&trash, ", case=insensitive");
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005041 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005042 chunk_appendf(&trash, ", case=sensitive");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005043
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005044 /* display pattern */
5045 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5046 if (pat->ref && pat->ref->pattern)
5047 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5048 else
5049 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005050 }
5051 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005052 if (pat->ref && pat->ref->pattern)
5053 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5054 else
5055 chunk_appendf(&trash, ", pattern=unknown");
5056 }
5057
5058 /* display return value */
5059 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5060 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005061 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005062 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005063 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005064 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005065 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005066 }
5067
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005068 chunk_appendf(&trash, "\n");
5069
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005070 /* display response */
5071 if (bi_putchk(si->ib, &trash) == -1) {
5072 /* let's try again later from this session. We add ourselves into
5073 * this session's users so that it can remove us upon termination.
5074 */
5075 return 0;
5076 }
5077
5078 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005079 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5080 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005081 }
5082
5083 appctx->st2 = STAT_ST_FIN;
5084 /* fall through */
5085
5086 default:
5087 appctx->st2 = STAT_ST_FIN;
5088 free(appctx->ctx.map.chunk.str);
5089 return 1;
5090 }
5091}
5092
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005093static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005094{
5095 struct appctx *appctx = __objt_appctx(si->end);
5096
5097 switch (appctx->st2) {
5098
5099 case STAT_ST_INIT:
5100 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005101 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5102 struct pat_ref_elt *, list);
5103 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5104 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005105 appctx->st2 = STAT_ST_LIST;
5106 /* fall through */
5107
5108 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005109 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005110 chunk_reset(&trash);
5111
5112 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005113 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005114 chunk_appendf(&trash, "%p %s %s\n",
5115 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5116 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005117 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005118 chunk_appendf(&trash, "%p %s\n",
5119 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005120
5121 if (bi_putchk(si->ib, &trash) == -1) {
5122 /* let's try again later from this session. We add ourselves into
5123 * this session's users so that it can remove us upon termination.
5124 */
5125 return 0;
5126 }
5127
5128 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005129 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5130 struct pat_ref_elt *, list);
5131 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005132 break;
5133 }
5134
5135 appctx->st2 = STAT_ST_FIN;
5136 /* fall through */
5137
5138 default:
5139 appctx->st2 = STAT_ST_FIN;
5140 return 1;
5141 }
5142}
5143
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005144/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005145 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005146 * to be called again, otherwise non-zero. It is designed to be called
5147 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005148 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005149static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005150{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005151 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005152 struct connection *conn;
5153
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005154 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005155 /* If we're forced to shut down, we might have to remove our
5156 * reference to the last session being dumped.
5157 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005158 if (appctx->st2 == STAT_ST_LIST) {
5159 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5160 LIST_DEL(&appctx->ctx.sess.bref.users);
5161 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005162 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005163 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005164 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005165 }
5166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005167 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005168
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005169 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005170 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005171 /* the function had not been called yet, let's prepare the
5172 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005173 * pointer to the first in the global list. When a target
5174 * session is being destroyed, it is responsible for updating
5175 * this pointer. We know we have reached the end when this
5176 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005177 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005178 LIST_INIT(&appctx->ctx.sess.bref.users);
5179 appctx->ctx.sess.bref.ref = sessions.n;
5180 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005181 /* fall through */
5182
Willy Tarreau295a8372011-03-10 11:25:07 +01005183 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005184 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005185 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5186 LIST_DEL(&appctx->ctx.sess.bref.users);
5187 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005188 }
5189
5190 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005191 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005192 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005193 struct session *curr_sess;
5194
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005195 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005196
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005197 if (appctx->ctx.sess.target) {
5198 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005199 goto next_sess;
5200
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005201 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005202 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005203 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005204 return 0;
5205
5206 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005207 LIST_DEL(&appctx->ctx.sess.bref.users);
5208 LIST_INIT(&appctx->ctx.sess.bref.users);
5209 if (appctx->ctx.sess.target != (void *)-1) {
5210 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005211 break;
5212 }
5213 else
5214 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005215 }
5216
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005217 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005218 "%p: proto=%s",
5219 curr_sess,
5220 curr_sess->listener->proto->name);
5221
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005222
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005223 conn = objt_conn(curr_sess->si[0].end);
5224 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005225 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005226 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005227 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005228 " src=%s:%d fe=%s be=%s srv=%s",
5229 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005230 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005231 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005232 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005233 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005234 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005235 break;
5236 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005237 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005238 " src=unix:%d fe=%s be=%s srv=%s",
5239 curr_sess->listener->luid,
5240 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005241 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005242 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005243 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005244 break;
5245 }
5246
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005247 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005248 " ts=%02x age=%s calls=%d",
5249 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005250 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5251 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005252
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005253 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005254 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005255 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005256 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005257 curr_sess->req->analysers,
5258 curr_sess->req->rex ?
5259 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5260 TICKS_TO_MS(1000)) : "");
5261
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005262 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005263 ",wx=%s",
5264 curr_sess->req->wex ?
5265 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5266 TICKS_TO_MS(1000)) : "");
5267
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005268 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005269 ",ax=%s]",
5270 curr_sess->req->analyse_exp ?
5271 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5272 TICKS_TO_MS(1000)) : "");
5273
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005274 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005275 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005276 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005277 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005278 curr_sess->rep->analysers,
5279 curr_sess->rep->rex ?
5280 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5281 TICKS_TO_MS(1000)) : "");
5282
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005283 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005284 ",wx=%s",
5285 curr_sess->rep->wex ?
5286 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5287 TICKS_TO_MS(1000)) : "");
5288
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005289 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005290 ",ax=%s]",
5291 curr_sess->rep->analyse_exp ?
5292 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5293 TICKS_TO_MS(1000)) : "");
5294
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005295 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005296 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005297 " s0=[%d,%1xh,fd=%d,ex=%s]",
5298 curr_sess->si[0].state,
5299 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005300 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005301 curr_sess->si[0].exp ?
5302 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5303 TICKS_TO_MS(1000)) : "");
5304
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005305 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005306 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005307 " s1=[%d,%1xh,fd=%d,ex=%s]",
5308 curr_sess->si[1].state,
5309 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005310 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005311 curr_sess->si[1].exp ?
5312 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5313 TICKS_TO_MS(1000)) : "");
5314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005315 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005316 " exp=%s",
5317 curr_sess->task->expire ?
5318 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5319 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005320 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005321 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005322
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005323 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005324
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005325 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005326 /* let's try again later from this session. We add ourselves into
5327 * this session's users so that it can remove us upon termination.
5328 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005329 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005330 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005331 }
5332
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005333 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005334 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005335 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005336
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005337 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005338 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005339 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005340 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005341 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005342 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005343
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005344 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005345 return 0;
5346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005347 appctx->ctx.sess.target = NULL;
5348 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005349 return 1;
5350 }
5351
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005352 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005353 /* fall through */
5354
5355 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005356 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005357 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005358 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005359}
5360
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005361/* This is called when the stream interface is closed. For instance, upon an
5362 * external abort, we won't call the i/o handler anymore so we may need to
5363 * remove back references to the session currently being dumped.
5364 */
Simon Horman74d88312013-02-12 10:45:50 +09005365static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005366{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005367 struct appctx *appctx = __objt_appctx(si->end);
5368
5369 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5370 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5371 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005372 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005373 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5374 free(appctx->ctx.cli.err);
5375 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005376 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5377 free(appctx->ctx.map.chunk.str);
5378 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005379}
5380
Willy Tarreau20e99322013-04-13 09:22:25 +02005381/* This function is used to either dump tables states (when action is set
5382 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005383 * It returns 0 if the output buffer is full and it needs to be called
5384 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005385 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005386static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005387{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005388 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005389 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005390 struct ebmb_node *eb;
5391 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005392 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005393 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005394
5395 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005396 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005397 * - STAT_ST_INIT : the first call
5398 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005399 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005400 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005401 * and the entry pointer points to the next entry to be dumped,
5402 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005403 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005404 * data though.
5405 */
5406
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005407 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005408 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005409 if (appctx->st2 == STAT_ST_LIST) {
5410 appctx->ctx.table.entry->ref_cnt--;
5411 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005412 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005413 return 1;
5414 }
5415
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005416 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005417
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005418 while (appctx->st2 != STAT_ST_FIN) {
5419 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005420 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005421 appctx->ctx.table.proxy = appctx->ctx.table.target;
5422 if (!appctx->ctx.table.proxy)
5423 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005424
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005425 appctx->ctx.table.entry = NULL;
5426 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005427 break;
5428
Willy Tarreau295a8372011-03-10 11:25:07 +01005429 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005430 if (!appctx->ctx.table.proxy ||
5431 (appctx->ctx.table.target &&
5432 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5433 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005434 break;
5435 }
5436
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005437 if (appctx->ctx.table.proxy->table.size) {
5438 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5439 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005440 return 0;
5441
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005442 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005443 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005444 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005445 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005446 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005447 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5448 appctx->ctx.table.entry->ref_cnt++;
5449 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005450 break;
5451 }
5452 }
5453 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005454 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005455 break;
5456
Willy Tarreau295a8372011-03-10 11:25:07 +01005457 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005458 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005459
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005460 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005461 /* we're filtering on some data contents */
5462 void *ptr;
5463 long long data;
5464
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005465 dt = appctx->ctx.table.data_type;
5466 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5467 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005468 dt);
5469
5470 data = 0;
5471 switch (stktable_data_types[dt].std_type) {
5472 case STD_T_SINT:
5473 data = stktable_data_cast(ptr, std_t_sint);
5474 break;
5475 case STD_T_UINT:
5476 data = stktable_data_cast(ptr, std_t_uint);
5477 break;
5478 case STD_T_ULL:
5479 data = stktable_data_cast(ptr, std_t_ull);
5480 break;
5481 case STD_T_FRQP:
5482 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005483 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005484 break;
5485 }
5486
5487 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005488 if ((data < appctx->ctx.table.value &&
5489 (appctx->ctx.table.data_op == STD_OP_EQ ||
5490 appctx->ctx.table.data_op == STD_OP_GT ||
5491 appctx->ctx.table.data_op == STD_OP_GE)) ||
5492 (data == appctx->ctx.table.value &&
5493 (appctx->ctx.table.data_op == STD_OP_NE ||
5494 appctx->ctx.table.data_op == STD_OP_GT ||
5495 appctx->ctx.table.data_op == STD_OP_LT)) ||
5496 (data > appctx->ctx.table.value &&
5497 (appctx->ctx.table.data_op == STD_OP_EQ ||
5498 appctx->ctx.table.data_op == STD_OP_LT ||
5499 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005500 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005501 }
5502
Simon Hormanc88b8872011-06-15 15:18:49 +09005503 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005504 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5505 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005506 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005508 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005509
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005510 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005511 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005512 struct stksess *old = appctx->ctx.table.entry;
5513 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005514 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005515 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5516 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5517 stksess_kill(&appctx->ctx.table.proxy->table, old);
5518 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005519 break;
5520 }
5521
Simon Hormanc88b8872011-06-15 15:18:49 +09005522
5523 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005524 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5525 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5526 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005528 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5529 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005530 break;
5531
Willy Tarreau295a8372011-03-10 11:25:07 +01005532 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005533 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005534 break;
5535 }
5536 }
5537 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005538}
5539
Willy Tarreaud426a182010-03-05 14:58:26 +01005540/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005541 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5542 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5543 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5544 * lines are respected within the limit of 70 output chars. Lines that are
5545 * continuation of a previous truncated line begin with "+" instead of " "
5546 * after the offset. The new pointer is returned.
5547 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005548static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5549 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005550{
5551 int end;
5552 unsigned char c;
5553
5554 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005555 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005556 return ptr;
5557
Willy Tarreau77804732012-10-29 16:14:26 +01005558 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005559
Willy Tarreaud426a182010-03-05 14:58:26 +01005560 while (ptr < len && ptr < bsize) {
5561 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005562 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005563 if (out->len > end - 2)
5564 break;
5565 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005566 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005567 if (out->len > end - 3)
5568 break;
5569 out->str[out->len++] = '\\';
5570 switch (c) {
5571 case '\t': c = 't'; break;
5572 case '\n': c = 'n'; break;
5573 case '\r': c = 'r'; break;
5574 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005575 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005576 }
5577 out->str[out->len++] = c;
5578 } else {
5579 if (out->len > end - 5)
5580 break;
5581 out->str[out->len++] = '\\';
5582 out->str[out->len++] = 'x';
5583 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5584 out->str[out->len++] = hextab[c & 0xF];
5585 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005586 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005587 /* we had a line break, let's return now */
5588 out->str[out->len++] = '\n';
5589 *line = ptr;
5590 return ptr;
5591 }
5592 }
5593 /* we have an incomplete line, we return it as-is */
5594 out->str[out->len++] = '\n';
5595 return ptr;
5596}
5597
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005598/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005599 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005600 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005601 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005602static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005603{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005604 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005605 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005606
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005607 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005608 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005610 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005611
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005612 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005613 /* the function had not been called yet, let's prepare the
5614 * buffer for a response.
5615 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005616 struct tm tm;
5617
5618 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005619 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005620 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5621 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5622 error_snapshot_id);
5623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005624 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005625 /* Socket buffer full. Let's try again later from the same point */
5626 return 0;
5627 }
5628
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005629 appctx->ctx.errors.px = proxy;
5630 appctx->ctx.errors.buf = 0;
5631 appctx->ctx.errors.bol = 0;
5632 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005633 }
5634
5635 /* we have two inner loops here, one for the proxy, the other one for
5636 * the buffer.
5637 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005639 struct error_snapshot *es;
5640
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005641 if (appctx->ctx.errors.buf == 0)
5642 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005643 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005644 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005645
5646 if (!es->when.tv_sec)
5647 goto next;
5648
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005649 if (appctx->ctx.errors.iid >= 0 &&
5650 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5651 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005652 goto next;
5653
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005654 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005655 /* just print headers now */
5656
5657 char pn[INET6_ADDRSTRLEN];
5658 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005659 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005660
5661 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005662 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005663 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005664 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005665
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005666 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5667 case AF_INET:
5668 case AF_INET6:
5669 port = get_host_port(&es->src);
5670 break;
5671 default:
5672 port = 0;
5673 }
5674
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005675 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005676 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005677 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005678 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005679 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005680 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005681 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5682 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005683 break;
5684 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005685 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005686 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005687 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005688 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005689 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005690 break;
5691 }
5692
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005693 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005694 ", server %s (#%d), event #%u\n"
5695 " src %s:%d, session #%d, session flags 0x%08x\n"
5696 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5697 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5698 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5699 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5700 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5701 es->ev_id,
5702 pn, port, es->sid, es->s_flags,
5703 es->state, es->m_flags, es->t_flags,
5704 es->m_clen, es->m_blen,
5705 es->b_flags, es->b_out, es->b_tot,
5706 es->len, es->b_wrap, es->pos);
5707
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005708 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005709 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005710 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005711 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005712 appctx->ctx.errors.ptr = 0;
5713 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005714 }
5715
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005716 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005717 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005718 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005719 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005720 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005721 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005722 goto next;
5723 }
5724
5725 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005726 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005727 int newptr;
5728 int newline;
5729
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005730 newline = appctx->ctx.errors.bol;
5731 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5732 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005733 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005734
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005735 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005736 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005737 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005738 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005739 appctx->ctx.errors.ptr = newptr;
5740 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005741 };
5742 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005743 appctx->ctx.errors.bol = 0;
5744 appctx->ctx.errors.ptr = -1;
5745 appctx->ctx.errors.buf++;
5746 if (appctx->ctx.errors.buf > 1) {
5747 appctx->ctx.errors.buf = 0;
5748 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005749 }
5750 }
5751
5752 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005753 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005754}
5755
Willy Tarreaud5781202012-09-22 19:32:35 +02005756/* parse the "level" argument on the bind lines */
5757static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5758{
5759 if (!*args[cur_arg + 1]) {
5760 memprintf(err, "'%s' : missing level", args[cur_arg]);
5761 return ERR_ALERT | ERR_FATAL;
5762 }
5763
5764 if (!strcmp(args[cur_arg+1], "user"))
5765 conf->level = ACCESS_LVL_USER;
5766 else if (!strcmp(args[cur_arg+1], "operator"))
5767 conf->level = ACCESS_LVL_OPER;
5768 else if (!strcmp(args[cur_arg+1], "admin"))
5769 conf->level = ACCESS_LVL_ADMIN;
5770 else {
5771 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5772 args[cur_arg], args[cur_arg+1]);
5773 return ERR_ALERT | ERR_FATAL;
5774 }
5775
5776 return 0;
5777}
5778
Willy Tarreaub24281b2011-02-13 13:16:36 +01005779struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005780 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005781 .name = "<STATS>", /* used for logging */
5782 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005783 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005784};
5785
Simon Horman9bd2c732011-06-15 15:18:44 +09005786static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005787 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005788 .name = "<CLI>", /* used for logging */
5789 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005790 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005791};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005792
Willy Tarreaudc13c112013-06-21 23:16:39 +02005793static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005794 { CFG_GLOBAL, "stats", stats_parse_global },
5795 { 0, NULL, NULL },
5796}};
5797
Willy Tarreaud5781202012-09-22 19:32:35 +02005798static struct bind_kw_list bind_kws = { "STAT", { }, {
5799 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5800 { NULL, NULL, 0 },
5801}};
5802
Willy Tarreau10522fd2008-07-09 20:12:41 +02005803__attribute__((constructor))
5804static void __dumpstats_module_init(void)
5805{
5806 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005807 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005808}
5809
Willy Tarreau91861262007-10-17 17:06:05 +02005810/*
5811 * Local variables:
5812 * c-indent-level: 8
5813 * c-basic-offset: 8
5814 * End:
5815 */