blob: 12bace2f5b6c502d276c7b3b11f39047105dacde [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010049#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010053#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020054#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020055#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010056#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020057#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020059#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010060#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010061#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020062
Willy Tarreau7a0169a2012-11-19 17:13:16 +010063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif
66
Willy Tarreau91b843d2014-01-28 16:27:17 +010067/* stats socket states */
68enum {
69 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
70 STAT_CLI_END, /* final state, let's close */
71 STAT_CLI_GETREQ, /* wait for a request */
72 STAT_CLI_OUTPUT, /* all states after this one are responses */
73 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
74 STAT_CLI_PRINT, /* display message in cli->msg */
75 STAT_CLI_O_INFO, /* dump info */
76 STAT_CLI_O_SESS, /* dump sessions */
77 STAT_CLI_O_ERR, /* dump errors */
78 STAT_CLI_O_TAB, /* dump tables */
79 STAT_CLI_O_CLR, /* clear tables */
80 STAT_CLI_O_SET, /* set entries in tables */
81 STAT_CLI_O_STAT, /* dump stats */
82 STAT_CLI_O_MAPS, /* list all maps */
83 STAT_CLI_O_MAP, /* list all map entries of a map */
84 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010085 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010086};
87
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010088static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +010089static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010090static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090091static int stats_dump_sess_to_buffer(struct stream_interface *si);
92static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010093static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010094static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
95static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010096static int stats_maps_list(struct stream_interface *si);
97static int stats_map_list(struct stream_interface *si);
98static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +090099
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100100/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100101 * cli_io_handler()
102 * -> stats_dump_sess_to_buffer() // "show sess"
103 * -> stats_dump_errors_to_buffer() // "show errors"
104 * -> stats_dump_info_to_buffer() // "show info"
105 * -> stats_dump_stat_to_buffer() // "show stat"
106 * -> stats_dump_csv_header()
107 * -> stats_dump_proxy_to_buffer()
108 * -> stats_dump_fe_stats()
109 * -> stats_dump_li_stats()
110 * -> stats_dump_sv_stats()
111 * -> stats_dump_be_stats()
112 *
113 * http_stats_io_handler()
114 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
115 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
116 * -> stats_dump_html_head() // emits the HTML headers
117 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
118 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
119 * -> stats_dump_html_px_hdr()
120 * -> stats_dump_fe_stats()
121 * -> stats_dump_li_stats()
122 * -> stats_dump_sv_stats()
123 * -> stats_dump_be_stats()
124 * -> stats_dump_html_px_end()
125 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100126 */
127
Simon Horman9bd2c732011-06-15 15:18:44 +0900128static struct si_applet cli_applet;
129
130static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200131 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200132 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200133 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200134 " help : this message\n"
135 " prompt : toggle interactive mode with prompt\n"
136 " quit : disconnect\n"
137 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100138 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200139 " show stat : report counters for each proxy and server\n"
140 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100141 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200142 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200143 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200144 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200145 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100146 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200147 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200148 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200149 " disable : put a server or frontend in maintenance mode\n"
150 " enable : re-enable a server or frontend which is in maintenance mode\n"
151 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100152 " show map [id] : report avalaible maps or dump a map's contents\n"
153 " get map : reports the keys and values matching a sample for a map\n"
154 " set map : modify map entry\n"
155 " add map : add map entry\n"
156 " del map : delete map entry\n"
157 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200158 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200159
Simon Horman9bd2c732011-06-15 15:18:44 +0900160static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200161 "Permission denied\n"
162 "";
163
Willy Tarreau295a8372011-03-10 11:25:07 +0100164/* data transmission states for the stats responses */
165enum {
166 STAT_ST_INIT = 0,
167 STAT_ST_HEAD,
168 STAT_ST_INFO,
169 STAT_ST_LIST,
170 STAT_ST_END,
171 STAT_ST_FIN,
172};
173
174/* data transmission states for the stats responses inside a proxy */
175enum {
176 STAT_PX_ST_INIT = 0,
177 STAT_PX_ST_TH,
178 STAT_PX_ST_FE,
179 STAT_PX_ST_LI,
180 STAT_PX_ST_SV,
181 STAT_PX_ST_BE,
182 STAT_PX_ST_END,
183 STAT_PX_ST_FIN,
184};
185
Cyril Bonté19979e12012-04-04 12:57:21 +0200186extern const char *stat_status_codes[];
187
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200188/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200189 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200190 * needs to be closed and ignored, or a negative value upon critical failure.
191 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900192static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200193{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100194 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100195 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200196
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200197 tv_zero(&s->logs.tv_request);
198 s->logs.t_queue = 0;
199 s->logs.t_connect = 0;
200 s->logs.t_data = 0;
201 s->logs.t_close = 0;
202 s->logs.bytes_in = s->logs.bytes_out = 0;
203 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
204 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200205
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200206 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200207
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200208 if (s->listener->timeout) {
209 s->req->rto = *s->listener->timeout;
210 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200212 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200213}
214
Willy Tarreau07e9e642010-08-17 21:48:17 +0200215/* allocate a new stats frontend named <name>, and return it
216 * (or NULL in case of lack of memory).
217 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200218static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200219{
220 struct proxy *fe;
221
222 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
223 if (!fe)
224 return NULL;
225
Willy Tarreau237250c2011-07-29 01:49:03 +0200226 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200227 fe->next = proxy;
228 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200229 fe->last_change = now.tv_sec;
230 fe->id = strdup("GLOBAL");
231 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200232 fe->maxconn = 10; /* default to 10 concurrent connections */
233 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200234 fe->conf.file = strdup(file);
235 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200236 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200237
238 /* the stats frontend is the only one able to assign ID #0 */
239 fe->conf.id.key = fe->uuid = 0;
240 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200241 return fe;
242}
243
Willy Tarreaufbee7132007-10-18 13:53:22 +0200244/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200245 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
246 * error message into the <err> buffer which will be preallocated. The trailing
247 * '\n' must not be written. The function must be called with <args> pointing to
248 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200249 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200250static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200251 struct proxy *defpx, const char *file, int line,
252 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200254 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200255 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200256
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200257 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200258 int cur_arg;
259
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200260 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200261 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 +0200262 return -1;
263 }
264
Willy Tarreau89a63132009-08-16 17:41:45 +0200265 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200266 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200267 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200268 return -1;
269 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200270 }
271
Willy Tarreau4348fad2012-09-20 16:48:07 +0200272 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200273 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200274
Willy Tarreauc53d4222012-09-20 20:19:28 +0200275 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
276 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
277 file, line, args[0], args[1], err && *err ? *err : "error");
278 return -1;
279 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200283 static int bind_dumped;
284 struct bind_kw *kw;
285
286 kw = bind_find_kw(args[cur_arg]);
287 if (kw) {
288 if (!kw->parse) {
289 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
290 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291 return -1;
292 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200293
294 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
295 if (err && *err)
296 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
297 else
298 memprintf(err, "'%s %s' : error encountered while processing '%s'",
299 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200300 return -1;
301 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200302
303 cur_arg += 1 + kw->skip;
304 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200305 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200306
307 if (!bind_dumped) {
308 bind_dump_kws(err);
309 indent_msg(err, 4);
310 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200312
313 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
314 args[0], args[1], args[cur_arg],
315 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
316 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200317 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100318
Willy Tarreauc53d4222012-09-20 20:19:28 +0200319 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
320 l->maxconn = global.stats_fe->maxconn;
321 l->backlog = global.stats_fe->backlog;
322 l->timeout = &global.stats_fe->timeout.client;
323 l->accept = session_accept;
324 l->handler = process_session;
325 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
326 l->nice = -64; /* we want to boost priority for local stats */
327 global.maxsock += l->maxconn;
328 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200329 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200330 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100331 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200332 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100333
334 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200335 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100336 return -1;
337 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200338
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100339 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200340 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200341 return -1;
342 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200343 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200344 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200345 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200346 return -1;
347 }
348 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200349 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200350 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200351 else if (!strcmp(args[1], "maxconn")) {
352 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200353
354 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200355 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200356 return -1;
357 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200358
359 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200360 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200361 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200362 return -1;
363 }
364 }
365 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200366 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200367 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
368 int cur_arg = 2;
369 unsigned int set = 0;
370
Willy Tarreau91319572013-04-20 09:48:50 +0200371 if (!global.stats_fe) {
372 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
373 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
374 return -1;
375 }
376 }
377
Willy Tarreau35b7b162012-10-22 23:17:18 +0200378 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100379 unsigned int low, high;
380
Willy Tarreau35b7b162012-10-22 23:17:18 +0200381 if (strcmp(args[cur_arg], "all") == 0) {
382 set = 0;
383 break;
384 }
385 else if (strcmp(args[cur_arg], "odd") == 0) {
386 set |= 0x55555555;
387 }
388 else if (strcmp(args[cur_arg], "even") == 0) {
389 set |= 0xAAAAAAAA;
390 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100391 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100392 char *dash = strchr(args[cur_arg], '-');
393
394 low = high = str2uic(args[cur_arg]);
395 if (dash)
396 high = str2uic(dash + 1);
397
398 if (high < low) {
399 unsigned int swap = low;
400 low = high;
401 high = swap;
402 }
403
404 if (low < 1 || high > 32) {
405 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
406 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200407 return -1;
408 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100409
410 while (low <= high)
411 set |= 1 << (low++ - 1);
412 }
413 else {
414 memprintf(err,
415 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
416 args[0], args[1]);
417 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200418 }
419 cur_arg++;
420 }
421 global.stats_fe->bind_proc = set;
422 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200423 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200424 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200425 return -1;
426 }
427 return 0;
428}
429
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100430/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
431 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100432 * NOTE: Some tools happen to rely on the field position instead of its name,
433 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100434 */
435static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100436{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100437 chunk_appendf(&trash,
438 "# pxname,svname,"
439 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100440 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100441 "bin,bout,"
442 "dreq,dresp,"
443 "ereq,econ,eresp,"
444 "wretr,wredis,"
445 "status,weight,act,bck,"
446 "chkfail,chkdown,lastchg,downtime,qlimit,"
447 "pid,iid,sid,throttle,lbtot,tracked,type,"
448 "rate,rate_lim,rate_max,"
449 "check_status,check_code,check_duration,"
450 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
451 "req_rate,req_rate_max,req_tot,"
452 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100453 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100454 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100455}
456
Simon Hormand9366582011-06-15 15:18:45 +0900457/* print a string of text buffer to <out>. The format is :
458 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
459 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
460 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
461 */
462static int dump_text(struct chunk *out, const char *buf, int bsize)
463{
464 unsigned char c;
465 int ptr = 0;
466
467 while (buf[ptr] && ptr < bsize) {
468 c = buf[ptr];
469 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
470 if (out->len > out->size - 1)
471 break;
472 out->str[out->len++] = c;
473 }
474 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
475 if (out->len > out->size - 2)
476 break;
477 out->str[out->len++] = '\\';
478 switch (c) {
479 case ' ': c = ' '; break;
480 case '\t': c = 't'; break;
481 case '\n': c = 'n'; break;
482 case '\r': c = 'r'; break;
483 case '\e': c = 'e'; break;
484 case '\\': c = '\\'; break;
485 }
486 out->str[out->len++] = c;
487 }
488 else {
489 if (out->len > out->size - 4)
490 break;
491 out->str[out->len++] = '\\';
492 out->str[out->len++] = 'x';
493 out->str[out->len++] = hextab[(c >> 4) & 0xF];
494 out->str[out->len++] = hextab[c & 0xF];
495 }
496 ptr++;
497 }
498
499 return ptr;
500}
501
502/* print a buffer in hexa.
503 * Print stopped if <bsize> is reached, or if no more place in the chunk.
504 */
505static int dump_binary(struct chunk *out, const char *buf, int bsize)
506{
507 unsigned char c;
508 int ptr = 0;
509
510 while (ptr < bsize) {
511 c = buf[ptr];
512
513 if (out->len > out->size - 2)
514 break;
515 out->str[out->len++] = hextab[(c >> 4) & 0xF];
516 out->str[out->len++] = hextab[c & 0xF];
517
518 ptr++;
519 }
520 return ptr;
521}
522
523/* Dump the status of a table to a stream interface's
524 * read buffer. It returns 0 if the output buffer is full
525 * and needs to be called again, otherwise non-zero.
526 */
527static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
528 struct proxy *proxy, struct proxy *target)
529{
Willy Tarreau306f8302013-07-08 15:53:06 +0200530 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900531
Willy Tarreau77804732012-10-29 16:14:26 +0100532 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900533 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
534
535 /* any other information should be dumped here */
536
Willy Tarreau290e63a2012-09-20 18:07:14 +0200537 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900539
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200540 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900541 return 0;
542
543 return 1;
544}
545
546/* Dump the a table entry to a stream interface's
547 * read buffer. It returns 0 if the output buffer is full
548 * and needs to be called again, otherwise non-zero.
549 */
550static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
551 struct proxy *proxy, struct stksess *entry)
552{
553 int dt;
554
Willy Tarreau77804732012-10-29 16:14:26 +0100555 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900556
557 if (proxy->table.type == STKTABLE_TYPE_IP) {
558 char addr[INET_ADDRSTRLEN];
559 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900561 }
562 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
563 char addr[INET6_ADDRSTRLEN];
564 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100565 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900566 }
567 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100568 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900569 }
570 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100571 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900572 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
573 }
574 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100575 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900576 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
577 }
578
Willy Tarreau77804732012-10-29 16:14:26 +0100579 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900580
581 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
582 void *ptr;
583
584 if (proxy->table.data_ofs[dt] == 0)
585 continue;
586 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100587 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900588 else
Willy Tarreau77804732012-10-29 16:14:26 +0100589 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900590
591 ptr = stktable_data_ptr(&proxy->table, entry, dt);
592 switch (stktable_data_types[dt].std_type) {
593 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900595 break;
596 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100597 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900598 break;
599 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100600 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900601 break;
602 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100603 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900604 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
605 proxy->table.data_arg[dt].u));
606 break;
607 }
608 }
Willy Tarreau77804732012-10-29 16:14:26 +0100609 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900610
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200611 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900612 return 0;
613
614 return 1;
615}
616
Willy Tarreaudec98142012-06-06 23:37:08 +0200617static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900618{
Willy Tarreau306f8302013-07-08 15:53:06 +0200619 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100620 struct appctx *appctx = __objt_appctx(si->end);
621 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900622 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900623 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900624 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200625 long long value;
626 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200627 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200628 void *ptr;
629 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900630
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100631 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900632
633 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100634 appctx->ctx.cli.msg = "Key value expected\n";
635 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900636 return;
637 }
638
Simon Hormanc5b89f62011-06-15 15:18:50 +0900639 switch (px->table.type) {
640 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900641 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100642 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900643 break;
644 case STKTABLE_TYPE_IPV6:
645 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100646 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900647 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900648 case STKTABLE_TYPE_INTEGER:
649 {
650 char *endptr;
651 unsigned long val;
652 errno = 0;
653 val = strtoul(args[4], &endptr, 10);
654 if ((errno == ERANGE && val == ULONG_MAX) ||
655 (errno != 0 && val == 0) || endptr == args[4] ||
656 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100657 appctx->ctx.cli.msg = "Invalid key\n";
658 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900659 return;
660 }
661 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100662 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900663 break;
664 }
665 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900666 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100667 static_table_key->key = args[4];
668 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900669 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900670 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200671 switch (action) {
672 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100673 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 +0200674 break;
675 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100676 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 +0200677 break;
678 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100679 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200680 break;
681 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100682 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900683 return;
684 }
685
686 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200687 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100688 appctx->ctx.cli.msg = stats_permission_denied_msg;
689 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900690 return;
691 }
692
Willy Tarreau07115412012-10-29 21:56:59 +0100693 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900694
Willy Tarreaudec98142012-06-06 23:37:08 +0200695 switch (action) {
696 case STAT_CLI_O_TAB:
697 if (!ts)
698 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100699 chunk_reset(&trash);
700 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900701 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100702 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900703 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200704
705 case STAT_CLI_O_CLR:
706 if (!ts)
707 return;
708 if (ts->ref_cnt) {
709 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100710 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
711 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200712 return;
713 }
714 stksess_kill(&px->table, ts);
715 break;
Simon Horman17bce342011-06-15 15:18:47 +0900716
Willy Tarreau654694e2012-06-07 01:03:16 +0200717 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200718 if (ts)
719 stktable_touch(&px->table, ts, 1);
720 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100721 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200722 if (!ts) {
723 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100724 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
725 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200726 return;
727 }
728 stktable_store(&px->table, ts, 1);
729 }
730
Willy Tarreau47060b62013-08-01 21:11:42 +0200731 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
732 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100733 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
734 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200735 return;
736 }
737
738 data_type = stktable_get_data_type(args[cur_arg] + 5);
739 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100740 appctx->ctx.cli.msg = "Unknown data type\n";
741 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200742 return;
743 }
744
745 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100746 appctx->ctx.cli.msg = "Data type not stored in this table\n";
747 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200748 return;
749 }
750
751 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200754 return;
755 }
756
757 ptr = stktable_data_ptr(&px->table, ts, data_type);
758
759 switch (stktable_data_types[data_type].std_type) {
760 case STD_T_SINT:
761 stktable_data_cast(ptr, std_t_sint) = value;
762 break;
763 case STD_T_UINT:
764 stktable_data_cast(ptr, std_t_uint) = value;
765 break;
766 case STD_T_ULL:
767 stktable_data_cast(ptr, std_t_ull) = value;
768 break;
769 case STD_T_FRQP:
770 /* We set both the current and previous values. That way
771 * the reported frequency is stable during all the period
772 * then slowly fades out. This allows external tools to
773 * push measures without having to update them too often.
774 */
775 frqp = &stktable_data_cast(ptr, std_t_frqp);
776 frqp->curr_tick = now_ms;
777 frqp->prev_ctr = 0;
778 frqp->curr_ctr = value;
779 break;
780 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200781 }
782 break;
783
Willy Tarreaudec98142012-06-06 23:37:08 +0200784 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100785 appctx->ctx.cli.msg = "Unknown action\n";
786 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200787 break;
Simon Horman121f3052011-06-15 15:18:46 +0900788 }
Simon Horman121f3052011-06-15 15:18:46 +0900789}
790
Willy Tarreau654694e2012-06-07 01:03:16 +0200791static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900792{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100793 struct appctx *appctx = __objt_appctx(si->end);
794
Willy Tarreau04b3a192013-04-13 09:41:37 +0200795 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100796 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
797 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200798 return;
799 }
800
Simon Hormand5b9fd92011-06-15 15:18:48 +0900801 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100802 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
803 if (appctx->ctx.table.data_type < 0) {
804 appctx->ctx.cli.msg = "Unknown data type\n";
805 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900806 return;
807 }
808
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100809 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
810 appctx->ctx.cli.msg = "Data type not stored in this table\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 appctx->ctx.table.data_op = get_std_op(args[4]);
816 if (appctx->ctx.table.data_op < 0) {
817 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
818 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900819 return;
820 }
821
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100822 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
823 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
824 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900825 return;
826 }
827}
828
Willy Tarreaudec98142012-06-06 23:37:08 +0200829static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900830{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100831 struct appctx *appctx = __objt_appctx(si->end);
832
833 appctx->ctx.table.data_type = -1;
834 appctx->st2 = STAT_ST_INIT;
835 appctx->ctx.table.target = NULL;
836 appctx->ctx.table.proxy = NULL;
837 appctx->ctx.table.entry = NULL;
838 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900839
840 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100841 appctx->ctx.table.target = find_stktable(args[2]);
842 if (!appctx->ctx.table.target) {
843 appctx->ctx.cli.msg = "No such table\n";
844 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900845 return;
846 }
847 }
848 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200849 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900850 goto err_args;
851 return;
852 }
853
854 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200855 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900856 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200857 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900858 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900859 goto err_args;
860
861 return;
862
863err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200864 switch (action) {
865 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100866 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 +0200867 break;
868 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100869 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 +0200870 break;
871 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100872 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200873 break;
874 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100875 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900876}
877
Willy Tarreau532a4502011-09-07 22:37:44 +0200878/* Expects to find a frontend named <arg> and returns it, otherwise displays various
879 * adequate error messages and returns NULL. This function also expects the session
880 * level to be admin.
881 */
882static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
883{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100884 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200885 struct proxy *px;
886
Willy Tarreau290e63a2012-09-20 18:07:14 +0200887 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100888 appctx->ctx.cli.msg = stats_permission_denied_msg;
889 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200890 return NULL;
891 }
892
893 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = "A frontend name is expected.\n";
895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200896 return NULL;
897 }
898
899 px = findproxy(arg, PR_CAP_FE);
900 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100901 appctx->ctx.cli.msg = "No such frontend.\n";
902 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200903 return NULL;
904 }
905 return px;
906}
907
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200908/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
909 * and returns the pointer to the server. Otherwise, display adequate error messages
910 * and returns NULL. This function also expects the session level to be admin. Note:
911 * the <arg> is modified to remove the '/'.
912 */
913static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
914{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100915 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200916 struct proxy *px;
917 struct server *sv;
918 char *line;
919
Willy Tarreau290e63a2012-09-20 18:07:14 +0200920 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100921 appctx->ctx.cli.msg = stats_permission_denied_msg;
922 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200923 return NULL;
924 }
925
926 /* split "backend/server" and make <line> point to server */
927 for (line = arg; *line; line++)
928 if (*line == '/') {
929 *line++ = '\0';
930 break;
931 }
932
933 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100934 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
935 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200936 return NULL;
937 }
938
939 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100940 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
941 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942 return NULL;
943 }
944
945 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100946 appctx->ctx.cli.msg = "Proxy is disabled.\n";
947 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200948 return NULL;
949 }
950
951 return sv;
952}
953
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100954/* This function is used with map management. It permits to browse each
955 * really allocated descriptors of one map reference. The variable
956 * <appctx->ctx.map.ref> must contain the map reference to browse.
957 * The variable <appctx->ctx.map.desc> contain the descriptor of the
958 * current allocated map descriptor. This variable must be initialized
959 * to NULL.
960 */
961static inline void stats_map_lookup_next(struct stream_interface *si)
962{
963 struct appctx *appctx = __objt_appctx(si->end);
964
965 /* search the next allocated map */
966 while (1) {
967 /* get next descriptor */
968 if (!appctx->ctx.map.desc)
969 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.ref->maps,
970 struct map_descriptor *, list);
971 else
972 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.desc->list,
973 struct map_descriptor *, list);
974
975 /* detect end of list */
976 if (&appctx->ctx.map.desc->list == &appctx->ctx.map.ref->maps) {
977 appctx->ctx.map.desc = NULL;
978 return;
979 }
980
981 /* do not lookup this entry */
982 if (!appctx->ctx.map.desc->do_free)
983 continue;
984
985 /* avalaible descriptor */
986 return;
987 }
988}
989
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200990/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200991 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100992 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200993 * designating the function which will have to process the request, which can
994 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200995 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900996static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200997{
Willy Tarreau306f8302013-07-08 15:53:06 +0200998 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100999 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000 char *args[MAX_STATS_ARGS + 1];
1001 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001002 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001003
1004 while (isspace((unsigned char)*line))
1005 line++;
1006
1007 arg = 0;
1008 args[arg] = line;
1009
1010 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001011 if (*line == '\\') {
1012 line++;
1013 if (*line == '\0')
1014 break;
1015 }
1016 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001017 *line++ = '\0';
1018
1019 while (isspace((unsigned char)*line))
1020 line++;
1021
1022 args[++arg] = line;
1023 continue;
1024 }
1025
1026 line++;
1027 }
1028
1029 while (++arg <= MAX_STATS_ARGS)
1030 args[arg] = line;
1031
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001032 /* remove \ */
1033 arg = 0;
1034 while (*args[arg] != '\0') {
1035 j = 0;
1036 for (i=0; args[arg][i] != '\0'; i++) {
1037 if (args[arg][i] == '\\')
1038 continue;
1039 args[arg][j] = args[arg][i];
1040 j++;
1041 }
1042 args[arg][j] = '\0';
1043 arg++;
1044 }
1045
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001046 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047 if (strcmp(args[0], "show") == 0) {
1048 if (strcmp(args[1], "stat") == 0) {
1049 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001050 appctx->ctx.stats.flags |= STAT_BOUND;
1051 appctx->ctx.stats.iid = atoi(args[2]);
1052 appctx->ctx.stats.type = atoi(args[3]);
1053 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001054 }
1055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001056 appctx->st2 = STAT_ST_INIT;
1057 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001058 }
1059 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001060 appctx->st2 = STAT_ST_INIT;
1061 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001062 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001063 else if (strcmp(args[1], "pools") == 0) {
1064 appctx->st2 = STAT_ST_INIT;
1065 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1066 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001067 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001068 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001069 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001070 appctx->ctx.cli.msg = stats_permission_denied_msg;
1071 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001072 return 1;
1073 }
Willy Tarreau76153662012-11-26 01:16:39 +01001074 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001075 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001076 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001077 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001078 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001079 appctx->ctx.sess.target = NULL;
1080 appctx->ctx.sess.section = 0; /* start with session status */
1081 appctx->ctx.sess.pos = 0;
1082 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001083 }
1084 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001085 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001086 appctx->ctx.cli.msg = stats_permission_denied_msg;
1087 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001088 return 1;
1089 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001090 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001091 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001092 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001093 appctx->ctx.errors.iid = -1;
1094 appctx->ctx.errors.px = NULL;
1095 appctx->st2 = STAT_ST_INIT;
1096 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001097 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001098 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001099 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001100 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001101 else if (strcmp(args[1], "map") == 0) {
1102
1103 /* no parameter: display all map avalaible */
1104 if (!*args[2]) {
1105 appctx->st2 = STAT_ST_INIT;
1106 appctx->st0 = STAT_CLI_O_MAPS;
1107 return 1;
1108 }
1109
1110 /* lookup into the maps */
1111 appctx->ctx.map.ref = map_get_reference(args[2]);
1112 if (!appctx->ctx.map.ref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001113 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001114 appctx->st0 = STAT_CLI_PRINT;
1115 return 1;
1116 }
1117 appctx->st2 = STAT_ST_INIT;
1118 appctx->st0 = STAT_CLI_O_MAP;
1119 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001120 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001121 return 0;
1122 }
1123 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001124 else if (strcmp(args[0], "clear") == 0) {
1125 if (strcmp(args[1], "counters") == 0) {
1126 struct proxy *px;
1127 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001128 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001129 int clrall = 0;
1130
1131 if (strcmp(args[2], "all") == 0)
1132 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001133
Willy Tarreau6162db22009-10-10 17:13:00 +02001134 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001135 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1136 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001137 appctx->ctx.cli.msg = stats_permission_denied_msg;
1138 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001139 return 1;
1140 }
1141
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001142 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001143 if (clrall) {
1144 memset(&px->be_counters, 0, sizeof(px->be_counters));
1145 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1146 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001147 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001148 px->be_counters.conn_max = 0;
1149 px->be_counters.p.http.rps_max = 0;
1150 px->be_counters.sps_max = 0;
1151 px->be_counters.cps_max = 0;
1152 px->be_counters.nbpend_max = 0;
1153
1154 px->fe_counters.conn_max = 0;
1155 px->fe_counters.p.http.rps_max = 0;
1156 px->fe_counters.sps_max = 0;
1157 px->fe_counters.cps_max = 0;
1158 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001159 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001160
1161 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001162 if (clrall)
1163 memset(&sv->counters, 0, sizeof(sv->counters));
1164 else {
1165 sv->counters.cur_sess_max = 0;
1166 sv->counters.nbpend_max = 0;
1167 sv->counters.sps_max = 0;
1168 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001169
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001170 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001171 if (li->counters) {
1172 if (clrall)
1173 memset(li->counters, 0, sizeof(*li->counters));
1174 else
1175 li->counters->conn_max = 0;
1176 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001177 }
1178
Willy Tarreau81c25d02011-09-07 15:17:21 +02001179 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001180 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001181 return 1;
1182 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001183 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001184 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001185 /* end of processing */
1186 return 1;
1187 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001188 else if (strcmp(args[1], "map") == 0) {
1189 struct map_reference *mref;
1190 struct map_descriptor *mdesc;
1191 struct map_entry *ent, *nent;
1192
1193 /* no parameter */
1194 if (!*args[2]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001195 appctx->ctx.cli.msg = "Missing map identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001196 appctx->st0 = STAT_CLI_PRINT;
1197 return 1;
1198 }
1199
1200 /* lookup into the maps */
1201 mref = map_get_reference(args[2]);
1202 if (!mref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001203 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001204 appctx->st0 = STAT_CLI_PRINT;
1205 return 1;
1206 }
1207
1208 /* clear all maps */
1209 list_for_each_entry(mdesc, &mref->maps, list)
1210 if (mdesc->do_free)
1211 pattern_prune_expr(mdesc->pat);
1212
1213 /* clear map reference */
1214 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1215 LIST_DEL(&ent->list);
1216 free(ent->key);
1217 free(ent->value);
1218 free(ent);
1219 }
1220
1221 /* return response */
1222 appctx->ctx.cli.msg = "Done.\n";
1223 appctx->st0 = STAT_CLI_PRINT;
1224 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001225 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001226 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001227 return 0;
1228 }
1229 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001230 else if (strcmp(args[0], "get") == 0) {
1231 if (strcmp(args[1], "weight") == 0) {
1232 struct proxy *px;
1233 struct server *sv;
1234
1235 /* split "backend/server" and make <line> point to server */
1236 for (line = args[2]; *line; line++)
1237 if (*line == '/') {
1238 *line++ = '\0';
1239 break;
1240 }
1241
1242 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001243 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1244 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001245 return 1;
1246 }
1247
1248 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001249 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1250 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001251 return 1;
1252 }
1253
1254 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001255 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1256 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001257 return 1;
1258 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001259 else if (strcmp(args[1], "map") == 0) {
1260
1261 /* no parameter */
1262 if (!*args[2] || !*args[3]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001263 appctx->ctx.cli.msg = "Missing identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001264 appctx->st0 = STAT_CLI_PRINT;
1265 return 1;
1266 }
1267
1268 /* lookup into the maps */
1269 appctx->ctx.map.ref = map_get_reference(args[2]);
1270 if (!appctx->ctx.map.ref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001271 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001272 appctx->st0 = STAT_CLI_PRINT;
1273 return 1;
1274 }
1275
1276 /* copy input string */
1277 appctx->ctx.map.chunk.len = strlen(args[3]);
1278 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1279 appctx->ctx.map.chunk.str = strdup(args[3]);
1280 if (!appctx->ctx.map.chunk.str) {
1281 appctx->ctx.cli.msg = "Out of memory error.\n";
1282 appctx->st0 = STAT_CLI_PRINT;
1283 return 1;
1284 }
1285
1286 /* prepare response */
1287 appctx->st2 = STAT_ST_INIT;
1288 appctx->st0 = STAT_CLI_O_MLOOK;
1289 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001290 else { /* not "get weight" */
1291 return 0;
1292 }
1293 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001294 else if (strcmp(args[0], "set") == 0) {
1295 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001296 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001297 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001298
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001299 sv = expect_server_admin(s, si, args[2]);
1300 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001301 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001302
Simon Horman7d09b9a2013-02-12 10:45:51 +09001303 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001304 /*
1305 * The user-weight may now be zero and thus
1306 * the server considered to be draining.
1307 * Update the server's drain state as necessary.
1308 */
1309 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001310 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001311 appctx->ctx.cli.msg = warning;
1312 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001313 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001314 return 1;
1315 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001316 else if (strcmp(args[1], "timeout") == 0) {
1317 if (strcmp(args[2], "cli") == 0) {
1318 unsigned timeout;
1319 const char *res;
1320
1321 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001322 appctx->ctx.cli.msg = "Expects an integer value.\n";
1323 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001324 return 1;
1325 }
1326
1327 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1328 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001329 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1330 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001331 return 1;
1332 }
1333
1334 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1335 return 1;
1336 }
1337 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001338 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1339 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001340 return 1;
1341 }
1342 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001343 else if (strcmp(args[1], "maxconn") == 0) {
1344 if (strcmp(args[2], "frontend") == 0) {
1345 struct proxy *px;
1346 struct listener *l;
1347 int v;
1348
Willy Tarreau532a4502011-09-07 22:37:44 +02001349 px = expect_frontend_admin(s, si, args[3]);
1350 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001351 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001352
1353 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001354 appctx->ctx.cli.msg = "Integer value expected.\n";
1355 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001356 return 1;
1357 }
1358
1359 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001360 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001361 appctx->ctx.cli.msg = "Value out of range.\n";
1362 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001363 return 1;
1364 }
1365
1366 /* OK, the value is fine, so we assign it to the proxy and to all of
1367 * its listeners. The blocked ones will be dequeued.
1368 */
1369 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001370 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001371 l->maxconn = v;
1372 if (l->state == LI_FULL)
1373 resume_listener(l);
1374 }
1375
1376 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1377 dequeue_all_listeners(&s->fe->listener_queue);
1378
1379 return 1;
1380 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001381 else if (strcmp(args[2], "global") == 0) {
1382 int v;
1383
Willy Tarreau290e63a2012-09-20 18:07:14 +02001384 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001385 appctx->ctx.cli.msg = stats_permission_denied_msg;
1386 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001387 return 1;
1388 }
1389
1390 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001391 appctx->ctx.cli.msg = "Expects an integer value.\n";
1392 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001393 return 1;
1394 }
1395
1396 v = atoi(args[3]);
1397 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001398 appctx->ctx.cli.msg = "Value out of range.\n";
1399 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001400 return 1;
1401 }
1402
1403 /* check for unlimited values */
1404 if (v <= 0)
1405 v = global.hardmaxconn;
1406
1407 global.maxconn = v;
1408
1409 /* Dequeues all of the listeners waiting for a resource */
1410 if (!LIST_ISEMPTY(&global_listener_queue))
1411 dequeue_all_listeners(&global_listener_queue);
1412
1413 return 1;
1414 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001415 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001418 return 1;
1419 }
1420 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001421 else if (strcmp(args[1], "rate-limit") == 0) {
1422 if (strcmp(args[2], "connections") == 0) {
1423 if (strcmp(args[3], "global") == 0) {
1424 int v;
1425
Willy Tarreau290e63a2012-09-20 18:07:14 +02001426 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.cli.msg = stats_permission_denied_msg;
1428 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001429 return 1;
1430 }
1431
1432 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001433 appctx->ctx.cli.msg = "Expects an integer value.\n";
1434 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001435 return 1;
1436 }
1437
1438 v = atoi(args[4]);
1439 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001440 appctx->ctx.cli.msg = "Value out of range.\n";
1441 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001442 return 1;
1443 }
1444
1445 global.cps_lim = v;
1446
1447 /* Dequeues all of the listeners waiting for a resource */
1448 if (!LIST_ISEMPTY(&global_listener_queue))
1449 dequeue_all_listeners(&global_listener_queue);
1450
1451 return 1;
1452 }
1453 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001454 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1455 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001456 return 1;
1457 }
1458 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001459 else if (strcmp(args[2], "sessions") == 0) {
1460 if (strcmp(args[3], "global") == 0) {
1461 int v;
1462
1463 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1464 appctx->ctx.cli.msg = stats_permission_denied_msg;
1465 appctx->st0 = STAT_CLI_PRINT;
1466 return 1;
1467 }
1468
1469 if (!*args[4]) {
1470 appctx->ctx.cli.msg = "Expects an integer value.\n";
1471 appctx->st0 = STAT_CLI_PRINT;
1472 return 1;
1473 }
1474
1475 v = atoi(args[4]);
1476 if (v < 0) {
1477 appctx->ctx.cli.msg = "Value out of range.\n";
1478 appctx->st0 = STAT_CLI_PRINT;
1479 return 1;
1480 }
1481
1482 global.sps_lim = v;
1483
1484 /* Dequeues all of the listeners waiting for a resource */
1485 if (!LIST_ISEMPTY(&global_listener_queue))
1486 dequeue_all_listeners(&global_listener_queue);
1487
1488 return 1;
1489 }
1490 else {
1491 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1492 appctx->st0 = STAT_CLI_PRINT;
1493 return 1;
1494 }
1495 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001496#ifdef USE_OPENSSL
1497 else if (strcmp(args[2], "ssl-sessions") == 0) {
1498 if (strcmp(args[3], "global") == 0) {
1499 int v;
1500
1501 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1502 appctx->ctx.cli.msg = stats_permission_denied_msg;
1503 appctx->st0 = STAT_CLI_PRINT;
1504 return 1;
1505 }
1506
1507 if (!*args[4]) {
1508 appctx->ctx.cli.msg = "Expects an integer value.\n";
1509 appctx->st0 = STAT_CLI_PRINT;
1510 return 1;
1511 }
1512
1513 v = atoi(args[4]);
1514 if (v < 0) {
1515 appctx->ctx.cli.msg = "Value out of range.\n";
1516 appctx->st0 = STAT_CLI_PRINT;
1517 return 1;
1518 }
1519
1520 global.ssl_lim = v;
1521
1522 /* Dequeues all of the listeners waiting for a resource */
1523 if (!LIST_ISEMPTY(&global_listener_queue))
1524 dequeue_all_listeners(&global_listener_queue);
1525
1526 return 1;
1527 }
1528 else {
1529 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1530 appctx->st0 = STAT_CLI_PRINT;
1531 return 1;
1532 }
1533 }
1534#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001535 else if (strcmp(args[2], "http-compression") == 0) {
1536 if (strcmp(args[3], "global") == 0) {
1537 int v;
1538
Willy Tarreau85d47f92012-11-21 00:29:50 +01001539 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001540 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1541 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001542 return 1;
1543 }
1544
William Lallemandd85f9172012-11-09 17:05:39 +01001545 v = atoi(args[4]);
1546 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1547 }
1548 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001549 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1550 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001551 return 1;
1552 }
1553 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001554 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001555 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001557 return 1;
1558 }
1559 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001560 else if (strcmp(args[1], "table") == 0) {
1561 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1562 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001563 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001564 struct pattern_list *pat_elt;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001565 struct pattern_tree *idx_elt;
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001566 char *value = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001567
1568 /* Expect three parameters: map name, key and new value. */
1569 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001570 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001571 appctx->st0 = STAT_CLI_PRINT;
1572 return 1;
1573 }
1574
1575 /* Lookup the reference in the maps. */
1576 appctx->ctx.map.ref = map_get_reference(args[2]);
1577 if (!appctx->ctx.map.ref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001578 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001579 appctx->st0 = STAT_CLI_PRINT;
1580 return 1;
1581 }
1582
1583 /* Lookup the entry in the reference values. */
1584 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1585 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1586 break;
1587
1588 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001589 appctx->ctx.cli.msg = "\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001590 appctx->st0 = STAT_CLI_PRINT;
1591 return 1;
1592 }
1593
1594 /* Update each reference entries. */
1595 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1596 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1597 value = strdup(args[4]);
1598 if (!value) {
1599 appctx->ctx.cli.msg = "Out of memory error.\n";
1600 appctx->st0 = STAT_CLI_PRINT;
1601 return 1;
1602 }
1603 free(appctx->ctx.map.ent->value);
1604 appctx->ctx.map.ent->value = value;
1605 }
1606 }
1607
1608 /* Change the sample. The lookup juste return the first entry, other
1609 * entries are not changed, but are never matched.
1610 */
1611 appctx->ctx.map.desc = NULL;
1612 for (stats_map_lookup_next(si);
1613 appctx->ctx.map.desc;
1614 stats_map_lookup_next(si)) {
1615 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1616 if (pat_elt != NULL)
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001617 appctx->ctx.map.desc->parse(value, pat_elt->pat.smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001618 if (idx_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001619 appctx->ctx.map.desc->parse(value, idx_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001620 }
1621
1622 /* The set is done, send message. */
1623 appctx->ctx.cli.msg = "Done.\n";
1624 appctx->st0 = STAT_CLI_PRINT;
1625 return 1;
1626 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001627 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001628 return 0;
1629 }
1630 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001631 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001632 if (strcmp(args[1], "agent") == 0) {
1633 struct server *sv;
1634
1635 sv = expect_server_admin(s, si, args[2]);
1636 if (!sv)
1637 return 1;
1638
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001639 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1640 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1641 appctx->st0 = STAT_CLI_PRINT;
1642 return 1;
1643 }
1644
1645 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001646 return 1;
1647 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001648 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001649 struct server *sv;
1650
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001651 sv = expect_server_admin(s, si, args[2]);
1652 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001653 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001654
Cyril Bontécd19e512010-01-31 22:34:03 +01001655 if (sv->state & SRV_MAINTAIN) {
1656 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001657 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001658 /* If this server tracks the status of another one,
1659 * we must restore the good status.
1660 */
Willy Tarreau44267702011-10-28 15:35:33 +02001661 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001662 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001663 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001664 } else {
1665 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001666 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001667 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001668 }
1669 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001670 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001671 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001672 }
1673 }
1674
Willy Tarreau532a4502011-09-07 22:37:44 +02001675 return 1;
1676 }
1677 else if (strcmp(args[1], "frontend") == 0) {
1678 struct proxy *px;
1679
1680 px = expect_frontend_admin(s, si, args[2]);
1681 if (!px)
1682 return 1;
1683
1684 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1686 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001687 return 1;
1688 }
1689
1690 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001691 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001693 return 1;
1694 }
1695
1696 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001697 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1698 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001699 return 1;
1700 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001701 return 1;
1702 }
1703 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001704 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1705 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001706 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001707 }
1708 }
1709 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001710 if (strcmp(args[1], "agent") == 0) {
1711 struct server *sv;
1712
1713 sv = expect_server_admin(s, si, args[2]);
1714 if (!sv)
1715 return 1;
1716
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001717 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001718 return 1;
1719 }
1720 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001721 struct server *sv;
1722
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001723 sv = expect_server_admin(s, si, args[2]);
1724 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001725 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001726
Cyril Bontécd19e512010-01-31 22:34:03 +01001727 if (! (sv->state & SRV_MAINTAIN)) {
1728 /* Not already in maintenance, we can change the server state */
1729 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001730 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001731 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001732 }
1733
Willy Tarreau532a4502011-09-07 22:37:44 +02001734 return 1;
1735 }
1736 else if (strcmp(args[1], "frontend") == 0) {
1737 struct proxy *px;
1738
1739 px = expect_frontend_admin(s, si, args[2]);
1740 if (!px)
1741 return 1;
1742
1743 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001744 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001746 return 1;
1747 }
1748
1749 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001750 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1751 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001752 return 1;
1753 }
1754
1755 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001756 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1757 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001758 return 1;
1759 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001760 return 1;
1761 }
1762 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001763 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1764 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001765 return 1;
1766 }
1767 }
1768 else if (strcmp(args[0], "shutdown") == 0) {
1769 if (strcmp(args[1], "frontend") == 0) {
1770 struct proxy *px;
1771
1772 px = expect_frontend_admin(s, si, args[2]);
1773 if (!px)
1774 return 1;
1775
1776 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001777 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1778 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001779 return 1;
1780 }
1781
1782 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1783 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1784 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1785 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1786 stop_proxy(px);
1787 return 1;
1788 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001789 else if (strcmp(args[1], "session") == 0) {
1790 struct session *sess, *ptr;
1791
Willy Tarreau290e63a2012-09-20 18:07:14 +02001792 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001793 appctx->ctx.cli.msg = stats_permission_denied_msg;
1794 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001795 return 1;
1796 }
1797
1798 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001799 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1800 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001801 return 1;
1802 }
1803
1804 ptr = (void *)strtoul(args[2], NULL, 0);
1805
1806 /* first, look for the requested session in the session table */
1807 list_for_each_entry(sess, &sessions, list) {
1808 if (sess == ptr)
1809 break;
1810 }
1811
1812 /* do we have the session ? */
1813 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001814 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1815 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001816 return 1;
1817 }
1818
1819 session_shutdown(sess, SN_ERR_KILLED);
1820 return 1;
1821 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001822 else if (strcmp(args[1], "sessions") == 0) {
1823 if (strcmp(args[2], "server") == 0) {
1824 struct server *sv;
1825 struct session *sess, *sess_bck;
1826
1827 sv = expect_server_admin(s, si, args[3]);
1828 if (!sv)
1829 return 1;
1830
1831 /* kill all the session that are on this server */
1832 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1833 if (sess->srv_conn == sv)
1834 session_shutdown(sess, SN_ERR_KILLED);
1835
1836 return 1;
1837 }
1838 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001839 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1840 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001841 return 1;
1842 }
1843 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001844 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001845 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1846 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001847 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001848 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001849 }
1850 else if (strcmp(args[0], "del") == 0) {
1851 if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001852 struct pattern_list *pat_elt;
Thierry FOURNIERe1bcac52013-12-13 16:09:50 +01001853 struct pattern_tree *idx_elt;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001854 struct map_entry *ent;
1855
1856 /* Expect two parameters: map name and key. */
1857 if (!*args[2] || !*args[3]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001858 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001859 appctx->st0 = STAT_CLI_PRINT;
1860 return 1;
1861 }
1862
1863 /* Lookup the reference in the maps. */
1864 appctx->ctx.map.ref = map_get_reference(args[2]);
1865 if (!appctx->ctx.map.ref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001866 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001867 appctx->st0 = STAT_CLI_PRINT;
1868 return 1;
1869 }
1870
1871 /* Lookup the entry in the reference values.
1872 * If the entry is not found in the reference, return error message.
1873 */
1874 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1875 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1876 break;
1877
1878 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001879 appctx->ctx.cli.msg = "Key not found.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001880 appctx->st0 = STAT_CLI_PRINT;
1881 return 1;
1882 }
1883
1884 /* Delete each enties from reference. */
1885 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1886 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1887 LIST_DEL(&appctx->ctx.map.ent->list);
1888 free(appctx->ctx.map.ent->key);
1889 free(appctx->ctx.map.ent->value);
1890 free(appctx->ctx.map.ent);
1891 }
1892 }
1893
1894 /* Delete all matching entries for each map descritor. */
1895 appctx->ctx.map.desc = NULL;
1896 stats_map_lookup_next(si);
1897 while (appctx->ctx.map.desc) {
1898 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1899 if (pat_elt != NULL) {
1900 LIST_DEL(&pat_elt->list);
1901 pattern_free(pat_elt);
1902 }
1903 if (idx_elt != NULL) {
1904 ebmb_delete(&idx_elt->node);
1905 free(idx_elt);
1906 }
1907 }
1908 stats_map_lookup_next(si);
1909 }
1910
1911 /* The deletion is done, send message. */
1912 appctx->ctx.cli.msg = "Done.\n";
1913 appctx->st0 = STAT_CLI_PRINT;
1914 return 1;
1915 }
1916 else { /* unknown "del" parameter */
1917 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1918 appctx->st0 = STAT_CLI_PRINT;
1919 return 1;
1920 }
1921 }
1922 else if (strcmp(args[0], "add") == 0) {
1923 if (strcmp(args[1], "map") == 0) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001924 struct map_entry *ent;
1925 struct sample_storage *smp;
1926
1927 /* Expect three parameters: map name, key and new value. */
1928 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001929 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001930 appctx->st0 = STAT_CLI_PRINT;
1931 return 1;
1932 }
1933
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001934 /* Lookup the reference in the maps. */
1935 appctx->ctx.map.ref = map_get_reference(args[2]);
1936 if (!appctx->ctx.map.ref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001937 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001938 appctx->st0 = STAT_CLI_PRINT;
1939 return 1;
1940 }
1941
1942 /* Prepare and link the new map_entry element. If out of memory
1943 * error the action is cancelled and the descriptor are left
1944 * coherents.
1945 */
1946 ent = malloc(sizeof(*ent));
1947 if (!ent) {
1948 appctx->ctx.cli.msg = "Out of memory error.\n";
1949 appctx->st0 = STAT_CLI_PRINT;
1950 return 1;
1951 }
1952 ent->key = strdup(args[3]);
1953 if (!ent->key) {
1954 free(ent);
1955 appctx->ctx.cli.msg = "Out of memory error.\n";
1956 appctx->st0 = STAT_CLI_PRINT;
1957 return 1;
1958 }
1959 ent->value = strdup(args[4]);
1960 if (!ent->value) {
1961 free(ent->key);
1962 free(ent);
1963 appctx->ctx.cli.msg = "Out of memory error.\n";
1964 appctx->st0 = STAT_CLI_PRINT;
1965 return 1;
1966 }
1967 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1968
1969 /* Browse each map descritor and try to insert this new value. */
1970 appctx->ctx.map.desc = NULL;
1971 for (stats_map_lookup_next(si);
1972 appctx->ctx.map.desc;
1973 stats_map_lookup_next(si)) {
1974
1975 /* Create new sample. Return out of memory error
1976 * if the memory cannot be allocated. The 'add' process
1977 * is aborted, but the already inserted entries are not
1978 * deleted.
1979 */
1980 smp = calloc(1, sizeof(*smp));
1981 if (!smp) {
1982 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1983 appctx->st0 = STAT_CLI_PRINT;
1984 return 1;
1985 }
1986
1987 /* Create sample. If this function fails, the insertion
1988 * is canceled for this 'descriptor', but continue, for
1989 * the other descriptors.
1990 */
1991 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1992 free(smp);
1993 continue;
1994 }
1995
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001996 if (!pattern_register(appctx->ctx.map.desc->pat, args[3], smp, 0, NULL)) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001997 free(smp);
1998 continue;
1999 }
2000 }
2001
2002 /* The add is done, send message. */
2003 appctx->ctx.cli.msg = "Done.\n";
2004 appctx->st0 = STAT_CLI_PRINT;
2005 return 1;
2006 }
2007 else { /* unknown "del" parameter */
2008 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2009 appctx->st0 = STAT_CLI_PRINT;
2010 return 1;
2011 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002012 }
2013 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002014 return 0;
2015 }
2016 return 1;
2017}
2018
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002019/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002020 * used to processes I/O from/to the stats unix socket. The system relies on a
2021 * state machine handling requests and various responses. We read a request,
2022 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002023 * Then we can read again. The state is stored in appctx->st0 and is one of the
2024 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002025 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002026 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002027static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002028{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002029 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002030 struct channel *req = si->ob;
2031 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002032 int reql;
2033 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002034
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002035 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2036 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002037
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002038 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002039 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002040 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002041 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2042 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002043 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002044 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002045 /* Let's close for real now. We just close the request
2046 * side, the conditions below will complete if needed.
2047 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002048 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002049 break;
2050 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002051 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002052 /* ensure we have some output room left in the event we
2053 * would want to return some info right after parsing.
2054 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002055 if (buffer_almost_full(si->ib->buf)) {
2056 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002057 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002058 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002059
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002060 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002061 if (reql <= 0) { /* closed or EOL not found */
2062 if (reql == 0)
2063 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002064 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002065 continue;
2066 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002067
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002068 /* seek for a possible semi-colon. If we find one, we
2069 * replace it with an LF and skip only this part.
2070 */
2071 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002072 if (trash.str[len] == ';') {
2073 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002074 reql = len + 1;
2075 break;
2076 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002077
Willy Tarreau816fc222009-10-04 07:36:58 +02002078 /* now it is time to check that we have a full line,
2079 * remove the trailing \n and possibly \r, then cut the
2080 * line.
2081 */
2082 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002083 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002084 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002085 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002086 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002087
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002088 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002089 len--;
2090
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002091 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002092
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002093 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002094 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002095 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002096 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002097 continue;
2098 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002099 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002100 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002101 else if (strcmp(trash.str, "help") == 0 ||
2102 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002103 appctx->ctx.cli.msg = stats_sock_usage_msg;
2104 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002105 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002106 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002107 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002108 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002109 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002110 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002111 /* if prompt is disabled, print help on empty lines,
2112 * so that the user at least knows how to enable
2113 * prompt and find help.
2114 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002115 appctx->ctx.cli.msg = stats_sock_usage_msg;
2116 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002117 }
2118
2119 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002120 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002121 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002122 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002123 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002124 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002125 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002126 continue;
2127 }
2128
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002129 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002130 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002131 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2132 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002133 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002134 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002135 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002136 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002137 break;
2138 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002139 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002140 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002141 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002142 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002143 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002144 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002145 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002146 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002147 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002148 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002149 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002150 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002151 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 if (stats_table_request(si, appctx->st0))
2153 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002154 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002155 case STAT_CLI_O_MAPS:
2156 if (stats_maps_list(si))
2157 appctx->st0 = STAT_CLI_PROMPT;
2158 break;
2159 case STAT_CLI_O_MAP:
2160 if (stats_map_list(si))
2161 appctx->st0 = STAT_CLI_PROMPT;
2162 break;
2163 case STAT_CLI_O_MLOOK:
2164 if (stats_map_lookup(si))
2165 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002166 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002167 case STAT_CLI_O_POOLS:
2168 if (stats_dump_pools_to_buffer(si))
2169 appctx->st0 = STAT_CLI_PROMPT;
2170 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002171 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002172 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002173 break;
2174 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002175
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002176 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002177 if (appctx->st0 == STAT_CLI_PROMPT) {
2178 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2179 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002180 }
2181
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002182 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002183 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002185
2186 /* Now we close the output if one of the writers did so,
2187 * or if we're not in interactive mode and the request
2188 * buffer is empty. This still allows pipelined requests
2189 * to be sent in non-interactive mode.
2190 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002191 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2192 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002193 continue;
2194 }
2195
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002196 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002197 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002198 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002199 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002200
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002201 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002202 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2203 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002204 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002205 * and nothing more to consume. This is comparable to a broken pipe, so
2206 * we forward the close to the request side so that it flows upstream to
2207 * the client.
2208 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002209 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002210 }
2211
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002212 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002213 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2214 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2215 /* We have no more processing to do, and nothing more to send, and
2216 * the client side has closed. So we'll forward this state downstream
2217 * on the response buffer.
2218 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002219 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002220 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002221 }
2222
2223 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002224 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002225
2226 /* we don't want to expire timeouts while we're processing requests */
2227 si->ib->rex = TICK_ETERNITY;
2228 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002229
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002230 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002231 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 +02002232 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002233 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 +02002234
2235 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2236 /* check that we have released everything then unregister */
2237 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002238 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002239}
2240
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002241/* This function dumps information onto the stream interface's read buffer.
2242 * It returns 0 as long as it does not complete, non-zero upon completion.
2243 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002244 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002245static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002246{
2247 unsigned int up = (now.tv_sec - start_date.tv_sec);
2248
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002249 chunk_printf(&trash,
2250 "Name: " PRODUCT_NAME "\n"
2251 "Version: " HAPROXY_VERSION "\n"
2252 "Release_date: " HAPROXY_DATE "\n"
2253 "Nbproc: %d\n"
2254 "Process_num: %d\n"
2255 "Pid: %d\n"
2256 "Uptime: %dd %dh%02dm%02ds\n"
2257 "Uptime_sec: %d\n"
2258 "Memmax_MB: %d\n"
2259 "Ulimit-n: %d\n"
2260 "Maxsock: %d\n"
2261 "Maxconn: %d\n"
2262 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002263 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002264 "CumConns: %d\n"
2265 "CumReq: %d\n"
2266#ifdef USE_OPENSSL
2267 "MaxSslConns: %d\n"
2268 "CurrSslConns: %d\n"
2269 "CumSslConns: %d\n"
2270#endif
2271 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002272 "PipesUsed: %d\n"
2273 "PipesFree: %d\n"
2274 "ConnRate: %d\n"
2275 "ConnRateLimit: %d\n"
2276 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002277 "SessRate: %d\n"
2278 "SessRateLimit: %d\n"
2279 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002280#ifdef USE_OPENSSL
2281 "SslRate: %d\n"
2282 "SslRateLimit: %d\n"
2283 "MaxSslRate: %d\n"
2284#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002285 "CompressBpsIn: %u\n"
2286 "CompressBpsOut: %u\n"
2287 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002288#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002289 "ZlibMemUsage: %ld\n"
2290 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002291#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002292 "Tasks: %d\n"
2293 "Run_queue: %d\n"
2294 "Idle_pct: %d\n"
2295 "node: %s\n"
2296 "description: %s\n"
2297 "",
2298 global.nbproc,
2299 relative_pid,
2300 pid,
2301 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2302 up,
2303 global.rlimit_memmax,
2304 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002305 global.maxsock, global.maxconn, global.hardmaxconn,
2306 actconn, totalconn, global.req_count,
2307#ifdef USE_OPENSSL
2308 global.maxsslconn, sslconns, totalsslconns,
2309#endif
2310 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002311 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002312 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002313#ifdef USE_OPENSSL
2314 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2315#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002316 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2317 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002318#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002319 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002320#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002321 nb_tasks_cur, run_queue_cur, idle_pct,
2322 global.node, global.desc ? global.desc : ""
2323 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002324
2325 if (bi_putchk(si->ib, &trash) == -1)
2326 return 0;
2327
2328 return 1;
2329}
2330
Willy Tarreau12833bb2014-01-28 16:49:56 +01002331/* This function dumps memory usage information onto the stream interface's
2332 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2333 * completion. No state is used.
2334 */
2335static int stats_dump_pools_to_buffer(struct stream_interface *si)
2336{
2337 dump_pools_to_trash();
2338 if (bi_putchk(si->ib, &trash) == -1)
2339 return 0;
2340 return 1;
2341}
2342
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002343/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2344 * the state from stream interface <si>. The caller is responsible for clearing
2345 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002346 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002347static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002348{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002349 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002350 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002351
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002352 if (!(px->cap & PR_CAP_FE))
2353 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002354
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002355 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002356 return 0;
2357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002358 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002359 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002360 /* name, queue */
2361 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002362
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002363 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002364 /* Column sub-heading for Enable or Disable server */
2365 chunk_appendf(&trash, "<td></td>");
2366 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002368 chunk_appendf(&trash,
2369 "<td class=ac>"
2370 "<a name=\"%s/Frontend\"></a>"
2371 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2372 "<td colspan=3></td>"
2373 "",
2374 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002375
Willy Tarreau466c9b52012-12-23 02:25:03 +01002376 chunk_appendf(&trash,
2377 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002378 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002379 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2380 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2381 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002382 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2383 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2384 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002385
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002386 if (px->mode == PR_MODE_HTTP)
2387 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002388 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002389 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002390
2391 chunk_appendf(&trash,
2392 "</table></div></u></td>"
2393 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002394 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002395 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2396 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2397 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002398 U2H(px->fe_counters.sps_max),
2399 U2H(px->fe_counters.cps_max),
2400 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002401
2402 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002403 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002404 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002405 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002406
2407 chunk_appendf(&trash,
2408 "</table></div></u></td>"
2409 /* sessions rate : limit */
2410 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002411 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002412
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002413 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002414 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002415 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002416 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002417 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2418 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002419 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002420 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2421 U2H(px->fe_counters.cum_sess),
2422 U2H(px->fe_counters.cum_conn),
2423 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002424
Willy Tarreau466c9b52012-12-23 02:25:03 +01002425 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002426 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002427 chunk_appendf(&trash,
2428 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2429 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2430 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2431 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2432 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2433 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2434 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2435 "<tr><th>- other responses:</th><td>%s</td></tr>"
2436 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2437 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002438 U2H(px->fe_counters.p.http.cum_req),
2439 U2H(px->fe_counters.p.http.rsp[1]),
2440 U2H(px->fe_counters.p.http.rsp[2]),
2441 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002442 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002443 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002444 U2H(px->fe_counters.p.http.rsp[3]),
2445 U2H(px->fe_counters.p.http.rsp[4]),
2446 U2H(px->fe_counters.p.http.rsp[5]),
2447 U2H(px->fe_counters.p.http.rsp[0]),
2448 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002449 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002450
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002451 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002452 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002453 /* sessions: lbtot, lastsess */
2454 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002455 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002456 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002457 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002458 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002459
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002460 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002461 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002462 "<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 +01002463 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002464 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002465 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2466 px->fe_counters.comp_in ?
2467 (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 +01002468 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002469
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002470 chunk_appendf(&trash,
2471 /* denied: req, resp */
2472 "<td>%s</td><td>%s</td>"
2473 /* errors : request, connect, response */
2474 "<td>%s</td><td></td><td></td>"
2475 /* warnings: retries, redispatches */
2476 "<td></td><td></td>"
2477 /* server status : reflect frontend status */
2478 "<td class=ac>%s</td>"
2479 /* rest of server: nothing */
2480 "<td class=ac colspan=8></td></tr>"
2481 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002482 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2483 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002484 px->state == PR_STREADY ? "OPEN" :
2485 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002486 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002487 else { /* CSV mode */
2488 chunk_appendf(&trash,
2489 /* pxid, name, queue cur, queue max, */
2490 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002491 /* sessions : current, max, limit, total */
2492 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002493 /* bytes : in, out */
2494 "%lld,%lld,"
2495 /* denied: req, resp */
2496 "%lld,%lld,"
2497 /* errors : request, connect, response */
2498 "%lld,,,"
2499 /* warnings: retries, redispatches */
2500 ",,"
2501 /* server status : reflect frontend status */
2502 "%s,"
2503 /* rest of server: nothing */
2504 ",,,,,,,,"
2505 /* pid, iid, sid, throttle, lbtot, tracked, type */
2506 "%d,%d,0,,,,%d,"
2507 /* rate, rate_lim, rate_max */
2508 "%u,%u,%u,"
2509 /* check_status, check_code, check_duration */
2510 ",,,",
2511 px->id,
2512 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2513 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2514 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2515 px->fe_counters.failed_req,
2516 px->state == PR_STREADY ? "OPEN" :
2517 px->state == PR_STFULL ? "FULL" : "STOP",
2518 relative_pid, px->uuid, STATS_TYPE_FE,
2519 read_freq_ctr(&px->fe_sess_per_sec),
2520 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002521
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002522 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2523 if (px->mode == PR_MODE_HTTP) {
2524 for (i=1; i<6; i++)
2525 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2526 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2527 }
2528 else
2529 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002530
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002531 /* failed health analyses */
2532 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002533
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 /* requests : req_rate, req_rate_max, req_tot, */
2535 chunk_appendf(&trash, "%u,%u,%lld,",
2536 read_freq_ctr(&px->fe_req_per_sec),
2537 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2538
2539 /* errors: cli_aborts, srv_aborts */
2540 chunk_appendf(&trash, ",,");
2541
2542 /* compression: in, out, bypassed */
2543 chunk_appendf(&trash, "%lld,%lld,%lld,",
2544 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2545
2546 /* compression: comp_rsp */
2547 chunk_appendf(&trash, "%lld,",
2548 px->fe_counters.p.http.comp_rsp);
2549
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002550 /* lastsess */
2551 chunk_appendf(&trash, ",");
2552
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002553 /* finish with EOL */
2554 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002555 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002556 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002557}
2558
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002559/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2560 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2561 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2562 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002563 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002564static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002565{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002566 struct appctx *appctx = __objt_appctx(si->end);
2567
2568 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002570 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 /* Column sub-heading for Enable or Disable server */
2572 chunk_appendf(&trash, "<td></td>");
2573 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002574 chunk_appendf(&trash,
2575 /* frontend name, listener name */
2576 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2577 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2578 "",
2579 px->id, l->name,
2580 (flags & ST_SHLGNDS)?"<u>":"",
2581 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002582
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583 if (flags & ST_SHLGNDS) {
2584 char str[INET6_ADDRSTRLEN];
2585 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002586
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002587 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002588
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 port = get_host_port(&l->addr);
2590 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2591 case AF_INET:
2592 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2593 break;
2594 case AF_INET6:
2595 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2596 break;
2597 case AF_UNIX:
2598 chunk_appendf(&trash, "unix, ");
2599 break;
2600 case -1:
2601 chunk_appendf(&trash, "(%s), ", strerror(errno));
2602 break;
2603 }
Willy Tarreau91861262007-10-17 17:06:05 +02002604
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002605 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002606 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607 }
Willy Tarreau91861262007-10-17 17:06:05 +02002608
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002609 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002610 /* queue */
2611 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 /* sessions rate: current, max, limit */
2613 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002614 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002615 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002616 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002617 /* bytes: in, out */
2618 "<td>%s</td><td>%s</td>"
2619 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002620 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002621 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2622 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 chunk_appendf(&trash,
2625 /* denied: req, resp */
2626 "<td>%s</td><td>%s</td>"
2627 /* errors: request, connect, response */
2628 "<td>%s</td><td></td><td></td>"
2629 /* warnings: retries, redispatches */
2630 "<td></td><td></td>"
2631 /* server status: reflect listener status */
2632 "<td class=ac>%s</td>"
2633 /* rest of server: nothing */
2634 "<td class=ac colspan=8></td></tr>"
2635 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002636 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2637 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002638 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2639 }
2640 else { /* CSV mode */
2641 chunk_appendf(&trash,
2642 /* pxid, name, queue cur, queue max, */
2643 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002644 /* sessions: current, max, limit, total */
2645 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002646 /* bytes: in, out */
2647 "%lld,%lld,"
2648 /* denied: req, resp */
2649 "%lld,%lld,"
2650 /* errors: request, connect, response */
2651 "%lld,,,"
2652 /* warnings: retries, redispatches */
2653 ",,"
2654 /* server status: reflect listener status */
2655 "%s,"
2656 /* rest of server: nothing */
2657 ",,,,,,,,"
2658 /* pid, iid, sid, throttle, lbtot, tracked, type */
2659 "%d,%d,%d,,,,%d,"
2660 /* rate, rate_lim, rate_max */
2661 ",,,"
2662 /* check_status, check_code, check_duration */
2663 ",,,"
2664 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2665 ",,,,,,"
2666 /* failed health analyses */
2667 ","
2668 /* requests : req_rate, req_rate_max, req_tot, */
2669 ",,,"
2670 /* errors: cli_aborts, srv_aborts */
2671 ",,"
2672 /* compression: in, out, bypassed, comp_rsp */
2673 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002674 /* lastsess */
2675 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002676 "\n",
2677 px->id, l->name,
2678 l->nbconn, l->counters->conn_max,
2679 l->maxconn, l->counters->cum_conn,
2680 l->counters->bytes_in, l->counters->bytes_out,
2681 l->counters->denied_req, l->counters->denied_resp,
2682 l->counters->failed_req,
2683 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2684 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2685 }
2686 return 1;
2687}
Willy Tarreau91861262007-10-17 17:06:05 +02002688
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002689/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2690 * from stream interface <si>, stats flags <flags>, and server state <state>.
2691 * The caller is responsible for clearing the trash if needed. Returns non-zero
2692 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002693 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2694 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002695 */
2696static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2697{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002698 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002699 struct server *ref = sv->track ? sv->track : sv;
2700 char str[INET6_ADDRSTRLEN];
2701 struct chunk src;
2702 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002704 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002705 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002706 "DOWN",
2707 "DN %d/%d &uarr;",
2708 "UP %d/%d &darr;",
2709 "UP",
2710 "NOLB %d/%d &darr;",
2711 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002712 "DRAIN %d/%d &darr;",
2713 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002714 "<i>no check</i>"
2715 };
Willy Tarreau91861262007-10-17 17:06:05 +02002716
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002717 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2718 chunk_appendf(&trash, "<tr class=\"maintain\">");
2719 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002720 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002721 "<tr class=\"%s%d\">",
2722 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002723
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002724 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002725 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002726 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2727 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002728
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002729 chunk_appendf(&trash,
2730 "<td class=ac><a name=\"%s/%s\"></a>%s"
2731 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2732 "",
2733 px->id, sv->id,
2734 (flags & ST_SHLGNDS) ? "<u>" : "",
2735 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002736
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002737 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002738 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002739
2740 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2741 case AF_INET:
2742 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2743 break;
2744 case AF_INET6:
2745 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2746 break;
2747 case AF_UNIX:
2748 chunk_appendf(&trash, "unix, ");
2749 break;
2750 case -1:
2751 chunk_appendf(&trash, "(%s), ", strerror(errno));
2752 break;
2753 default: /* address family not supported */
2754 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002755 }
Willy Tarreau91861262007-10-17 17:06:05 +02002756
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002757 /* id */
2758 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002759
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002760 /* cookie */
2761 if (sv->cookie) {
2762 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002763
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2765 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002766
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002768 }
2769
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002770 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002771 }
Willy Tarreau91861262007-10-17 17:06:05 +02002772
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002773 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002774 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002775 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 /* sessions rate : current, max, limit */
2777 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002779 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002780 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2781 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002782
Willy Tarreau466c9b52012-12-23 02:25:03 +01002783
2784 chunk_appendf(&trash,
2785 /* sessions: current, max, limit, total */
2786 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002787 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002788 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2789 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002790 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2791 U2H(sv->counters.cum_sess),
2792 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002793
Willy Tarreau466c9b52012-12-23 02:25:03 +01002794 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2795 if (px->mode == PR_MODE_HTTP) {
2796 unsigned long long tot;
2797 for (tot = i = 0; i < 6; i++)
2798 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002799
Willy Tarreau466c9b52012-12-23 02:25:03 +01002800 chunk_appendf(&trash,
2801 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2802 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2803 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2804 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2805 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2806 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2807 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2808 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002809 U2H(tot),
2810 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2811 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2812 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2813 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2814 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2815 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 +02002816 }
Willy Tarreau91861262007-10-17 17:06:05 +02002817
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002819 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002820 /* sessions: lbtot, last */
2821 "<td>%s</td><td>%s</td>",
2822 U2H(sv->counters.cum_lbconn),
2823 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002824
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002825 chunk_appendf(&trash,
2826 /* bytes : in, out */
2827 "<td>%s</td><td>%s</td>"
2828 /* denied: req, resp */
2829 "<td></td><td>%s</td>"
2830 /* errors : request, connect */
2831 "<td></td><td>%s</td>"
2832 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002833 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002834 /* warnings: retries, redispatches */
2835 "<td>%lld</td><td>%lld</td>"
2836 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002837 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2838 U2H(sv->counters.failed_secu),
2839 U2H(sv->counters.failed_conns),
2840 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841 sv->counters.cli_aborts,
2842 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843 sv->counters.retries, sv->counters.redispatches);
2844
2845 /* status, lest check */
2846 chunk_appendf(&trash, "<td class=ac>");
2847
2848 if (sv->state & SRV_MAINTAIN) {
2849 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2850 chunk_appendf(&trash, "MAINT");
2851 }
2852 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2853 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2854 chunk_appendf(&trash, "MAINT(via)");
2855 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002856 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002857 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2858 chunk_appendf(&trash,
2859 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002860 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2861 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002862 }
Willy Tarreau91861262007-10-17 17:06:05 +02002863
Willy Tarreauff5ae352013-12-11 20:36:34 +01002864 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002865 chunk_appendf(&trash,
2866 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002867 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002868 get_check_status_info(sv->check.status));
2869
2870 if (sv->check.status >= HCHK_STATUS_L57DATA)
2871 chunk_appendf(&trash, "/%d", sv->check.code);
2872
2873 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002874 chunk_appendf(&trash, " in %lums", sv->check.duration);
2875
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002876 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002877 get_check_status_description(sv->check.status));
2878 if (*sv->check.desc) {
2879 chunk_appendf(&trash, ": ");
2880 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2881 chunk_htmlencode(&trash, &src);
2882 }
2883 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002884 }
2885 else
2886 chunk_appendf(&trash, "</td><td>");
2887
2888 chunk_appendf(&trash,
2889 /* weight */
2890 "</td><td class=ac>%d</td>"
2891 /* act, bck */
2892 "<td class=ac>%s</td><td class=ac>%s</td>"
2893 "",
2894 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2895 (sv->state & SRV_BACKUP) ? "-" : "Y",
2896 (sv->state & SRV_BACKUP) ? "Y" : "-");
2897
2898 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002899 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002900 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002901
2902 if (ref->observe)
2903 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2904
2905 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002906 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002907 "<td>%lld</td><td>%s</td>"
2908 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002909 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2911 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002912 else if (sv != ref) {
2913 if (sv->state & SRV_MAINTAIN)
2914 chunk_appendf(&trash,
2915 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2916 ref->proxy->id, ref->id);
2917 else
2918 chunk_appendf(&trash,
2919 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2920 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2921 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002922 else
2923 chunk_appendf(&trash, "<td colspan=3></td>");
2924
2925 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002926 if (sv->state & SRV_WARMINGUP)
2927 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002928 else
2929 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2930 }
2931 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002932 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002933 "DOWN,",
2934 "DOWN %d/%d,",
2935 "UP %d/%d,",
2936 "UP,",
2937 "NOLB %d/%d,",
2938 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002939 "DRAIN %d/%d,",
2940 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002941 "no check,"
2942 };
2943
2944 chunk_appendf(&trash,
2945 /* pxid, name */
2946 "%s,%s,"
2947 /* queue : current, max */
2948 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002949 /* sessions : current, max, limit, total */
2950 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 /* bytes : in, out */
2952 "%lld,%lld,"
2953 /* denied: req, resp */
2954 ",%lld,"
2955 /* errors : request, connect, response */
2956 ",%lld,%lld,"
2957 /* warnings: retries, redispatches */
2958 "%lld,%lld,"
2959 "",
2960 px->id, sv->id,
2961 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002962 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002963 sv->counters.bytes_in, sv->counters.bytes_out,
2964 sv->counters.failed_secu,
2965 sv->counters.failed_conns, sv->counters.failed_resp,
2966 sv->counters.retries, sv->counters.redispatches);
2967
2968 /* status */
2969 if (sv->state & SRV_MAINTAIN)
2970 chunk_appendf(&trash, "MAINT,");
2971 else if (ref != sv && ref->state & SRV_MAINTAIN)
2972 chunk_appendf(&trash, "MAINT(via),");
2973 else
2974 chunk_appendf(&trash,
2975 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002976 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2977 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002978
2979 chunk_appendf(&trash,
2980 /* weight, active, backup */
2981 "%d,%d,%d,"
2982 "",
2983 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2984 (sv->state & SRV_BACKUP) ? 0 : 1,
2985 (sv->state & SRV_BACKUP) ? 1 : 0);
2986
2987 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002988 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002989 chunk_appendf(&trash,
2990 "%lld,%lld,%d,%d,",
2991 sv->counters.failed_checks, sv->counters.down_trans,
2992 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2993 else
2994 chunk_appendf(&trash, ",,,,");
2995
2996 /* queue limit, pid, iid, sid, */
2997 chunk_appendf(&trash,
2998 "%s,"
2999 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003000 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003001 relative_pid, px->uuid, sv->puid);
3002
3003 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01003004 if (sv->state & SRV_WARMINGUP)
3005 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006
3007 /* sessions: lbtot */
3008 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3009
3010 /* tracked */
3011 if (sv->track)
3012 chunk_appendf(&trash, "%s/%s,",
3013 sv->track->proxy->id, sv->track->id);
3014 else
3015 chunk_appendf(&trash, ",");
3016
3017 /* type */
3018 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3019
3020 /* rate */
3021 chunk_appendf(&trash, "%u,,%u,",
3022 read_freq_ctr(&sv->sess_per_sec),
3023 sv->counters.sps_max);
3024
Willy Tarreauff5ae352013-12-11 20:36:34 +01003025 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003026 /* check_status */
3027 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3028
3029 /* check_code */
3030 if (sv->check.status >= HCHK_STATUS_L57DATA)
3031 chunk_appendf(&trash, "%u,", sv->check.code);
3032 else
3033 chunk_appendf(&trash, ",");
3034
3035 /* check_duration */
3036 if (sv->check.status >= HCHK_STATUS_CHECKED)
3037 chunk_appendf(&trash, "%lu,", sv->check.duration);
3038 else
3039 chunk_appendf(&trash, ",");
3040
3041 }
3042 else
3043 chunk_appendf(&trash, ",,,");
3044
3045 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3046 if (px->mode == PR_MODE_HTTP) {
3047 for (i=1; i<6; i++)
3048 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3049
3050 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3051 }
3052 else
3053 chunk_appendf(&trash, ",,,,,,");
3054
3055 /* failed health analyses */
3056 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3057
3058 /* requests : req_rate, req_rate_max, req_tot, */
3059 chunk_appendf(&trash, ",,,");
3060
3061 /* errors: cli_aborts, srv_aborts */
3062 chunk_appendf(&trash, "%lld,%lld,",
3063 sv->counters.cli_aborts, sv->counters.srv_aborts);
3064
3065 /* compression: in, out, bypassed, comp_rsp */
3066 chunk_appendf(&trash, ",,,,");
3067
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003068 /* lastsess */
3069 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3070
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003071 /* finish with EOL */
3072 chunk_appendf(&trash, "\n");
3073 }
3074 return 1;
3075}
3076
3077/* Dumps a line for backend <px> to the trash for and uses the state from stream
3078 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3079 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3080 */
3081static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3082{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003083 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003084 struct chunk src;
3085 int i;
3086
3087 if (!(px->cap & PR_CAP_BE))
3088 return 0;
3089
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003090 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003091 return 0;
3092
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003093 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003094 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003095 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003096 /* Column sub-heading for Enable or Disable server */
3097 chunk_appendf(&trash, "<td></td>");
3098 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003099 chunk_appendf(&trash,
3100 "<td class=ac>"
3101 /* name */
3102 "%s<a name=\"%s/Backend\"></a>"
3103 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3104 "",
3105 (flags & ST_SHLGNDS)?"<u>":"",
3106 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107
3108 if (flags & ST_SHLGNDS) {
3109 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003110 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003111 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3112
3113 /* cookie */
3114 if (px->cookie_name) {
3115 chunk_appendf(&trash, ", cookie: '");
3116 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3117 chunk_htmlencode(&trash, &src);
3118 chunk_appendf(&trash, "'");
3119 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003120 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003121 }
3122
3123 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003124 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125 /* queue : current, max */
3126 "<td>%s</td><td>%s</td><td></td>"
3127 /* sessions rate : current, max, limit */
3128 "<td>%s</td><td>%s</td><td></td>"
3129 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003131 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3132 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003133
3134 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003135 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003136 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003137 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003138 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003139 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003140 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3141 U2H(px->be_counters.cum_conn),
3142 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143
Willy Tarreau466c9b52012-12-23 02:25:03 +01003144 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003146 chunk_appendf(&trash,
3147 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3148 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3149 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3150 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3151 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3152 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3153 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3154 "<tr><th>- other responses:</th><td>%s</td></tr>"
3155 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3156 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003157 U2H(px->be_counters.p.http.cum_req),
3158 U2H(px->be_counters.p.http.rsp[1]),
3159 U2H(px->be_counters.p.http.rsp[2]),
3160 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003161 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003162 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003163 U2H(px->be_counters.p.http.rsp[3]),
3164 U2H(px->be_counters.p.http.rsp[4]),
3165 U2H(px->be_counters.p.http.rsp[5]),
3166 U2H(px->be_counters.p.http.rsp[0]),
3167 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168 }
3169
3170 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003171 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003172 /* sessions: lbtot, last */
3173 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003174 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003175 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003177 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003178 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003179 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003181 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003182 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003183 "<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 +01003184 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003185 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003186 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3187 px->be_counters.comp_in ?
3188 (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 +01003189 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3190
3191 chunk_appendf(&trash,
3192 /* denied: req, resp */
3193 "<td>%s</td><td>%s</td>"
3194 /* errors : request, connect */
3195 "<td></td><td>%s</td>"
3196 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003197 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003198 /* warnings: retries, redispatches */
3199 "<td>%lld</td><td>%lld</td>"
3200 /* backend status: reflect backend status (up/down): we display UP
3201 * if the backend has known working servers or if it has no server at
3202 * all (eg: for stats). Then we display the total weight, number of
3203 * active and backups. */
3204 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3205 "<td class=ac>%d</td><td class=ac>%d</td>"
3206 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003207 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3208 U2H(px->be_counters.failed_conns),
3209 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003210 px->be_counters.cli_aborts,
3211 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212 px->be_counters.retries, px->be_counters.redispatches,
3213 human_time(now.tv_sec - px->last_change, 1),
3214 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3215 "<font color=\"red\"><b>DOWN</b></font>",
3216 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3217 px->srv_act, px->srv_bck);
3218
3219 chunk_appendf(&trash,
3220 /* rest of backend: nothing, down transitions, total downtime, throttle */
3221 "<td class=ac>&nbsp;</td><td>%d</td>"
3222 "<td>%s</td>"
3223 "<td></td>"
3224 "</tr>",
3225 px->down_trans,
3226 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3227 }
3228 else { /* CSV mode */
3229 chunk_appendf(&trash,
3230 /* pxid, name */
3231 "%s,BACKEND,"
3232 /* queue : current, max */
3233 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003234 /* sessions : current, max, limit, total */
3235 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003236 /* bytes : in, out */
3237 "%lld,%lld,"
3238 /* denied: req, resp */
3239 "%lld,%lld,"
3240 /* errors : request, connect, response */
3241 ",%lld,%lld,"
3242 /* warnings: retries, redispatches */
3243 "%lld,%lld,"
3244 /* backend status: reflect backend status (up/down): we display UP
3245 * if the backend has known working servers or if it has no server at
3246 * all (eg: for stats). Then we display the total weight, number of
3247 * active and backups. */
3248 "%s,"
3249 "%d,%d,%d,"
3250 /* rest of backend: nothing, down transitions, last change, total downtime */
3251 ",%d,%d,%d,,"
3252 /* pid, iid, sid, throttle, lbtot, tracked, type */
3253 "%d,%d,0,,%lld,,%d,"
3254 /* rate, rate_lim, rate_max, */
3255 "%u,,%u,"
3256 /* check_status, check_code, check_duration */
3257 ",,,",
3258 px->id,
3259 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3260 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3261 px->be_counters.bytes_in, px->be_counters.bytes_out,
3262 px->be_counters.denied_req, px->be_counters.denied_resp,
3263 px->be_counters.failed_conns, px->be_counters.failed_resp,
3264 px->be_counters.retries, px->be_counters.redispatches,
3265 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3266 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3267 px->srv_act, px->srv_bck,
3268 px->down_trans, (int)(now.tv_sec - px->last_change),
3269 px->srv?be_downtime(px):0,
3270 relative_pid, px->uuid,
3271 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3272 read_freq_ctr(&px->be_sess_per_sec),
3273 px->be_counters.sps_max);
3274
3275 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3276 if (px->mode == PR_MODE_HTTP) {
3277 for (i=1; i<6; i++)
3278 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3279 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3280 }
3281 else
3282 chunk_appendf(&trash, ",,,,,,");
3283
3284 /* failed health analyses */
3285 chunk_appendf(&trash, ",");
3286
3287 /* requests : req_rate, req_rate_max, req_tot, */
3288 chunk_appendf(&trash, ",,,");
3289
3290 /* errors: cli_aborts, srv_aborts */
3291 chunk_appendf(&trash, "%lld,%lld,",
3292 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3293
3294 /* compression: in, out, bypassed */
3295 chunk_appendf(&trash, "%lld,%lld,%lld,",
3296 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3297
3298 /* compression: comp_rsp */
3299 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3300
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003301 /* lastsess */
3302 chunk_appendf(&trash, "%d,", be_lastsession(px));
3303
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003304 /* finish with EOL */
3305 chunk_appendf(&trash, "\n");
3306 }
3307 return 1;
3308}
3309
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003310/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003312 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003313 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003314static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003315{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003316 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003317 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3318
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003319 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003322 /* 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 +02003323 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003324 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003325 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3327 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003328 }
3329
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003330 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003331 "<form action=\"%s%s%s%s\" method=\"post\">",
3332 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003333 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3334 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003335 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003336 }
3337
3338 /* print a new table */
3339 chunk_appendf(&trash,
3340 "<table class=\"tbl\" width=\"100%%\">\n"
3341 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003342 "<th class=\"pxname\" width=\"10%%\">");
3343
3344 chunk_appendf(&trash,
3345 "<a name=\"%s\"></a>%s"
3346 "<a class=px href=\"#%s\">%s</a>",
3347 px->id,
3348 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3349 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350
3351 if (uri->flags & ST_SHLGNDS) {
3352 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003353 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3355 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003356 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 }
3358
3359 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003360 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 "<th class=\"%s\" width=\"90%%\">%s</th>"
3362 "</tr>\n"
3363 "</table>\n"
3364 "<table class=\"tbl\" width=\"100%%\">\n"
3365 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3367 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3368
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003369 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 /* Column heading for Enable or Disable server */
3371 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003372 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373
3374 chunk_appendf(&trash,
3375 "<th rowspan=2></th>"
3376 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003377 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3379 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3380 "<th colspan=9>Server</th>"
3381 "</tr>\n"
3382 "<tr class=\"titre\">"
3383 "<th>Cur</th><th>Max</th><th>Limit</th>"
3384 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003385 "<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 +01003386 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3387 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3388 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3389 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3390 "<th>Thrtle</th>\n"
3391 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003392}
3393
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003394/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003397static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003399 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 chunk_appendf(&trash, "</table>");
3401
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003402 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 /* close the form used to enable/disable this proxy servers */
3404 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003405 "Choose the action to perform on the checked servers : "
3406 "<select name=action>"
3407 "<option value=\"\"></option>"
3408 "<option value=\"disable\">Disable</option>"
3409 "<option value=\"enable\">Enable</option>"
3410 "<option value=\"stop\">Soft Stop</option>"
3411 "<option value=\"start\">Soft Start</option>"
3412 "<option value=\"shutdown\">Kill Sessions</option>"
3413 "</select>"
3414 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3415 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3416 "</form>",
3417 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003418 }
3419
3420 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421}
Willy Tarreau91861262007-10-17 17:06:05 +02003422
3423/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003424 * Dumps statistics for a proxy. The output is sent to the stream interface's
3425 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3426 * buffer space, or non-zero if everything completed. This function is used
3427 * both by the CLI and the HTTP entry points, and is able to dump the output
3428 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003429 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003430static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003431{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003432 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003433 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003434 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003435 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003436 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003437
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003438 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003439
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003440 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003441 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003442 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003443 if (uri && uri->scope) {
3444 /* we have a limited scope, we have to check the proxy name */
3445 struct stat_scope *scope;
3446 int len;
3447
3448 len = strlen(px->id);
3449 scope = uri->scope;
3450
3451 while (scope) {
3452 /* match exact proxy name */
3453 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3454 break;
3455
3456 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003457 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003458 break;
3459 scope = scope->next;
3460 }
3461
3462 /* proxy name not found : don't dump anything */
3463 if (scope == NULL)
3464 return 1;
3465 }
3466
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003467 /* if the user has requested a limited output and the proxy
3468 * name does not match, skip it.
3469 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003470 if (appctx->ctx.stats.scope_len &&
3471 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 +02003472 return 1;
3473
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003474 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3475 (appctx->ctx.stats.iid != -1) &&
3476 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003477 return 1;
3478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003479 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003480 /* fall through */
3481
Willy Tarreau295a8372011-03-10 11:25:07 +01003482 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003483 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003484 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003485 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003486 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003487 }
Willy Tarreau91861262007-10-17 17:06:05 +02003488
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003489 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003490 /* fall through */
3491
Willy Tarreau295a8372011-03-10 11:25:07 +01003492 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003493 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003495 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003496 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003498 appctx->ctx.stats.l = px->conf.listeners.n;
3499 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003500 /* fall through */
3501
Willy Tarreau295a8372011-03-10 11:25:07 +01003502 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003503 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003504 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003505 if (buffer_almost_full(rep->buf)) {
3506 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003507 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003508 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003509
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003510 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003511 if (!l->counters)
3512 continue;
3513
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003514 if (appctx->ctx.stats.flags & STAT_BOUND) {
3515 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003516 break;
3517
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003518 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003519 continue;
3520 }
3521
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003522 /* print the frontend */
3523 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3524 if (bi_putchk(rep, &trash) == -1)
3525 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003526 }
3527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003528 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3529 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003530 /* fall through */
3531
Willy Tarreau295a8372011-03-10 11:25:07 +01003532 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003533 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003534 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003535 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 +02003536
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003537 if (buffer_almost_full(rep->buf)) {
3538 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003539 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003540 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003541
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003542 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003543
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003544 if (appctx->ctx.stats.flags & STAT_BOUND) {
3545 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003546 break;
3547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003548 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003549 continue;
3550 }
3551
Willy Tarreau44267702011-10-28 15:35:33 +02003552 if (sv->track)
3553 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003554 else
3555 svs = sv;
3556
Willy Tarreau91861262007-10-17 17:06:05 +02003557 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003558 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003559 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003560 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003561 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003562 sv_state = 3; /* UP */
3563 else
3564 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003565
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003566 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003567 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003568 else if (svs->state & SRV_GOINGDOWN)
3569 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003570 }
Willy Tarreau91861262007-10-17 17:06:05 +02003571 else
Simon Horman125d0992013-02-24 17:23:38 +09003572 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003573 sv_state = 1; /* going up */
3574 else
3575 sv_state = 0; /* DOWN */
3576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003577 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003578 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003579 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003580 continue;
3581 }
3582
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003583 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3584 if (bi_putchk(rep, &trash) == -1)
3585 return 0;
3586 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003588 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003589 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003590
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003591 case STAT_PX_ST_BE:
3592 /* print the backend */
3593 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3594 if (bi_putchk(rep, &trash) == -1)
3595 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003597 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003599
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003600 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003601 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003602 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003603 if (bi_putchk(rep, &trash) == -1)
3604 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003605 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003606
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003607 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003610 case STAT_PX_ST_FIN:
3611 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003613 default:
3614 /* unknown state, we should put an abort() here ! */
3615 return 1;
3616 }
3617}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003618
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003619/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3620 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003621 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003622static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003623{
3624 /* WARNING! This must fit in the first buffer !!! */
3625 chunk_appendf(&trash,
3626 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3627 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3628 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3629 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3630 "<style type=\"text/css\"><!--\n"
3631 "body {"
3632 " font-family: arial, helvetica, sans-serif;"
3633 " font-size: 12px;"
3634 " font-weight: normal;"
3635 " color: black;"
3636 " background: white;"
3637 "}\n"
3638 "th,td {"
3639 " font-size: 10px;"
3640 "}\n"
3641 "h1 {"
3642 " font-size: x-large;"
3643 " margin-bottom: 0.5em;"
3644 "}\n"
3645 "h2 {"
3646 " font-family: helvetica, arial;"
3647 " font-size: x-large;"
3648 " font-weight: bold;"
3649 " font-style: italic;"
3650 " color: #6020a0;"
3651 " margin-top: 0em;"
3652 " margin-bottom: 0em;"
3653 "}\n"
3654 "h3 {"
3655 " font-family: helvetica, arial;"
3656 " font-size: 16px;"
3657 " font-weight: bold;"
3658 " color: #b00040;"
3659 " background: #e8e8d0;"
3660 " margin-top: 0em;"
3661 " margin-bottom: 0em;"
3662 "}\n"
3663 "li {"
3664 " margin-top: 0.25em;"
3665 " margin-right: 2em;"
3666 "}\n"
3667 ".hr {margin-top: 0.25em;"
3668 " border-color: black;"
3669 " border-bottom-style: solid;"
3670 "}\n"
3671 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3672 ".total {background: #20D0D0;color: #ffff80;}\n"
3673 ".frontend {background: #e8e8d0;}\n"
3674 ".socket {background: #d0d0d0;}\n"
3675 ".backend {background: #e8e8d0;}\n"
3676 ".active0 {background: #ff9090;}\n"
3677 ".active1 {background: #ffd020;}\n"
3678 ".active2 {background: #ffffa0;}\n"
3679 ".active3 {background: #c0ffc0;}\n"
3680 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003681 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3682 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3683 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003684 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003685 ".backup0 {background: #ff9090;}\n"
3686 ".backup1 {background: #ff80ff;}\n"
3687 ".backup2 {background: #c060ff;}\n"
3688 ".backup3 {background: #b0d0ff;}\n"
3689 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3690 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003691 ".backup6 {background: #c060ff;}\n"
3692 ".backup7 {background: #cc9900;}\n"
3693 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 ".maintain {background: #c07820;}\n"
3695 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3696 "\n"
3697 "a.px:link {color: #ffff40; text-decoration: none;}"
3698 "a.px:visited {color: #ffff40; text-decoration: none;}"
3699 "a.px:hover {color: #ffffff; text-decoration: none;}"
3700 "a.lfsb:link {color: #000000; text-decoration: none;}"
3701 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3702 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3703 "\n"
3704 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3705 "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"
3706 "table.tbl td.ac { text-align: center;}\n"
3707 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3708 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3709 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3710 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3711 "\n"
3712 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3713 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3714 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003715 "table.det { border-collapse: collapse; border-style: none; }\n"
3716 "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 +01003717 "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 +01003718 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003719 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003720 " display:block;\n"
3721 " visibility:hidden;\n"
3722 " z-index:2147483647;\n"
3723 " position:absolute;\n"
3724 " padding:2px 4px 3px;\n"
3725 " background:#f0f060; color:#000000;\n"
3726 " border:1px solid #7040c0;\n"
3727 " white-space:nowrap;\n"
3728 " font-style:normal;font-size:11px;font-weight:normal;\n"
3729 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3730 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3731 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003732 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003733 "-->\n"
3734 "</style></head>\n",
3735 (uri->flags & ST_SHNODE) ? " on " : "",
3736 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3737 );
3738}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003739
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003740/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003741 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003742 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003743 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003744static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003745{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003746 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003747 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003748 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003749
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003750 /* WARNING! this has to fit the first packet too.
3751 * We are around 3.5 kB, add adding entries will
3752 * become tricky if we want to support 4kB buffers !
3753 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003754 chunk_appendf(&trash,
3755 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3756 PRODUCT_NAME "%s</a></h1>\n"
3757 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3758 "<hr width=\"100%%\" class=\"hr\">\n"
3759 "<h3>&gt; General process information</h3>\n"
3760 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3761 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3762 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3763 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3764 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3765 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3766 "Running tasks: %d/%d; idle = %d %%<br>\n"
3767 "</td><td align=\"center\" nowrap>\n"
3768 "<table class=\"lgd\"><tr>\n"
3769 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3770 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3771 "</tr><tr>\n"
3772 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3773 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3774 "</tr><tr>\n"
3775 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3776 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3777 "</tr><tr>\n"
3778 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003779 "</tr><tr>\n"
3780 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003781 "</tr><tr>\n"
3782 "<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 -07003783 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003784 "<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 +01003785 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003786 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003787 "</td>"
3788 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3789 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3790 "",
3791 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3792 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3793 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3794 (uri->flags & ST_SHDESC) ? ": " : "",
3795 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3796 pid, relative_pid, global.nbproc,
3797 up / 86400, (up % 86400) / 3600,
3798 (up % 3600) / 60, (up % 60),
3799 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3800 global.rlimit_memmax ? " MB" : "",
3801 global.rlimit_nofile,
3802 global.maxsock, global.maxconn, global.maxpipes,
3803 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3804 run_queue_cur, nb_tasks_cur, idle_pct
3805 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003806
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003807 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3808 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3809 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003810
3811 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003812 "<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 +02003813 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003814 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3815 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3816 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003817 STAT_SCOPE_TXT_MAXLEN);
3818
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003819 /* 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 +02003820 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003821 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003822 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003823 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3824 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003825 }
3826
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003827 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003829 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003830 uri->uri_prefix,
3831 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003832 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003833 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 else
3835 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003836 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003837 uri->uri_prefix,
3838 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003839 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003840 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003841
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003843 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003844 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003845 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003847 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003848 "",
3849 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003850 else
3851 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003852 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003853 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003854 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003855 ";norefresh",
3856 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003857 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003858
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003859 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003860 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3863 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003864 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003865
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003866 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003867 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003868 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003869 (uri->refresh > 0) ? ";norefresh" : "",
3870 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003871
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003872 chunk_appendf(&trash,
3873 "</ul></td>"
3874 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3875 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3876 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3877 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3878 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3879 "</ul>"
3880 "</td>"
3881 "</tr></table>\n"
3882 ""
3883 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003885 if (appctx->ctx.stats.st_code) {
3886 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003887 case STAT_STATUS_DONE:
3888 chunk_appendf(&trash,
3889 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003890 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003891 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003892 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003893 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3894 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003895 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003896 break;
3897 case STAT_STATUS_NONE:
3898 chunk_appendf(&trash,
3899 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003900 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003901 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003902 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003903 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3904 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003905 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003906 break;
3907 case STAT_STATUS_PART:
3908 chunk_appendf(&trash,
3909 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003910 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003911 "Action partially processed.<br>"
3912 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003913 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003914 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3915 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003916 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003917 break;
3918 case STAT_STATUS_ERRP:
3919 chunk_appendf(&trash,
3920 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003921 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003922 "Action not processed because of invalid parameters."
3923 "<ul>"
3924 "<li>The action is maybe unknown.</li>"
3925 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3926 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3927 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003928 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003929 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3930 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003931 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003932 break;
3933 case STAT_STATUS_EXCD:
3934 chunk_appendf(&trash,
3935 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003936 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003937 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3938 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003939 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003940 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3941 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003942 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003943 break;
3944 case STAT_STATUS_DENY:
3945 chunk_appendf(&trash,
3946 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003947 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003948 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003949 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003950 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3951 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003952 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003953 break;
3954 default:
3955 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003956 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003957 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003958 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003959 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003960 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3961 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003962 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003963 }
3964 chunk_appendf(&trash, "<p>\n");
3965 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003967
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003968/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3969 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003970 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003971static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003973 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003974}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003975
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003976/* This function dumps statistics onto the stream interface's read buffer in
3977 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003978 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3979 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3980 * and the session must be closed, or -1 in case of any error. This function is
3981 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003982 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003983static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003984{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003985 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003986 struct channel *rep = si->ib;
3987 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003989 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003990
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003991 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003993 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003994 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003995
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003996 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003997 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003998 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003999 else
4000 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004001
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004002 if (bi_putchk(rep, &trash) == -1)
4003 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004004
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004005 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004006 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004008 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004009 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004010 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004011 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004012 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004013 }
Willy Tarreau91861262007-10-17 17:06:05 +02004014
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004015 appctx->ctx.stats.px = proxy;
4016 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4017 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004018 /* fall through */
4019
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004020 case STAT_ST_LIST:
4021 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004022 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004023 if (buffer_almost_full(rep->buf)) {
4024 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004025 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004026 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004027
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004028 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004029 /* skip the disabled proxies, global frontend and non-networked ones */
4030 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004031 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004034 appctx->ctx.stats.px = px->next;
4035 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004036 }
4037 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004039 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004040 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004042 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004043 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004044 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004045 if (bi_putchk(rep, &trash) == -1)
4046 return 0;
4047 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004048
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004049 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004050 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004051
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004052 case STAT_ST_FIN:
4053 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004054
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004055 default:
4056 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004057 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004058 return -1;
4059 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004060}
Willy Tarreauae526782010-03-04 20:34:23 +01004061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004062/* We reached the stats page through a POST request. The appctx is
4063 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004064 * Parse the posted data and enable/disable servers if necessary.
4065 * Returns 1 if request was parsed or zero if it needs more data.
4066 */
4067static int stats_process_http_post(struct stream_interface *si)
4068{
4069 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004070 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004071
4072 struct proxy *px = NULL;
4073 struct server *sv = NULL;
4074
4075 char key[LINESIZE];
4076 int action = ST_ADM_ACTION_NONE;
4077 int reprocess = 0;
4078
4079 int total_servers = 0;
4080 int altered_servers = 0;
4081
4082 char *first_param, *cur_param, *next_param, *end_params;
4083 char *st_cur_param = NULL;
4084 char *st_next_param = NULL;
4085
4086 struct chunk *temp;
4087 int reql;
4088
4089 temp = get_trash_chunk();
4090 if (temp->size < s->txn.req.body_len) {
4091 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004092 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004093 goto out;
4094 }
4095
4096 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4097 if (reql <= 0) {
4098 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004099 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004100 return 0;
4101 }
4102
4103 first_param = temp->str;
4104 end_params = temp->str + reql;
4105 cur_param = next_param = end_params;
4106 *end_params = '\0';
4107
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004108 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004109
4110 /*
4111 * Parse the parameters in reverse order to only store the last value.
4112 * From the html form, the backend and the action are at the end.
4113 */
4114 while (cur_param > first_param) {
4115 char *value;
4116 int poffset, plen;
4117
4118 cur_param--;
4119
4120 if ((*cur_param == '&') || (cur_param == first_param)) {
4121 reprocess_servers:
4122 /* Parse the key */
4123 poffset = (cur_param != first_param ? 1 : 0);
4124 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4125 if ((plen > 0) && (plen <= sizeof(key))) {
4126 strncpy(key, cur_param + poffset, plen);
4127 key[plen - 1] = '\0';
4128 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004129 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004130 goto out;
4131 }
4132
4133 /* Parse the value */
4134 value = key;
4135 while (*value != '\0' && *value != '=') {
4136 value++;
4137 }
4138 if (*value == '=') {
4139 /* Ok, a value is found, we can mark the end of the key */
4140 *value++ = '\0';
4141 }
4142 if (url_decode(key) < 0 || url_decode(value) < 0)
4143 break;
4144
4145 /* Now we can check the key to see what to do */
4146 if (!px && (strcmp(key, "b") == 0)) {
4147 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4148 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004149 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004150 goto out;
4151 }
4152 }
4153 else if (!action && (strcmp(key, "action") == 0)) {
4154 if (strcmp(value, "disable") == 0) {
4155 action = ST_ADM_ACTION_DISABLE;
4156 }
4157 else if (strcmp(value, "enable") == 0) {
4158 action = ST_ADM_ACTION_ENABLE;
4159 }
4160 else if (strcmp(value, "stop") == 0) {
4161 action = ST_ADM_ACTION_STOP;
4162 }
4163 else if (strcmp(value, "start") == 0) {
4164 action = ST_ADM_ACTION_START;
4165 }
4166 else if (strcmp(value, "shutdown") == 0) {
4167 action = ST_ADM_ACTION_SHUTDOWN;
4168 }
4169 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004170 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004171 goto out;
4172 }
4173 }
4174 else if (strcmp(key, "s") == 0) {
4175 if (!(px && action)) {
4176 /*
4177 * Indicates that we'll need to reprocess the parameters
4178 * as soon as backend and action are known
4179 */
4180 if (!reprocess) {
4181 st_cur_param = cur_param;
4182 st_next_param = next_param;
4183 }
4184 reprocess = 1;
4185 }
4186 else if ((sv = findserver(px, value)) != NULL) {
4187 switch (action) {
4188 case ST_ADM_ACTION_DISABLE:
4189 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4190 /* Not already in maintenance, we can change the server state */
4191 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004192 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004193 set_server_down(&sv->check);
4194 altered_servers++;
4195 total_servers++;
4196 }
4197 break;
4198 case ST_ADM_ACTION_ENABLE:
4199 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004200 /* Already in maintenance, we can change the server state.
4201 * If this server tracks the status of another one,
4202 * we must restore the good status.
4203 */
4204 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4205 set_server_up(&sv->check);
4206 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4207 }
4208 else {
4209 sv->state &= ~SRV_MAINTAIN;
4210 sv->check.state &= ~CHK_ST_PAUSED;
4211 set_server_down(&sv->check);
4212 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004213 altered_servers++;
4214 total_servers++;
4215 }
4216 break;
4217 case ST_ADM_ACTION_STOP:
4218 case ST_ADM_ACTION_START:
4219 if (action == ST_ADM_ACTION_START)
4220 sv->uweight = sv->iweight;
4221 else
4222 sv->uweight = 0;
4223
4224 server_recalc_eweight(sv);
4225 set_server_drain_state(sv);
4226
4227 altered_servers++;
4228 total_servers++;
4229 break;
4230 case ST_ADM_ACTION_SHUTDOWN:
4231 if (px->state != PR_STSTOPPED) {
4232 struct session *sess, *sess_bck;
4233
4234 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4235 if (sess->srv_conn == sv)
4236 session_shutdown(sess, SN_ERR_KILLED);
4237
4238 altered_servers++;
4239 total_servers++;
4240 }
4241 break;
4242 }
4243 } else {
4244 /* the server name is unknown or ambiguous (duplicate names) */
4245 total_servers++;
4246 }
4247 }
4248 if (reprocess && px && action) {
4249 /* Now, we know the backend and the action chosen by the user.
4250 * We can safely restart from the first server parameter
4251 * to reprocess them
4252 */
4253 cur_param = st_cur_param;
4254 next_param = st_next_param;
4255 reprocess = 0;
4256 goto reprocess_servers;
4257 }
4258
4259 next_param = cur_param;
4260 }
4261 }
4262
4263 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004264 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004265 }
4266 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004267 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004268 }
4269 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004270 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004271 }
4272 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004273 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004274 }
4275 out:
4276 return 1;
4277}
4278
4279
4280static int stats_send_http_headers(struct stream_interface *si)
4281{
4282 struct session *s = session_from_task(si->owner);
4283 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004284 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004285
4286 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004287 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004288 "Cache-Control: no-cache\r\n"
4289 "Connection: close\r\n"
4290 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004291 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004292
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004293 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004294 chunk_appendf(&trash, "Refresh: %d\r\n",
4295 uri->refresh);
4296
Willy Tarreau51437d22013-12-29 00:43:40 +01004297 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004298
4299 s->txn.status = 200;
4300 s->logs.tv_request = now;
4301
4302 if (bi_putchk(si->ib, &trash) == -1)
4303 return 0;
4304
4305 return 1;
4306}
4307
4308static int stats_send_http_redirect(struct stream_interface *si)
4309{
4310 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4311 struct session *s = session_from_task(si->owner);
4312 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004313 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004314
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004315 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004316 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004317 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004318 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004319 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4320 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004321 }
4322
4323 /* We don't want to land on the posted stats page because a refresh will
4324 * repost the data. We don't want this to happen on accident so we redirect
4325 * the browse to the stats page with a GET.
4326 */
4327 chunk_printf(&trash,
4328 "HTTP/1.1 303 See Other\r\n"
4329 "Cache-Control: no-cache\r\n"
4330 "Content-Type: text/plain\r\n"
4331 "Connection: close\r\n"
4332 "Location: %s;st=%s%s%s%s\r\n"
4333 "\r\n",
4334 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004335 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4336 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4337 stat_status_codes[appctx->ctx.stats.st_code]) ?
4338 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004339 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004340 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4341 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004342 scope_txt);
4343
4344 s->txn.status = 303;
4345 s->logs.tv_request = now;
4346
4347 if (bi_putchk(si->ib, &trash) == -1)
4348 return 0;
4349
4350 return 1;
4351}
4352
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353/* This I/O handler runs as an applet embedded in a stream interface. It is
4354 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004355 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356 * automatically unregisters itself once transfer is complete.
4357 */
4358static void http_stats_io_handler(struct stream_interface *si)
4359{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004360 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004361 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 struct channel *req = si->ob;
4363 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004364
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4366 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 /* check that the output is not closed */
4369 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004370 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004371
Willy Tarreau347a35d2013-11-22 17:51:09 +01004372 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004373 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004374 if (stats_send_http_headers(si)) {
4375 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004377 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004378 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004379 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004380 }
4381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004382 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004383 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004384 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004385 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004386
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004388 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004389 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004390 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004391 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004392 }
4393
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004394 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004395 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004396 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004397 }
4398
Willy Tarreau51437d22013-12-29 00:43:40 +01004399 if (appctx->st0 == STAT_HTTP_DONE)
4400 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004401
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004402 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4403 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004404
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004405 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004406 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4407 si_shutr(si);
4408 res->flags |= CF_READ_NULL;
4409 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004410 }
Willy Tarreau91861262007-10-17 17:06:05 +02004411
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004412 /* update all other flags and resync with the other side */
4413 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004414
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004415 /* we don't want to expire timeouts while we're processing requests */
4416 si->ib->rex = TICK_ETERNITY;
4417 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004418
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004419 out:
4420 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4421 /* check that we have released everything then unregister */
4422 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004423 }
4424}
4425
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004426
Willy Tarreau909d5172012-11-26 03:04:41 +01004427static inline const char *get_conn_ctrl_name(const struct connection *conn)
4428{
Willy Tarreau3c728722014-01-23 13:50:42 +01004429 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004430 return "NONE";
4431 return conn->ctrl->name;
4432}
4433
4434static inline const char *get_conn_xprt_name(const struct connection *conn)
4435{
4436 static char ptr[17];
4437
Willy Tarreauaad69382014-01-23 14:21:42 +01004438 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004439 return "NONE";
4440
4441 if (conn->xprt == &raw_sock)
4442 return "RAW";
4443
4444#ifdef USE_OPENSSL
4445 if (conn->xprt == &ssl_sock)
4446 return "SSL";
4447#endif
4448 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4449 return ptr;
4450}
4451
4452static inline const char *get_conn_data_name(const struct connection *conn)
4453{
4454 static char ptr[17];
4455
4456 if (!conn->data)
4457 return "NONE";
4458
4459 if (conn->data == &sess_conn_cb)
4460 return "SESS";
4461
4462 if (conn->data == &si_conn_cb)
4463 return "STRM";
4464
4465 if (conn->data == &check_conn_cb)
4466 return "CHCK";
4467
4468 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4469 return ptr;
4470}
4471
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004472/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004473 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004474 * 0 if the output buffer is full and it needs to be called again, otherwise
4475 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004476 */
Willy Tarreau76153662012-11-26 01:16:39 +01004477static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004478{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004479 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004480 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004481 extern const char *monthname[12];
4482 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004483 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004484 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004485
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004486 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004487
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004488 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004489 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004490 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4491 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004492 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004493 appctx->ctx.sess.uid = 0;
4494 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004495 return 1;
4496 }
4497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004499 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004500 appctx->ctx.sess.uid = sess->uniq_id;
4501 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004502 /* fall through */
4503
4504 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004505 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004506 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004507 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004508 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004509 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4510 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004511 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004512 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004513
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004514 conn = objt_conn(sess->si[0].end);
4515 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004516 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004517 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004518 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004520 break;
4521 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004522 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004523 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004524 default:
4525 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004526 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004527 break;
4528 }
4529
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004530 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004531 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004532 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004533
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004534 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004535 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004536 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4537 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4538 sess->listener ? sess->listener->luid : 0);
4539
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004540 if (conn)
4541 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004542
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004543 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004544 case AF_INET:
4545 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004546 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004547 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004548 break;
4549 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004550 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004551 break;
4552 default:
4553 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004554 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004555 break;
4556 }
4557
Willy Tarreau50943332011-09-02 17:33:05 +02004558 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004559 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004560 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004561 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004562 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4563 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004564 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004565
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004566 conn = objt_conn(sess->si[1].end);
4567 if (conn)
4568 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004569
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004570 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004571 case AF_INET:
4572 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004573 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004574 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004575 break;
4576 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004577 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004578 break;
4579 default:
4580 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004581 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004582 break;
4583 }
4584
4585 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004586 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004587 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004588 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4589 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004590 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004591 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004592
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004593 if (conn)
4594 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004595
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004596 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004597 case AF_INET:
4598 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004599 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004600 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004601 break;
4602 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004603 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004604 break;
4605 default:
4606 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004607 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004608 break;
4609 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004610
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004611 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004612 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004613 sess->task,
4614 sess->task->state,
4615 sess->task->nice, sess->task->calls,
4616 sess->task->expire ?
4617 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4618 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4619 TICKS_TO_MS(1000)) : "<NEVER>",
4620 task_in_rq(sess->task) ? ", running" : "");
4621
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004622 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004623 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004624 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004626 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004627 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4628 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4629 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4630
4631 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004632 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004633 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004634 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004635 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004636 obj_type_name(sess->si[0].end),
4637 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004638 sess->si[0].exp ?
4639 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4640 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4641 TICKS_TO_MS(1000)) : "<NEVER>",
4642 sess->si[0].err_type);
4643
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004644 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004645 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004646 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004647 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004648 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004649 obj_type_name(sess->si[1].end),
4650 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004651 sess->si[1].exp ?
4652 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4653 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4654 TICKS_TO_MS(1000)) : "<NEVER>",
4655 sess->si[1].err_type);
4656
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004657 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004658 chunk_appendf(&trash,
4659 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004660 conn,
4661 get_conn_ctrl_name(conn),
4662 get_conn_xprt_name(conn),
4663 get_conn_data_name(conn),
4664 obj_type_name(conn->target),
4665 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004666
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004667 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004668 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 conn->flags,
4670 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004671 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004672 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004673 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004674 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004675 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4676 chunk_appendf(&trash,
4677 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4678 tmpctx,
4679 tmpctx->st0,
4680 tmpctx->st1,
4681 tmpctx->st2,
4682 tmpctx->applet->name);
4683 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004684
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004685 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004686 chunk_appendf(&trash,
4687 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 conn,
4689 get_conn_ctrl_name(conn),
4690 get_conn_xprt_name(conn),
4691 get_conn_data_name(conn),
4692 obj_type_name(conn->target),
4693 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004694
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004695 chunk_appendf(&trash,
4696 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004697 conn->flags,
4698 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004699 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004700 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004701 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004702 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004703 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4704 chunk_appendf(&trash,
4705 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4706 tmpctx,
4707 tmpctx->st0,
4708 tmpctx->st1,
4709 tmpctx->st2,
4710 tmpctx->applet->name);
4711 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004712
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004713 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004714 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004715 " an_exp=%s",
4716 sess->req,
4717 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004718 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004719 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004720 sess->req->analyse_exp ?
4721 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4722 TICKS_TO_MS(1000)) : "<NEVER>");
4723
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004724 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004725 " rex=%s",
4726 sess->req->rex ?
4727 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4728 TICKS_TO_MS(1000)) : "<NEVER>");
4729
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004730 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004731 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004732 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004733 sess->req->wex ?
4734 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4735 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004736 sess->req->buf,
4737 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004738 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004739 sess->txn.req.next, sess->req->buf->i,
4740 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004741
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004742 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004743 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004744 " an_exp=%s",
4745 sess->rep,
4746 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004747 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004748 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004749 sess->rep->analyse_exp ?
4750 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4751 TICKS_TO_MS(1000)) : "<NEVER>");
4752
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004753 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004754 " rex=%s",
4755 sess->rep->rex ?
4756 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4757 TICKS_TO_MS(1000)) : "<NEVER>");
4758
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004759 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004760 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004761 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004762 sess->rep->wex ?
4763 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4764 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004765 sess->rep->buf,
4766 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004767 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004768 sess->txn.rsp.next, sess->rep->buf->i,
4769 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004770
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004771 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004772 return 0;
4773
4774 /* use other states to dump the contents */
4775 }
4776 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004777 appctx->ctx.sess.uid = 0;
4778 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004779 return 1;
4780}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004781
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004782static int stats_maps_list(struct stream_interface *si)
4783{
4784 struct appctx *appctx = __objt_appctx(si->end);
4785
4786 switch (appctx->st2) {
4787 case STAT_ST_INIT:
4788 /* Init to the first entry. The list cannot be change */
4789 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4790 appctx->st2 = STAT_ST_LIST;
4791 /* fall through */
4792
4793 case STAT_ST_LIST:
4794 while (appctx->ctx.map.ref) {
4795
4796 chunk_reset(&trash);
4797
4798 /* build messages */
4799 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4800
4801 if (bi_putchk(si->ib, &trash) == -1) {
4802 /* let's try again later from this session. We add ourselves into
4803 * this session's users so that it can remove us upon termination.
4804 */
4805 return 0;
4806 }
4807
4808 /* get next list entry and check the end of the list */
4809 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4810 struct map_reference *, list);
4811 if (&appctx->ctx.map.ref->list == &maps)
4812 break;
4813 }
4814
4815 appctx->st2 = STAT_ST_FIN;
4816 /* fall through */
4817
4818 default:
4819 appctx->st2 = STAT_ST_FIN;
4820 return 1;
4821 }
4822}
4823
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004824static int stats_map_lookup(struct stream_interface *si)
4825{
4826 struct appctx *appctx = __objt_appctx(si->end);
4827 struct sample_storage *smp;
4828 struct sample sample;
4829 struct pattern *pat;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004830 struct sockaddr_storage addr;
4831 char s_addr[INET_ADDRSTRLEN];
4832 char s_mask[INET_ADDRSTRLEN];
4833 char s_addr6[INET6_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004834
4835 switch (appctx->st2) {
4836 case STAT_ST_INIT:
4837 appctx->ctx.map.desc = NULL;
4838 stats_map_lookup_next(si);
4839 appctx->st2 = STAT_ST_LIST;
4840 /* fall through */
4841
4842 case STAT_ST_LIST:
4843 /* for each lookup type */
4844 while (appctx->ctx.map.desc) {
4845 /* initialise chunk to build new message */
4846 chunk_reset(&trash);
4847
4848 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004849 sample.type = SMP_T_STR;
4850 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004851 sample.data.str.len = appctx->ctx.map.chunk.len;
4852 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004853 pat = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, 1);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004854
4855 /* build return message: set type of match */
4856 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004857 chunk_appendf(&trash, "type=found");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004858 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004859 chunk_appendf(&trash, "type=bool");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004860 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004861 chunk_appendf(&trash, "type=int");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004862 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004863 chunk_appendf(&trash, "type=ip");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004864 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004865 chunk_appendf(&trash, "type=bin");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004866 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004867 chunk_appendf(&trash, "type=len");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004868 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004869 chunk_appendf(&trash, "type=str");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004870 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004871 chunk_appendf(&trash, "type=beg");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004872 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004873 chunk_appendf(&trash, "type=sub");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004874 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004875 chunk_appendf(&trash, "type=dir");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004876 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004877 chunk_appendf(&trash, "type=dom");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004878 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004879 chunk_appendf(&trash, "type=end");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004880 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004881 chunk_appendf(&trash, "type=reg");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004882 else /* The never appens case */
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004883 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.desc->pat->match);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004884
4885 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004886 if (!pat) {
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004887 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004888 }
4889
4890 /* Display match and match info */
4891 else {
4892 /* display match */
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004893 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004894
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004895 /* display index mode */
4896 if (pat->flags & PAT_F_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004897 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004898 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004899 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004900
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004901 /* case sensitive */
4902 if (pat->flags & PAT_F_IGNORE_CASE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004903 chunk_appendf(&trash, ", case=insensitive");
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004904 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004905 chunk_appendf(&trash, ", case=sensitive");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004906
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004907 /* display source */
4908 if (pat->flags & PAT_F_FROM_FILE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004909 chunk_appendf(&trash, ", src=file");
Thierry FOURNIERd5723432014-03-11 13:52:44 +01004910 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004911 chunk_appendf(&trash, ", src=conf");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004912
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004913 /* display string */
4914 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
4915 appctx->ctx.map.desc->pat->match == pat_match_str ||
4916 appctx->ctx.map.desc->pat->match == pat_match_beg ||
4917 appctx->ctx.map.desc->pat->match == pat_match_sub ||
4918 appctx->ctx.map.desc->pat->match == pat_match_dir ||
4919 appctx->ctx.map.desc->pat->match == pat_match_dom ||
4920 appctx->ctx.map.desc->pat->match == pat_match_end) {
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004921 chunk_appendf(&trash, ", key=\"%s\"", pat->ptr.str);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004922 }
4923 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4924 /* display IPv4/v6 */
4925 if (pat->type == SMP_T_IPV4) {
4926 ((struct sockaddr_in *)&addr)->sin_family = AF_INET;
4927 memcpy(&((struct sockaddr_in *)&addr)->sin_addr, &pat->val.ipv4.addr,
4928 sizeof(pat->val.ipv4.addr));
4929 if (addr_to_str(&addr, s_addr, INET_ADDRSTRLEN)) {
4930 memcpy(&((struct sockaddr_in *)&addr)->sin_addr, &pat->val.ipv4.mask,
4931 sizeof(pat->val.ipv4.mask));
4932 if (addr_to_str(&addr, s_mask, INET_ADDRSTRLEN))
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004933 chunk_appendf(&trash, ", key=\"%s/%s\"", s_addr, s_mask);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004934 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004935 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004936 else if (pat->type == SMP_T_IPV6) {
4937 ((struct sockaddr_in6 *)&addr)->sin6_family = AF_INET6;
4938 memcpy(&((struct sockaddr_in6 *)&addr)->sin6_addr, &pat->val.ipv6.addr,
4939 sizeof(pat->val.ipv6.addr));
4940 if (addr_to_str(&addr, s_addr6, INET6_ADDRSTRLEN))
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004941 chunk_appendf(&trash, ", key=\"%s/%d\"", s_addr6, pat->val.ipv6.mask);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004942 }
4943 }
Thierry FOURNIERb4be1292014-01-17 15:14:41 +01004944 else if (appctx->ctx.map.desc->pat->match == pat_match_int) {
4945 /* display int */
4946 chunk_appendf(&trash, "match=\"");
4947 if (pat->val.range.min_set && pat->val.range.max_set &&
4948 pat->val.range.min == pat->val.range.max) {
4949 chunk_appendf(&trash, "%lld", pat->val.range.min);
4950 }
4951 else {
4952 if (pat->val.range.min_set)
4953 chunk_appendf(&trash, "is >= %lld", pat->val.range.min);
4954 if (pat->val.range.min_set && pat->val.range.max_set)
4955 chunk_appendf(&trash, " and ");
4956 if (pat->val.range.max_set)
4957 chunk_appendf(&trash, "is <= %lld", pat->val.range.max);
4958 }
4959 chunk_appendf(&trash, "\", ");
4960 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004961 }
4962
4963 /* display return value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004964 if (!pat || !pat->smp) {
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004965 chunk_appendf(&trash, ", value=nothing");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004966 }
4967 else {
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004968 smp = pat->smp;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004969 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4970 sample.type = smp->type;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004971 if (sample_casts[sample.type][SMP_T_STR] &&
4972 sample_casts[sample.type][SMP_T_STR](&sample))
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004973 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004974 sample.data.str.str, smp_to_type[smp->type]);
4975 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004976 chunk_appendf(&trash, ", value=cannot-display, type=\"%s\"",
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004977 smp_to_type[smp->type]);
4978 }
4979
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004980 chunk_appendf(&trash, "\n");
4981
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004982 /* display response */
4983 if (bi_putchk(si->ib, &trash) == -1) {
4984 /* let's try again later from this session. We add ourselves into
4985 * this session's users so that it can remove us upon termination.
4986 */
4987 return 0;
4988 }
4989
4990 /* get next entry */
4991 stats_map_lookup_next(si);
4992 }
4993
4994 appctx->st2 = STAT_ST_FIN;
4995 /* fall through */
4996
4997 default:
4998 appctx->st2 = STAT_ST_FIN;
4999 free(appctx->ctx.map.chunk.str);
5000 return 1;
5001 }
5002}
5003
5004static int stats_map_list(struct stream_interface *si)
5005{
5006 struct appctx *appctx = __objt_appctx(si->end);
5007
5008 switch (appctx->st2) {
5009
5010 case STAT_ST_INIT:
5011 /* Init to the first entry. The list cannot be change */
5012 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
5013 struct map_entry *, list);
5014 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
5015 appctx->ctx.map.ent = NULL;
5016 appctx->st2 = STAT_ST_LIST;
5017 /* fall through */
5018
5019 case STAT_ST_LIST:
5020 while (appctx->ctx.map.ent) {
5021 chunk_reset(&trash);
5022
5023 /* build messages */
5024 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
5025
5026 if (bi_putchk(si->ib, &trash) == -1) {
5027 /* let's try again later from this session. We add ourselves into
5028 * this session's users so that it can remove us upon termination.
5029 */
5030 return 0;
5031 }
5032
5033 /* get next list entry and check the end of the list */
5034 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
5035 struct map_entry *, list);
5036 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
5037 break;
5038 }
5039
5040 appctx->st2 = STAT_ST_FIN;
5041 /* fall through */
5042
5043 default:
5044 appctx->st2 = STAT_ST_FIN;
5045 return 1;
5046 }
5047}
5048
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005049/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005050 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005051 * to be called again, otherwise non-zero. It is designed to be called
5052 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005053 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005054static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005055{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005056 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005057 struct connection *conn;
5058
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005059 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005060 /* If we're forced to shut down, we might have to remove our
5061 * reference to the last session being dumped.
5062 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005063 if (appctx->st2 == STAT_ST_LIST) {
5064 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5065 LIST_DEL(&appctx->ctx.sess.bref.users);
5066 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005067 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005068 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005069 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005070 }
5071
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005072 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005073
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005074 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005075 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005076 /* the function had not been called yet, let's prepare the
5077 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005078 * pointer to the first in the global list. When a target
5079 * session is being destroyed, it is responsible for updating
5080 * this pointer. We know we have reached the end when this
5081 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005082 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005083 LIST_INIT(&appctx->ctx.sess.bref.users);
5084 appctx->ctx.sess.bref.ref = sessions.n;
5085 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005086 /* fall through */
5087
Willy Tarreau295a8372011-03-10 11:25:07 +01005088 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005089 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005090 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5091 LIST_DEL(&appctx->ctx.sess.bref.users);
5092 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005093 }
5094
5095 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005096 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005097 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005098 struct session *curr_sess;
5099
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005100 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005101
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005102 if (appctx->ctx.sess.target) {
5103 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005104 goto next_sess;
5105
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005106 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005107 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005108 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005109 return 0;
5110
5111 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005112 LIST_DEL(&appctx->ctx.sess.bref.users);
5113 LIST_INIT(&appctx->ctx.sess.bref.users);
5114 if (appctx->ctx.sess.target != (void *)-1) {
5115 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005116 break;
5117 }
5118 else
5119 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005120 }
5121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005122 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005123 "%p: proto=%s",
5124 curr_sess,
5125 curr_sess->listener->proto->name);
5126
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005127
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005128 conn = objt_conn(curr_sess->si[0].end);
5129 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005130 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005131 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005132 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005133 " src=%s:%d fe=%s be=%s srv=%s",
5134 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005135 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005136 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005137 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005138 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005139 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005140 break;
5141 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005142 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005143 " src=unix:%d fe=%s be=%s srv=%s",
5144 curr_sess->listener->luid,
5145 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005146 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005147 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005148 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005149 break;
5150 }
5151
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005152 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005153 " ts=%02x age=%s calls=%d",
5154 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005155 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5156 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005157
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005158 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005159 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005160 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005161 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005162 curr_sess->req->analysers,
5163 curr_sess->req->rex ?
5164 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5165 TICKS_TO_MS(1000)) : "");
5166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005167 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005168 ",wx=%s",
5169 curr_sess->req->wex ?
5170 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5171 TICKS_TO_MS(1000)) : "");
5172
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005173 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005174 ",ax=%s]",
5175 curr_sess->req->analyse_exp ?
5176 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5177 TICKS_TO_MS(1000)) : "");
5178
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005179 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005180 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005181 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005182 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005183 curr_sess->rep->analysers,
5184 curr_sess->rep->rex ?
5185 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5186 TICKS_TO_MS(1000)) : "");
5187
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005188 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005189 ",wx=%s",
5190 curr_sess->rep->wex ?
5191 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5192 TICKS_TO_MS(1000)) : "");
5193
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005194 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005195 ",ax=%s]",
5196 curr_sess->rep->analyse_exp ?
5197 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5198 TICKS_TO_MS(1000)) : "");
5199
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005200 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005201 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005202 " s0=[%d,%1xh,fd=%d,ex=%s]",
5203 curr_sess->si[0].state,
5204 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005205 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005206 curr_sess->si[0].exp ?
5207 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5208 TICKS_TO_MS(1000)) : "");
5209
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005210 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005211 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005212 " s1=[%d,%1xh,fd=%d,ex=%s]",
5213 curr_sess->si[1].state,
5214 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005215 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005216 curr_sess->si[1].exp ?
5217 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5218 TICKS_TO_MS(1000)) : "");
5219
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005220 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005221 " exp=%s",
5222 curr_sess->task->expire ?
5223 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5224 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005225 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005226 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005227
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005228 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005229
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005230 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005231 /* let's try again later from this session. We add ourselves into
5232 * this session's users so that it can remove us upon termination.
5233 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005234 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005235 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005236 }
5237
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005238 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005239 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005240 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005241
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005242 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005243 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005244 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005245 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005246 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005247 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005248
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005249 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005250 return 0;
5251
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005252 appctx->ctx.sess.target = NULL;
5253 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005254 return 1;
5255 }
5256
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005257 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005258 /* fall through */
5259
5260 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005261 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005262 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005263 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005264}
5265
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005266/* This is called when the stream interface is closed. For instance, upon an
5267 * external abort, we won't call the i/o handler anymore so we may need to
5268 * remove back references to the session currently being dumped.
5269 */
Simon Horman74d88312013-02-12 10:45:50 +09005270static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005271{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005272 struct appctx *appctx = __objt_appctx(si->end);
5273
5274 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5275 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5276 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005277 }
5278}
5279
Willy Tarreau20e99322013-04-13 09:22:25 +02005280/* This function is used to either dump tables states (when action is set
5281 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005282 * It returns 0 if the output buffer is full and it needs to be called
5283 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005284 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005285static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005286{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005287 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005288 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005289 struct ebmb_node *eb;
5290 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005291 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005292 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005293
5294 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005295 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005296 * - STAT_ST_INIT : the first call
5297 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005298 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005299 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005300 * and the entry pointer points to the next entry to be dumped,
5301 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005302 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005303 * data though.
5304 */
5305
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005306 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005307 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005308 if (appctx->st2 == STAT_ST_LIST) {
5309 appctx->ctx.table.entry->ref_cnt--;
5310 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005311 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005312 return 1;
5313 }
5314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005315 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005316
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005317 while (appctx->st2 != STAT_ST_FIN) {
5318 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005319 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005320 appctx->ctx.table.proxy = appctx->ctx.table.target;
5321 if (!appctx->ctx.table.proxy)
5322 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005324 appctx->ctx.table.entry = NULL;
5325 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005326 break;
5327
Willy Tarreau295a8372011-03-10 11:25:07 +01005328 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005329 if (!appctx->ctx.table.proxy ||
5330 (appctx->ctx.table.target &&
5331 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5332 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005333 break;
5334 }
5335
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005336 if (appctx->ctx.table.proxy->table.size) {
5337 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5338 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005339 return 0;
5340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005341 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005342 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005343 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005344 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005345 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005346 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5347 appctx->ctx.table.entry->ref_cnt++;
5348 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005349 break;
5350 }
5351 }
5352 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005353 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005354 break;
5355
Willy Tarreau295a8372011-03-10 11:25:07 +01005356 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005357 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005358
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005359 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005360 /* we're filtering on some data contents */
5361 void *ptr;
5362 long long data;
5363
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005364 dt = appctx->ctx.table.data_type;
5365 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5366 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005367 dt);
5368
5369 data = 0;
5370 switch (stktable_data_types[dt].std_type) {
5371 case STD_T_SINT:
5372 data = stktable_data_cast(ptr, std_t_sint);
5373 break;
5374 case STD_T_UINT:
5375 data = stktable_data_cast(ptr, std_t_uint);
5376 break;
5377 case STD_T_ULL:
5378 data = stktable_data_cast(ptr, std_t_ull);
5379 break;
5380 case STD_T_FRQP:
5381 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005382 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005383 break;
5384 }
5385
5386 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005387 if ((data < appctx->ctx.table.value &&
5388 (appctx->ctx.table.data_op == STD_OP_EQ ||
5389 appctx->ctx.table.data_op == STD_OP_GT ||
5390 appctx->ctx.table.data_op == STD_OP_GE)) ||
5391 (data == appctx->ctx.table.value &&
5392 (appctx->ctx.table.data_op == STD_OP_NE ||
5393 appctx->ctx.table.data_op == STD_OP_GT ||
5394 appctx->ctx.table.data_op == STD_OP_LT)) ||
5395 (data > appctx->ctx.table.value &&
5396 (appctx->ctx.table.data_op == STD_OP_EQ ||
5397 appctx->ctx.table.data_op == STD_OP_LT ||
5398 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005399 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005400 }
5401
Simon Hormanc88b8872011-06-15 15:18:49 +09005402 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005403 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5404 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005405 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005406
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005407 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005408
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005409 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005410 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005411 struct stksess *old = appctx->ctx.table.entry;
5412 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005413 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005414 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5415 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5416 stksess_kill(&appctx->ctx.table.proxy->table, old);
5417 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005418 break;
5419 }
5420
Simon Hormanc88b8872011-06-15 15:18:49 +09005421
5422 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5424 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5425 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005426
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005427 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5428 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005429 break;
5430
Willy Tarreau295a8372011-03-10 11:25:07 +01005431 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005432 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005433 break;
5434 }
5435 }
5436 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005437}
5438
Willy Tarreaud426a182010-03-05 14:58:26 +01005439/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005440 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5441 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5442 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5443 * lines are respected within the limit of 70 output chars. Lines that are
5444 * continuation of a previous truncated line begin with "+" instead of " "
5445 * after the offset. The new pointer is returned.
5446 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005447static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5448 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005449{
5450 int end;
5451 unsigned char c;
5452
5453 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005454 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005455 return ptr;
5456
Willy Tarreau77804732012-10-29 16:14:26 +01005457 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005458
Willy Tarreaud426a182010-03-05 14:58:26 +01005459 while (ptr < len && ptr < bsize) {
5460 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005461 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005462 if (out->len > end - 2)
5463 break;
5464 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005465 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005466 if (out->len > end - 3)
5467 break;
5468 out->str[out->len++] = '\\';
5469 switch (c) {
5470 case '\t': c = 't'; break;
5471 case '\n': c = 'n'; break;
5472 case '\r': c = 'r'; break;
5473 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005474 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005475 }
5476 out->str[out->len++] = c;
5477 } else {
5478 if (out->len > end - 5)
5479 break;
5480 out->str[out->len++] = '\\';
5481 out->str[out->len++] = 'x';
5482 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5483 out->str[out->len++] = hextab[c & 0xF];
5484 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005485 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005486 /* we had a line break, let's return now */
5487 out->str[out->len++] = '\n';
5488 *line = ptr;
5489 return ptr;
5490 }
5491 }
5492 /* we have an incomplete line, we return it as-is */
5493 out->str[out->len++] = '\n';
5494 return ptr;
5495}
5496
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005497/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005498 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005499 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005500 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005501static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005502{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005503 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005504 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005505
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005506 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005507 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005508
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005509 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005511 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005512 /* the function had not been called yet, let's prepare the
5513 * buffer for a response.
5514 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005515 struct tm tm;
5516
5517 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005518 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005519 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5520 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5521 error_snapshot_id);
5522
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005523 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005524 /* Socket buffer full. Let's try again later from the same point */
5525 return 0;
5526 }
5527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005528 appctx->ctx.errors.px = proxy;
5529 appctx->ctx.errors.buf = 0;
5530 appctx->ctx.errors.bol = 0;
5531 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005532 }
5533
5534 /* we have two inner loops here, one for the proxy, the other one for
5535 * the buffer.
5536 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005537 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005538 struct error_snapshot *es;
5539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005540 if (appctx->ctx.errors.buf == 0)
5541 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005542 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005543 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005544
5545 if (!es->when.tv_sec)
5546 goto next;
5547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005548 if (appctx->ctx.errors.iid >= 0 &&
5549 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5550 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005551 goto next;
5552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005553 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005554 /* just print headers now */
5555
5556 char pn[INET6_ADDRSTRLEN];
5557 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005558 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005559
5560 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005561 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005562 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005563 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005564
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005565 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5566 case AF_INET:
5567 case AF_INET6:
5568 port = get_host_port(&es->src);
5569 break;
5570 default:
5571 port = 0;
5572 }
5573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005575 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005576 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005577 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005578 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005579 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005580 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5581 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005582 break;
5583 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005584 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005585 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005586 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005587 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005588 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005589 break;
5590 }
5591
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005592 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005593 ", server %s (#%d), event #%u\n"
5594 " src %s:%d, session #%d, session flags 0x%08x\n"
5595 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5596 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5597 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5598 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5599 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5600 es->ev_id,
5601 pn, port, es->sid, es->s_flags,
5602 es->state, es->m_flags, es->t_flags,
5603 es->m_clen, es->m_blen,
5604 es->b_flags, es->b_out, es->b_tot,
5605 es->len, es->b_wrap, es->pos);
5606
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005607 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005608 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005609 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005610 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005611 appctx->ctx.errors.ptr = 0;
5612 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005613 }
5614
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005615 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005616 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005617 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005618 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005619 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005620 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005621 goto next;
5622 }
5623
5624 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005625 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005626 int newptr;
5627 int newline;
5628
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005629 newline = appctx->ctx.errors.bol;
5630 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5631 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005632 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005633
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005634 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005635 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005636 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005637 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 appctx->ctx.errors.ptr = newptr;
5639 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005640 };
5641 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005642 appctx->ctx.errors.bol = 0;
5643 appctx->ctx.errors.ptr = -1;
5644 appctx->ctx.errors.buf++;
5645 if (appctx->ctx.errors.buf > 1) {
5646 appctx->ctx.errors.buf = 0;
5647 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005648 }
5649 }
5650
5651 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005652 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005653}
5654
Willy Tarreaud5781202012-09-22 19:32:35 +02005655/* parse the "level" argument on the bind lines */
5656static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5657{
5658 if (!*args[cur_arg + 1]) {
5659 memprintf(err, "'%s' : missing level", args[cur_arg]);
5660 return ERR_ALERT | ERR_FATAL;
5661 }
5662
5663 if (!strcmp(args[cur_arg+1], "user"))
5664 conf->level = ACCESS_LVL_USER;
5665 else if (!strcmp(args[cur_arg+1], "operator"))
5666 conf->level = ACCESS_LVL_OPER;
5667 else if (!strcmp(args[cur_arg+1], "admin"))
5668 conf->level = ACCESS_LVL_ADMIN;
5669 else {
5670 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5671 args[cur_arg], args[cur_arg+1]);
5672 return ERR_ALERT | ERR_FATAL;
5673 }
5674
5675 return 0;
5676}
5677
Willy Tarreaub24281b2011-02-13 13:16:36 +01005678struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005679 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005680 .name = "<STATS>", /* used for logging */
5681 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005682 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005683};
5684
Simon Horman9bd2c732011-06-15 15:18:44 +09005685static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005686 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005687 .name = "<CLI>", /* used for logging */
5688 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005689 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005690};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005691
Willy Tarreaudc13c112013-06-21 23:16:39 +02005692static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005693 { CFG_GLOBAL, "stats", stats_parse_global },
5694 { 0, NULL, NULL },
5695}};
5696
Willy Tarreaud5781202012-09-22 19:32:35 +02005697static struct bind_kw_list bind_kws = { "STAT", { }, {
5698 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5699 { NULL, NULL, 0 },
5700}};
5701
Willy Tarreau10522fd2008-07-09 20:12:41 +02005702__attribute__((constructor))
5703static void __dumpstats_module_init(void)
5704{
5705 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005706 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005707}
5708
Willy Tarreau91861262007-10-17 17:06:05 +02005709/*
5710 * Local variables:
5711 * c-indent-level: 8
5712 * c-basic-offset: 8
5713 * End:
5714 */