blob: 0eb563970a8ea99ef103efb7c82b882d039d77e9 [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"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100134 " clear map [id] : clear the content of this map\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200135 " help : this message\n"
136 " prompt : toggle interactive mode with prompt\n"
137 " quit : disconnect\n"
138 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100139 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200140 " show stat : report counters for each proxy and server\n"
141 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100142 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200143 " show table [id]: report table usage stats or dump this table's contents\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100144 " show map [id] : report avalaible maps or dump this map's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200145 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200146 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200147 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100148 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200149 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200150 " set rate-limit : change a rate limiting value\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100151 " set map [id] [key] [value] : modify map entry\n"
152 " add map [id] [key] [value] : add map entry\n"
153 " del map [id] [key] : delete map entry\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200154 " disable : put a server or frontend in maintenance mode\n"
155 " enable : re-enable a server or frontend which is in maintenance mode\n"
156 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200157 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200158
Simon Horman9bd2c732011-06-15 15:18:44 +0900159static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200160 "Permission denied\n"
161 "";
162
Willy Tarreau295a8372011-03-10 11:25:07 +0100163/* data transmission states for the stats responses */
164enum {
165 STAT_ST_INIT = 0,
166 STAT_ST_HEAD,
167 STAT_ST_INFO,
168 STAT_ST_LIST,
169 STAT_ST_END,
170 STAT_ST_FIN,
171};
172
173/* data transmission states for the stats responses inside a proxy */
174enum {
175 STAT_PX_ST_INIT = 0,
176 STAT_PX_ST_TH,
177 STAT_PX_ST_FE,
178 STAT_PX_ST_LI,
179 STAT_PX_ST_SV,
180 STAT_PX_ST_BE,
181 STAT_PX_ST_END,
182 STAT_PX_ST_FIN,
183};
184
Cyril Bonté19979e12012-04-04 12:57:21 +0200185extern const char *stat_status_codes[];
186
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200187/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200188 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200189 * needs to be closed and ignored, or a negative value upon critical failure.
190 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900191static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200192{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100193 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100194 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200195
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200196 tv_zero(&s->logs.tv_request);
197 s->logs.t_queue = 0;
198 s->logs.t_connect = 0;
199 s->logs.t_data = 0;
200 s->logs.t_close = 0;
201 s->logs.bytes_in = s->logs.bytes_out = 0;
202 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
203 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200204
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200205 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200206
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200207 if (s->listener->timeout) {
208 s->req->rto = *s->listener->timeout;
209 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200210 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200212}
213
Willy Tarreau07e9e642010-08-17 21:48:17 +0200214/* allocate a new stats frontend named <name>, and return it
215 * (or NULL in case of lack of memory).
216 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200217static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200218{
219 struct proxy *fe;
220
221 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
222 if (!fe)
223 return NULL;
224
Willy Tarreau237250c2011-07-29 01:49:03 +0200225 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200226 fe->next = proxy;
227 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200228 fe->last_change = now.tv_sec;
229 fe->id = strdup("GLOBAL");
230 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200231 fe->maxconn = 10; /* default to 10 concurrent connections */
232 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200233 fe->conf.file = strdup(file);
234 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200235 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200236
237 /* the stats frontend is the only one able to assign ID #0 */
238 fe->conf.id.key = fe->uuid = 0;
239 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200240 return fe;
241}
242
Willy Tarreaufbee7132007-10-18 13:53:22 +0200243/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200244 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
245 * error message into the <err> buffer which will be preallocated. The trailing
246 * '\n' must not be written. The function must be called with <args> pointing to
247 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200249static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200250 struct proxy *defpx, const char *file, int line,
251 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200252{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200253 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200254 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200255
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200256 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200257 int cur_arg;
258
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200259 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200260 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 +0200261 return -1;
262 }
263
Willy Tarreau89a63132009-08-16 17:41:45 +0200264 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200265 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200267 return -1;
268 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200269 }
270
Willy Tarreau4348fad2012-09-20 16:48:07 +0200271 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200272 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200273
Willy Tarreauc53d4222012-09-20 20:19:28 +0200274 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
275 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
276 file, line, args[0], args[1], err && *err ? *err : "error");
277 return -1;
278 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200281 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200282 static int bind_dumped;
283 struct bind_kw *kw;
284
285 kw = bind_find_kw(args[cur_arg]);
286 if (kw) {
287 if (!kw->parse) {
288 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
289 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290 return -1;
291 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200292
293 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
294 if (err && *err)
295 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
296 else
297 memprintf(err, "'%s %s' : error encountered while processing '%s'",
298 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200299 return -1;
300 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200301
302 cur_arg += 1 + kw->skip;
303 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200304 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200305
306 if (!bind_dumped) {
307 bind_dump_kws(err);
308 indent_msg(err, 4);
309 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200310 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200311
312 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
313 args[0], args[1], args[cur_arg],
314 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
315 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100317
Willy Tarreauc53d4222012-09-20 20:19:28 +0200318 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
319 l->maxconn = global.stats_fe->maxconn;
320 l->backlog = global.stats_fe->backlog;
321 l->timeout = &global.stats_fe->timeout.client;
322 l->accept = session_accept;
323 l->handler = process_session;
324 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
325 l->nice = -64; /* we want to boost priority for local stats */
326 global.maxsock += l->maxconn;
327 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200328 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200329 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100330 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200331 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100332
333 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200334 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100335 return -1;
336 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200337
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100338 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200339 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200340 return -1;
341 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200342 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200343 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200344 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200345 return -1;
346 }
347 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200348 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200349 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 else if (!strcmp(args[1], "maxconn")) {
351 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200352
353 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200354 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200355 return -1;
356 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200357
358 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200359 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200361 return -1;
362 }
363 }
364 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200365 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200366 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
367 int cur_arg = 2;
368 unsigned int set = 0;
369
Willy Tarreau91319572013-04-20 09:48:50 +0200370 if (!global.stats_fe) {
371 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
372 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
373 return -1;
374 }
375 }
376
Willy Tarreau35b7b162012-10-22 23:17:18 +0200377 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100378 unsigned int low, high;
379
Willy Tarreau35b7b162012-10-22 23:17:18 +0200380 if (strcmp(args[cur_arg], "all") == 0) {
381 set = 0;
382 break;
383 }
384 else if (strcmp(args[cur_arg], "odd") == 0) {
385 set |= 0x55555555;
386 }
387 else if (strcmp(args[cur_arg], "even") == 0) {
388 set |= 0xAAAAAAAA;
389 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100390 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100391 char *dash = strchr(args[cur_arg], '-');
392
393 low = high = str2uic(args[cur_arg]);
394 if (dash)
395 high = str2uic(dash + 1);
396
397 if (high < low) {
398 unsigned int swap = low;
399 low = high;
400 high = swap;
401 }
402
403 if (low < 1 || high > 32) {
404 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
405 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200406 return -1;
407 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100408
409 while (low <= high)
410 set |= 1 << (low++ - 1);
411 }
412 else {
413 memprintf(err,
414 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
415 args[0], args[1]);
416 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200417 }
418 cur_arg++;
419 }
420 global.stats_fe->bind_proc = set;
421 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200422 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200423 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200424 return -1;
425 }
426 return 0;
427}
428
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100429/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
430 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100431 * NOTE: Some tools happen to rely on the field position instead of its name,
432 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100433 */
434static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100435{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100436 chunk_appendf(&trash,
437 "# pxname,svname,"
438 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100439 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100440 "bin,bout,"
441 "dreq,dresp,"
442 "ereq,econ,eresp,"
443 "wretr,wredis,"
444 "status,weight,act,bck,"
445 "chkfail,chkdown,lastchg,downtime,qlimit,"
446 "pid,iid,sid,throttle,lbtot,tracked,type,"
447 "rate,rate_lim,rate_max,"
448 "check_status,check_code,check_duration,"
449 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
450 "req_rate,req_rate_max,req_tot,"
451 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100452 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100453 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100454}
455
Simon Hormand9366582011-06-15 15:18:45 +0900456/* print a string of text buffer to <out>. The format is :
457 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
458 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
459 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
460 */
461static int dump_text(struct chunk *out, const char *buf, int bsize)
462{
463 unsigned char c;
464 int ptr = 0;
465
466 while (buf[ptr] && ptr < bsize) {
467 c = buf[ptr];
468 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
469 if (out->len > out->size - 1)
470 break;
471 out->str[out->len++] = c;
472 }
473 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
474 if (out->len > out->size - 2)
475 break;
476 out->str[out->len++] = '\\';
477 switch (c) {
478 case ' ': c = ' '; break;
479 case '\t': c = 't'; break;
480 case '\n': c = 'n'; break;
481 case '\r': c = 'r'; break;
482 case '\e': c = 'e'; break;
483 case '\\': c = '\\'; break;
484 }
485 out->str[out->len++] = c;
486 }
487 else {
488 if (out->len > out->size - 4)
489 break;
490 out->str[out->len++] = '\\';
491 out->str[out->len++] = 'x';
492 out->str[out->len++] = hextab[(c >> 4) & 0xF];
493 out->str[out->len++] = hextab[c & 0xF];
494 }
495 ptr++;
496 }
497
498 return ptr;
499}
500
501/* print a buffer in hexa.
502 * Print stopped if <bsize> is reached, or if no more place in the chunk.
503 */
504static int dump_binary(struct chunk *out, const char *buf, int bsize)
505{
506 unsigned char c;
507 int ptr = 0;
508
509 while (ptr < bsize) {
510 c = buf[ptr];
511
512 if (out->len > out->size - 2)
513 break;
514 out->str[out->len++] = hextab[(c >> 4) & 0xF];
515 out->str[out->len++] = hextab[c & 0xF];
516
517 ptr++;
518 }
519 return ptr;
520}
521
522/* Dump the status of a table to a stream interface's
523 * read buffer. It returns 0 if the output buffer is full
524 * and needs to be called again, otherwise non-zero.
525 */
526static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
527 struct proxy *proxy, struct proxy *target)
528{
Willy Tarreau306f8302013-07-08 15:53:06 +0200529 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900530
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900532 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
533
534 /* any other information should be dumped here */
535
Willy Tarreau290e63a2012-09-20 18:07:14 +0200536 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900538
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200539 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900540 return 0;
541
542 return 1;
543}
544
545/* Dump the a table entry to a stream interface's
546 * read buffer. It returns 0 if the output buffer is full
547 * and needs to be called again, otherwise non-zero.
548 */
549static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
550 struct proxy *proxy, struct stksess *entry)
551{
552 int dt;
553
Willy Tarreau77804732012-10-29 16:14:26 +0100554 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900555
556 if (proxy->table.type == STKTABLE_TYPE_IP) {
557 char addr[INET_ADDRSTRLEN];
558 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100559 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900560 }
561 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
562 char addr[INET6_ADDRSTRLEN];
563 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100564 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900565 }
566 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100567 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900568 }
569 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100570 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900571 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
572 }
573 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100574 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900575 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
576 }
577
Willy Tarreau77804732012-10-29 16:14:26 +0100578 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900579
580 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
581 void *ptr;
582
583 if (proxy->table.data_ofs[dt] == 0)
584 continue;
585 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100586 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900587 else
Willy Tarreau77804732012-10-29 16:14:26 +0100588 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900589
590 ptr = stktable_data_ptr(&proxy->table, entry, dt);
591 switch (stktable_data_types[dt].std_type) {
592 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100593 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900594 break;
595 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100596 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900597 break;
598 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100599 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900600 break;
601 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100602 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900603 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
604 proxy->table.data_arg[dt].u));
605 break;
606 }
607 }
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900609
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200610 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900611 return 0;
612
613 return 1;
614}
615
Willy Tarreaudec98142012-06-06 23:37:08 +0200616static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900617{
Willy Tarreau306f8302013-07-08 15:53:06 +0200618 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100619 struct appctx *appctx = __objt_appctx(si->end);
620 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900621 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900622 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900623 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200624 long long value;
625 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200626 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200627 void *ptr;
628 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900629
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100630 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900631
632 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100633 appctx->ctx.cli.msg = "Key value expected\n";
634 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900635 return;
636 }
637
Simon Hormanc5b89f62011-06-15 15:18:50 +0900638 switch (px->table.type) {
639 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900640 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100641 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900642 break;
643 case STKTABLE_TYPE_IPV6:
644 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100645 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900646 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900647 case STKTABLE_TYPE_INTEGER:
648 {
649 char *endptr;
650 unsigned long val;
651 errno = 0;
652 val = strtoul(args[4], &endptr, 10);
653 if ((errno == ERANGE && val == ULONG_MAX) ||
654 (errno != 0 && val == 0) || endptr == args[4] ||
655 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100656 appctx->ctx.cli.msg = "Invalid key\n";
657 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900658 return;
659 }
660 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100661 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900662 break;
663 }
664 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900665 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100666 static_table_key->key = args[4];
667 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900668 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900669 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200670 switch (action) {
671 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100672 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 +0200673 break;
674 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100675 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 +0200676 break;
677 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100678 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200679 break;
680 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100681 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900682 return;
683 }
684
685 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200686 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100687 appctx->ctx.cli.msg = stats_permission_denied_msg;
688 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900689 return;
690 }
691
Willy Tarreau07115412012-10-29 21:56:59 +0100692 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900693
Willy Tarreaudec98142012-06-06 23:37:08 +0200694 switch (action) {
695 case STAT_CLI_O_TAB:
696 if (!ts)
697 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100698 chunk_reset(&trash);
699 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900700 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100701 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900702 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200703
704 case STAT_CLI_O_CLR:
705 if (!ts)
706 return;
707 if (ts->ref_cnt) {
708 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100709 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
710 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200711 return;
712 }
713 stksess_kill(&px->table, ts);
714 break;
Simon Horman17bce342011-06-15 15:18:47 +0900715
Willy Tarreau654694e2012-06-07 01:03:16 +0200716 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200717 if (ts)
718 stktable_touch(&px->table, ts, 1);
719 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100720 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200721 if (!ts) {
722 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100723 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
724 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200725 return;
726 }
727 stktable_store(&px->table, ts, 1);
728 }
729
Willy Tarreau47060b62013-08-01 21:11:42 +0200730 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
731 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100732 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
733 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200734 return;
735 }
736
737 data_type = stktable_get_data_type(args[cur_arg] + 5);
738 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100739 appctx->ctx.cli.msg = "Unknown data type\n";
740 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200741 return;
742 }
743
744 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100745 appctx->ctx.cli.msg = "Data type not stored in this table\n";
746 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200747 return;
748 }
749
750 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100751 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
752 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200753 return;
754 }
755
756 ptr = stktable_data_ptr(&px->table, ts, data_type);
757
758 switch (stktable_data_types[data_type].std_type) {
759 case STD_T_SINT:
760 stktable_data_cast(ptr, std_t_sint) = value;
761 break;
762 case STD_T_UINT:
763 stktable_data_cast(ptr, std_t_uint) = value;
764 break;
765 case STD_T_ULL:
766 stktable_data_cast(ptr, std_t_ull) = value;
767 break;
768 case STD_T_FRQP:
769 /* We set both the current and previous values. That way
770 * the reported frequency is stable during all the period
771 * then slowly fades out. This allows external tools to
772 * push measures without having to update them too often.
773 */
774 frqp = &stktable_data_cast(ptr, std_t_frqp);
775 frqp->curr_tick = now_ms;
776 frqp->prev_ctr = 0;
777 frqp->curr_ctr = value;
778 break;
779 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200780 }
781 break;
782
Willy Tarreaudec98142012-06-06 23:37:08 +0200783 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100784 appctx->ctx.cli.msg = "Unknown action\n";
785 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200786 break;
Simon Horman121f3052011-06-15 15:18:46 +0900787 }
Simon Horman121f3052011-06-15 15:18:46 +0900788}
789
Willy Tarreau654694e2012-06-07 01:03:16 +0200790static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900791{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100792 struct appctx *appctx = __objt_appctx(si->end);
793
Willy Tarreau04b3a192013-04-13 09:41:37 +0200794 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100795 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
796 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200797 return;
798 }
799
Simon Hormand5b9fd92011-06-15 15:18:48 +0900800 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100801 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
802 if (appctx->ctx.table.data_type < 0) {
803 appctx->ctx.cli.msg = "Unknown data type\n";
804 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900805 return;
806 }
807
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
809 appctx->ctx.cli.msg = "Data type not stored in this table\n";
810 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811 return;
812 }
813
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100814 appctx->ctx.table.data_op = get_std_op(args[4]);
815 if (appctx->ctx.table.data_op < 0) {
816 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
817 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900818 return;
819 }
820
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100821 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
822 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
823 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900824 return;
825 }
826}
827
Willy Tarreaudec98142012-06-06 23:37:08 +0200828static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900829{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100830 struct appctx *appctx = __objt_appctx(si->end);
831
832 appctx->ctx.table.data_type = -1;
833 appctx->st2 = STAT_ST_INIT;
834 appctx->ctx.table.target = NULL;
835 appctx->ctx.table.proxy = NULL;
836 appctx->ctx.table.entry = NULL;
837 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900838
839 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100840 appctx->ctx.table.target = find_stktable(args[2]);
841 if (!appctx->ctx.table.target) {
842 appctx->ctx.cli.msg = "No such table\n";
843 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900844 return;
845 }
846 }
847 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200848 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900849 goto err_args;
850 return;
851 }
852
853 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200854 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900855 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200856 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900857 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900858 goto err_args;
859
860 return;
861
862err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200863 switch (action) {
864 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100865 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 +0200866 break;
867 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100868 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 +0200869 break;
870 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100871 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200872 break;
873 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100874 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900875}
876
Willy Tarreau532a4502011-09-07 22:37:44 +0200877/* Expects to find a frontend named <arg> and returns it, otherwise displays various
878 * adequate error messages and returns NULL. This function also expects the session
879 * level to be admin.
880 */
881static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
882{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100883 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200884 struct proxy *px;
885
Willy Tarreau290e63a2012-09-20 18:07:14 +0200886 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100887 appctx->ctx.cli.msg = stats_permission_denied_msg;
888 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200889 return NULL;
890 }
891
892 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100893 appctx->ctx.cli.msg = "A frontend name is expected.\n";
894 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200895 return NULL;
896 }
897
898 px = findproxy(arg, PR_CAP_FE);
899 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100900 appctx->ctx.cli.msg = "No such frontend.\n";
901 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200902 return NULL;
903 }
904 return px;
905}
906
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200907/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
908 * and returns the pointer to the server. Otherwise, display adequate error messages
909 * and returns NULL. This function also expects the session level to be admin. Note:
910 * the <arg> is modified to remove the '/'.
911 */
912static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
913{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200915 struct proxy *px;
916 struct server *sv;
917 char *line;
918
Willy Tarreau290e63a2012-09-20 18:07:14 +0200919 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100920 appctx->ctx.cli.msg = stats_permission_denied_msg;
921 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200922 return NULL;
923 }
924
925 /* split "backend/server" and make <line> point to server */
926 for (line = arg; *line; line++)
927 if (*line == '/') {
928 *line++ = '\0';
929 break;
930 }
931
932 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200935 return NULL;
936 }
937
938 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200941 return NULL;
942 }
943
944 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100945 appctx->ctx.cli.msg = "Proxy is disabled.\n";
946 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200947 return NULL;
948 }
949
950 return sv;
951}
952
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100953/* This function is used with map management. It permits to browse each
954 * really allocated descriptors of one map reference. The variable
955 * <appctx->ctx.map.ref> must contain the map reference to browse.
956 * The variable <appctx->ctx.map.desc> contain the descriptor of the
957 * current allocated map descriptor. This variable must be initialized
958 * to NULL.
959 */
960static inline void stats_map_lookup_next(struct stream_interface *si)
961{
962 struct appctx *appctx = __objt_appctx(si->end);
963
964 /* search the next allocated map */
965 while (1) {
966 /* get next descriptor */
967 if (!appctx->ctx.map.desc)
968 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.ref->maps,
969 struct map_descriptor *, list);
970 else
971 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.desc->list,
972 struct map_descriptor *, list);
973
974 /* detect end of list */
975 if (&appctx->ctx.map.desc->list == &appctx->ctx.map.ref->maps) {
976 appctx->ctx.map.desc = NULL;
977 return;
978 }
979
980 /* do not lookup this entry */
981 if (!appctx->ctx.map.desc->do_free)
982 continue;
983
984 /* avalaible descriptor */
985 return;
986 }
987}
988
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200989/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200990 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100991 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200992 * designating the function which will have to process the request, which can
993 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200994 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900995static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200996{
Willy Tarreau306f8302013-07-08 15:53:06 +0200997 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100998 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200999 char *args[MAX_STATS_ARGS + 1];
1000 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001001 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001002
1003 while (isspace((unsigned char)*line))
1004 line++;
1005
1006 arg = 0;
1007 args[arg] = line;
1008
1009 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001010 if (*line == '\\') {
1011 line++;
1012 if (*line == '\0')
1013 break;
1014 }
1015 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001016 *line++ = '\0';
1017
1018 while (isspace((unsigned char)*line))
1019 line++;
1020
1021 args[++arg] = line;
1022 continue;
1023 }
1024
1025 line++;
1026 }
1027
1028 while (++arg <= MAX_STATS_ARGS)
1029 args[arg] = line;
1030
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001031 /* remove \ */
1032 arg = 0;
1033 while (*args[arg] != '\0') {
1034 j = 0;
1035 for (i=0; args[arg][i] != '\0'; i++) {
1036 if (args[arg][i] == '\\')
1037 continue;
1038 args[arg][j] = args[arg][i];
1039 j++;
1040 }
1041 args[arg][j] = '\0';
1042 arg++;
1043 }
1044
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001045 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001046 if (strcmp(args[0], "show") == 0) {
1047 if (strcmp(args[1], "stat") == 0) {
1048 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001049 appctx->ctx.stats.flags |= STAT_BOUND;
1050 appctx->ctx.stats.iid = atoi(args[2]);
1051 appctx->ctx.stats.type = atoi(args[3]);
1052 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001053 }
1054
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001055 appctx->st2 = STAT_ST_INIT;
1056 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001057 }
1058 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001059 appctx->st2 = STAT_ST_INIT;
1060 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001061 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001062 else if (strcmp(args[1], "pools") == 0) {
1063 appctx->st2 = STAT_ST_INIT;
1064 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1065 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001066 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001067 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001068 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001069 appctx->ctx.cli.msg = stats_permission_denied_msg;
1070 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001071 return 1;
1072 }
Willy Tarreau76153662012-11-26 01:16:39 +01001073 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001074 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001075 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001076 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001077 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001078 appctx->ctx.sess.target = NULL;
1079 appctx->ctx.sess.section = 0; /* start with session status */
1080 appctx->ctx.sess.pos = 0;
1081 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001082 }
1083 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001084 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001085 appctx->ctx.cli.msg = stats_permission_denied_msg;
1086 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001087 return 1;
1088 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001089 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001090 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001091 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001092 appctx->ctx.errors.iid = -1;
1093 appctx->ctx.errors.px = NULL;
1094 appctx->st2 = STAT_ST_INIT;
1095 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001096 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001097 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001098 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001099 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001100 else if (strcmp(args[1], "map") == 0) {
1101
1102 /* no parameter: display all map avalaible */
1103 if (!*args[2]) {
1104 appctx->st2 = STAT_ST_INIT;
1105 appctx->st0 = STAT_CLI_O_MAPS;
1106 return 1;
1107 }
1108
1109 /* lookup into the maps */
1110 appctx->ctx.map.ref = map_get_reference(args[2]);
1111 if (!appctx->ctx.map.ref) {
1112 appctx->ctx.cli.msg = "Unknown map reference.\n";
1113 appctx->st0 = STAT_CLI_PRINT;
1114 return 1;
1115 }
1116 appctx->st2 = STAT_ST_INIT;
1117 appctx->st0 = STAT_CLI_O_MAP;
1118 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001119 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001120 return 0;
1121 }
1122 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001123 else if (strcmp(args[0], "clear") == 0) {
1124 if (strcmp(args[1], "counters") == 0) {
1125 struct proxy *px;
1126 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001127 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001128 int clrall = 0;
1129
1130 if (strcmp(args[2], "all") == 0)
1131 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001132
Willy Tarreau6162db22009-10-10 17:13:00 +02001133 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001134 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1135 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001136 appctx->ctx.cli.msg = stats_permission_denied_msg;
1137 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001138 return 1;
1139 }
1140
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001141 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001142 if (clrall) {
1143 memset(&px->be_counters, 0, sizeof(px->be_counters));
1144 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1145 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001146 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001147 px->be_counters.conn_max = 0;
1148 px->be_counters.p.http.rps_max = 0;
1149 px->be_counters.sps_max = 0;
1150 px->be_counters.cps_max = 0;
1151 px->be_counters.nbpend_max = 0;
1152
1153 px->fe_counters.conn_max = 0;
1154 px->fe_counters.p.http.rps_max = 0;
1155 px->fe_counters.sps_max = 0;
1156 px->fe_counters.cps_max = 0;
1157 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001158 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001159
1160 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001161 if (clrall)
1162 memset(&sv->counters, 0, sizeof(sv->counters));
1163 else {
1164 sv->counters.cur_sess_max = 0;
1165 sv->counters.nbpend_max = 0;
1166 sv->counters.sps_max = 0;
1167 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001168
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001169 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001170 if (li->counters) {
1171 if (clrall)
1172 memset(li->counters, 0, sizeof(*li->counters));
1173 else
1174 li->counters->conn_max = 0;
1175 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001176 }
1177
Willy Tarreau81c25d02011-09-07 15:17:21 +02001178 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001179 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001180 return 1;
1181 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001182 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001183 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001184 /* end of processing */
1185 return 1;
1186 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001187 else if (strcmp(args[1], "map") == 0) {
1188 struct map_reference *mref;
1189 struct map_descriptor *mdesc;
1190 struct map_entry *ent, *nent;
1191
1192 /* no parameter */
1193 if (!*args[2]) {
1194 appctx->ctx.cli.msg = "Expect map reference.\n";
1195 appctx->st0 = STAT_CLI_PRINT;
1196 return 1;
1197 }
1198
1199 /* lookup into the maps */
1200 mref = map_get_reference(args[2]);
1201 if (!mref) {
1202 appctx->ctx.cli.msg = "Unknown map reference.\n";
1203 appctx->st0 = STAT_CLI_PRINT;
1204 return 1;
1205 }
1206
1207 /* clear all maps */
1208 list_for_each_entry(mdesc, &mref->maps, list)
1209 if (mdesc->do_free)
1210 pattern_prune_expr(mdesc->pat);
1211
1212 /* clear map reference */
1213 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1214 LIST_DEL(&ent->list);
1215 free(ent->key);
1216 free(ent->value);
1217 free(ent);
1218 }
1219
1220 /* return response */
1221 appctx->ctx.cli.msg = "Done.\n";
1222 appctx->st0 = STAT_CLI_PRINT;
1223 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001224 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001225 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001226 return 0;
1227 }
1228 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001229 else if (strcmp(args[0], "get") == 0) {
1230 if (strcmp(args[1], "weight") == 0) {
1231 struct proxy *px;
1232 struct server *sv;
1233
1234 /* split "backend/server" and make <line> point to server */
1235 for (line = args[2]; *line; line++)
1236 if (*line == '/') {
1237 *line++ = '\0';
1238 break;
1239 }
1240
1241 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001242 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1243 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001244 return 1;
1245 }
1246
1247 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001248 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1249 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001250 return 1;
1251 }
1252
1253 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001254 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1255 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001256 return 1;
1257 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001258 else if (strcmp(args[1], "map") == 0) {
1259
1260 /* no parameter */
1261 if (!*args[2] || !*args[3]) {
1262 appctx->ctx.cli.msg = "Expect map reference and required key.\n";
1263 appctx->st0 = STAT_CLI_PRINT;
1264 return 1;
1265 }
1266
1267 /* lookup into the maps */
1268 appctx->ctx.map.ref = map_get_reference(args[2]);
1269 if (!appctx->ctx.map.ref) {
1270 appctx->ctx.cli.msg = "Unknown map reference.\n";
1271 appctx->st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274
1275 /* copy input string */
1276 appctx->ctx.map.chunk.len = strlen(args[3]);
1277 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1278 appctx->ctx.map.chunk.str = strdup(args[3]);
1279 if (!appctx->ctx.map.chunk.str) {
1280 appctx->ctx.cli.msg = "Out of memory error.\n";
1281 appctx->st0 = STAT_CLI_PRINT;
1282 return 1;
1283 }
1284
1285 /* prepare response */
1286 appctx->st2 = STAT_ST_INIT;
1287 appctx->st0 = STAT_CLI_O_MLOOK;
1288 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001289 else { /* not "get weight" */
1290 return 0;
1291 }
1292 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001293 else if (strcmp(args[0], "set") == 0) {
1294 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001295 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001296 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001297
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001298 sv = expect_server_admin(s, si, args[2]);
1299 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001300 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001301
Simon Horman7d09b9a2013-02-12 10:45:51 +09001302 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001303 /*
1304 * The user-weight may now be zero and thus
1305 * the server considered to be draining.
1306 * Update the server's drain state as necessary.
1307 */
1308 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001309 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001310 appctx->ctx.cli.msg = warning;
1311 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001312 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001313 return 1;
1314 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001315 else if (strcmp(args[1], "timeout") == 0) {
1316 if (strcmp(args[2], "cli") == 0) {
1317 unsigned timeout;
1318 const char *res;
1319
1320 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001321 appctx->ctx.cli.msg = "Expects an integer value.\n";
1322 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001323 return 1;
1324 }
1325
1326 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1327 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001328 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1329 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001330 return 1;
1331 }
1332
1333 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1334 return 1;
1335 }
1336 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001337 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1338 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001339 return 1;
1340 }
1341 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001342 else if (strcmp(args[1], "maxconn") == 0) {
1343 if (strcmp(args[2], "frontend") == 0) {
1344 struct proxy *px;
1345 struct listener *l;
1346 int v;
1347
Willy Tarreau532a4502011-09-07 22:37:44 +02001348 px = expect_frontend_admin(s, si, args[3]);
1349 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001350 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001351
1352 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001353 appctx->ctx.cli.msg = "Integer value expected.\n";
1354 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001355 return 1;
1356 }
1357
1358 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001359 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001360 appctx->ctx.cli.msg = "Value out of range.\n";
1361 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001362 return 1;
1363 }
1364
1365 /* OK, the value is fine, so we assign it to the proxy and to all of
1366 * its listeners. The blocked ones will be dequeued.
1367 */
1368 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001369 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001370 l->maxconn = v;
1371 if (l->state == LI_FULL)
1372 resume_listener(l);
1373 }
1374
1375 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1376 dequeue_all_listeners(&s->fe->listener_queue);
1377
1378 return 1;
1379 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001380 else if (strcmp(args[2], "global") == 0) {
1381 int v;
1382
Willy Tarreau290e63a2012-09-20 18:07:14 +02001383 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001384 appctx->ctx.cli.msg = stats_permission_denied_msg;
1385 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001386 return 1;
1387 }
1388
1389 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001390 appctx->ctx.cli.msg = "Expects an integer value.\n";
1391 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001392 return 1;
1393 }
1394
1395 v = atoi(args[3]);
1396 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001397 appctx->ctx.cli.msg = "Value out of range.\n";
1398 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001399 return 1;
1400 }
1401
1402 /* check for unlimited values */
1403 if (v <= 0)
1404 v = global.hardmaxconn;
1405
1406 global.maxconn = v;
1407
1408 /* Dequeues all of the listeners waiting for a resource */
1409 if (!LIST_ISEMPTY(&global_listener_queue))
1410 dequeue_all_listeners(&global_listener_queue);
1411
1412 return 1;
1413 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001414 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001415 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1416 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001417 return 1;
1418 }
1419 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001420 else if (strcmp(args[1], "rate-limit") == 0) {
1421 if (strcmp(args[2], "connections") == 0) {
1422 if (strcmp(args[3], "global") == 0) {
1423 int v;
1424
Willy Tarreau290e63a2012-09-20 18:07:14 +02001425 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001426 appctx->ctx.cli.msg = stats_permission_denied_msg;
1427 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001428 return 1;
1429 }
1430
1431 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001432 appctx->ctx.cli.msg = "Expects an integer value.\n";
1433 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001434 return 1;
1435 }
1436
1437 v = atoi(args[4]);
1438 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.cli.msg = "Value out of range.\n";
1440 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001441 return 1;
1442 }
1443
1444 global.cps_lim = v;
1445
1446 /* Dequeues all of the listeners waiting for a resource */
1447 if (!LIST_ISEMPTY(&global_listener_queue))
1448 dequeue_all_listeners(&global_listener_queue);
1449
1450 return 1;
1451 }
1452 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001453 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1454 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001455 return 1;
1456 }
1457 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001458 else if (strcmp(args[2], "sessions") == 0) {
1459 if (strcmp(args[3], "global") == 0) {
1460 int v;
1461
1462 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1463 appctx->ctx.cli.msg = stats_permission_denied_msg;
1464 appctx->st0 = STAT_CLI_PRINT;
1465 return 1;
1466 }
1467
1468 if (!*args[4]) {
1469 appctx->ctx.cli.msg = "Expects an integer value.\n";
1470 appctx->st0 = STAT_CLI_PRINT;
1471 return 1;
1472 }
1473
1474 v = atoi(args[4]);
1475 if (v < 0) {
1476 appctx->ctx.cli.msg = "Value out of range.\n";
1477 appctx->st0 = STAT_CLI_PRINT;
1478 return 1;
1479 }
1480
1481 global.sps_lim = v;
1482
1483 /* Dequeues all of the listeners waiting for a resource */
1484 if (!LIST_ISEMPTY(&global_listener_queue))
1485 dequeue_all_listeners(&global_listener_queue);
1486
1487 return 1;
1488 }
1489 else {
1490 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1491 appctx->st0 = STAT_CLI_PRINT;
1492 return 1;
1493 }
1494 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001495#ifdef USE_OPENSSL
1496 else if (strcmp(args[2], "ssl-sessions") == 0) {
1497 if (strcmp(args[3], "global") == 0) {
1498 int v;
1499
1500 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1501 appctx->ctx.cli.msg = stats_permission_denied_msg;
1502 appctx->st0 = STAT_CLI_PRINT;
1503 return 1;
1504 }
1505
1506 if (!*args[4]) {
1507 appctx->ctx.cli.msg = "Expects an integer value.\n";
1508 appctx->st0 = STAT_CLI_PRINT;
1509 return 1;
1510 }
1511
1512 v = atoi(args[4]);
1513 if (v < 0) {
1514 appctx->ctx.cli.msg = "Value out of range.\n";
1515 appctx->st0 = STAT_CLI_PRINT;
1516 return 1;
1517 }
1518
1519 global.ssl_lim = v;
1520
1521 /* Dequeues all of the listeners waiting for a resource */
1522 if (!LIST_ISEMPTY(&global_listener_queue))
1523 dequeue_all_listeners(&global_listener_queue);
1524
1525 return 1;
1526 }
1527 else {
1528 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1529 appctx->st0 = STAT_CLI_PRINT;
1530 return 1;
1531 }
1532 }
1533#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001534 else if (strcmp(args[2], "http-compression") == 0) {
1535 if (strcmp(args[3], "global") == 0) {
1536 int v;
1537
Willy Tarreau85d47f92012-11-21 00:29:50 +01001538 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001539 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1540 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001541 return 1;
1542 }
1543
William Lallemandd85f9172012-11-09 17:05:39 +01001544 v = atoi(args[4]);
1545 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1546 }
1547 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001548 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1549 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001550 return 1;
1551 }
1552 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001553 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001554 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001556 return 1;
1557 }
1558 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001559 else if (strcmp(args[1], "table") == 0) {
1560 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1561 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001562 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001563 struct pattern_list *pat_elt;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001564 struct pat_idx_elt *idx_elt;
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001565 char *value = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001566
1567 /* Expect three parameters: map name, key and new value. */
1568 if (!*args[2] || !*args[3] || !*args[4]) {
1569 appctx->ctx.cli.msg = "'set map' expect three parameters: map name, key and value.\n";
1570 appctx->st0 = STAT_CLI_PRINT;
1571 return 1;
1572 }
1573
1574 /* Lookup the reference in the maps. */
1575 appctx->ctx.map.ref = map_get_reference(args[2]);
1576 if (!appctx->ctx.map.ref) {
1577 appctx->ctx.cli.msg = "Unknown map reference.\n";
1578 appctx->st0 = STAT_CLI_PRINT;
1579 return 1;
1580 }
1581
1582 /* Lookup the entry in the reference values. */
1583 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1584 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1585 break;
1586
1587 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1588 appctx->ctx.cli.msg = "Entry not found.\n";
1589 appctx->st0 = STAT_CLI_PRINT;
1590 return 1;
1591 }
1592
1593 /* Update each reference entries. */
1594 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1595 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1596 value = strdup(args[4]);
1597 if (!value) {
1598 appctx->ctx.cli.msg = "Out of memory error.\n";
1599 appctx->st0 = STAT_CLI_PRINT;
1600 return 1;
1601 }
1602 free(appctx->ctx.map.ent->value);
1603 appctx->ctx.map.ent->value = value;
1604 }
1605 }
1606
1607 /* Change the sample. The lookup juste return the first entry, other
1608 * entries are not changed, but are never matched.
1609 */
1610 appctx->ctx.map.desc = NULL;
1611 for (stats_map_lookup_next(si);
1612 appctx->ctx.map.desc;
1613 stats_map_lookup_next(si)) {
1614 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1615 if (pat_elt != NULL)
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001616 appctx->ctx.map.desc->parse(value, pat_elt->pat.smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001617 if (idx_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001618 appctx->ctx.map.desc->parse(value, idx_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001619 }
1620
1621 /* The set is done, send message. */
1622 appctx->ctx.cli.msg = "Done.\n";
1623 appctx->st0 = STAT_CLI_PRINT;
1624 return 1;
1625 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001626 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001627 return 0;
1628 }
1629 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001630 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001631 if (strcmp(args[1], "agent") == 0) {
1632 struct server *sv;
1633
1634 sv = expect_server_admin(s, si, args[2]);
1635 if (!sv)
1636 return 1;
1637
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001638 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1639 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1640 appctx->st0 = STAT_CLI_PRINT;
1641 return 1;
1642 }
1643
1644 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001645 return 1;
1646 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001647 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001648 struct server *sv;
1649
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001650 sv = expect_server_admin(s, si, args[2]);
1651 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001652 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001653
Cyril Bontécd19e512010-01-31 22:34:03 +01001654 if (sv->state & SRV_MAINTAIN) {
1655 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001656 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001657 /* If this server tracks the status of another one,
1658 * we must restore the good status.
1659 */
Willy Tarreau44267702011-10-28 15:35:33 +02001660 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001661 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001662 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001663 } else {
1664 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001665 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001666 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001667 }
1668 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001669 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001670 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001671 }
1672 }
1673
Willy Tarreau532a4502011-09-07 22:37:44 +02001674 return 1;
1675 }
1676 else if (strcmp(args[1], "frontend") == 0) {
1677 struct proxy *px;
1678
1679 px = expect_frontend_admin(s, si, args[2]);
1680 if (!px)
1681 return 1;
1682
1683 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001684 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1685 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001686 return 1;
1687 }
1688
1689 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001690 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1691 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001692 return 1;
1693 }
1694
1695 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001696 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1697 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001698 return 1;
1699 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001700 return 1;
1701 }
1702 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001703 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1704 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001705 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001706 }
1707 }
1708 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001709 if (strcmp(args[1], "agent") == 0) {
1710 struct server *sv;
1711
1712 sv = expect_server_admin(s, si, args[2]);
1713 if (!sv)
1714 return 1;
1715
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001716 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001717 return 1;
1718 }
1719 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001720 struct server *sv;
1721
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001722 sv = expect_server_admin(s, si, args[2]);
1723 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001724 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001725
Cyril Bontécd19e512010-01-31 22:34:03 +01001726 if (! (sv->state & SRV_MAINTAIN)) {
1727 /* Not already in maintenance, we can change the server state */
1728 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001729 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001730 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001731 }
1732
Willy Tarreau532a4502011-09-07 22:37:44 +02001733 return 1;
1734 }
1735 else if (strcmp(args[1], "frontend") == 0) {
1736 struct proxy *px;
1737
1738 px = expect_frontend_admin(s, si, args[2]);
1739 if (!px)
1740 return 1;
1741
1742 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001743 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1744 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001745 return 1;
1746 }
1747
1748 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001749 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1750 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001751 return 1;
1752 }
1753
1754 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001755 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1756 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001757 return 1;
1758 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001759 return 1;
1760 }
1761 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001762 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1763 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001764 return 1;
1765 }
1766 }
1767 else if (strcmp(args[0], "shutdown") == 0) {
1768 if (strcmp(args[1], "frontend") == 0) {
1769 struct proxy *px;
1770
1771 px = expect_frontend_admin(s, si, args[2]);
1772 if (!px)
1773 return 1;
1774
1775 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001776 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1777 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001778 return 1;
1779 }
1780
1781 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1782 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1783 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1784 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1785 stop_proxy(px);
1786 return 1;
1787 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001788 else if (strcmp(args[1], "session") == 0) {
1789 struct session *sess, *ptr;
1790
Willy Tarreau290e63a2012-09-20 18:07:14 +02001791 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001792 appctx->ctx.cli.msg = stats_permission_denied_msg;
1793 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001794 return 1;
1795 }
1796
1797 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001798 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1799 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001800 return 1;
1801 }
1802
1803 ptr = (void *)strtoul(args[2], NULL, 0);
1804
1805 /* first, look for the requested session in the session table */
1806 list_for_each_entry(sess, &sessions, list) {
1807 if (sess == ptr)
1808 break;
1809 }
1810
1811 /* do we have the session ? */
1812 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001813 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1814 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001815 return 1;
1816 }
1817
1818 session_shutdown(sess, SN_ERR_KILLED);
1819 return 1;
1820 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001821 else if (strcmp(args[1], "sessions") == 0) {
1822 if (strcmp(args[2], "server") == 0) {
1823 struct server *sv;
1824 struct session *sess, *sess_bck;
1825
1826 sv = expect_server_admin(s, si, args[3]);
1827 if (!sv)
1828 return 1;
1829
1830 /* kill all the session that are on this server */
1831 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1832 if (sess->srv_conn == sv)
1833 session_shutdown(sess, SN_ERR_KILLED);
1834
1835 return 1;
1836 }
1837 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001838 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1839 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001840 return 1;
1841 }
1842 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001843 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001844 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1845 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001846 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001847 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001848 }
1849 else if (strcmp(args[0], "del") == 0) {
1850 if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER3ead5b92013-12-13 12:12:18 +01001851 struct pattern_list *pat_elt;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001852 struct pat_idx_elt *idx_elt;
1853 struct map_entry *ent;
1854
1855 /* Expect two parameters: map name and key. */
1856 if (!*args[2] || !*args[3]) {
1857 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1858 appctx->st0 = STAT_CLI_PRINT;
1859 return 1;
1860 }
1861
1862 /* Lookup the reference in the maps. */
1863 appctx->ctx.map.ref = map_get_reference(args[2]);
1864 if (!appctx->ctx.map.ref) {
1865 appctx->ctx.cli.msg = "Unknown map reference.\n";
1866 appctx->st0 = STAT_CLI_PRINT;
1867 return 1;
1868 }
1869
1870 /* Lookup the entry in the reference values.
1871 * If the entry is not found in the reference, return error message.
1872 */
1873 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1874 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1875 break;
1876
1877 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1878 appctx->ctx.cli.msg = "Entry not found.\n";
1879 appctx->st0 = STAT_CLI_PRINT;
1880 return 1;
1881 }
1882
1883 /* Delete each enties from reference. */
1884 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1885 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1886 LIST_DEL(&appctx->ctx.map.ent->list);
1887 free(appctx->ctx.map.ent->key);
1888 free(appctx->ctx.map.ent->value);
1889 free(appctx->ctx.map.ent);
1890 }
1891 }
1892
1893 /* Delete all matching entries for each map descritor. */
1894 appctx->ctx.map.desc = NULL;
1895 stats_map_lookup_next(si);
1896 while (appctx->ctx.map.desc) {
1897 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1898 if (pat_elt != NULL) {
1899 LIST_DEL(&pat_elt->list);
1900 pattern_free(pat_elt);
1901 }
1902 if (idx_elt != NULL) {
1903 ebmb_delete(&idx_elt->node);
1904 free(idx_elt);
1905 }
1906 }
1907 stats_map_lookup_next(si);
1908 }
1909
1910 /* The deletion is done, send message. */
1911 appctx->ctx.cli.msg = "Done.\n";
1912 appctx->st0 = STAT_CLI_PRINT;
1913 return 1;
1914 }
1915 else { /* unknown "del" parameter */
1916 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1917 appctx->st0 = STAT_CLI_PRINT;
1918 return 1;
1919 }
1920 }
1921 else if (strcmp(args[0], "add") == 0) {
1922 if (strcmp(args[1], "map") == 0) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001923 struct map_entry *ent;
1924 struct sample_storage *smp;
1925
1926 /* Expect three parameters: map name, key and new value. */
1927 if (!*args[2] || !*args[3] || !*args[4]) {
1928 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1929 appctx->st0 = STAT_CLI_PRINT;
1930 return 1;
1931 }
1932
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001933 /* Lookup the reference in the maps. */
1934 appctx->ctx.map.ref = map_get_reference(args[2]);
1935 if (!appctx->ctx.map.ref) {
1936 appctx->ctx.cli.msg = "Unknown map reference.\n";
1937 appctx->st0 = STAT_CLI_PRINT;
1938 return 1;
1939 }
1940
1941 /* Prepare and link the new map_entry element. If out of memory
1942 * error the action is cancelled and the descriptor are left
1943 * coherents.
1944 */
1945 ent = malloc(sizeof(*ent));
1946 if (!ent) {
1947 appctx->ctx.cli.msg = "Out of memory error.\n";
1948 appctx->st0 = STAT_CLI_PRINT;
1949 return 1;
1950 }
1951 ent->key = strdup(args[3]);
1952 if (!ent->key) {
1953 free(ent);
1954 appctx->ctx.cli.msg = "Out of memory error.\n";
1955 appctx->st0 = STAT_CLI_PRINT;
1956 return 1;
1957 }
1958 ent->value = strdup(args[4]);
1959 if (!ent->value) {
1960 free(ent->key);
1961 free(ent);
1962 appctx->ctx.cli.msg = "Out of memory error.\n";
1963 appctx->st0 = STAT_CLI_PRINT;
1964 return 1;
1965 }
1966 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1967
1968 /* Browse each map descritor and try to insert this new value. */
1969 appctx->ctx.map.desc = NULL;
1970 for (stats_map_lookup_next(si);
1971 appctx->ctx.map.desc;
1972 stats_map_lookup_next(si)) {
1973
1974 /* Create new sample. Return out of memory error
1975 * if the memory cannot be allocated. The 'add' process
1976 * is aborted, but the already inserted entries are not
1977 * deleted.
1978 */
1979 smp = calloc(1, sizeof(*smp));
1980 if (!smp) {
1981 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1982 appctx->st0 = STAT_CLI_PRINT;
1983 return 1;
1984 }
1985
1986 /* Create sample. If this function fails, the insertion
1987 * is canceled for this 'descriptor', but continue, for
1988 * the other descriptors.
1989 */
1990 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1991 free(smp);
1992 continue;
1993 }
1994
Thierry FOURNIERb9b08462013-12-13 15:12:32 +01001995 if (!pattern_register(appctx->ctx.map.desc->pat, args[3], smp, 0, NULL)) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001996 free(smp);
1997 continue;
1998 }
1999 }
2000
2001 /* The add is done, send message. */
2002 appctx->ctx.cli.msg = "Done.\n";
2003 appctx->st0 = STAT_CLI_PRINT;
2004 return 1;
2005 }
2006 else { /* unknown "del" parameter */
2007 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2008 appctx->st0 = STAT_CLI_PRINT;
2009 return 1;
2010 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002011 }
2012 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002013 return 0;
2014 }
2015 return 1;
2016}
2017
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002018/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002019 * used to processes I/O from/to the stats unix socket. The system relies on a
2020 * state machine handling requests and various responses. We read a request,
2021 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002022 * Then we can read again. The state is stored in appctx->st0 and is one of the
2023 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002024 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002025 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002026static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002027{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002028 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002029 struct channel *req = si->ob;
2030 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002031 int reql;
2032 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002033
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002034 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2035 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002036
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002037 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002038 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002039 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2041 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002042 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002043 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002044 /* Let's close for real now. We just close the request
2045 * side, the conditions below will complete if needed.
2046 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002047 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002048 break;
2049 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002050 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002051 /* ensure we have some output room left in the event we
2052 * would want to return some info right after parsing.
2053 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002054 if (buffer_almost_full(si->ib->buf)) {
2055 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002056 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002057 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002059 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002060 if (reql <= 0) { /* closed or EOL not found */
2061 if (reql == 0)
2062 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002063 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002064 continue;
2065 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002066
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002067 /* seek for a possible semi-colon. If we find one, we
2068 * replace it with an LF and skip only this part.
2069 */
2070 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002071 if (trash.str[len] == ';') {
2072 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002073 reql = len + 1;
2074 break;
2075 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002076
Willy Tarreau816fc222009-10-04 07:36:58 +02002077 /* now it is time to check that we have a full line,
2078 * remove the trailing \n and possibly \r, then cut the
2079 * line.
2080 */
2081 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002082 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002083 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002085 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002086
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002087 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002088 len--;
2089
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002090 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002091
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002092 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002093 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002094 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002095 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002096 continue;
2097 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002098 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002099 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002100 else if (strcmp(trash.str, "help") == 0 ||
2101 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002102 appctx->ctx.cli.msg = stats_sock_usage_msg;
2103 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002104 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002105 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002106 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002107 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002108 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002109 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002110 /* if prompt is disabled, print help on empty lines,
2111 * so that the user at least knows how to enable
2112 * prompt and find help.
2113 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002114 appctx->ctx.cli.msg = stats_sock_usage_msg;
2115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002116 }
2117
2118 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002119 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002120 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002121 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002122 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002123 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002124 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002125 continue;
2126 }
2127
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002128 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002129 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002130 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2131 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002132 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002133 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002134 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002135 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002136 break;
2137 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002138 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002139 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002140 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002141 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002142 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002143 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002144 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002145 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002146 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002147 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002148 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002149 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002150 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002151 if (stats_table_request(si, appctx->st0))
2152 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002153 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002154 case STAT_CLI_O_MAPS:
2155 if (stats_maps_list(si))
2156 appctx->st0 = STAT_CLI_PROMPT;
2157 break;
2158 case STAT_CLI_O_MAP:
2159 if (stats_map_list(si))
2160 appctx->st0 = STAT_CLI_PROMPT;
2161 break;
2162 case STAT_CLI_O_MLOOK:
2163 if (stats_map_lookup(si))
2164 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002165 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002166 case STAT_CLI_O_POOLS:
2167 if (stats_dump_pools_to_buffer(si))
2168 appctx->st0 = STAT_CLI_PROMPT;
2169 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002170 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002171 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002172 break;
2173 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002174
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002175 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002176 if (appctx->st0 == STAT_CLI_PROMPT) {
2177 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2178 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002179 }
2180
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002181 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002182 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002183 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184
2185 /* Now we close the output if one of the writers did so,
2186 * or if we're not in interactive mode and the request
2187 * buffer is empty. This still allows pipelined requests
2188 * to be sent in non-interactive mode.
2189 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002190 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2191 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002192 continue;
2193 }
2194
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002195 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002196 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002197 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002198 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002199
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002200 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002201 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2202 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002203 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002204 * and nothing more to consume. This is comparable to a broken pipe, so
2205 * we forward the close to the request side so that it flows upstream to
2206 * the client.
2207 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002208 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002209 }
2210
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002211 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002212 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2213 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2214 /* We have no more processing to do, and nothing more to send, and
2215 * the client side has closed. So we'll forward this state downstream
2216 * on the response buffer.
2217 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002218 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002219 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002220 }
2221
2222 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002223 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002224
2225 /* we don't want to expire timeouts while we're processing requests */
2226 si->ib->rex = TICK_ETERNITY;
2227 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002228
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002229 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002230 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 +02002231 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002232 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 +02002233
2234 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2235 /* check that we have released everything then unregister */
2236 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002237 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002238}
2239
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002240/* This function dumps information onto the stream interface's read buffer.
2241 * It returns 0 as long as it does not complete, non-zero upon completion.
2242 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002243 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002244static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002245{
2246 unsigned int up = (now.tv_sec - start_date.tv_sec);
2247
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002248 chunk_printf(&trash,
2249 "Name: " PRODUCT_NAME "\n"
2250 "Version: " HAPROXY_VERSION "\n"
2251 "Release_date: " HAPROXY_DATE "\n"
2252 "Nbproc: %d\n"
2253 "Process_num: %d\n"
2254 "Pid: %d\n"
2255 "Uptime: %dd %dh%02dm%02ds\n"
2256 "Uptime_sec: %d\n"
2257 "Memmax_MB: %d\n"
2258 "Ulimit-n: %d\n"
2259 "Maxsock: %d\n"
2260 "Maxconn: %d\n"
2261 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002262 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002263 "CumConns: %d\n"
2264 "CumReq: %d\n"
2265#ifdef USE_OPENSSL
2266 "MaxSslConns: %d\n"
2267 "CurrSslConns: %d\n"
2268 "CumSslConns: %d\n"
2269#endif
2270 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002271 "PipesUsed: %d\n"
2272 "PipesFree: %d\n"
2273 "ConnRate: %d\n"
2274 "ConnRateLimit: %d\n"
2275 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002276 "SessRate: %d\n"
2277 "SessRateLimit: %d\n"
2278 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002279#ifdef USE_OPENSSL
2280 "SslRate: %d\n"
2281 "SslRateLimit: %d\n"
2282 "MaxSslRate: %d\n"
2283#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002284 "CompressBpsIn: %u\n"
2285 "CompressBpsOut: %u\n"
2286 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002287#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002288 "ZlibMemUsage: %ld\n"
2289 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002290#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002291 "Tasks: %d\n"
2292 "Run_queue: %d\n"
2293 "Idle_pct: %d\n"
2294 "node: %s\n"
2295 "description: %s\n"
2296 "",
2297 global.nbproc,
2298 relative_pid,
2299 pid,
2300 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2301 up,
2302 global.rlimit_memmax,
2303 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002304 global.maxsock, global.maxconn, global.hardmaxconn,
2305 actconn, totalconn, global.req_count,
2306#ifdef USE_OPENSSL
2307 global.maxsslconn, sslconns, totalsslconns,
2308#endif
2309 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002310 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002311 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002312#ifdef USE_OPENSSL
2313 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2314#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002315 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2316 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002317#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002318 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002319#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002320 nb_tasks_cur, run_queue_cur, idle_pct,
2321 global.node, global.desc ? global.desc : ""
2322 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002323
2324 if (bi_putchk(si->ib, &trash) == -1)
2325 return 0;
2326
2327 return 1;
2328}
2329
Willy Tarreau12833bb2014-01-28 16:49:56 +01002330/* This function dumps memory usage information onto the stream interface's
2331 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2332 * completion. No state is used.
2333 */
2334static int stats_dump_pools_to_buffer(struct stream_interface *si)
2335{
2336 dump_pools_to_trash();
2337 if (bi_putchk(si->ib, &trash) == -1)
2338 return 0;
2339 return 1;
2340}
2341
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002342/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2343 * the state from stream interface <si>. The caller is responsible for clearing
2344 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002345 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002346static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002347{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002348 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002349 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002350
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002351 if (!(px->cap & PR_CAP_FE))
2352 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002353
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002354 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002355 return 0;
2356
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002358 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359 /* name, queue */
2360 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002361
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002362 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002363 /* Column sub-heading for Enable or Disable server */
2364 chunk_appendf(&trash, "<td></td>");
2365 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002366
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002367 chunk_appendf(&trash,
2368 "<td class=ac>"
2369 "<a name=\"%s/Frontend\"></a>"
2370 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2371 "<td colspan=3></td>"
2372 "",
2373 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002374
Willy Tarreau466c9b52012-12-23 02:25:03 +01002375 chunk_appendf(&trash,
2376 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002377 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002378 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2379 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2380 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002381 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2382 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2383 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002385 if (px->mode == PR_MODE_HTTP)
2386 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002387 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002388 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002389
2390 chunk_appendf(&trash,
2391 "</table></div></u></td>"
2392 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002393 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002394 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2395 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2396 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002397 U2H(px->fe_counters.sps_max),
2398 U2H(px->fe_counters.cps_max),
2399 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002400
2401 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002402 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002403 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002404 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002405
2406 chunk_appendf(&trash,
2407 "</table></div></u></td>"
2408 /* sessions rate : limit */
2409 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002410 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002411
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002412 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002413 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002414 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002415 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002416 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2417 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002418 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002419 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2420 U2H(px->fe_counters.cum_sess),
2421 U2H(px->fe_counters.cum_conn),
2422 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002423
Willy Tarreau466c9b52012-12-23 02:25:03 +01002424 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002425 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002426 chunk_appendf(&trash,
2427 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2428 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2429 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2430 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2431 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2432 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2433 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2434 "<tr><th>- other responses:</th><td>%s</td></tr>"
2435 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2436 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002437 U2H(px->fe_counters.p.http.cum_req),
2438 U2H(px->fe_counters.p.http.rsp[1]),
2439 U2H(px->fe_counters.p.http.rsp[2]),
2440 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002441 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002442 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002443 U2H(px->fe_counters.p.http.rsp[3]),
2444 U2H(px->fe_counters.p.http.rsp[4]),
2445 U2H(px->fe_counters.p.http.rsp[5]),
2446 U2H(px->fe_counters.p.http.rsp[0]),
2447 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002448 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002449
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002450 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002451 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002452 /* sessions: lbtot, lastsess */
2453 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002454 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002455 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002456 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002457 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002458
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002459 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002460 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002461 "<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 +01002462 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002463 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002464 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2465 px->fe_counters.comp_in ?
2466 (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 +01002467 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002469 chunk_appendf(&trash,
2470 /* denied: req, resp */
2471 "<td>%s</td><td>%s</td>"
2472 /* errors : request, connect, response */
2473 "<td>%s</td><td></td><td></td>"
2474 /* warnings: retries, redispatches */
2475 "<td></td><td></td>"
2476 /* server status : reflect frontend status */
2477 "<td class=ac>%s</td>"
2478 /* rest of server: nothing */
2479 "<td class=ac colspan=8></td></tr>"
2480 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002481 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2482 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002483 px->state == PR_STREADY ? "OPEN" :
2484 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002485 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002486 else { /* CSV mode */
2487 chunk_appendf(&trash,
2488 /* pxid, name, queue cur, queue max, */
2489 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002490 /* sessions : current, max, limit, total */
2491 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002492 /* bytes : in, out */
2493 "%lld,%lld,"
2494 /* denied: req, resp */
2495 "%lld,%lld,"
2496 /* errors : request, connect, response */
2497 "%lld,,,"
2498 /* warnings: retries, redispatches */
2499 ",,"
2500 /* server status : reflect frontend status */
2501 "%s,"
2502 /* rest of server: nothing */
2503 ",,,,,,,,"
2504 /* pid, iid, sid, throttle, lbtot, tracked, type */
2505 "%d,%d,0,,,,%d,"
2506 /* rate, rate_lim, rate_max */
2507 "%u,%u,%u,"
2508 /* check_status, check_code, check_duration */
2509 ",,,",
2510 px->id,
2511 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2512 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2513 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2514 px->fe_counters.failed_req,
2515 px->state == PR_STREADY ? "OPEN" :
2516 px->state == PR_STFULL ? "FULL" : "STOP",
2517 relative_pid, px->uuid, STATS_TYPE_FE,
2518 read_freq_ctr(&px->fe_sess_per_sec),
2519 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002520
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002521 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2522 if (px->mode == PR_MODE_HTTP) {
2523 for (i=1; i<6; i++)
2524 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2525 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2526 }
2527 else
2528 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002529
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002530 /* failed health analyses */
2531 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002532
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002533 /* requests : req_rate, req_rate_max, req_tot, */
2534 chunk_appendf(&trash, "%u,%u,%lld,",
2535 read_freq_ctr(&px->fe_req_per_sec),
2536 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2537
2538 /* errors: cli_aborts, srv_aborts */
2539 chunk_appendf(&trash, ",,");
2540
2541 /* compression: in, out, bypassed */
2542 chunk_appendf(&trash, "%lld,%lld,%lld,",
2543 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2544
2545 /* compression: comp_rsp */
2546 chunk_appendf(&trash, "%lld,",
2547 px->fe_counters.p.http.comp_rsp);
2548
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002549 /* lastsess */
2550 chunk_appendf(&trash, ",");
2551
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002552 /* finish with EOL */
2553 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002554 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002556}
2557
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002558/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2559 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2560 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2561 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002562 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002564{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002565 struct appctx *appctx = __objt_appctx(si->end);
2566
2567 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002568 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002569 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002570 /* Column sub-heading for Enable or Disable server */
2571 chunk_appendf(&trash, "<td></td>");
2572 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002573 chunk_appendf(&trash,
2574 /* frontend name, listener name */
2575 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2576 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2577 "",
2578 px->id, l->name,
2579 (flags & ST_SHLGNDS)?"<u>":"",
2580 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002581
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582 if (flags & ST_SHLGNDS) {
2583 char str[INET6_ADDRSTRLEN];
2584 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002585
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002586 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002587
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002588 port = get_host_port(&l->addr);
2589 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2590 case AF_INET:
2591 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2592 break;
2593 case AF_INET6:
2594 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2595 break;
2596 case AF_UNIX:
2597 chunk_appendf(&trash, "unix, ");
2598 break;
2599 case -1:
2600 chunk_appendf(&trash, "(%s), ", strerror(errno));
2601 break;
2602 }
Willy Tarreau91861262007-10-17 17:06:05 +02002603
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002604 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002605 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002606 }
Willy Tarreau91861262007-10-17 17:06:05 +02002607
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002608 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002609 /* queue */
2610 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002611 /* sessions rate: current, max, limit */
2612 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002613 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002615 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 /* bytes: in, out */
2617 "<td>%s</td><td>%s</td>"
2618 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002619 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002620 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2621 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002622
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002623 chunk_appendf(&trash,
2624 /* denied: req, resp */
2625 "<td>%s</td><td>%s</td>"
2626 /* errors: request, connect, response */
2627 "<td>%s</td><td></td><td></td>"
2628 /* warnings: retries, redispatches */
2629 "<td></td><td></td>"
2630 /* server status: reflect listener status */
2631 "<td class=ac>%s</td>"
2632 /* rest of server: nothing */
2633 "<td class=ac colspan=8></td></tr>"
2634 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002635 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2636 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2638 }
2639 else { /* CSV mode */
2640 chunk_appendf(&trash,
2641 /* pxid, name, queue cur, queue max, */
2642 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002643 /* sessions: current, max, limit, total */
2644 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 /* bytes: in, out */
2646 "%lld,%lld,"
2647 /* denied: req, resp */
2648 "%lld,%lld,"
2649 /* errors: request, connect, response */
2650 "%lld,,,"
2651 /* warnings: retries, redispatches */
2652 ",,"
2653 /* server status: reflect listener status */
2654 "%s,"
2655 /* rest of server: nothing */
2656 ",,,,,,,,"
2657 /* pid, iid, sid, throttle, lbtot, tracked, type */
2658 "%d,%d,%d,,,,%d,"
2659 /* rate, rate_lim, rate_max */
2660 ",,,"
2661 /* check_status, check_code, check_duration */
2662 ",,,"
2663 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2664 ",,,,,,"
2665 /* failed health analyses */
2666 ","
2667 /* requests : req_rate, req_rate_max, req_tot, */
2668 ",,,"
2669 /* errors: cli_aborts, srv_aborts */
2670 ",,"
2671 /* compression: in, out, bypassed, comp_rsp */
2672 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002673 /* lastsess */
2674 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002675 "\n",
2676 px->id, l->name,
2677 l->nbconn, l->counters->conn_max,
2678 l->maxconn, l->counters->cum_conn,
2679 l->counters->bytes_in, l->counters->bytes_out,
2680 l->counters->denied_req, l->counters->denied_resp,
2681 l->counters->failed_req,
2682 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2683 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2684 }
2685 return 1;
2686}
Willy Tarreau91861262007-10-17 17:06:05 +02002687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2689 * from stream interface <si>, stats flags <flags>, and server state <state>.
2690 * The caller is responsible for clearing the trash if needed. Returns non-zero
2691 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002692 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2693 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002694 */
2695static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2696{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002697 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002698 struct server *ref = sv->track ? sv->track : sv;
2699 char str[INET6_ADDRSTRLEN];
2700 struct chunk src;
2701 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002702
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002703 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002704 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002705 "DOWN",
2706 "DN %d/%d &uarr;",
2707 "UP %d/%d &darr;",
2708 "UP",
2709 "NOLB %d/%d &darr;",
2710 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002711 "DRAIN %d/%d &darr;",
2712 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002713 "<i>no check</i>"
2714 };
Willy Tarreau91861262007-10-17 17:06:05 +02002715
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002716 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2717 chunk_appendf(&trash, "<tr class=\"maintain\">");
2718 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002719 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002720 "<tr class=\"%s%d\">",
2721 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002722
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002723 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002724 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2726 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002727
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002728 chunk_appendf(&trash,
2729 "<td class=ac><a name=\"%s/%s\"></a>%s"
2730 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2731 "",
2732 px->id, sv->id,
2733 (flags & ST_SHLGNDS) ? "<u>" : "",
2734 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002735
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002737 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002738
2739 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2740 case AF_INET:
2741 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2742 break;
2743 case AF_INET6:
2744 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2745 break;
2746 case AF_UNIX:
2747 chunk_appendf(&trash, "unix, ");
2748 break;
2749 case -1:
2750 chunk_appendf(&trash, "(%s), ", strerror(errno));
2751 break;
2752 default: /* address family not supported */
2753 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002754 }
Willy Tarreau91861262007-10-17 17:06:05 +02002755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002756 /* id */
2757 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002758
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002759 /* cookie */
2760 if (sv->cookie) {
2761 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002762
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002763 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2764 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002765
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002766 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002767 }
2768
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002769 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002770 }
Willy Tarreau91861262007-10-17 17:06:05 +02002771
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002772 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002773 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002774 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 /* sessions rate : current, max, limit */
2776 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002777 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002779 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2780 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002781
Willy Tarreau466c9b52012-12-23 02:25:03 +01002782
2783 chunk_appendf(&trash,
2784 /* sessions: current, max, limit, total */
2785 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002786 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002787 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2788 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002789 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2790 U2H(sv->counters.cum_sess),
2791 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002792
Willy Tarreau466c9b52012-12-23 02:25:03 +01002793 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2794 if (px->mode == PR_MODE_HTTP) {
2795 unsigned long long tot;
2796 for (tot = i = 0; i < 6; i++)
2797 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798
Willy Tarreau466c9b52012-12-23 02:25:03 +01002799 chunk_appendf(&trash,
2800 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2801 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2802 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2803 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2804 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2805 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2806 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2807 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002808 U2H(tot),
2809 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2810 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2811 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2812 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2813 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2814 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 +02002815 }
Willy Tarreau91861262007-10-17 17:06:05 +02002816
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002818 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002819 /* sessions: lbtot, last */
2820 "<td>%s</td><td>%s</td>",
2821 U2H(sv->counters.cum_lbconn),
2822 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002823
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002824 chunk_appendf(&trash,
2825 /* bytes : in, out */
2826 "<td>%s</td><td>%s</td>"
2827 /* denied: req, resp */
2828 "<td></td><td>%s</td>"
2829 /* errors : request, connect */
2830 "<td></td><td>%s</td>"
2831 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002832 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002833 /* warnings: retries, redispatches */
2834 "<td>%lld</td><td>%lld</td>"
2835 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002836 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2837 U2H(sv->counters.failed_secu),
2838 U2H(sv->counters.failed_conns),
2839 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002840 sv->counters.cli_aborts,
2841 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842 sv->counters.retries, sv->counters.redispatches);
2843
2844 /* status, lest check */
2845 chunk_appendf(&trash, "<td class=ac>");
2846
2847 if (sv->state & SRV_MAINTAIN) {
2848 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2849 chunk_appendf(&trash, "MAINT");
2850 }
2851 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2852 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2853 chunk_appendf(&trash, "MAINT(via)");
2854 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002855 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002856 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2857 chunk_appendf(&trash,
2858 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002859 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2860 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002861 }
Willy Tarreau91861262007-10-17 17:06:05 +02002862
Willy Tarreauff5ae352013-12-11 20:36:34 +01002863 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002864 chunk_appendf(&trash,
2865 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002866 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002867 get_check_status_info(sv->check.status));
2868
2869 if (sv->check.status >= HCHK_STATUS_L57DATA)
2870 chunk_appendf(&trash, "/%d", sv->check.code);
2871
2872 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002873 chunk_appendf(&trash, " in %lums", sv->check.duration);
2874
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002875 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002876 get_check_status_description(sv->check.status));
2877 if (*sv->check.desc) {
2878 chunk_appendf(&trash, ": ");
2879 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2880 chunk_htmlencode(&trash, &src);
2881 }
2882 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002883 }
2884 else
2885 chunk_appendf(&trash, "</td><td>");
2886
2887 chunk_appendf(&trash,
2888 /* weight */
2889 "</td><td class=ac>%d</td>"
2890 /* act, bck */
2891 "<td class=ac>%s</td><td class=ac>%s</td>"
2892 "",
2893 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2894 (sv->state & SRV_BACKUP) ? "-" : "Y",
2895 (sv->state & SRV_BACKUP) ? "Y" : "-");
2896
2897 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002898 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002899 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002900
2901 if (ref->observe)
2902 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2903
2904 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002905 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002906 "<td>%lld</td><td>%s</td>"
2907 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002908 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002909 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2910 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002911 else if (sv != ref) {
2912 if (sv->state & SRV_MAINTAIN)
2913 chunk_appendf(&trash,
2914 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2915 ref->proxy->id, ref->id);
2916 else
2917 chunk_appendf(&trash,
2918 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2919 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2920 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002921 else
2922 chunk_appendf(&trash, "<td colspan=3></td>");
2923
2924 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002925 if (sv->state & SRV_WARMINGUP)
2926 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002927 else
2928 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2929 }
2930 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002931 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932 "DOWN,",
2933 "DOWN %d/%d,",
2934 "UP %d/%d,",
2935 "UP,",
2936 "NOLB %d/%d,",
2937 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002938 "DRAIN %d/%d,",
2939 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002940 "no check,"
2941 };
2942
2943 chunk_appendf(&trash,
2944 /* pxid, name */
2945 "%s,%s,"
2946 /* queue : current, max */
2947 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002948 /* sessions : current, max, limit, total */
2949 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002950 /* bytes : in, out */
2951 "%lld,%lld,"
2952 /* denied: req, resp */
2953 ",%lld,"
2954 /* errors : request, connect, response */
2955 ",%lld,%lld,"
2956 /* warnings: retries, redispatches */
2957 "%lld,%lld,"
2958 "",
2959 px->id, sv->id,
2960 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002961 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002962 sv->counters.bytes_in, sv->counters.bytes_out,
2963 sv->counters.failed_secu,
2964 sv->counters.failed_conns, sv->counters.failed_resp,
2965 sv->counters.retries, sv->counters.redispatches);
2966
2967 /* status */
2968 if (sv->state & SRV_MAINTAIN)
2969 chunk_appendf(&trash, "MAINT,");
2970 else if (ref != sv && ref->state & SRV_MAINTAIN)
2971 chunk_appendf(&trash, "MAINT(via),");
2972 else
2973 chunk_appendf(&trash,
2974 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002975 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2976 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977
2978 chunk_appendf(&trash,
2979 /* weight, active, backup */
2980 "%d,%d,%d,"
2981 "",
2982 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2983 (sv->state & SRV_BACKUP) ? 0 : 1,
2984 (sv->state & SRV_BACKUP) ? 1 : 0);
2985
2986 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002987 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988 chunk_appendf(&trash,
2989 "%lld,%lld,%d,%d,",
2990 sv->counters.failed_checks, sv->counters.down_trans,
2991 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2992 else
2993 chunk_appendf(&trash, ",,,,");
2994
2995 /* queue limit, pid, iid, sid, */
2996 chunk_appendf(&trash,
2997 "%s,"
2998 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002999 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003000 relative_pid, px->uuid, sv->puid);
3001
3002 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01003003 if (sv->state & SRV_WARMINGUP)
3004 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005
3006 /* sessions: lbtot */
3007 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3008
3009 /* tracked */
3010 if (sv->track)
3011 chunk_appendf(&trash, "%s/%s,",
3012 sv->track->proxy->id, sv->track->id);
3013 else
3014 chunk_appendf(&trash, ",");
3015
3016 /* type */
3017 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3018
3019 /* rate */
3020 chunk_appendf(&trash, "%u,,%u,",
3021 read_freq_ctr(&sv->sess_per_sec),
3022 sv->counters.sps_max);
3023
Willy Tarreauff5ae352013-12-11 20:36:34 +01003024 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 /* check_status */
3026 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3027
3028 /* check_code */
3029 if (sv->check.status >= HCHK_STATUS_L57DATA)
3030 chunk_appendf(&trash, "%u,", sv->check.code);
3031 else
3032 chunk_appendf(&trash, ",");
3033
3034 /* check_duration */
3035 if (sv->check.status >= HCHK_STATUS_CHECKED)
3036 chunk_appendf(&trash, "%lu,", sv->check.duration);
3037 else
3038 chunk_appendf(&trash, ",");
3039
3040 }
3041 else
3042 chunk_appendf(&trash, ",,,");
3043
3044 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3045 if (px->mode == PR_MODE_HTTP) {
3046 for (i=1; i<6; i++)
3047 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3048
3049 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3050 }
3051 else
3052 chunk_appendf(&trash, ",,,,,,");
3053
3054 /* failed health analyses */
3055 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3056
3057 /* requests : req_rate, req_rate_max, req_tot, */
3058 chunk_appendf(&trash, ",,,");
3059
3060 /* errors: cli_aborts, srv_aborts */
3061 chunk_appendf(&trash, "%lld,%lld,",
3062 sv->counters.cli_aborts, sv->counters.srv_aborts);
3063
3064 /* compression: in, out, bypassed, comp_rsp */
3065 chunk_appendf(&trash, ",,,,");
3066
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003067 /* lastsess */
3068 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3069
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003070 /* finish with EOL */
3071 chunk_appendf(&trash, "\n");
3072 }
3073 return 1;
3074}
3075
3076/* Dumps a line for backend <px> to the trash for and uses the state from stream
3077 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3078 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3079 */
3080static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3081{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003082 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003083 struct chunk src;
3084 int i;
3085
3086 if (!(px->cap & PR_CAP_BE))
3087 return 0;
3088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003089 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003090 return 0;
3091
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003092 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003093 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003094 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003095 /* Column sub-heading for Enable or Disable server */
3096 chunk_appendf(&trash, "<td></td>");
3097 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003098 chunk_appendf(&trash,
3099 "<td class=ac>"
3100 /* name */
3101 "%s<a name=\"%s/Backend\"></a>"
3102 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3103 "",
3104 (flags & ST_SHLGNDS)?"<u>":"",
3105 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003106
3107 if (flags & ST_SHLGNDS) {
3108 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003109 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3111
3112 /* cookie */
3113 if (px->cookie_name) {
3114 chunk_appendf(&trash, ", cookie: '");
3115 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3116 chunk_htmlencode(&trash, &src);
3117 chunk_appendf(&trash, "'");
3118 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003119 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120 }
3121
3122 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003123 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003124 /* queue : current, max */
3125 "<td>%s</td><td>%s</td><td></td>"
3126 /* sessions rate : current, max, limit */
3127 "<td>%s</td><td>%s</td><td></td>"
3128 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003129 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003130 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3131 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132
3133 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003134 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003135 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003136 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003137 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003139 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3140 U2H(px->be_counters.cum_conn),
3141 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003142
Willy Tarreau466c9b52012-12-23 02:25:03 +01003143 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003144 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003145 chunk_appendf(&trash,
3146 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3147 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3148 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3149 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3150 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3151 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3152 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3153 "<tr><th>- other responses:</th><td>%s</td></tr>"
3154 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3155 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003156 U2H(px->be_counters.p.http.cum_req),
3157 U2H(px->be_counters.p.http.rsp[1]),
3158 U2H(px->be_counters.p.http.rsp[2]),
3159 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003160 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003161 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003162 U2H(px->be_counters.p.http.rsp[3]),
3163 U2H(px->be_counters.p.http.rsp[4]),
3164 U2H(px->be_counters.p.http.rsp[5]),
3165 U2H(px->be_counters.p.http.rsp[0]),
3166 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167 }
3168
3169 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003170 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003171 /* sessions: lbtot, last */
3172 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003174 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003176 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003177 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003178 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003181 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003182 "<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 +01003183 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003184 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003185 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3186 px->be_counters.comp_in ?
3187 (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 +01003188 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3189
3190 chunk_appendf(&trash,
3191 /* denied: req, resp */
3192 "<td>%s</td><td>%s</td>"
3193 /* errors : request, connect */
3194 "<td></td><td>%s</td>"
3195 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003196 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003197 /* warnings: retries, redispatches */
3198 "<td>%lld</td><td>%lld</td>"
3199 /* backend status: reflect backend status (up/down): we display UP
3200 * if the backend has known working servers or if it has no server at
3201 * all (eg: for stats). Then we display the total weight, number of
3202 * active and backups. */
3203 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3204 "<td class=ac>%d</td><td class=ac>%d</td>"
3205 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003206 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3207 U2H(px->be_counters.failed_conns),
3208 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209 px->be_counters.cli_aborts,
3210 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211 px->be_counters.retries, px->be_counters.redispatches,
3212 human_time(now.tv_sec - px->last_change, 1),
3213 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3214 "<font color=\"red\"><b>DOWN</b></font>",
3215 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3216 px->srv_act, px->srv_bck);
3217
3218 chunk_appendf(&trash,
3219 /* rest of backend: nothing, down transitions, total downtime, throttle */
3220 "<td class=ac>&nbsp;</td><td>%d</td>"
3221 "<td>%s</td>"
3222 "<td></td>"
3223 "</tr>",
3224 px->down_trans,
3225 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3226 }
3227 else { /* CSV mode */
3228 chunk_appendf(&trash,
3229 /* pxid, name */
3230 "%s,BACKEND,"
3231 /* queue : current, max */
3232 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003233 /* sessions : current, max, limit, total */
3234 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235 /* bytes : in, out */
3236 "%lld,%lld,"
3237 /* denied: req, resp */
3238 "%lld,%lld,"
3239 /* errors : request, connect, response */
3240 ",%lld,%lld,"
3241 /* warnings: retries, redispatches */
3242 "%lld,%lld,"
3243 /* backend status: reflect backend status (up/down): we display UP
3244 * if the backend has known working servers or if it has no server at
3245 * all (eg: for stats). Then we display the total weight, number of
3246 * active and backups. */
3247 "%s,"
3248 "%d,%d,%d,"
3249 /* rest of backend: nothing, down transitions, last change, total downtime */
3250 ",%d,%d,%d,,"
3251 /* pid, iid, sid, throttle, lbtot, tracked, type */
3252 "%d,%d,0,,%lld,,%d,"
3253 /* rate, rate_lim, rate_max, */
3254 "%u,,%u,"
3255 /* check_status, check_code, check_duration */
3256 ",,,",
3257 px->id,
3258 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3259 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3260 px->be_counters.bytes_in, px->be_counters.bytes_out,
3261 px->be_counters.denied_req, px->be_counters.denied_resp,
3262 px->be_counters.failed_conns, px->be_counters.failed_resp,
3263 px->be_counters.retries, px->be_counters.redispatches,
3264 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3265 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3266 px->srv_act, px->srv_bck,
3267 px->down_trans, (int)(now.tv_sec - px->last_change),
3268 px->srv?be_downtime(px):0,
3269 relative_pid, px->uuid,
3270 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3271 read_freq_ctr(&px->be_sess_per_sec),
3272 px->be_counters.sps_max);
3273
3274 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3275 if (px->mode == PR_MODE_HTTP) {
3276 for (i=1; i<6; i++)
3277 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3278 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3279 }
3280 else
3281 chunk_appendf(&trash, ",,,,,,");
3282
3283 /* failed health analyses */
3284 chunk_appendf(&trash, ",");
3285
3286 /* requests : req_rate, req_rate_max, req_tot, */
3287 chunk_appendf(&trash, ",,,");
3288
3289 /* errors: cli_aborts, srv_aborts */
3290 chunk_appendf(&trash, "%lld,%lld,",
3291 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3292
3293 /* compression: in, out, bypassed */
3294 chunk_appendf(&trash, "%lld,%lld,%lld,",
3295 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3296
3297 /* compression: comp_rsp */
3298 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3299
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003300 /* lastsess */
3301 chunk_appendf(&trash, "%d,", be_lastsession(px));
3302
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303 /* finish with EOL */
3304 chunk_appendf(&trash, "\n");
3305 }
3306 return 1;
3307}
3308
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003309/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003311 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003313static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003315 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003316 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003319 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003320
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003321 /* 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 +02003322 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003323 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003324 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003325 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3326 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003327 }
3328
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003330 "<form action=\"%s%s%s%s\" method=\"post\">",
3331 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003332 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3333 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003334 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 }
3336
3337 /* print a new table */
3338 chunk_appendf(&trash,
3339 "<table class=\"tbl\" width=\"100%%\">\n"
3340 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003341 "<th class=\"pxname\" width=\"10%%\">");
3342
3343 chunk_appendf(&trash,
3344 "<a name=\"%s\"></a>%s"
3345 "<a class=px href=\"#%s\">%s</a>",
3346 px->id,
3347 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3348 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003349
3350 if (uri->flags & ST_SHLGNDS) {
3351 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003352 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3354 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003355 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356 }
3357
3358 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003359 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 "<th class=\"%s\" width=\"90%%\">%s</th>"
3361 "</tr>\n"
3362 "</table>\n"
3363 "<table class=\"tbl\" width=\"100%%\">\n"
3364 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3366 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3367
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003368 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 /* Column heading for Enable or Disable server */
3370 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003371 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003372
3373 chunk_appendf(&trash,
3374 "<th rowspan=2></th>"
3375 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003376 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003377 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3378 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3379 "<th colspan=9>Server</th>"
3380 "</tr>\n"
3381 "<tr class=\"titre\">"
3382 "<th>Cur</th><th>Max</th><th>Limit</th>"
3383 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003384 "<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 +01003385 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3386 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3387 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3388 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3389 "<th>Thrtle</th>\n"
3390 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003391}
3392
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003393/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003394 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003396static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003398 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 chunk_appendf(&trash, "</table>");
3400
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003401 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402 /* close the form used to enable/disable this proxy servers */
3403 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003404 "Choose the action to perform on the checked servers : "
3405 "<select name=action>"
3406 "<option value=\"\"></option>"
3407 "<option value=\"disable\">Disable</option>"
3408 "<option value=\"enable\">Enable</option>"
3409 "<option value=\"stop\">Soft Stop</option>"
3410 "<option value=\"start\">Soft Start</option>"
3411 "<option value=\"shutdown\">Kill Sessions</option>"
3412 "</select>"
3413 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3414 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3415 "</form>",
3416 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 }
3418
3419 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420}
Willy Tarreau91861262007-10-17 17:06:05 +02003421
3422/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003423 * Dumps statistics for a proxy. The output is sent to the stream interface's
3424 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3425 * buffer space, or non-zero if everything completed. This function is used
3426 * both by the CLI and the HTTP entry points, and is able to dump the output
3427 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003428 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003429static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003430{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003431 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003432 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003433 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003434 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003435 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003436
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003437 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003438
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003439 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003440 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003441 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003442 if (uri && uri->scope) {
3443 /* we have a limited scope, we have to check the proxy name */
3444 struct stat_scope *scope;
3445 int len;
3446
3447 len = strlen(px->id);
3448 scope = uri->scope;
3449
3450 while (scope) {
3451 /* match exact proxy name */
3452 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3453 break;
3454
3455 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003456 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003457 break;
3458 scope = scope->next;
3459 }
3460
3461 /* proxy name not found : don't dump anything */
3462 if (scope == NULL)
3463 return 1;
3464 }
3465
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003466 /* if the user has requested a limited output and the proxy
3467 * name does not match, skip it.
3468 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003469 if (appctx->ctx.stats.scope_len &&
3470 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 +02003471 return 1;
3472
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003473 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3474 (appctx->ctx.stats.iid != -1) &&
3475 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003476 return 1;
3477
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003478 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003479 /* fall through */
3480
Willy Tarreau295a8372011-03-10 11:25:07 +01003481 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003482 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003483 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003484 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003485 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003486 }
Willy Tarreau91861262007-10-17 17:06:05 +02003487
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003488 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003489 /* fall through */
3490
Willy Tarreau295a8372011-03-10 11:25:07 +01003491 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003492 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003493 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003494 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003495 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003496
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003497 appctx->ctx.stats.l = px->conf.listeners.n;
3498 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003499 /* fall through */
3500
Willy Tarreau295a8372011-03-10 11:25:07 +01003501 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003502 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003503 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003504 if (buffer_almost_full(rep->buf)) {
3505 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003506 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003507 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003508
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003509 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003510 if (!l->counters)
3511 continue;
3512
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003513 if (appctx->ctx.stats.flags & STAT_BOUND) {
3514 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003515 break;
3516
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003517 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003518 continue;
3519 }
3520
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003521 /* print the frontend */
3522 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3523 if (bi_putchk(rep, &trash) == -1)
3524 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003525 }
3526
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003527 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3528 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003529 /* fall through */
3530
Willy Tarreau295a8372011-03-10 11:25:07 +01003531 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003532 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003533 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003534 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 +02003535
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003536 if (buffer_almost_full(rep->buf)) {
3537 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003538 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003539 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003541 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003542
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003543 if (appctx->ctx.stats.flags & STAT_BOUND) {
3544 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003545 break;
3546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003548 continue;
3549 }
3550
Willy Tarreau44267702011-10-28 15:35:33 +02003551 if (sv->track)
3552 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003553 else
3554 svs = sv;
3555
Willy Tarreau91861262007-10-17 17:06:05 +02003556 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003557 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003558 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003559 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003560 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003561 sv_state = 3; /* UP */
3562 else
3563 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003564
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003565 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003566 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003567 else if (svs->state & SRV_GOINGDOWN)
3568 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003569 }
Willy Tarreau91861262007-10-17 17:06:05 +02003570 else
Simon Horman125d0992013-02-24 17:23:38 +09003571 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003572 sv_state = 1; /* going up */
3573 else
3574 sv_state = 0; /* DOWN */
3575
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003576 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003577 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003578 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003579 continue;
3580 }
3581
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003582 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3583 if (bi_putchk(rep, &trash) == -1)
3584 return 0;
3585 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003587 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003588 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003589
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590 case STAT_PX_ST_BE:
3591 /* print the backend */
3592 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3593 if (bi_putchk(rep, &trash) == -1)
3594 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003595
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003596 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003597 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003598
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003599 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003600 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003601 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003602 if (bi_putchk(rep, &trash) == -1)
3603 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003604 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003605
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003606 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003607 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003608
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003609 case STAT_PX_ST_FIN:
3610 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003611
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003612 default:
3613 /* unknown state, we should put an abort() here ! */
3614 return 1;
3615 }
3616}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003617
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003618/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3619 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003620 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003621static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003622{
3623 /* WARNING! This must fit in the first buffer !!! */
3624 chunk_appendf(&trash,
3625 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3626 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3627 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3628 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3629 "<style type=\"text/css\"><!--\n"
3630 "body {"
3631 " font-family: arial, helvetica, sans-serif;"
3632 " font-size: 12px;"
3633 " font-weight: normal;"
3634 " color: black;"
3635 " background: white;"
3636 "}\n"
3637 "th,td {"
3638 " font-size: 10px;"
3639 "}\n"
3640 "h1 {"
3641 " font-size: x-large;"
3642 " margin-bottom: 0.5em;"
3643 "}\n"
3644 "h2 {"
3645 " font-family: helvetica, arial;"
3646 " font-size: x-large;"
3647 " font-weight: bold;"
3648 " font-style: italic;"
3649 " color: #6020a0;"
3650 " margin-top: 0em;"
3651 " margin-bottom: 0em;"
3652 "}\n"
3653 "h3 {"
3654 " font-family: helvetica, arial;"
3655 " font-size: 16px;"
3656 " font-weight: bold;"
3657 " color: #b00040;"
3658 " background: #e8e8d0;"
3659 " margin-top: 0em;"
3660 " margin-bottom: 0em;"
3661 "}\n"
3662 "li {"
3663 " margin-top: 0.25em;"
3664 " margin-right: 2em;"
3665 "}\n"
3666 ".hr {margin-top: 0.25em;"
3667 " border-color: black;"
3668 " border-bottom-style: solid;"
3669 "}\n"
3670 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3671 ".total {background: #20D0D0;color: #ffff80;}\n"
3672 ".frontend {background: #e8e8d0;}\n"
3673 ".socket {background: #d0d0d0;}\n"
3674 ".backend {background: #e8e8d0;}\n"
3675 ".active0 {background: #ff9090;}\n"
3676 ".active1 {background: #ffd020;}\n"
3677 ".active2 {background: #ffffa0;}\n"
3678 ".active3 {background: #c0ffc0;}\n"
3679 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003680 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3681 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3682 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003683 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 ".backup0 {background: #ff9090;}\n"
3685 ".backup1 {background: #ff80ff;}\n"
3686 ".backup2 {background: #c060ff;}\n"
3687 ".backup3 {background: #b0d0ff;}\n"
3688 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3689 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003690 ".backup6 {background: #c060ff;}\n"
3691 ".backup7 {background: #cc9900;}\n"
3692 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003693 ".maintain {background: #c07820;}\n"
3694 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3695 "\n"
3696 "a.px:link {color: #ffff40; text-decoration: none;}"
3697 "a.px:visited {color: #ffff40; text-decoration: none;}"
3698 "a.px:hover {color: #ffffff; text-decoration: none;}"
3699 "a.lfsb:link {color: #000000; text-decoration: none;}"
3700 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3701 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3702 "\n"
3703 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3704 "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"
3705 "table.tbl td.ac { text-align: center;}\n"
3706 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3707 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3708 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3709 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3710 "\n"
3711 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3712 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3713 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003714 "table.det { border-collapse: collapse; border-style: none; }\n"
3715 "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 +01003716 "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 +01003717 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003718 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003719 " display:block;\n"
3720 " visibility:hidden;\n"
3721 " z-index:2147483647;\n"
3722 " position:absolute;\n"
3723 " padding:2px 4px 3px;\n"
3724 " background:#f0f060; color:#000000;\n"
3725 " border:1px solid #7040c0;\n"
3726 " white-space:nowrap;\n"
3727 " font-style:normal;font-size:11px;font-weight:normal;\n"
3728 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3729 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3730 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003731 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003732 "-->\n"
3733 "</style></head>\n",
3734 (uri->flags & ST_SHNODE) ? " on " : "",
3735 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3736 );
3737}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003738
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003739/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003740 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003741 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003742 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003743static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003744{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003745 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003746 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003747 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003748
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003749 /* WARNING! this has to fit the first packet too.
3750 * We are around 3.5 kB, add adding entries will
3751 * become tricky if we want to support 4kB buffers !
3752 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003753 chunk_appendf(&trash,
3754 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3755 PRODUCT_NAME "%s</a></h1>\n"
3756 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3757 "<hr width=\"100%%\" class=\"hr\">\n"
3758 "<h3>&gt; General process information</h3>\n"
3759 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3760 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3761 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3762 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3763 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3764 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3765 "Running tasks: %d/%d; idle = %d %%<br>\n"
3766 "</td><td align=\"center\" nowrap>\n"
3767 "<table class=\"lgd\"><tr>\n"
3768 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3769 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3770 "</tr><tr>\n"
3771 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3772 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3773 "</tr><tr>\n"
3774 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3775 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3776 "</tr><tr>\n"
3777 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003778 "</tr><tr>\n"
3779 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003780 "</tr><tr>\n"
3781 "<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 -07003782 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003783 "<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 +01003784 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003785 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003786 "</td>"
3787 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3788 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3789 "",
3790 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3791 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3792 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3793 (uri->flags & ST_SHDESC) ? ": " : "",
3794 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3795 pid, relative_pid, global.nbproc,
3796 up / 86400, (up % 86400) / 3600,
3797 (up % 3600) / 60, (up % 60),
3798 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3799 global.rlimit_memmax ? " MB" : "",
3800 global.rlimit_nofile,
3801 global.maxsock, global.maxconn, global.maxpipes,
3802 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3803 run_queue_cur, nb_tasks_cur, idle_pct
3804 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003805
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003806 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3807 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3808 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003809
3810 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003811 "<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 +02003812 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003813 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3814 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3815 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003816 STAT_SCOPE_TXT_MAXLEN);
3817
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003818 /* 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 +02003819 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003820 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003821 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003822 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3823 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003824 }
3825
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003826 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003827 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003828 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003829 uri->uri_prefix,
3830 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003831 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003832 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833 else
3834 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003835 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003836 uri->uri_prefix,
3837 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003838 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003839 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003841 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003842 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003843 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003844 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003845 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003846 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003847 "",
3848 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849 else
3850 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003851 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003852 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003854 ";norefresh",
3855 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003856 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003858 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003859 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003861 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3862 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003863 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003864
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003865 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003866 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003867 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003868 (uri->refresh > 0) ? ";norefresh" : "",
3869 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003870
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003871 chunk_appendf(&trash,
3872 "</ul></td>"
3873 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3874 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3875 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3876 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3877 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3878 "</ul>"
3879 "</td>"
3880 "</tr></table>\n"
3881 ""
3882 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003883
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003884 if (appctx->ctx.stats.st_code) {
3885 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 case STAT_STATUS_DONE:
3887 chunk_appendf(&trash,
3888 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003889 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003890 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003891 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003892 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3893 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003894 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003895 break;
3896 case STAT_STATUS_NONE:
3897 chunk_appendf(&trash,
3898 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003899 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003900 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003901 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003902 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3903 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003904 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003905 break;
3906 case STAT_STATUS_PART:
3907 chunk_appendf(&trash,
3908 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003909 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910 "Action partially processed.<br>"
3911 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003912 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003913 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3914 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003915 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003916 break;
3917 case STAT_STATUS_ERRP:
3918 chunk_appendf(&trash,
3919 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003920 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003921 "Action not processed because of invalid parameters."
3922 "<ul>"
3923 "<li>The action is maybe unknown.</li>"
3924 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3925 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3926 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003927 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003928 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3929 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003930 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003931 break;
3932 case STAT_STATUS_EXCD:
3933 chunk_appendf(&trash,
3934 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003935 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003936 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3937 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003938 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003939 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3940 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003941 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003942 break;
3943 case STAT_STATUS_DENY:
3944 chunk_appendf(&trash,
3945 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003946 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003947 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003948 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003949 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3950 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003951 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003952 break;
3953 default:
3954 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003955 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003956 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003957 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003958 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003959 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3960 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003961 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962 }
3963 chunk_appendf(&trash, "<p>\n");
3964 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003965}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003966
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003967/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3968 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003969 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003970static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003971{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003973}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003974
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003975/* This function dumps statistics onto the stream interface's read buffer in
3976 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003977 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3978 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3979 * and the session must be closed, or -1 in case of any error. This function is
3980 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003981 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003982static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003984 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 struct channel *rep = si->ib;
3986 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003987
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003988 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003989
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003990 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003991 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003992 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003994
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003997 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003998 else
3999 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004000
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004001 if (bi_putchk(rep, &trash) == -1)
4002 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004003
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004004 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004006
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004007 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004008 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004009 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004010 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004011 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004012 }
Willy Tarreau91861262007-10-17 17:06:05 +02004013
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004014 appctx->ctx.stats.px = proxy;
4015 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4016 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004017 /* fall through */
4018
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004019 case STAT_ST_LIST:
4020 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004021 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004022 if (buffer_almost_full(rep->buf)) {
4023 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004024 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004025 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004026
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004027 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004028 /* skip the disabled proxies, global frontend and non-networked ones */
4029 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004030 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004031 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004032
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004033 appctx->ctx.stats.px = px->next;
4034 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004035 }
4036 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004037
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004038 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004040
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004041 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004042 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004043 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004044 if (bi_putchk(rep, &trash) == -1)
4045 return 0;
4046 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004048 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004049 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004051 case STAT_ST_FIN:
4052 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004053
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004054 default:
4055 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004056 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004057 return -1;
4058 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004059}
Willy Tarreauae526782010-03-04 20:34:23 +01004060
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004061/* We reached the stats page through a POST request. The appctx is
4062 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004063 * Parse the posted data and enable/disable servers if necessary.
4064 * Returns 1 if request was parsed or zero if it needs more data.
4065 */
4066static int stats_process_http_post(struct stream_interface *si)
4067{
4068 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004069 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004070
4071 struct proxy *px = NULL;
4072 struct server *sv = NULL;
4073
4074 char key[LINESIZE];
4075 int action = ST_ADM_ACTION_NONE;
4076 int reprocess = 0;
4077
4078 int total_servers = 0;
4079 int altered_servers = 0;
4080
4081 char *first_param, *cur_param, *next_param, *end_params;
4082 char *st_cur_param = NULL;
4083 char *st_next_param = NULL;
4084
4085 struct chunk *temp;
4086 int reql;
4087
4088 temp = get_trash_chunk();
4089 if (temp->size < s->txn.req.body_len) {
4090 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004091 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004092 goto out;
4093 }
4094
4095 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4096 if (reql <= 0) {
4097 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004098 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004099 return 0;
4100 }
4101
4102 first_param = temp->str;
4103 end_params = temp->str + reql;
4104 cur_param = next_param = end_params;
4105 *end_params = '\0';
4106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004107 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004108
4109 /*
4110 * Parse the parameters in reverse order to only store the last value.
4111 * From the html form, the backend and the action are at the end.
4112 */
4113 while (cur_param > first_param) {
4114 char *value;
4115 int poffset, plen;
4116
4117 cur_param--;
4118
4119 if ((*cur_param == '&') || (cur_param == first_param)) {
4120 reprocess_servers:
4121 /* Parse the key */
4122 poffset = (cur_param != first_param ? 1 : 0);
4123 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4124 if ((plen > 0) && (plen <= sizeof(key))) {
4125 strncpy(key, cur_param + poffset, plen);
4126 key[plen - 1] = '\0';
4127 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004128 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004129 goto out;
4130 }
4131
4132 /* Parse the value */
4133 value = key;
4134 while (*value != '\0' && *value != '=') {
4135 value++;
4136 }
4137 if (*value == '=') {
4138 /* Ok, a value is found, we can mark the end of the key */
4139 *value++ = '\0';
4140 }
4141 if (url_decode(key) < 0 || url_decode(value) < 0)
4142 break;
4143
4144 /* Now we can check the key to see what to do */
4145 if (!px && (strcmp(key, "b") == 0)) {
4146 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4147 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004148 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004149 goto out;
4150 }
4151 }
4152 else if (!action && (strcmp(key, "action") == 0)) {
4153 if (strcmp(value, "disable") == 0) {
4154 action = ST_ADM_ACTION_DISABLE;
4155 }
4156 else if (strcmp(value, "enable") == 0) {
4157 action = ST_ADM_ACTION_ENABLE;
4158 }
4159 else if (strcmp(value, "stop") == 0) {
4160 action = ST_ADM_ACTION_STOP;
4161 }
4162 else if (strcmp(value, "start") == 0) {
4163 action = ST_ADM_ACTION_START;
4164 }
4165 else if (strcmp(value, "shutdown") == 0) {
4166 action = ST_ADM_ACTION_SHUTDOWN;
4167 }
4168 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004169 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004170 goto out;
4171 }
4172 }
4173 else if (strcmp(key, "s") == 0) {
4174 if (!(px && action)) {
4175 /*
4176 * Indicates that we'll need to reprocess the parameters
4177 * as soon as backend and action are known
4178 */
4179 if (!reprocess) {
4180 st_cur_param = cur_param;
4181 st_next_param = next_param;
4182 }
4183 reprocess = 1;
4184 }
4185 else if ((sv = findserver(px, value)) != NULL) {
4186 switch (action) {
4187 case ST_ADM_ACTION_DISABLE:
4188 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4189 /* Not already in maintenance, we can change the server state */
4190 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004191 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004192 set_server_down(&sv->check);
4193 altered_servers++;
4194 total_servers++;
4195 }
4196 break;
4197 case ST_ADM_ACTION_ENABLE:
4198 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004199 /* Already in maintenance, we can change the server state.
4200 * If this server tracks the status of another one,
4201 * we must restore the good status.
4202 */
4203 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4204 set_server_up(&sv->check);
4205 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4206 }
4207 else {
4208 sv->state &= ~SRV_MAINTAIN;
4209 sv->check.state &= ~CHK_ST_PAUSED;
4210 set_server_down(&sv->check);
4211 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004212 altered_servers++;
4213 total_servers++;
4214 }
4215 break;
4216 case ST_ADM_ACTION_STOP:
4217 case ST_ADM_ACTION_START:
4218 if (action == ST_ADM_ACTION_START)
4219 sv->uweight = sv->iweight;
4220 else
4221 sv->uweight = 0;
4222
4223 server_recalc_eweight(sv);
4224 set_server_drain_state(sv);
4225
4226 altered_servers++;
4227 total_servers++;
4228 break;
4229 case ST_ADM_ACTION_SHUTDOWN:
4230 if (px->state != PR_STSTOPPED) {
4231 struct session *sess, *sess_bck;
4232
4233 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4234 if (sess->srv_conn == sv)
4235 session_shutdown(sess, SN_ERR_KILLED);
4236
4237 altered_servers++;
4238 total_servers++;
4239 }
4240 break;
4241 }
4242 } else {
4243 /* the server name is unknown or ambiguous (duplicate names) */
4244 total_servers++;
4245 }
4246 }
4247 if (reprocess && px && action) {
4248 /* Now, we know the backend and the action chosen by the user.
4249 * We can safely restart from the first server parameter
4250 * to reprocess them
4251 */
4252 cur_param = st_cur_param;
4253 next_param = st_next_param;
4254 reprocess = 0;
4255 goto reprocess_servers;
4256 }
4257
4258 next_param = cur_param;
4259 }
4260 }
4261
4262 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004263 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004264 }
4265 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004266 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004267 }
4268 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004269 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004270 }
4271 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004272 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004273 }
4274 out:
4275 return 1;
4276}
4277
4278
4279static int stats_send_http_headers(struct stream_interface *si)
4280{
4281 struct session *s = session_from_task(si->owner);
4282 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004283 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004284
4285 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004286 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004287 "Cache-Control: no-cache\r\n"
4288 "Connection: close\r\n"
4289 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004290 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004291
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004292 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004293 chunk_appendf(&trash, "Refresh: %d\r\n",
4294 uri->refresh);
4295
Willy Tarreau51437d22013-12-29 00:43:40 +01004296 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004297
4298 s->txn.status = 200;
4299 s->logs.tv_request = now;
4300
4301 if (bi_putchk(si->ib, &trash) == -1)
4302 return 0;
4303
4304 return 1;
4305}
4306
4307static int stats_send_http_redirect(struct stream_interface *si)
4308{
4309 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4310 struct session *s = session_from_task(si->owner);
4311 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004313
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004314 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004315 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004316 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004317 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004318 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4319 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004320 }
4321
4322 /* We don't want to land on the posted stats page because a refresh will
4323 * repost the data. We don't want this to happen on accident so we redirect
4324 * the browse to the stats page with a GET.
4325 */
4326 chunk_printf(&trash,
4327 "HTTP/1.1 303 See Other\r\n"
4328 "Cache-Control: no-cache\r\n"
4329 "Content-Type: text/plain\r\n"
4330 "Connection: close\r\n"
4331 "Location: %s;st=%s%s%s%s\r\n"
4332 "\r\n",
4333 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004334 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4335 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4336 stat_status_codes[appctx->ctx.stats.st_code]) ?
4337 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004338 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004339 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4340 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004341 scope_txt);
4342
4343 s->txn.status = 303;
4344 s->logs.tv_request = now;
4345
4346 if (bi_putchk(si->ib, &trash) == -1)
4347 return 0;
4348
4349 return 1;
4350}
4351
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004352/* This I/O handler runs as an applet embedded in a stream interface. It is
4353 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004354 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004355 * automatically unregisters itself once transfer is complete.
4356 */
4357static void http_stats_io_handler(struct stream_interface *si)
4358{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004359 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004360 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361 struct channel *req = si->ob;
4362 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004363
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004364 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4365 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004366
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004367 /* check that the output is not closed */
4368 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004370
Willy Tarreau347a35d2013-11-22 17:51:09 +01004371 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004372 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004373 if (stats_send_http_headers(si)) {
4374 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004375 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004376 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004377 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004379 }
4380
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004381 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004382 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004383 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004384 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004386 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004387 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004388 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004389 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004390 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004391 }
4392
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004393 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004394 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004395 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004396 }
4397
Willy Tarreau51437d22013-12-29 00:43:40 +01004398 if (appctx->st0 == STAT_HTTP_DONE)
4399 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004400
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004401 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4402 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004403
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004404 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004405 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4406 si_shutr(si);
4407 res->flags |= CF_READ_NULL;
4408 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004409 }
Willy Tarreau91861262007-10-17 17:06:05 +02004410
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004411 /* update all other flags and resync with the other side */
4412 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004413
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004414 /* we don't want to expire timeouts while we're processing requests */
4415 si->ib->rex = TICK_ETERNITY;
4416 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004417
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004418 out:
4419 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4420 /* check that we have released everything then unregister */
4421 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004422 }
4423}
4424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004425
Willy Tarreau909d5172012-11-26 03:04:41 +01004426static inline const char *get_conn_ctrl_name(const struct connection *conn)
4427{
Willy Tarreau3c728722014-01-23 13:50:42 +01004428 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004429 return "NONE";
4430 return conn->ctrl->name;
4431}
4432
4433static inline const char *get_conn_xprt_name(const struct connection *conn)
4434{
4435 static char ptr[17];
4436
Willy Tarreauaad69382014-01-23 14:21:42 +01004437 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004438 return "NONE";
4439
4440 if (conn->xprt == &raw_sock)
4441 return "RAW";
4442
4443#ifdef USE_OPENSSL
4444 if (conn->xprt == &ssl_sock)
4445 return "SSL";
4446#endif
4447 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4448 return ptr;
4449}
4450
4451static inline const char *get_conn_data_name(const struct connection *conn)
4452{
4453 static char ptr[17];
4454
4455 if (!conn->data)
4456 return "NONE";
4457
4458 if (conn->data == &sess_conn_cb)
4459 return "SESS";
4460
4461 if (conn->data == &si_conn_cb)
4462 return "STRM";
4463
4464 if (conn->data == &check_conn_cb)
4465 return "CHCK";
4466
4467 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4468 return ptr;
4469}
4470
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004471/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004472 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004473 * 0 if the output buffer is full and it needs to be called again, otherwise
4474 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004475 */
Willy Tarreau76153662012-11-26 01:16:39 +01004476static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004477{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004478 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004479 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004480 extern const char *monthname[12];
4481 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004482 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004483 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004484
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004485 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004486
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004487 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004488 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004489 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4490 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004491 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004492 appctx->ctx.sess.uid = 0;
4493 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004494 return 1;
4495 }
4496
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004497 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004498 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 appctx->ctx.sess.uid = sess->uniq_id;
4500 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004501 /* fall through */
4502
4503 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004504 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004505 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004506 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004507 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004508 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4509 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004510 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004511 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004512
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004513 conn = objt_conn(sess->si[0].end);
4514 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004515 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004516 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004517 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004518 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004519 break;
4520 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004521 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004522 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004523 default:
4524 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004525 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004526 break;
4527 }
4528
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004529 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004530 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004531 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004532
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004533 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004534 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004535 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4536 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4537 sess->listener ? sess->listener->luid : 0);
4538
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004539 if (conn)
4540 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004541
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004542 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004543 case AF_INET:
4544 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004545 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004546 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004547 break;
4548 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004549 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004550 break;
4551 default:
4552 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004553 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004554 break;
4555 }
4556
Willy Tarreau50943332011-09-02 17:33:05 +02004557 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004558 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004559 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004560 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004561 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4562 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004563 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004564
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004565 conn = objt_conn(sess->si[1].end);
4566 if (conn)
4567 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004568
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004569 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004570 case AF_INET:
4571 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004572 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004573 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004574 break;
4575 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004576 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004577 break;
4578 default:
4579 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004580 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004581 break;
4582 }
4583
4584 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004585 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004586 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004587 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4588 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004589 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004590 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004591
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004592 if (conn)
4593 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004594
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004595 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004596 case AF_INET:
4597 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004598 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004599 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004600 break;
4601 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004602 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004603 break;
4604 default:
4605 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004606 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004607 break;
4608 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004610 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004611 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004612 sess->task,
4613 sess->task->state,
4614 sess->task->nice, sess->task->calls,
4615 sess->task->expire ?
4616 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4617 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4618 TICKS_TO_MS(1000)) : "<NEVER>",
4619 task_in_rq(sess->task) ? ", running" : "");
4620
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004621 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004622 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004623 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4624
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004625 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004626 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4627 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4628 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4629
4630 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004631 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004632 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004633 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004634 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004635 obj_type_name(sess->si[0].end),
4636 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004637 sess->si[0].exp ?
4638 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4639 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4640 TICKS_TO_MS(1000)) : "<NEVER>",
4641 sess->si[0].err_type);
4642
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004643 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004644 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004645 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004646 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004647 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004648 obj_type_name(sess->si[1].end),
4649 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004650 sess->si[1].exp ?
4651 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4652 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4653 TICKS_TO_MS(1000)) : "<NEVER>",
4654 sess->si[1].err_type);
4655
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004656 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004657 chunk_appendf(&trash,
4658 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004659 conn,
4660 get_conn_ctrl_name(conn),
4661 get_conn_xprt_name(conn),
4662 get_conn_data_name(conn),
4663 obj_type_name(conn->target),
4664 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004665
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004666 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004667 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 conn->flags,
4669 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004670 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004671 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004672 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004673 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004674 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4675 chunk_appendf(&trash,
4676 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4677 tmpctx,
4678 tmpctx->st0,
4679 tmpctx->st1,
4680 tmpctx->st2,
4681 tmpctx->applet->name);
4682 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004683
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004684 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004685 chunk_appendf(&trash,
4686 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004687 conn,
4688 get_conn_ctrl_name(conn),
4689 get_conn_xprt_name(conn),
4690 get_conn_data_name(conn),
4691 obj_type_name(conn->target),
4692 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004693
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004694 chunk_appendf(&trash,
4695 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004696 conn->flags,
4697 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004698 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004699 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004700 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004701 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004702 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4703 chunk_appendf(&trash,
4704 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4705 tmpctx,
4706 tmpctx->st0,
4707 tmpctx->st1,
4708 tmpctx->st2,
4709 tmpctx->applet->name);
4710 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004712 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004713 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004714 " an_exp=%s",
4715 sess->req,
4716 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004717 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004718 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004719 sess->req->analyse_exp ?
4720 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4721 TICKS_TO_MS(1000)) : "<NEVER>");
4722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004723 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004724 " rex=%s",
4725 sess->req->rex ?
4726 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4727 TICKS_TO_MS(1000)) : "<NEVER>");
4728
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004729 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004730 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004731 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004732 sess->req->wex ?
4733 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4734 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004735 sess->req->buf,
4736 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004737 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004738 sess->txn.req.next, sess->req->buf->i,
4739 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004740
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004741 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004742 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004743 " an_exp=%s",
4744 sess->rep,
4745 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004746 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004747 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004748 sess->rep->analyse_exp ?
4749 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4750 TICKS_TO_MS(1000)) : "<NEVER>");
4751
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004752 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004753 " rex=%s",
4754 sess->rep->rex ?
4755 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4756 TICKS_TO_MS(1000)) : "<NEVER>");
4757
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004758 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004759 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004760 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004761 sess->rep->wex ?
4762 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4763 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004764 sess->rep->buf,
4765 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004766 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004767 sess->txn.rsp.next, sess->rep->buf->i,
4768 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004769
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004770 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004771 return 0;
4772
4773 /* use other states to dump the contents */
4774 }
4775 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004776 appctx->ctx.sess.uid = 0;
4777 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004778 return 1;
4779}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004780
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004781static int stats_maps_list(struct stream_interface *si)
4782{
4783 struct appctx *appctx = __objt_appctx(si->end);
4784
4785 switch (appctx->st2) {
4786 case STAT_ST_INIT:
4787 /* Init to the first entry. The list cannot be change */
4788 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4789 appctx->st2 = STAT_ST_LIST;
4790 /* fall through */
4791
4792 case STAT_ST_LIST:
4793 while (appctx->ctx.map.ref) {
4794
4795 chunk_reset(&trash);
4796
4797 /* build messages */
4798 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4799
4800 if (bi_putchk(si->ib, &trash) == -1) {
4801 /* let's try again later from this session. We add ourselves into
4802 * this session's users so that it can remove us upon termination.
4803 */
4804 return 0;
4805 }
4806
4807 /* get next list entry and check the end of the list */
4808 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4809 struct map_reference *, list);
4810 if (&appctx->ctx.map.ref->list == &maps)
4811 break;
4812 }
4813
4814 appctx->st2 = STAT_ST_FIN;
4815 /* fall through */
4816
4817 default:
4818 appctx->st2 = STAT_ST_FIN;
4819 return 1;
4820 }
4821}
4822
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004823static int stats_map_lookup(struct stream_interface *si)
4824{
4825 struct appctx *appctx = __objt_appctx(si->end);
4826 struct sample_storage *smp;
4827 struct sample sample;
4828 struct pattern *pat;
4829 struct pat_idx_elt *elt;
4830 enum pat_match_res res;
4831 struct sockaddr_in addr;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004832 char addr_str[INET_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004833
4834 switch (appctx->st2) {
4835 case STAT_ST_INIT:
4836 appctx->ctx.map.desc = NULL;
4837 stats_map_lookup_next(si);
4838 appctx->st2 = STAT_ST_LIST;
4839 /* fall through */
4840
4841 case STAT_ST_LIST:
4842 /* for each lookup type */
4843 while (appctx->ctx.map.desc) {
4844 /* initialise chunk to build new message */
4845 chunk_reset(&trash);
4846
4847 /* execute pattern matching */
4848 sample.type = SMP_T_CSTR;
4849 sample.data.str.len = appctx->ctx.map.chunk.len;
4850 sample.data.str.str = appctx->ctx.map.chunk.str;
4851 pat = NULL;
4852 elt = NULL;
4853 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4854
4855 /* build return message: set type of match */
4856 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4857 chunk_appendf(&trash, "found, ");
4858 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4859 chunk_appendf(&trash, "bool, ");
4860 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4861 chunk_appendf(&trash, "int, ");
4862 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4863 chunk_appendf(&trash, "ip, ");
4864 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4865 chunk_appendf(&trash, "bin, ");
4866 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4867 chunk_appendf(&trash, "len, ");
4868 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4869 chunk_appendf(&trash, "str, ");
4870 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4871 chunk_appendf(&trash, "beg, ");
4872 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4873 chunk_appendf(&trash, "sub, ");
4874 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4875 chunk_appendf(&trash, "dir, ");
4876 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4877 chunk_appendf(&trash, "dom, ");
4878 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4879 chunk_appendf(&trash, "end, ");
4880 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4881 chunk_appendf(&trash, "reg, ");
4882 else /* The never appens case */
4883 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4884
4885 /* Display no match, and set default value */
4886 if (res == PAT_NOMATCH) {
4887 chunk_appendf(&trash, "no-match, ");
4888 smp = appctx->ctx.map.desc->def;
4889 }
4890
4891 /* Display match and match info */
4892 else {
4893 /* display match */
4894 chunk_appendf(&trash, "match, ");
4895
4896 /* display search mode */
4897 if (elt)
4898 chunk_appendf(&trash, "tree, ");
4899 else
4900 chunk_appendf(&trash, "list, ");
4901
4902 /* display search options */
4903 if (pat) {
4904 /* case sensitive */
4905 if (pat->flags & PAT_F_IGNORE_CASE)
4906 chunk_appendf(&trash, "case-insensitive, ");
4907 else
4908 chunk_appendf(&trash, "case-sensitive, ");
4909
4910 /* display source */
4911 if (pat->flags & PAT_F_FROM_FILE)
4912 chunk_appendf(&trash, "from-file, ");
4913 }
4914
4915 /* display match expresion */
4916 if (elt) {
4917 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4918 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4919 }
4920 /* only IPv4 */
4921 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4922 /* convert ip */
4923 memcpy(&addr.sin_addr, elt->node.key, 4);
4924 addr.sin_family = AF_INET;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004925 if (addr_to_str((struct sockaddr_storage *)&addr, addr_str, INET_ADDRSTRLEN))
4926 chunk_appendf(&trash, "match=\"%s/%d\", ", addr_str, elt->node.node.pfx);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004927 }
4928 }
4929 }
4930
4931 /* display return value */
4932 if (!smp) {
4933 chunk_appendf(&trash, "return=nothing\n");
4934 }
4935 else {
4936 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4937 sample.type = smp->type;
4938 if (sample_casts[sample.type][SMP_T_CSTR] &&
4939 sample_casts[sample.type][SMP_T_CSTR](&sample))
4940 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4941 sample.data.str.str, smp_to_type[smp->type]);
4942 else
4943 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4944 smp_to_type[smp->type]);
4945 }
4946
4947 /* display response */
4948 if (bi_putchk(si->ib, &trash) == -1) {
4949 /* let's try again later from this session. We add ourselves into
4950 * this session's users so that it can remove us upon termination.
4951 */
4952 return 0;
4953 }
4954
4955 /* get next entry */
4956 stats_map_lookup_next(si);
4957 }
4958
4959 appctx->st2 = STAT_ST_FIN;
4960 /* fall through */
4961
4962 default:
4963 appctx->st2 = STAT_ST_FIN;
4964 free(appctx->ctx.map.chunk.str);
4965 return 1;
4966 }
4967}
4968
4969static int stats_map_list(struct stream_interface *si)
4970{
4971 struct appctx *appctx = __objt_appctx(si->end);
4972
4973 switch (appctx->st2) {
4974
4975 case STAT_ST_INIT:
4976 /* Init to the first entry. The list cannot be change */
4977 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
4978 struct map_entry *, list);
4979 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4980 appctx->ctx.map.ent = NULL;
4981 appctx->st2 = STAT_ST_LIST;
4982 /* fall through */
4983
4984 case STAT_ST_LIST:
4985 while (appctx->ctx.map.ent) {
4986 chunk_reset(&trash);
4987
4988 /* build messages */
4989 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
4990
4991 if (bi_putchk(si->ib, &trash) == -1) {
4992 /* let's try again later from this session. We add ourselves into
4993 * this session's users so that it can remove us upon termination.
4994 */
4995 return 0;
4996 }
4997
4998 /* get next list entry and check the end of the list */
4999 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
5000 struct map_entry *, list);
5001 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
5002 break;
5003 }
5004
5005 appctx->st2 = STAT_ST_FIN;
5006 /* fall through */
5007
5008 default:
5009 appctx->st2 = STAT_ST_FIN;
5010 return 1;
5011 }
5012}
5013
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005014/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005015 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005016 * to be called again, otherwise non-zero. It is designed to be called
5017 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005018 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005019static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005020{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005021 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005022 struct connection *conn;
5023
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005024 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005025 /* If we're forced to shut down, we might have to remove our
5026 * reference to the last session being dumped.
5027 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005028 if (appctx->st2 == STAT_ST_LIST) {
5029 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5030 LIST_DEL(&appctx->ctx.sess.bref.users);
5031 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005032 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005033 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005034 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005035 }
5036
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005037 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005039 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005040 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005041 /* the function had not been called yet, let's prepare the
5042 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005043 * pointer to the first in the global list. When a target
5044 * session is being destroyed, it is responsible for updating
5045 * this pointer. We know we have reached the end when this
5046 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005047 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005048 LIST_INIT(&appctx->ctx.sess.bref.users);
5049 appctx->ctx.sess.bref.ref = sessions.n;
5050 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005051 /* fall through */
5052
Willy Tarreau295a8372011-03-10 11:25:07 +01005053 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005054 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005055 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5056 LIST_DEL(&appctx->ctx.sess.bref.users);
5057 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005058 }
5059
5060 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005062 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005063 struct session *curr_sess;
5064
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005065 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005066
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005067 if (appctx->ctx.sess.target) {
5068 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005069 goto next_sess;
5070
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005072 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005073 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005074 return 0;
5075
5076 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005077 LIST_DEL(&appctx->ctx.sess.bref.users);
5078 LIST_INIT(&appctx->ctx.sess.bref.users);
5079 if (appctx->ctx.sess.target != (void *)-1) {
5080 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005081 break;
5082 }
5083 else
5084 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005085 }
5086
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005087 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005088 "%p: proto=%s",
5089 curr_sess,
5090 curr_sess->listener->proto->name);
5091
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005092
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005093 conn = objt_conn(curr_sess->si[0].end);
5094 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005095 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005096 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005097 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005098 " src=%s:%d fe=%s be=%s srv=%s",
5099 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005100 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005101 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005102 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005103 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005104 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005105 break;
5106 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005107 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005108 " src=unix:%d fe=%s be=%s srv=%s",
5109 curr_sess->listener->luid,
5110 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005111 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005112 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005113 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005114 break;
5115 }
5116
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005117 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005118 " ts=%02x age=%s calls=%d",
5119 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005120 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5121 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005122
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005123 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005124 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005125 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005126 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005127 curr_sess->req->analysers,
5128 curr_sess->req->rex ?
5129 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5130 TICKS_TO_MS(1000)) : "");
5131
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005132 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005133 ",wx=%s",
5134 curr_sess->req->wex ?
5135 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5136 TICKS_TO_MS(1000)) : "");
5137
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005138 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005139 ",ax=%s]",
5140 curr_sess->req->analyse_exp ?
5141 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5142 TICKS_TO_MS(1000)) : "");
5143
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005144 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005145 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005146 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005147 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005148 curr_sess->rep->analysers,
5149 curr_sess->rep->rex ?
5150 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5151 TICKS_TO_MS(1000)) : "");
5152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005153 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005154 ",wx=%s",
5155 curr_sess->rep->wex ?
5156 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5157 TICKS_TO_MS(1000)) : "");
5158
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005159 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005160 ",ax=%s]",
5161 curr_sess->rep->analyse_exp ?
5162 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5163 TICKS_TO_MS(1000)) : "");
5164
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005165 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005166 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005167 " s0=[%d,%1xh,fd=%d,ex=%s]",
5168 curr_sess->si[0].state,
5169 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005170 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005171 curr_sess->si[0].exp ?
5172 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5173 TICKS_TO_MS(1000)) : "");
5174
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005175 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005176 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005177 " s1=[%d,%1xh,fd=%d,ex=%s]",
5178 curr_sess->si[1].state,
5179 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005180 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005181 curr_sess->si[1].exp ?
5182 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5183 TICKS_TO_MS(1000)) : "");
5184
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005185 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005186 " exp=%s",
5187 curr_sess->task->expire ?
5188 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5189 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005190 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005191 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005192
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005193 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005194
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005195 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005196 /* let's try again later from this session. We add ourselves into
5197 * this session's users so that it can remove us upon termination.
5198 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005199 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005200 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005201 }
5202
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005203 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005204 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005205 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005206
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005207 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005208 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005209 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005210 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005211 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005212 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005214 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005215 return 0;
5216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005217 appctx->ctx.sess.target = NULL;
5218 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005219 return 1;
5220 }
5221
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005222 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005223 /* fall through */
5224
5225 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005226 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005227 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005228 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005229}
5230
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005231/* This is called when the stream interface is closed. For instance, upon an
5232 * external abort, we won't call the i/o handler anymore so we may need to
5233 * remove back references to the session currently being dumped.
5234 */
Simon Horman74d88312013-02-12 10:45:50 +09005235static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005236{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005237 struct appctx *appctx = __objt_appctx(si->end);
5238
5239 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5240 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5241 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005242 }
5243}
5244
Willy Tarreau20e99322013-04-13 09:22:25 +02005245/* This function is used to either dump tables states (when action is set
5246 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005247 * It returns 0 if the output buffer is full and it needs to be called
5248 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005249 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005250static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005251{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005252 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005253 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005254 struct ebmb_node *eb;
5255 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005256 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005257 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005258
5259 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005260 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005261 * - STAT_ST_INIT : the first call
5262 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005263 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005264 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005265 * and the entry pointer points to the next entry to be dumped,
5266 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005267 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005268 * data though.
5269 */
5270
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005271 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005272 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005273 if (appctx->st2 == STAT_ST_LIST) {
5274 appctx->ctx.table.entry->ref_cnt--;
5275 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005276 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005277 return 1;
5278 }
5279
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005280 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005282 while (appctx->st2 != STAT_ST_FIN) {
5283 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005284 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005285 appctx->ctx.table.proxy = appctx->ctx.table.target;
5286 if (!appctx->ctx.table.proxy)
5287 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005288
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005289 appctx->ctx.table.entry = NULL;
5290 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005291 break;
5292
Willy Tarreau295a8372011-03-10 11:25:07 +01005293 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005294 if (!appctx->ctx.table.proxy ||
5295 (appctx->ctx.table.target &&
5296 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5297 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005298 break;
5299 }
5300
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005301 if (appctx->ctx.table.proxy->table.size) {
5302 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5303 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005304 return 0;
5305
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005306 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005307 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005308 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005309 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005310 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005311 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5312 appctx->ctx.table.entry->ref_cnt++;
5313 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005314 break;
5315 }
5316 }
5317 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005319 break;
5320
Willy Tarreau295a8372011-03-10 11:25:07 +01005321 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005322 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005324 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005325 /* we're filtering on some data contents */
5326 void *ptr;
5327 long long data;
5328
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005329 dt = appctx->ctx.table.data_type;
5330 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5331 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005332 dt);
5333
5334 data = 0;
5335 switch (stktable_data_types[dt].std_type) {
5336 case STD_T_SINT:
5337 data = stktable_data_cast(ptr, std_t_sint);
5338 break;
5339 case STD_T_UINT:
5340 data = stktable_data_cast(ptr, std_t_uint);
5341 break;
5342 case STD_T_ULL:
5343 data = stktable_data_cast(ptr, std_t_ull);
5344 break;
5345 case STD_T_FRQP:
5346 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005347 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005348 break;
5349 }
5350
5351 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005352 if ((data < appctx->ctx.table.value &&
5353 (appctx->ctx.table.data_op == STD_OP_EQ ||
5354 appctx->ctx.table.data_op == STD_OP_GT ||
5355 appctx->ctx.table.data_op == STD_OP_GE)) ||
5356 (data == appctx->ctx.table.value &&
5357 (appctx->ctx.table.data_op == STD_OP_NE ||
5358 appctx->ctx.table.data_op == STD_OP_GT ||
5359 appctx->ctx.table.data_op == STD_OP_LT)) ||
5360 (data > appctx->ctx.table.value &&
5361 (appctx->ctx.table.data_op == STD_OP_EQ ||
5362 appctx->ctx.table.data_op == STD_OP_LT ||
5363 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005364 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005365 }
5366
Simon Hormanc88b8872011-06-15 15:18:49 +09005367 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005368 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5369 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005370 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005372 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005374 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005375 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005376 struct stksess *old = appctx->ctx.table.entry;
5377 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005378 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005379 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5380 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5381 stksess_kill(&appctx->ctx.table.proxy->table, old);
5382 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005383 break;
5384 }
5385
Simon Hormanc88b8872011-06-15 15:18:49 +09005386
5387 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005388 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5389 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5390 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005391
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005392 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5393 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005394 break;
5395
Willy Tarreau295a8372011-03-10 11:25:07 +01005396 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005397 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005398 break;
5399 }
5400 }
5401 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005402}
5403
Willy Tarreaud426a182010-03-05 14:58:26 +01005404/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005405 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5406 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5407 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5408 * lines are respected within the limit of 70 output chars. Lines that are
5409 * continuation of a previous truncated line begin with "+" instead of " "
5410 * after the offset. The new pointer is returned.
5411 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005412static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5413 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005414{
5415 int end;
5416 unsigned char c;
5417
5418 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005419 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005420 return ptr;
5421
Willy Tarreau77804732012-10-29 16:14:26 +01005422 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005423
Willy Tarreaud426a182010-03-05 14:58:26 +01005424 while (ptr < len && ptr < bsize) {
5425 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005426 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005427 if (out->len > end - 2)
5428 break;
5429 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005430 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005431 if (out->len > end - 3)
5432 break;
5433 out->str[out->len++] = '\\';
5434 switch (c) {
5435 case '\t': c = 't'; break;
5436 case '\n': c = 'n'; break;
5437 case '\r': c = 'r'; break;
5438 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005439 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005440 }
5441 out->str[out->len++] = c;
5442 } else {
5443 if (out->len > end - 5)
5444 break;
5445 out->str[out->len++] = '\\';
5446 out->str[out->len++] = 'x';
5447 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5448 out->str[out->len++] = hextab[c & 0xF];
5449 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005450 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005451 /* we had a line break, let's return now */
5452 out->str[out->len++] = '\n';
5453 *line = ptr;
5454 return ptr;
5455 }
5456 }
5457 /* we have an incomplete line, we return it as-is */
5458 out->str[out->len++] = '\n';
5459 return ptr;
5460}
5461
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005462/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005463 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005464 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005465 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005466static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005467{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005468 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005469 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005470
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005471 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005472 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005473
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005474 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005475
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005476 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005477 /* the function had not been called yet, let's prepare the
5478 * buffer for a response.
5479 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005480 struct tm tm;
5481
5482 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005483 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005484 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5485 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5486 error_snapshot_id);
5487
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005488 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005489 /* Socket buffer full. Let's try again later from the same point */
5490 return 0;
5491 }
5492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005493 appctx->ctx.errors.px = proxy;
5494 appctx->ctx.errors.buf = 0;
5495 appctx->ctx.errors.bol = 0;
5496 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005497 }
5498
5499 /* we have two inner loops here, one for the proxy, the other one for
5500 * the buffer.
5501 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005502 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005503 struct error_snapshot *es;
5504
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005505 if (appctx->ctx.errors.buf == 0)
5506 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005507 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005508 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005509
5510 if (!es->when.tv_sec)
5511 goto next;
5512
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005513 if (appctx->ctx.errors.iid >= 0 &&
5514 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5515 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005516 goto next;
5517
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005518 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005519 /* just print headers now */
5520
5521 char pn[INET6_ADDRSTRLEN];
5522 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005523 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005524
5525 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005526 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005527 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005528 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005529
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005530 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5531 case AF_INET:
5532 case AF_INET6:
5533 port = get_host_port(&es->src);
5534 break;
5535 default:
5536 port = 0;
5537 }
5538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005539 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005540 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005541 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005542 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005543 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005544 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005545 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5546 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005547 break;
5548 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005549 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005550 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005551 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005552 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005553 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005554 break;
5555 }
5556
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005557 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005558 ", server %s (#%d), event #%u\n"
5559 " src %s:%d, session #%d, session flags 0x%08x\n"
5560 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5561 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5562 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5563 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5564 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5565 es->ev_id,
5566 pn, port, es->sid, es->s_flags,
5567 es->state, es->m_flags, es->t_flags,
5568 es->m_clen, es->m_blen,
5569 es->b_flags, es->b_out, es->b_tot,
5570 es->len, es->b_wrap, es->pos);
5571
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005572 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005573 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005574 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005575 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005576 appctx->ctx.errors.ptr = 0;
5577 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005578 }
5579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005580 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005581 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005582 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005583 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005584 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005585 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005586 goto next;
5587 }
5588
5589 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005590 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005591 int newptr;
5592 int newline;
5593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005594 newline = appctx->ctx.errors.bol;
5595 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5596 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005597 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005598
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005600 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005601 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005602 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005603 appctx->ctx.errors.ptr = newptr;
5604 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005605 };
5606 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005607 appctx->ctx.errors.bol = 0;
5608 appctx->ctx.errors.ptr = -1;
5609 appctx->ctx.errors.buf++;
5610 if (appctx->ctx.errors.buf > 1) {
5611 appctx->ctx.errors.buf = 0;
5612 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005613 }
5614 }
5615
5616 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005617 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005618}
5619
Willy Tarreaud5781202012-09-22 19:32:35 +02005620/* parse the "level" argument on the bind lines */
5621static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5622{
5623 if (!*args[cur_arg + 1]) {
5624 memprintf(err, "'%s' : missing level", args[cur_arg]);
5625 return ERR_ALERT | ERR_FATAL;
5626 }
5627
5628 if (!strcmp(args[cur_arg+1], "user"))
5629 conf->level = ACCESS_LVL_USER;
5630 else if (!strcmp(args[cur_arg+1], "operator"))
5631 conf->level = ACCESS_LVL_OPER;
5632 else if (!strcmp(args[cur_arg+1], "admin"))
5633 conf->level = ACCESS_LVL_ADMIN;
5634 else {
5635 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5636 args[cur_arg], args[cur_arg+1]);
5637 return ERR_ALERT | ERR_FATAL;
5638 }
5639
5640 return 0;
5641}
5642
Willy Tarreaub24281b2011-02-13 13:16:36 +01005643struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005644 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005645 .name = "<STATS>", /* used for logging */
5646 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005647 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005648};
5649
Simon Horman9bd2c732011-06-15 15:18:44 +09005650static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005651 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005652 .name = "<CLI>", /* used for logging */
5653 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005654 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005655};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005656
Willy Tarreaudc13c112013-06-21 23:16:39 +02005657static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005658 { CFG_GLOBAL, "stats", stats_parse_global },
5659 { 0, NULL, NULL },
5660}};
5661
Willy Tarreaud5781202012-09-22 19:32:35 +02005662static struct bind_kw_list bind_kws = { "STAT", { }, {
5663 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5664 { NULL, NULL, 0 },
5665}};
5666
Willy Tarreau10522fd2008-07-09 20:12:41 +02005667__attribute__((constructor))
5668static void __dumpstats_module_init(void)
5669{
5670 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005671 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005672}
5673
Willy Tarreau91861262007-10-17 17:06:05 +02005674/*
5675 * Local variables:
5676 * c-indent-level: 8
5677 * c-basic-offset: 8
5678 * End:
5679 */