blob: b8e822ff52332c6a8a0e3af395c1b8ca7132b2a3 [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>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010046#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
48#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010050#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020051#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010053#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010055#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010056#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020057#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020058#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010059#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020060#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020061#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020063#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010064#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010065#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020066
Willy Tarreau7a0169a2012-11-19 17:13:16 +010067#ifdef USE_OPENSSL
68#include <proto/ssl_sock.h>
69#endif
70
Willy Tarreau91b843d2014-01-28 16:27:17 +010071/* stats socket states */
72enum {
73 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
74 STAT_CLI_END, /* final state, let's close */
75 STAT_CLI_GETREQ, /* wait for a request */
76 STAT_CLI_OUTPUT, /* all states after this one are responses */
77 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
78 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010079 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010080 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020081 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_ERR, /* dump errors */
83 STAT_CLI_O_TAB, /* dump tables */
84 STAT_CLI_O_CLR, /* clear tables */
85 STAT_CLI_O_SET, /* set entries in tables */
86 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010087 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
88 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010089 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010090 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091};
92
Willy Tarreaued7df902014-05-22 18:04:49 +020093/* Actions available for the stats admin forms */
94enum {
95 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020096
97 /* enable/disable health checks */
98 ST_ADM_ACTION_DHLTH,
99 ST_ADM_ACTION_EHLTH,
100
101 /* force health check status */
102 ST_ADM_ACTION_HRUNN,
103 ST_ADM_ACTION_HNOLB,
104 ST_ADM_ACTION_HDOWN,
105
106 /* enable/disable agent checks */
107 ST_ADM_ACTION_DAGENT,
108 ST_ADM_ACTION_EAGENT,
109
110 /* force agent check status */
111 ST_ADM_ACTION_ARUNN,
112 ST_ADM_ACTION_ADOWN,
113
114 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200115 ST_ADM_ACTION_READY,
116 ST_ADM_ACTION_DRAIN,
117 ST_ADM_ACTION_MAINT,
118 ST_ADM_ACTION_SHUTDOWN,
119 /* these are the ancient actions, still available for compatibility */
120 ST_ADM_ACTION_DISABLE,
121 ST_ADM_ACTION_ENABLE,
122 ST_ADM_ACTION_STOP,
123 ST_ADM_ACTION_START,
124};
125
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100126static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100127static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200128static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900129static int stats_dump_sess_to_buffer(struct stream_interface *si);
130static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100131static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100132static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
133static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100134static int stats_pats_list(struct stream_interface *si);
135static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100136static int stats_map_lookup(struct stream_interface *si);
Willy Tarreau00a37f02015-04-13 12:05:19 +0200137static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900138
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100139/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100140 * cli_io_handler()
141 * -> stats_dump_sess_to_buffer() // "show sess"
142 * -> stats_dump_errors_to_buffer() // "show errors"
143 * -> stats_dump_info_to_buffer() // "show info"
144 * -> stats_dump_stat_to_buffer() // "show stat"
145 * -> stats_dump_csv_header()
146 * -> stats_dump_proxy_to_buffer()
147 * -> stats_dump_fe_stats()
148 * -> stats_dump_li_stats()
149 * -> stats_dump_sv_stats()
150 * -> stats_dump_be_stats()
151 *
152 * http_stats_io_handler()
153 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
154 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
155 * -> stats_dump_html_head() // emits the HTML headers
156 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
157 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
158 * -> stats_dump_html_px_hdr()
159 * -> stats_dump_fe_stats()
160 * -> stats_dump_li_stats()
161 * -> stats_dump_sv_stats()
162 * -> stats_dump_be_stats()
163 * -> stats_dump_html_px_end()
164 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100165 */
166
Willy Tarreau30576452015-04-13 13:50:30 +0200167static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900168
169static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200170 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200171 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200172 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200173 " help : this message\n"
174 " prompt : toggle interactive mode with prompt\n"
175 " quit : disconnect\n"
176 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100177 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200178 " show stat : report counters for each proxy and server\n"
179 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100180 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200181 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200182 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200183 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200184 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200185 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100186 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200187 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200188 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200189 " disable : put a server or frontend in maintenance mode\n"
190 " enable : re-enable a server or frontend which is in maintenance mode\n"
191 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100192 " show acl [id] : report avalaible acls or dump an acl's contents\n"
193 " get acl : reports the patterns matching a sample for an ACL\n"
194 " add acl : add acl entry\n"
195 " del acl : delete acl entry\n"
196 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100197 " show map [id] : report avalaible maps or dump a map's contents\n"
198 " get map : reports the keys and values matching a sample for a map\n"
199 " set map : modify map entry\n"
200 " add map : add map entry\n"
201 " del map : delete map entry\n"
202 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200203 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200204 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200205
Simon Horman9bd2c732011-06-15 15:18:44 +0900206static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200207 "Permission denied\n"
208 "";
209
Willy Tarreau295a8372011-03-10 11:25:07 +0100210/* data transmission states for the stats responses */
211enum {
212 STAT_ST_INIT = 0,
213 STAT_ST_HEAD,
214 STAT_ST_INFO,
215 STAT_ST_LIST,
216 STAT_ST_END,
217 STAT_ST_FIN,
218};
219
220/* data transmission states for the stats responses inside a proxy */
221enum {
222 STAT_PX_ST_INIT = 0,
223 STAT_PX_ST_TH,
224 STAT_PX_ST_FE,
225 STAT_PX_ST_LI,
226 STAT_PX_ST_SV,
227 STAT_PX_ST_BE,
228 STAT_PX_ST_END,
229 STAT_PX_ST_FIN,
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232extern const char *stat_status_codes[];
233
Willy Tarreau07e9e642010-08-17 21:48:17 +0200234/* allocate a new stats frontend named <name>, and return it
235 * (or NULL in case of lack of memory).
236 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200237static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200238{
239 struct proxy *fe;
240
241 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
242 if (!fe)
243 return NULL;
244
Willy Tarreau237250c2011-07-29 01:49:03 +0200245 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200246 fe->next = proxy;
247 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200248 fe->last_change = now.tv_sec;
249 fe->id = strdup("GLOBAL");
250 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200251 fe->maxconn = 10; /* default to 10 concurrent connections */
252 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200253 fe->conf.file = strdup(file);
254 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100255 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100256 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200257
258 /* the stats frontend is the only one able to assign ID #0 */
259 fe->conf.id.key = fe->uuid = 0;
260 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200261 return fe;
262}
263
Willy Tarreaufbee7132007-10-18 13:53:22 +0200264/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200265 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
266 * error message into the <err> buffer which will be preallocated. The trailing
267 * '\n' must not be written. The function must be called with <args> pointing to
268 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200270static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200271 struct proxy *defpx, const char *file, int line,
272 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200274 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200275 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200276
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200277 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 int cur_arg;
279
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200281 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 +0200282 return -1;
283 }
284
Willy Tarreau89a63132009-08-16 17:41:45 +0200285 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200286 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200288 return -1;
289 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200290 }
291
Willy Tarreau4348fad2012-09-20 16:48:07 +0200292 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200293 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200294
Willy Tarreauc53d4222012-09-20 20:19:28 +0200295 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
296 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
297 file, line, args[0], args[1], err && *err ? *err : "error");
298 return -1;
299 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200303 static int bind_dumped;
304 struct bind_kw *kw;
305
306 kw = bind_find_kw(args[cur_arg]);
307 if (kw) {
308 if (!kw->parse) {
309 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
310 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 return -1;
312 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200313
314 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
315 if (err && *err)
316 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
317 else
318 memprintf(err, "'%s %s' : error encountered while processing '%s'",
319 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200320 return -1;
321 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200322
323 cur_arg += 1 + kw->skip;
324 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200325 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200326
327 if (!bind_dumped) {
328 bind_dump_kws(err);
329 indent_msg(err, 4);
330 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200332
333 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
334 args[0], args[1], args[cur_arg],
335 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
336 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200337 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100338
Willy Tarreauc53d4222012-09-20 20:19:28 +0200339 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
340 l->maxconn = global.stats_fe->maxconn;
341 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200342 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200343 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100344 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200345 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
346 l->nice = -64; /* we want to boost priority for local stats */
347 global.maxsock += l->maxconn;
348 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200349 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100351 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200352 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100353
354 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200355 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100356 return -1;
357 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200358
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100359 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200361 return -1;
362 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200363 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200364 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200365 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200366 return -1;
367 }
368 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200369 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200370 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200371 else if (!strcmp(args[1], "maxconn")) {
372 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200373
374 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200375 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200376 return -1;
377 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200378
379 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200380 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200381 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200382 return -1;
383 }
384 }
385 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200386 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200387 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
388 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100389 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200390
Willy Tarreau91319572013-04-20 09:48:50 +0200391 if (!global.stats_fe) {
392 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
393 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
394 return -1;
395 }
396 }
397
Willy Tarreau35b7b162012-10-22 23:17:18 +0200398 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100399 unsigned int low, high;
400
Willy Tarreau35b7b162012-10-22 23:17:18 +0200401 if (strcmp(args[cur_arg], "all") == 0) {
402 set = 0;
403 break;
404 }
405 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100406 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200407 }
408 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100409 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200410 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100411 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100412 char *dash = strchr(args[cur_arg], '-');
413
414 low = high = str2uic(args[cur_arg]);
415 if (dash)
416 high = str2uic(dash + 1);
417
418 if (high < low) {
419 unsigned int swap = low;
420 low = high;
421 high = swap;
422 }
423
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100424 if (low < 1 || high > LONGBITS) {
425 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
426 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200427 return -1;
428 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100429 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100430 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100431 }
432 else {
433 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100434 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
435 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100436 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200437 }
438 cur_arg++;
439 }
440 global.stats_fe->bind_proc = set;
441 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200442 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200443 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200444 return -1;
445 }
446 return 0;
447}
448
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100449/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
450 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100451 * NOTE: Some tools happen to rely on the field position instead of its name,
452 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100453 */
454static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100455{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100456 chunk_appendf(&trash,
457 "# pxname,svname,"
458 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100459 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100460 "bin,bout,"
461 "dreq,dresp,"
462 "ereq,econ,eresp,"
463 "wretr,wredis,"
464 "status,weight,act,bck,"
465 "chkfail,chkdown,lastchg,downtime,qlimit,"
466 "pid,iid,sid,throttle,lbtot,tracked,type,"
467 "rate,rate_lim,rate_max,"
468 "check_status,check_code,check_duration,"
469 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
470 "req_rate,req_rate_max,req_tot,"
471 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200472 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100473 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100474}
475
Simon Hormand9366582011-06-15 15:18:45 +0900476/* print a string of text buffer to <out>. The format is :
477 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
478 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
479 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
480 */
481static int dump_text(struct chunk *out, const char *buf, int bsize)
482{
483 unsigned char c;
484 int ptr = 0;
485
486 while (buf[ptr] && ptr < bsize) {
487 c = buf[ptr];
488 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
489 if (out->len > out->size - 1)
490 break;
491 out->str[out->len++] = c;
492 }
493 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
494 if (out->len > out->size - 2)
495 break;
496 out->str[out->len++] = '\\';
497 switch (c) {
498 case ' ': c = ' '; break;
499 case '\t': c = 't'; break;
500 case '\n': c = 'n'; break;
501 case '\r': c = 'r'; break;
502 case '\e': c = 'e'; break;
503 case '\\': c = '\\'; break;
504 }
505 out->str[out->len++] = c;
506 }
507 else {
508 if (out->len > out->size - 4)
509 break;
510 out->str[out->len++] = '\\';
511 out->str[out->len++] = 'x';
512 out->str[out->len++] = hextab[(c >> 4) & 0xF];
513 out->str[out->len++] = hextab[c & 0xF];
514 }
515 ptr++;
516 }
517
518 return ptr;
519}
520
521/* print a buffer in hexa.
522 * Print stopped if <bsize> is reached, or if no more place in the chunk.
523 */
524static int dump_binary(struct chunk *out, const char *buf, int bsize)
525{
526 unsigned char c;
527 int ptr = 0;
528
529 while (ptr < bsize) {
530 c = buf[ptr];
531
532 if (out->len > out->size - 2)
533 break;
534 out->str[out->len++] = hextab[(c >> 4) & 0xF];
535 out->str[out->len++] = hextab[c & 0xF];
536
537 ptr++;
538 }
539 return ptr;
540}
541
542/* Dump the status of a table to a stream interface's
543 * read buffer. It returns 0 if the output buffer is full
544 * and needs to be called again, otherwise non-zero.
545 */
546static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
547 struct proxy *proxy, struct proxy *target)
548{
Willy Tarreau87b09662015-04-03 00:22:06 +0200549 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900550
Willy Tarreau77804732012-10-29 16:14:26 +0100551 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900552 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
553
554 /* any other information should be dumped here */
555
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200556 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100557 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900558
Willy Tarreaubc18da12015-03-13 14:00:47 +0100559 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200560 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900561 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100562 }
Simon Hormand9366582011-06-15 15:18:45 +0900563
564 return 1;
565}
566
567/* Dump the a table entry to a stream interface's
568 * read buffer. It returns 0 if the output buffer is full
569 * and needs to be called again, otherwise non-zero.
570 */
571static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
572 struct proxy *proxy, struct stksess *entry)
573{
574 int dt;
575
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900577
578 if (proxy->table.type == STKTABLE_TYPE_IP) {
579 char addr[INET_ADDRSTRLEN];
580 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100581 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900582 }
583 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
584 char addr[INET6_ADDRSTRLEN];
585 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100586 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900587 }
588 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100589 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900590 }
591 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100592 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900593 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
594 }
595 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100596 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900597 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
598 }
599
Willy Tarreau77804732012-10-29 16:14:26 +0100600 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900601
602 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
603 void *ptr;
604
605 if (proxy->table.data_ofs[dt] == 0)
606 continue;
607 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900609 else
Willy Tarreau77804732012-10-29 16:14:26 +0100610 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900611
612 ptr = stktable_data_ptr(&proxy->table, entry, dt);
613 switch (stktable_data_types[dt].std_type) {
614 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100615 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900616 break;
617 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100618 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900619 break;
620 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100621 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900622 break;
623 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100624 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900625 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
626 proxy->table.data_arg[dt].u));
627 break;
628 }
629 }
Willy Tarreau77804732012-10-29 16:14:26 +0100630 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900631
Willy Tarreaubc18da12015-03-13 14:00:47 +0100632 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200633 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900634 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100635 }
Simon Hormand9366582011-06-15 15:18:45 +0900636
637 return 1;
638}
639
Willy Tarreaudec98142012-06-06 23:37:08 +0200640static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900641{
Willy Tarreau87b09662015-04-03 00:22:06 +0200642 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100643 struct appctx *appctx = __objt_appctx(si->end);
644 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900645 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900646 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900647 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200648 long long value;
649 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200650 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200651 void *ptr;
652 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900653
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100654 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900655
656 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100657 appctx->ctx.cli.msg = "Key value expected\n";
658 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900659 return;
660 }
661
Simon Hormanc5b89f62011-06-15 15:18:50 +0900662 switch (px->table.type) {
663 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900664 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100665 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900666 break;
667 case STKTABLE_TYPE_IPV6:
668 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100669 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900670 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900671 case STKTABLE_TYPE_INTEGER:
672 {
673 char *endptr;
674 unsigned long val;
675 errno = 0;
676 val = strtoul(args[4], &endptr, 10);
677 if ((errno == ERANGE && val == ULONG_MAX) ||
678 (errno != 0 && val == 0) || endptr == args[4] ||
679 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100680 appctx->ctx.cli.msg = "Invalid key\n";
681 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900682 return;
683 }
684 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100685 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900686 break;
687 }
688 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900689 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100690 static_table_key->key = args[4];
691 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900692 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900693 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200694 switch (action) {
695 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 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 +0200697 break;
698 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100699 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 +0200700 break;
701 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100702 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200703 break;
704 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100705 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900706 return;
707 }
708
709 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200710 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100711 appctx->ctx.cli.msg = stats_permission_denied_msg;
712 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900713 return;
714 }
715
Willy Tarreau07115412012-10-29 21:56:59 +0100716 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900717
Willy Tarreaudec98142012-06-06 23:37:08 +0200718 switch (action) {
719 case STAT_CLI_O_TAB:
720 if (!ts)
721 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100722 chunk_reset(&trash);
723 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900724 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100725 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900726 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200727
728 case STAT_CLI_O_CLR:
729 if (!ts)
730 return;
731 if (ts->ref_cnt) {
732 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100733 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
734 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200735 return;
736 }
737 stksess_kill(&px->table, ts);
738 break;
Simon Horman17bce342011-06-15 15:18:47 +0900739
Willy Tarreau654694e2012-06-07 01:03:16 +0200740 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200741 if (ts)
742 stktable_touch(&px->table, ts, 1);
743 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100744 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200745 if (!ts) {
746 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100747 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
748 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200749 return;
750 }
751 stktable_store(&px->table, ts, 1);
752 }
753
Willy Tarreau47060b62013-08-01 21:11:42 +0200754 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
755 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100756 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
757 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200758 return;
759 }
760
761 data_type = stktable_get_data_type(args[cur_arg] + 5);
762 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100763 appctx->ctx.cli.msg = "Unknown data type\n";
764 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200765 return;
766 }
767
768 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100769 appctx->ctx.cli.msg = "Data type not stored in this table\n";
770 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200771 return;
772 }
773
774 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100775 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
776 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200777 return;
778 }
779
780 ptr = stktable_data_ptr(&px->table, ts, data_type);
781
782 switch (stktable_data_types[data_type].std_type) {
783 case STD_T_SINT:
784 stktable_data_cast(ptr, std_t_sint) = value;
785 break;
786 case STD_T_UINT:
787 stktable_data_cast(ptr, std_t_uint) = value;
788 break;
789 case STD_T_ULL:
790 stktable_data_cast(ptr, std_t_ull) = value;
791 break;
792 case STD_T_FRQP:
793 /* We set both the current and previous values. That way
794 * the reported frequency is stable during all the period
795 * then slowly fades out. This allows external tools to
796 * push measures without having to update them too often.
797 */
798 frqp = &stktable_data_cast(ptr, std_t_frqp);
799 frqp->curr_tick = now_ms;
800 frqp->prev_ctr = 0;
801 frqp->curr_ctr = value;
802 break;
803 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200804 }
805 break;
806
Willy Tarreaudec98142012-06-06 23:37:08 +0200807 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 appctx->ctx.cli.msg = "Unknown action\n";
809 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200810 break;
Simon Horman121f3052011-06-15 15:18:46 +0900811 }
Simon Horman121f3052011-06-15 15:18:46 +0900812}
813
Willy Tarreau654694e2012-06-07 01:03:16 +0200814static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900815{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100816 struct appctx *appctx = __objt_appctx(si->end);
817
Willy Tarreau04b3a192013-04-13 09:41:37 +0200818 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100819 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
820 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200821 return;
822 }
823
Simon Hormand5b9fd92011-06-15 15:18:48 +0900824 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100825 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
826 if (appctx->ctx.table.data_type < 0) {
827 appctx->ctx.cli.msg = "Unknown data type\n";
828 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900829 return;
830 }
831
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100832 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
833 appctx->ctx.cli.msg = "Data type not stored in this table\n";
834 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900835 return;
836 }
837
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 appctx->ctx.table.data_op = get_std_op(args[4]);
839 if (appctx->ctx.table.data_op < 0) {
840 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
841 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900842 return;
843 }
844
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100845 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
846 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
847 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900848 return;
849 }
850}
851
Willy Tarreaudec98142012-06-06 23:37:08 +0200852static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900853{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100854 struct appctx *appctx = __objt_appctx(si->end);
855
856 appctx->ctx.table.data_type = -1;
857 appctx->st2 = STAT_ST_INIT;
858 appctx->ctx.table.target = NULL;
859 appctx->ctx.table.proxy = NULL;
860 appctx->ctx.table.entry = NULL;
861 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900862
863 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 appctx->ctx.table.target = find_stktable(args[2]);
865 if (!appctx->ctx.table.target) {
866 appctx->ctx.cli.msg = "No such table\n";
867 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900868 return;
869 }
870 }
871 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200872 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900873 goto err_args;
874 return;
875 }
876
877 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200878 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900879 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200880 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900881 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900882 goto err_args;
883
884 return;
885
886err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200887 switch (action) {
888 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100889 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 +0200890 break;
891 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100892 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 +0200893 break;
894 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100895 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200896 break;
897 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100898 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900899}
900
Willy Tarreau532a4502011-09-07 22:37:44 +0200901/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200902 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200903 * level to be admin.
904 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200905static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200906{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200908 struct proxy *px;
909
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200910 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 appctx->ctx.cli.msg = stats_permission_denied_msg;
912 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200913 return NULL;
914 }
915
916 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->ctx.cli.msg = "A frontend name is expected.\n";
918 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200919 return NULL;
920 }
921
922 px = findproxy(arg, PR_CAP_FE);
923 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100924 appctx->ctx.cli.msg = "No such frontend.\n";
925 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200926 return NULL;
927 }
928 return px;
929}
930
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200931/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
932 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200933 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200934 * the <arg> is modified to remove the '/'.
935 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200936static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200937{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100938 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200939 struct proxy *px;
940 struct server *sv;
941 char *line;
942
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200943 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100944 appctx->ctx.cli.msg = stats_permission_denied_msg;
945 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200946 return NULL;
947 }
948
949 /* split "backend/server" and make <line> point to server */
950 for (line = arg; *line; line++)
951 if (*line == '/') {
952 *line++ = '\0';
953 break;
954 }
955
956 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100957 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
958 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200959 return NULL;
960 }
961
962 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100963 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
964 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200965 return NULL;
966 }
967
968 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100969 appctx->ctx.cli.msg = "Proxy is disabled.\n";
970 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200971 return NULL;
972 }
973
974 return sv;
975}
976
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100977/* This function is used with map and acl management. It permits to browse
978 * each reference. The variable <getnext> must contain the current node,
979 * <end> point to the root node and the <flags> permit to filter required
980 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100981 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100982static inline
983struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
984 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100985{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100986 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100987
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100988 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100989
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100990 /* Get next list entry. */
991 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100992
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100993 /* If the entry is the last of the list, return NULL. */
994 if (&ref->list == end)
995 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100996
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100997 /* If the entry match the flag, return it. */
998 if (ref->flags & flags)
999 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001000 }
1001}
1002
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001003static inline
1004struct pat_ref *pat_ref_lookup_ref(const char *reference)
1005{
1006 int id;
1007 char *error;
1008
1009 /* If the reference starts by a '#', this is numeric id. */
1010 if (reference[0] == '#') {
1011 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1012 id = strtol(reference + 1, &error, 10);
1013 if (*error != '\0')
1014 return NULL;
1015
1016 /* Perform the unique id lookup. */
1017 return pat_ref_lookupid(id);
1018 }
1019
1020 /* Perform the string lookup. */
1021 return pat_ref_lookup(reference);
1022}
1023
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001024/* This function is used with map and acl management. It permits to browse
1025 * each reference.
1026 */
1027static inline
1028struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1029{
1030 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001031 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1032 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001033 return NULL;
1034 return expr;
1035}
1036
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001037/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001038 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001039 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001040 * designating the function which will have to process the request, which can
1041 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001042 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001043static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001044{
Willy Tarreau87b09662015-04-03 00:22:06 +02001045 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001046 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047 char *args[MAX_STATS_ARGS + 1];
1048 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001049 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001050
1051 while (isspace((unsigned char)*line))
1052 line++;
1053
1054 arg = 0;
1055 args[arg] = line;
1056
1057 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001058 if (*line == '\\') {
1059 line++;
1060 if (*line == '\0')
1061 break;
1062 }
1063 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001064 *line++ = '\0';
1065
1066 while (isspace((unsigned char)*line))
1067 line++;
1068
1069 args[++arg] = line;
1070 continue;
1071 }
1072
1073 line++;
1074 }
1075
1076 while (++arg <= MAX_STATS_ARGS)
1077 args[arg] = line;
1078
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001079 /* remove \ */
1080 arg = 0;
1081 while (*args[arg] != '\0') {
1082 j = 0;
1083 for (i=0; args[arg][i] != '\0'; i++) {
1084 if (args[arg][i] == '\\')
1085 continue;
1086 args[arg][j] = args[arg][i];
1087 j++;
1088 }
1089 args[arg][j] = '\0';
1090 arg++;
1091 }
1092
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001093 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094 if (strcmp(args[0], "show") == 0) {
1095 if (strcmp(args[1], "stat") == 0) {
1096 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001097 appctx->ctx.stats.flags |= STAT_BOUND;
1098 appctx->ctx.stats.iid = atoi(args[2]);
1099 appctx->ctx.stats.type = atoi(args[3]);
1100 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001101 }
1102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001103 appctx->st2 = STAT_ST_INIT;
1104 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001105 }
1106 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001107 appctx->st2 = STAT_ST_INIT;
1108 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001109 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001110 else if (strcmp(args[1], "pools") == 0) {
1111 appctx->st2 = STAT_ST_INIT;
1112 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1113 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001114 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001116 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001117 appctx->ctx.cli.msg = stats_permission_denied_msg;
1118 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001119 return 1;
1120 }
Willy Tarreau76153662012-11-26 01:16:39 +01001121 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001123 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001124 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001125 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001127 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001128 appctx->ctx.sess.pos = 0;
1129 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001130 }
1131 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001132 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = stats_permission_denied_msg;
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001135 return 1;
1136 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001137 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001138 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001139 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001140 appctx->ctx.errors.iid = -1;
1141 appctx->ctx.errors.px = NULL;
1142 appctx->st2 = STAT_ST_INIT;
1143 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001144 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001145 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001146 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001147 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001148 else if (strcmp(args[1], "map") == 0 ||
1149 strcmp(args[1], "acl") == 0) {
1150
1151 /* Set ACL or MAP flags. */
1152 if (args[1][0] == 'm')
1153 appctx->ctx.map.display_flags = PAT_REF_MAP;
1154 else
1155 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001156
1157 /* no parameter: display all map avalaible */
1158 if (!*args[2]) {
1159 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001160 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001161 return 1;
1162 }
1163
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001164 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001165 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001166 if (!appctx->ctx.map.ref ||
1167 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1168 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001169 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001170 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001171 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001172 appctx->st0 = STAT_CLI_PRINT;
1173 return 1;
1174 }
1175 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001176 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001177 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001178 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001179 return 0;
1180 }
1181 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001182 else if (strcmp(args[0], "clear") == 0) {
1183 if (strcmp(args[1], "counters") == 0) {
1184 struct proxy *px;
1185 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001186 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001187 int clrall = 0;
1188
1189 if (strcmp(args[2], "all") == 0)
1190 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001191
Willy Tarreau6162db22009-10-10 17:13:00 +02001192 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001193 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1194 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001195 appctx->ctx.cli.msg = stats_permission_denied_msg;
1196 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001197 return 1;
1198 }
1199
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001200 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001201 if (clrall) {
1202 memset(&px->be_counters, 0, sizeof(px->be_counters));
1203 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1204 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001205 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001206 px->be_counters.conn_max = 0;
1207 px->be_counters.p.http.rps_max = 0;
1208 px->be_counters.sps_max = 0;
1209 px->be_counters.cps_max = 0;
1210 px->be_counters.nbpend_max = 0;
1211
1212 px->fe_counters.conn_max = 0;
1213 px->fe_counters.p.http.rps_max = 0;
1214 px->fe_counters.sps_max = 0;
1215 px->fe_counters.cps_max = 0;
1216 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001217 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001218
1219 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001220 if (clrall)
1221 memset(&sv->counters, 0, sizeof(sv->counters));
1222 else {
1223 sv->counters.cur_sess_max = 0;
1224 sv->counters.nbpend_max = 0;
1225 sv->counters.sps_max = 0;
1226 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001227
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001228 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001229 if (li->counters) {
1230 if (clrall)
1231 memset(li->counters, 0, sizeof(*li->counters));
1232 else
1233 li->counters->conn_max = 0;
1234 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001235 }
1236
Willy Tarreau81c25d02011-09-07 15:17:21 +02001237 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001238 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001239 return 1;
1240 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001241 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001242 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001243 /* end of processing */
1244 return 1;
1245 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001246 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1247 /* Set ACL or MAP flags. */
1248 if (args[1][0] == 'm')
1249 appctx->ctx.map.display_flags = PAT_REF_MAP;
1250 else
1251 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001252
1253 /* no parameter */
1254 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001255 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1256 appctx->ctx.cli.msg = "Missing map identifier.\n";
1257 else
1258 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001259 appctx->st0 = STAT_CLI_PRINT;
1260 return 1;
1261 }
1262
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001263 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001264 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001265 if (!appctx->ctx.map.ref ||
1266 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1267 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001268 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001269 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001270 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001271 appctx->st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001275 /* Clear all. */
1276 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001277
1278 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001279 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001280 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001281 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001282 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001283 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001284 return 0;
1285 }
1286 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001287 else if (strcmp(args[0], "get") == 0) {
1288 if (strcmp(args[1], "weight") == 0) {
1289 struct proxy *px;
1290 struct server *sv;
1291
1292 /* split "backend/server" and make <line> point to server */
1293 for (line = args[2]; *line; line++)
1294 if (*line == '/') {
1295 *line++ = '\0';
1296 break;
1297 }
1298
1299 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001300 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1301 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001302 return 1;
1303 }
1304
1305 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001306 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1307 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001308 return 1;
1309 }
1310
1311 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001312 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001313 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001314 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001315
Willy Tarreau38338fa2009-10-10 18:37:29 +02001316 return 1;
1317 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001318 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1319 /* Set flags. */
1320 if (args[1][0] == 'm')
1321 appctx->ctx.map.display_flags = PAT_REF_MAP;
1322 else
1323 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001324
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001325 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001326 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001327 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1328 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1329 else
1330 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001331 appctx->st0 = STAT_CLI_PRINT;
1332 return 1;
1333 }
1334
1335 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001336 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001337 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001338 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001339 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001340 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001341 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001342 appctx->st0 = STAT_CLI_PRINT;
1343 return 1;
1344 }
1345
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001346 /* copy input string. The string must be allocated because
1347 * it may be used over multiple iterations. It's released
1348 * at the end and upon abort anyway.
1349 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001350 appctx->ctx.map.chunk.len = strlen(args[3]);
1351 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1352 appctx->ctx.map.chunk.str = strdup(args[3]);
1353 if (!appctx->ctx.map.chunk.str) {
1354 appctx->ctx.cli.msg = "Out of memory error.\n";
1355 appctx->st0 = STAT_CLI_PRINT;
1356 return 1;
1357 }
1358
1359 /* prepare response */
1360 appctx->st2 = STAT_ST_INIT;
1361 appctx->st0 = STAT_CLI_O_MLOOK;
1362 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001363 else { /* not "get weight" */
1364 return 0;
1365 }
1366 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001367 else if (strcmp(args[0], "set") == 0) {
1368 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001369 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001370 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001371
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001372 sv = expect_server_admin(s, si, args[2]);
1373 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001374 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001375
Simon Horman7d09b9a2013-02-12 10:45:51 +09001376 warning = server_parse_weight_change_request(sv, args[3]);
1377 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001378 appctx->ctx.cli.msg = warning;
1379 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001380 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001381 return 1;
1382 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001383 else if (strcmp(args[1], "server") == 0) {
1384 struct server *sv;
1385 const char *warning;
1386
1387 sv = expect_server_admin(s, si, args[2]);
1388 if (!sv)
1389 return 1;
1390
1391 if (strcmp(args[3], "weight") == 0) {
1392 warning = server_parse_weight_change_request(sv, args[4]);
1393 if (warning) {
1394 appctx->ctx.cli.msg = warning;
1395 appctx->st0 = STAT_CLI_PRINT;
1396 }
1397 }
1398 else if (strcmp(args[3], "state") == 0) {
1399 if (strcmp(args[4], "ready") == 0)
1400 srv_adm_set_ready(sv);
1401 else if (strcmp(args[4], "drain") == 0)
1402 srv_adm_set_drain(sv);
1403 else if (strcmp(args[4], "maint") == 0)
1404 srv_adm_set_maint(sv);
1405 else {
1406 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1407 appctx->st0 = STAT_CLI_PRINT;
1408 }
1409 }
1410 else if (strcmp(args[3], "health") == 0) {
1411 if (sv->track) {
1412 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1413 appctx->st0 = STAT_CLI_PRINT;
1414 }
1415 else if (strcmp(args[4], "up") == 0) {
1416 sv->check.health = sv->check.rise + sv->check.fall - 1;
1417 srv_set_running(sv, "changed from CLI");
1418 }
1419 else if (strcmp(args[4], "stopping") == 0) {
1420 sv->check.health = sv->check.rise + sv->check.fall - 1;
1421 srv_set_stopping(sv, "changed from CLI");
1422 }
1423 else if (strcmp(args[4], "down") == 0) {
1424 sv->check.health = 0;
1425 srv_set_stopped(sv, "changed from CLI");
1426 }
1427 else {
1428 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1429 appctx->st0 = STAT_CLI_PRINT;
1430 }
1431 }
1432 else if (strcmp(args[3], "agent") == 0) {
1433 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1434 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1435 appctx->st0 = STAT_CLI_PRINT;
1436 }
1437 else if (strcmp(args[4], "up") == 0) {
1438 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1439 srv_set_running(sv, "changed from CLI");
1440 }
1441 else if (strcmp(args[4], "down") == 0) {
1442 sv->agent.health = 0;
1443 srv_set_stopped(sv, "changed from CLI");
1444 }
1445 else {
1446 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
1448 }
1449 }
1450 else {
1451 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1452 appctx->st0 = STAT_CLI_PRINT;
1453 }
1454 return 1;
1455 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001456 else if (strcmp(args[1], "timeout") == 0) {
1457 if (strcmp(args[2], "cli") == 0) {
1458 unsigned timeout;
1459 const char *res;
1460
1461 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001462 appctx->ctx.cli.msg = "Expects an integer value.\n";
1463 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001464 return 1;
1465 }
1466
1467 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1468 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001469 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1470 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001471 return 1;
1472 }
1473
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001474 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001475 return 1;
1476 }
1477 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001478 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1479 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001480 return 1;
1481 }
1482 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001483 else if (strcmp(args[1], "maxconn") == 0) {
1484 if (strcmp(args[2], "frontend") == 0) {
1485 struct proxy *px;
1486 struct listener *l;
1487 int v;
1488
Willy Tarreau532a4502011-09-07 22:37:44 +02001489 px = expect_frontend_admin(s, si, args[3]);
1490 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001491 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001492
1493 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 appctx->ctx.cli.msg = "Integer value expected.\n";
1495 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001496 return 1;
1497 }
1498
1499 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001500 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001501 appctx->ctx.cli.msg = "Value out of range.\n";
1502 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001503 return 1;
1504 }
1505
1506 /* OK, the value is fine, so we assign it to the proxy and to all of
1507 * its listeners. The blocked ones will be dequeued.
1508 */
1509 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001510 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001511 l->maxconn = v;
1512 if (l->state == LI_FULL)
1513 resume_listener(l);
1514 }
1515
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001516 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1517 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001518
1519 return 1;
1520 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001521 else if (strcmp(args[2], "global") == 0) {
1522 int v;
1523
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001524 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.cli.msg = stats_permission_denied_msg;
1526 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001527 return 1;
1528 }
1529
1530 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001531 appctx->ctx.cli.msg = "Expects an integer value.\n";
1532 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001533 return 1;
1534 }
1535
1536 v = atoi(args[3]);
1537 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001538 appctx->ctx.cli.msg = "Value out of range.\n";
1539 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001540 return 1;
1541 }
1542
1543 /* check for unlimited values */
1544 if (v <= 0)
1545 v = global.hardmaxconn;
1546
1547 global.maxconn = v;
1548
1549 /* Dequeues all of the listeners waiting for a resource */
1550 if (!LIST_ISEMPTY(&global_listener_queue))
1551 dequeue_all_listeners(&global_listener_queue);
1552
1553 return 1;
1554 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001555 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1557 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001558 return 1;
1559 }
1560 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001561 else if (strcmp(args[1], "rate-limit") == 0) {
1562 if (strcmp(args[2], "connections") == 0) {
1563 if (strcmp(args[3], "global") == 0) {
1564 int v;
1565
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001566 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001567 appctx->ctx.cli.msg = stats_permission_denied_msg;
1568 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001569 return 1;
1570 }
1571
1572 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001573 appctx->ctx.cli.msg = "Expects an integer value.\n";
1574 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001575 return 1;
1576 }
1577
1578 v = atoi(args[4]);
1579 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001580 appctx->ctx.cli.msg = "Value out of range.\n";
1581 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001582 return 1;
1583 }
1584
1585 global.cps_lim = v;
1586
1587 /* Dequeues all of the listeners waiting for a resource */
1588 if (!LIST_ISEMPTY(&global_listener_queue))
1589 dequeue_all_listeners(&global_listener_queue);
1590
1591 return 1;
1592 }
1593 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001596 return 1;
1597 }
1598 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001599 else if (strcmp(args[2], "sessions") == 0) {
1600 if (strcmp(args[3], "global") == 0) {
1601 int v;
1602
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001603 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001604 appctx->ctx.cli.msg = stats_permission_denied_msg;
1605 appctx->st0 = STAT_CLI_PRINT;
1606 return 1;
1607 }
1608
1609 if (!*args[4]) {
1610 appctx->ctx.cli.msg = "Expects an integer value.\n";
1611 appctx->st0 = STAT_CLI_PRINT;
1612 return 1;
1613 }
1614
1615 v = atoi(args[4]);
1616 if (v < 0) {
1617 appctx->ctx.cli.msg = "Value out of range.\n";
1618 appctx->st0 = STAT_CLI_PRINT;
1619 return 1;
1620 }
1621
1622 global.sps_lim = v;
1623
1624 /* Dequeues all of the listeners waiting for a resource */
1625 if (!LIST_ISEMPTY(&global_listener_queue))
1626 dequeue_all_listeners(&global_listener_queue);
1627
1628 return 1;
1629 }
1630 else {
1631 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1632 appctx->st0 = STAT_CLI_PRINT;
1633 return 1;
1634 }
1635 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001636#ifdef USE_OPENSSL
1637 else if (strcmp(args[2], "ssl-sessions") == 0) {
1638 if (strcmp(args[3], "global") == 0) {
1639 int v;
1640
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001641 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001642 appctx->ctx.cli.msg = stats_permission_denied_msg;
1643 appctx->st0 = STAT_CLI_PRINT;
1644 return 1;
1645 }
1646
1647 if (!*args[4]) {
1648 appctx->ctx.cli.msg = "Expects an integer value.\n";
1649 appctx->st0 = STAT_CLI_PRINT;
1650 return 1;
1651 }
1652
1653 v = atoi(args[4]);
1654 if (v < 0) {
1655 appctx->ctx.cli.msg = "Value out of range.\n";
1656 appctx->st0 = STAT_CLI_PRINT;
1657 return 1;
1658 }
1659
1660 global.ssl_lim = v;
1661
1662 /* Dequeues all of the listeners waiting for a resource */
1663 if (!LIST_ISEMPTY(&global_listener_queue))
1664 dequeue_all_listeners(&global_listener_queue);
1665
1666 return 1;
1667 }
1668 else {
1669 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1670 appctx->st0 = STAT_CLI_PRINT;
1671 return 1;
1672 }
1673 }
1674#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001675 else if (strcmp(args[2], "http-compression") == 0) {
1676 if (strcmp(args[3], "global") == 0) {
1677 int v;
1678
Willy Tarreau85d47f92012-11-21 00:29:50 +01001679 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001680 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001682 return 1;
1683 }
1684
William Lallemandd85f9172012-11-09 17:05:39 +01001685 v = atoi(args[4]);
1686 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1687 }
1688 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001689 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1690 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001691 return 1;
1692 }
1693 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001694 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001695 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001696 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001697 return 1;
1698 }
1699 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001700 else if (strcmp(args[1], "table") == 0) {
1701 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1702 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001703 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001704 char *err;
1705
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001706 /* Set flags. */
1707 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001708
1709 /* Expect three parameters: map name, key and new value. */
1710 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001711 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001712 appctx->st0 = STAT_CLI_PRINT;
1713 return 1;
1714 }
1715
1716 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001717 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001718 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001719 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001720 appctx->st0 = STAT_CLI_PRINT;
1721 return 1;
1722 }
1723
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001724 /* If the entry identifier start with a '#', it is considered as
1725 * pointer id
1726 */
1727 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1728 struct pat_ref_elt *ref;
1729 long long int conv;
1730 char *error;
1731
1732 /* Convert argument to integer value. */
1733 conv = strtoll(&args[3][1], &error, 16);
1734 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001735 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001736 appctx->st0 = STAT_CLI_PRINT;
1737 return 1;
1738 }
1739
1740 /* Convert and check integer to pointer. */
1741 ref = (struct pat_ref_elt *)(long)conv;
1742 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001743 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001744 appctx->st0 = STAT_CLI_PRINT;
1745 return 1;
1746 }
1747
1748 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001749 err = NULL;
1750 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1751 if (err)
1752 memprintf(&err, "%s.\n", err);
1753 appctx->ctx.cli.err = err;
1754 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001755 return 1;
1756 }
1757 }
1758 else {
1759 /* Else, use the entry identifier as pattern
1760 * string, and update the value.
1761 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001762 err = NULL;
1763 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1764 if (err)
1765 memprintf(&err, "%s.\n", err);
1766 appctx->ctx.cli.err = err;
1767 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001768 return 1;
1769 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001770 }
1771
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001772 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001773 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001774 return 1;
1775 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001776#ifdef USE_OPENSSL
1777 else if (strcmp(args[1], "ssl") == 0) {
1778 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001779#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001780 char *err = NULL;
1781
Emeric Brunaf4ef742014-06-19 14:10:45 +02001782 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001783 if (!*args[3]) {
1784 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1785 appctx->st0 = STAT_CLI_PRINT;
1786 return 1;
1787 }
1788
1789 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1790 if (trash.len < 0) {
1791 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
1793 return 1;
1794 }
1795
1796 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1797 if (err) {
1798 memprintf(&err, "%s.\n", err);
1799 appctx->ctx.cli.err = err;
1800 appctx->st0 = STAT_CLI_PRINT_FREE;
1801 }
1802 return 1;
1803 }
1804 appctx->ctx.cli.msg = "OCSP Response updated!";
1805 appctx->st0 = STAT_CLI_PRINT;
1806 return 1;
1807#else
1808 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
1810 return 1;
1811#endif
1812 }
1813 else {
1814 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1815 appctx->st0 = STAT_CLI_PRINT;
1816 return 1;
1817 }
1818 }
1819#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001820 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001821 return 0;
1822 }
1823 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001824 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001825 if (strcmp(args[1], "agent") == 0) {
1826 struct server *sv;
1827
1828 sv = expect_server_admin(s, si, args[2]);
1829 if (!sv)
1830 return 1;
1831
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001832 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1833 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1834 appctx->st0 = STAT_CLI_PRINT;
1835 return 1;
1836 }
1837
1838 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001839 return 1;
1840 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001841 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001842 struct server *sv;
1843
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001844 sv = expect_server_admin(s, si, args[2]);
1845 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001846 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001847
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001848 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1849 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1850 appctx->st0 = STAT_CLI_PRINT;
1851 return 1;
1852 }
1853
1854 sv->check.state |= CHK_ST_ENABLED;
1855 return 1;
1856 }
1857 else if (strcmp(args[1], "server") == 0) {
1858 struct server *sv;
1859
1860 sv = expect_server_admin(s, si, args[2]);
1861 if (!sv)
1862 return 1;
1863
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001864 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001865 return 1;
1866 }
1867 else if (strcmp(args[1], "frontend") == 0) {
1868 struct proxy *px;
1869
1870 px = expect_frontend_admin(s, si, args[2]);
1871 if (!px)
1872 return 1;
1873
1874 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001875 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1876 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001877 return 1;
1878 }
1879
1880 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001881 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1882 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001883 return 1;
1884 }
1885
1886 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001887 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1888 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001889 return 1;
1890 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001891 return 1;
1892 }
1893 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001894 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001896 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001897 }
1898 }
1899 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001900 if (strcmp(args[1], "agent") == 0) {
1901 struct server *sv;
1902
1903 sv = expect_server_admin(s, si, args[2]);
1904 if (!sv)
1905 return 1;
1906
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001907 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001908 return 1;
1909 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001910 else if (strcmp(args[1], "health") == 0) {
1911 struct server *sv;
1912
1913 sv = expect_server_admin(s, si, args[2]);
1914 if (!sv)
1915 return 1;
1916
1917 sv->check.state &= ~CHK_ST_ENABLED;
1918 return 1;
1919 }
Simon Horman671b6f02013-11-25 10:46:39 +09001920 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001921 struct server *sv;
1922
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001923 sv = expect_server_admin(s, si, args[2]);
1924 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001925 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001926
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001927 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001928 return 1;
1929 }
1930 else if (strcmp(args[1], "frontend") == 0) {
1931 struct proxy *px;
1932
1933 px = expect_frontend_admin(s, si, args[2]);
1934 if (!px)
1935 return 1;
1936
1937 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001938 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1939 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001940 return 1;
1941 }
1942
1943 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001944 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1945 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001946 return 1;
1947 }
1948
1949 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001950 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1951 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001952 return 1;
1953 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001954 return 1;
1955 }
1956 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001957 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001958 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001959 return 1;
1960 }
1961 }
1962 else if (strcmp(args[0], "shutdown") == 0) {
1963 if (strcmp(args[1], "frontend") == 0) {
1964 struct proxy *px;
1965
1966 px = expect_frontend_admin(s, si, args[2]);
1967 if (!px)
1968 return 1;
1969
1970 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001971 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1972 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001973 return 1;
1974 }
1975
1976 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1977 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1978 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1979 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1980 stop_proxy(px);
1981 return 1;
1982 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001983 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001984 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001985
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001986 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001987 appctx->ctx.cli.msg = stats_permission_denied_msg;
1988 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001989 return 1;
1990 }
1991
1992 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001993 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1994 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001995 return 1;
1996 }
1997
1998 ptr = (void *)strtoul(args[2], NULL, 0);
1999
Willy Tarreau87b09662015-04-03 00:22:06 +02002000 /* first, look for the requested stream in the stream table */
2001 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002002 if (sess == ptr)
2003 break;
2004 }
2005
Willy Tarreau87b09662015-04-03 00:22:06 +02002006 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002007 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002008 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2009 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002010 return 1;
2011 }
2012
Willy Tarreaue7dff022015-04-03 01:14:29 +02002013 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002014 return 1;
2015 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002016 else if (strcmp(args[1], "sessions") == 0) {
2017 if (strcmp(args[2], "server") == 0) {
2018 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002019 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002020
2021 sv = expect_server_admin(s, si, args[3]);
2022 if (!sv)
2023 return 1;
2024
Willy Tarreau87b09662015-04-03 00:22:06 +02002025 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002026 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2027 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002028 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002029
2030 return 1;
2031 }
2032 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002033 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2034 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002035 return 1;
2036 }
2037 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002038 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002039 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2040 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002041 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002042 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002043 }
2044 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002045 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2046 if (args[1][0] == 'm')
2047 appctx->ctx.map.display_flags = PAT_REF_MAP;
2048 else
2049 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002050
2051 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002052 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2053 if (!*args[2] || !*args[3]) {
2054 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2055 appctx->st0 = STAT_CLI_PRINT;
2056 return 1;
2057 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002058 }
2059
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060 else {
2061 if (!*args[2] || !*args[3]) {
2062 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2063 appctx->st0 = STAT_CLI_PRINT;
2064 return 1;
2065 }
2066 }
2067
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002068 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002069 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002070 if (!appctx->ctx.map.ref ||
2071 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002072 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002073 appctx->st0 = STAT_CLI_PRINT;
2074 return 1;
2075 }
2076
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002077 /* If the entry identifier start with a '#', it is considered as
2078 * pointer id
2079 */
2080 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2081 struct pat_ref_elt *ref;
2082 long long int conv;
2083 char *error;
2084
2085 /* Convert argument to integer value. */
2086 conv = strtoll(&args[3][1], &error, 16);
2087 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002088 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002089 appctx->st0 = STAT_CLI_PRINT;
2090 return 1;
2091 }
2092
2093 /* Convert and check integer to pointer. */
2094 ref = (struct pat_ref_elt *)(long)conv;
2095 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002096 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002097 appctx->st0 = STAT_CLI_PRINT;
2098 return 1;
2099 }
2100
2101 /* Try to delete the entry. */
2102 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2103 /* The entry is not found, send message. */
2104 appctx->ctx.cli.msg = "Key not found.\n";
2105 appctx->st0 = STAT_CLI_PRINT;
2106 return 1;
2107 }
2108 }
2109 else {
2110 /* Else, use the entry identifier as pattern
2111 * string and try to delete the entry.
2112 */
2113 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2114 /* The entry is not found, send message. */
2115 appctx->ctx.cli.msg = "Key not found.\n";
2116 appctx->st0 = STAT_CLI_PRINT;
2117 return 1;
2118 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002119 }
2120
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002121 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002122 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002123 return 1;
2124 }
2125 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002126 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002127 appctx->st0 = STAT_CLI_PRINT;
2128 return 1;
2129 }
2130 }
2131 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002132 if (strcmp(args[1], "map") == 0 ||
2133 strcmp(args[1], "acl") == 0) {
2134 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002135 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002136
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002137 /* Set flags. */
2138 if (args[1][0] == 'm')
2139 appctx->ctx.map.display_flags = PAT_REF_MAP;
2140 else
2141 appctx->ctx.map.display_flags = PAT_REF_ACL;
2142
2143 /* If the keywork is "map", we expect three parameters, if it
2144 * is "acl", we expect only two parameters
2145 */
2146 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2147 if (!*args[2] || !*args[3] || !*args[4]) {
2148 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2149 appctx->st0 = STAT_CLI_PRINT;
2150 return 1;
2151 }
2152 }
2153 else {
2154 if (!*args[2] || !*args[3]) {
2155 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2156 appctx->st0 = STAT_CLI_PRINT;
2157 return 1;
2158 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002159 }
2160
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002162 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002163 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002164 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002165 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002166 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002167 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002168 appctx->st0 = STAT_CLI_PRINT;
2169 return 1;
2170 }
2171
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002172 /* The command "add acl" is prohibited if the reference
2173 * use samples.
2174 */
2175 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2176 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2177 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2178 "You must use the command 'add map' to add values.\n";
2179 appctx->st0 = STAT_CLI_PRINT;
2180 return 1;
2181 }
2182
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002183 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002184 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002186 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002187 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002188 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002189 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002190 if (err)
2191 memprintf(&err, "%s.\n", err);
2192 appctx->ctx.cli.err = err;
2193 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002194 return 1;
2195 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002196
2197 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002198 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002199 return 1;
2200 }
2201 else { /* unknown "del" parameter */
2202 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2203 appctx->st0 = STAT_CLI_PRINT;
2204 return 1;
2205 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002206 }
2207 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002208 return 0;
2209 }
2210 return 1;
2211}
2212
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002213/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002214 * used to processes I/O from/to the stats unix socket. The system relies on a
2215 * state machine handling requests and various responses. We read a request,
2216 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002217 * Then we can read again. The state is stored in appctx->st0 and is one of the
2218 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002219 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002220 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002221static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002222{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002223 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002224 struct channel *req = si_oc(si);
2225 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002226 int reql;
2227 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002228
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002229 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2230 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002231
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002232 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002233 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002234 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002235 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2236 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002237 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002239 /* Let's close for real now. We just close the request
2240 * side, the conditions below will complete if needed.
2241 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002242 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002243 break;
2244 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002245 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002246 /* ensure we have some output room left in the event we
2247 * would want to return some info right after parsing.
2248 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002249 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002250 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002251 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002252 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002253
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002254 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002255 if (reql <= 0) { /* closed or EOL not found */
2256 if (reql == 0)
2257 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002258 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002259 continue;
2260 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002261
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002262 /* seek for a possible semi-colon. If we find one, we
2263 * replace it with an LF and skip only this part.
2264 */
2265 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002266 if (trash.str[len] == ';') {
2267 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002268 reql = len + 1;
2269 break;
2270 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002271
Willy Tarreau816fc222009-10-04 07:36:58 +02002272 /* now it is time to check that we have a full line,
2273 * remove the trailing \n and possibly \r, then cut the
2274 * line.
2275 */
2276 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002277 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002278 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002279 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002280 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002281
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002282 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002283 len--;
2284
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002285 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002286
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002287 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002288 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002289 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002290 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002291 continue;
2292 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002293 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002294 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002295 else if (strcmp(trash.str, "help") == 0 ||
2296 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 appctx->ctx.cli.msg = stats_sock_usage_msg;
2298 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002299 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002300 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002301 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002302 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002303 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002304 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002305 /* if prompt is disabled, print help on empty lines,
2306 * so that the user at least knows how to enable
2307 * prompt and find help.
2308 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002309 appctx->ctx.cli.msg = stats_sock_usage_msg;
2310 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002311 }
2312
2313 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002314 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002315 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002316 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002317 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002318 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002319 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002321 continue;
2322 }
2323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002325 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002326 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002327 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002328 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002329 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002330 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002331 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002332 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002333 free(appctx->ctx.cli.err);
2334 appctx->st0 = STAT_CLI_PROMPT;
2335 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002336 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002337 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002338 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002339 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002340 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002342 break;
2343 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002344 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002345 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002346 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002347 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002348 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002349 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002350 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002351 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002352 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002353 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002355 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002356 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 if (stats_table_request(si, appctx->st0))
2358 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002359 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002360 case STAT_CLI_O_PATS:
2361 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002362 appctx->st0 = STAT_CLI_PROMPT;
2363 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002364 case STAT_CLI_O_PAT:
2365 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002366 appctx->st0 = STAT_CLI_PROMPT;
2367 break;
2368 case STAT_CLI_O_MLOOK:
2369 if (stats_map_lookup(si))
2370 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002371 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002372 case STAT_CLI_O_POOLS:
2373 if (stats_dump_pools_to_buffer(si))
2374 appctx->st0 = STAT_CLI_PROMPT;
2375 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002376 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002377 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002378 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002379 break;
2380 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002381
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002382 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002383 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002384 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002385 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002386 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002387 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002388 }
2389
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002390 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002391 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002392 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002393
2394 /* Now we close the output if one of the writers did so,
2395 * or if we're not in interactive mode and the request
2396 * buffer is empty. This still allows pipelined requests
2397 * to be sent in non-interactive mode.
2398 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002399 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2400 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002401 continue;
2402 }
2403
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002404 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002405 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002406 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002407 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002408
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002409 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002410 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2411 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002412 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002413 * and nothing more to consume. This is comparable to a broken pipe, so
2414 * we forward the close to the request side so that it flows upstream to
2415 * the client.
2416 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002417 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002418 }
2419
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002420 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002421 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2422 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2423 /* We have no more processing to do, and nothing more to send, and
2424 * the client side has closed. So we'll forward this state downstream
2425 * on the response buffer.
2426 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002427 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002428 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002429 }
2430
Willy Tarreau828824a2015-04-19 17:20:03 +02002431 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002432 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 +02002433 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002434 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002435}
2436
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002437/* This function dumps information onto the stream interface's read buffer.
2438 * It returns 0 as long as it does not complete, non-zero upon completion.
2439 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002440 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002441static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002442{
2443 unsigned int up = (now.tv_sec - start_date.tv_sec);
2444
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002445#ifdef USE_OPENSSL
2446 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2447 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2448 int ssl_reuse = 0;
2449
2450 if (ssl_key_rate < ssl_sess_rate) {
2451 /* count the ssl reuse ratio and avoid overflows in both directions */
2452 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2453 }
2454#endif
2455
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002456 chunk_printf(&trash,
2457 "Name: " PRODUCT_NAME "\n"
2458 "Version: " HAPROXY_VERSION "\n"
2459 "Release_date: " HAPROXY_DATE "\n"
2460 "Nbproc: %d\n"
2461 "Process_num: %d\n"
2462 "Pid: %d\n"
2463 "Uptime: %dd %dh%02dm%02ds\n"
2464 "Uptime_sec: %d\n"
2465 "Memmax_MB: %d\n"
2466 "Ulimit-n: %d\n"
2467 "Maxsock: %d\n"
2468 "Maxconn: %d\n"
2469 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002470 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002471 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002472 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002473#ifdef USE_OPENSSL
2474 "MaxSslConns: %d\n"
2475 "CurrSslConns: %d\n"
2476 "CumSslConns: %d\n"
2477#endif
2478 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002479 "PipesUsed: %d\n"
2480 "PipesFree: %d\n"
2481 "ConnRate: %d\n"
2482 "ConnRateLimit: %d\n"
2483 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002484 "SessRate: %d\n"
2485 "SessRateLimit: %d\n"
2486 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002487#ifdef USE_OPENSSL
2488 "SslRate: %d\n"
2489 "SslRateLimit: %d\n"
2490 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002491 "SslFrontendKeyRate: %d\n"
2492 "SslFrontendMaxKeyRate: %d\n"
2493 "SslFrontendSessionReuse_pct: %d\n"
2494 "SslBackendKeyRate: %d\n"
2495 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002496 "SslCacheLookups: %u\n"
2497 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002498#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002499 "CompressBpsIn: %u\n"
2500 "CompressBpsOut: %u\n"
2501 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002502#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002503 "ZlibMemUsage: %ld\n"
2504 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002505#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002506 "Tasks: %d\n"
2507 "Run_queue: %d\n"
2508 "Idle_pct: %d\n"
2509 "node: %s\n"
2510 "description: %s\n"
2511 "",
2512 global.nbproc,
2513 relative_pid,
2514 pid,
2515 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2516 up,
2517 global.rlimit_memmax,
2518 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002519 global.maxsock, global.maxconn, global.hardmaxconn,
2520 actconn, totalconn, global.req_count,
2521#ifdef USE_OPENSSL
2522 global.maxsslconn, sslconns, totalsslconns,
2523#endif
2524 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002525 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002526 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002527#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002528 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2529 ssl_key_rate, global.ssl_fe_keys_max,
2530 ssl_reuse,
2531 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002532 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002533#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002534 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2535 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002536#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002537 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002538#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002539 nb_tasks_cur, run_queue_cur, idle_pct,
2540 global.node, global.desc ? global.desc : ""
2541 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002542
Willy Tarreaubc18da12015-03-13 14:00:47 +01002543 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002544 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002545 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002546 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547
2548 return 1;
2549}
2550
Willy Tarreau12833bb2014-01-28 16:49:56 +01002551/* This function dumps memory usage information onto the stream interface's
2552 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2553 * completion. No state is used.
2554 */
2555static int stats_dump_pools_to_buffer(struct stream_interface *si)
2556{
2557 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002558 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002559 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002560 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002561 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002562 return 1;
2563}
2564
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002565/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2566 * the state from stream interface <si>. The caller is responsible for clearing
2567 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002568 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002570{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002571 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002573
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002574 if (!(px->cap & PR_CAP_FE))
2575 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002577 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 return 0;
2579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002580 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002581 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582 /* name, queue */
2583 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002585 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 /* Column sub-heading for Enable or Disable server */
2587 chunk_appendf(&trash, "<td></td>");
2588 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002589
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002590 chunk_appendf(&trash,
2591 "<td class=ac>"
2592 "<a name=\"%s/Frontend\"></a>"
2593 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2594 "<td colspan=3></td>"
2595 "",
2596 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002597
Willy Tarreau466c9b52012-12-23 02:25:03 +01002598 chunk_appendf(&trash,
2599 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002600 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002601 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2602 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2603 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002604 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2605 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2606 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002607
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002608 if (px->mode == PR_MODE_HTTP)
2609 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002610 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002611 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002612
2613 chunk_appendf(&trash,
2614 "</table></div></u></td>"
2615 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002616 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002617 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2618 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2619 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002620 U2H(px->fe_counters.sps_max),
2621 U2H(px->fe_counters.cps_max),
2622 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002623
2624 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002625 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002626 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002627 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002628
2629 chunk_appendf(&trash,
2630 "</table></div></u></td>"
2631 /* sessions rate : limit */
2632 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002633 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002635 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002636 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002638 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002639 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2640 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002641 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002642 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2643 U2H(px->fe_counters.cum_sess),
2644 U2H(px->fe_counters.cum_conn),
2645 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646
Willy Tarreau466c9b52012-12-23 02:25:03 +01002647 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002649 chunk_appendf(&trash,
2650 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2651 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2652 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2653 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2654 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2655 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2656 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2657 "<tr><th>- other responses:</th><td>%s</td></tr>"
2658 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2659 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002660 U2H(px->fe_counters.p.http.cum_req),
2661 U2H(px->fe_counters.p.http.rsp[1]),
2662 U2H(px->fe_counters.p.http.rsp[2]),
2663 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002664 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002665 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002666 U2H(px->fe_counters.p.http.rsp[3]),
2667 U2H(px->fe_counters.p.http.rsp[4]),
2668 U2H(px->fe_counters.p.http.rsp[5]),
2669 U2H(px->fe_counters.p.http.rsp[0]),
2670 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002671 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002672
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002673 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002674 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002675 /* sessions: lbtot, lastsess */
2676 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002677 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002678 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002679 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002680 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002683 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002684 "<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 +01002685 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002686 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002687 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2688 px->fe_counters.comp_in ?
2689 (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 +01002690 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002691
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002692 chunk_appendf(&trash,
2693 /* denied: req, resp */
2694 "<td>%s</td><td>%s</td>"
2695 /* errors : request, connect, response */
2696 "<td>%s</td><td></td><td></td>"
2697 /* warnings: retries, redispatches */
2698 "<td></td><td></td>"
2699 /* server status : reflect frontend status */
2700 "<td class=ac>%s</td>"
2701 /* rest of server: nothing */
2702 "<td class=ac colspan=8></td></tr>"
2703 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002704 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2705 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002706 px->state == PR_STREADY ? "OPEN" :
2707 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002708 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002709 else { /* CSV mode */
2710 chunk_appendf(&trash,
2711 /* pxid, name, queue cur, queue max, */
2712 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002713 /* sessions : current, max, limit, total */
2714 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002715 /* bytes : in, out */
2716 "%lld,%lld,"
2717 /* denied: req, resp */
2718 "%lld,%lld,"
2719 /* errors : request, connect, response */
2720 "%lld,,,"
2721 /* warnings: retries, redispatches */
2722 ",,"
2723 /* server status : reflect frontend status */
2724 "%s,"
2725 /* rest of server: nothing */
2726 ",,,,,,,,"
2727 /* pid, iid, sid, throttle, lbtot, tracked, type */
2728 "%d,%d,0,,,,%d,"
2729 /* rate, rate_lim, rate_max */
2730 "%u,%u,%u,"
2731 /* check_status, check_code, check_duration */
2732 ",,,",
2733 px->id,
2734 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2735 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2736 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2737 px->fe_counters.failed_req,
2738 px->state == PR_STREADY ? "OPEN" :
2739 px->state == PR_STFULL ? "FULL" : "STOP",
2740 relative_pid, px->uuid, STATS_TYPE_FE,
2741 read_freq_ctr(&px->fe_sess_per_sec),
2742 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002743
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2745 if (px->mode == PR_MODE_HTTP) {
2746 for (i=1; i<6; i++)
2747 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2748 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2749 }
2750 else
2751 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 /* failed health analyses */
2754 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002756 /* requests : req_rate, req_rate_max, req_tot, */
2757 chunk_appendf(&trash, "%u,%u,%lld,",
2758 read_freq_ctr(&px->fe_req_per_sec),
2759 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2760
2761 /* errors: cli_aborts, srv_aborts */
2762 chunk_appendf(&trash, ",,");
2763
2764 /* compression: in, out, bypassed */
2765 chunk_appendf(&trash, "%lld,%lld,%lld,",
2766 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2767
2768 /* compression: comp_rsp */
2769 chunk_appendf(&trash, "%lld,",
2770 px->fe_counters.p.http.comp_rsp);
2771
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002772 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2773 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002774
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 /* finish with EOL */
2776 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002777 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002779}
2780
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002781/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2782 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2783 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2784 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002785 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002786static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002787{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002788 struct appctx *appctx = __objt_appctx(si->end);
2789
2790 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002791 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002792 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002793 /* Column sub-heading for Enable or Disable server */
2794 chunk_appendf(&trash, "<td></td>");
2795 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002796 chunk_appendf(&trash,
2797 /* frontend name, listener name */
2798 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2799 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2800 "",
2801 px->id, l->name,
2802 (flags & ST_SHLGNDS)?"<u>":"",
2803 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002804
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002805 if (flags & ST_SHLGNDS) {
2806 char str[INET6_ADDRSTRLEN];
2807 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002808
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002809 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002810
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002811 port = get_host_port(&l->addr);
2812 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2813 case AF_INET:
2814 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2815 break;
2816 case AF_INET6:
2817 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2818 break;
2819 case AF_UNIX:
2820 chunk_appendf(&trash, "unix, ");
2821 break;
2822 case -1:
2823 chunk_appendf(&trash, "(%s), ", strerror(errno));
2824 break;
2825 }
Willy Tarreau91861262007-10-17 17:06:05 +02002826
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002828 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829 }
Willy Tarreau91861262007-10-17 17:06:05 +02002830
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002831 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002832 /* queue */
2833 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002834 /* sessions rate: current, max, limit */
2835 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002836 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002837 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002838 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002839 /* bytes: in, out */
2840 "<td>%s</td><td>%s</td>"
2841 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002843 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2844 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002845
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002846 chunk_appendf(&trash,
2847 /* denied: req, resp */
2848 "<td>%s</td><td>%s</td>"
2849 /* errors: request, connect, response */
2850 "<td>%s</td><td></td><td></td>"
2851 /* warnings: retries, redispatches */
2852 "<td></td><td></td>"
2853 /* server status: reflect listener status */
2854 "<td class=ac>%s</td>"
2855 /* rest of server: nothing */
2856 "<td class=ac colspan=8></td></tr>"
2857 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002858 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2859 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002860 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2861 }
2862 else { /* CSV mode */
2863 chunk_appendf(&trash,
2864 /* pxid, name, queue cur, queue max, */
2865 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002866 /* sessions: current, max, limit, total */
2867 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002868 /* bytes: in, out */
2869 "%lld,%lld,"
2870 /* denied: req, resp */
2871 "%lld,%lld,"
2872 /* errors: request, connect, response */
2873 "%lld,,,"
2874 /* warnings: retries, redispatches */
2875 ",,"
2876 /* server status: reflect listener status */
2877 "%s,"
2878 /* rest of server: nothing */
2879 ",,,,,,,,"
2880 /* pid, iid, sid, throttle, lbtot, tracked, type */
2881 "%d,%d,%d,,,,%d,"
2882 /* rate, rate_lim, rate_max */
2883 ",,,"
2884 /* check_status, check_code, check_duration */
2885 ",,,"
2886 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2887 ",,,,,,"
2888 /* failed health analyses */
2889 ","
2890 /* requests : req_rate, req_rate_max, req_tot, */
2891 ",,,"
2892 /* errors: cli_aborts, srv_aborts */
2893 ",,"
2894 /* compression: in, out, bypassed, comp_rsp */
2895 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002896 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2897 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898 "\n",
2899 px->id, l->name,
2900 l->nbconn, l->counters->conn_max,
2901 l->maxconn, l->counters->cum_conn,
2902 l->counters->bytes_in, l->counters->bytes_out,
2903 l->counters->denied_req, l->counters->denied_resp,
2904 l->counters->failed_req,
2905 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2906 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2907 }
2908 return 1;
2909}
Willy Tarreau91861262007-10-17 17:06:05 +02002910
Simon Horman4d2eab62015-04-23 14:51:26 +09002911enum srv_stats_state {
2912 SRV_STATS_STATE_DOWN = 0,
2913 SRV_STATS_STATE_DOWN_AGENT,
2914 SRV_STATS_STATE_GOING_UP,
2915 SRV_STATS_STATE_UP_GOING_DOWN,
2916 SRV_STATS_STATE_UP,
2917 SRV_STATS_STATE_NOLB_GOING_DOWN,
2918 SRV_STATS_STATE_NOLB,
2919 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2920 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09002921 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09002922 SRV_STATS_STATE_NO_CHECK,
2923
2924 SRV_STATS_STATE_COUNT, /* Must be last */
2925};
2926
Simon Horman837bfa72015-04-23 14:51:27 +09002927enum srv_stats_colour {
2928 SRV_STATS_COLOUR_DOWN = 0,
2929 SRV_STATS_COLOUR_GOING_UP,
2930 SRV_STATS_COLOUR_GOING_DOWN,
2931 SRV_STATS_COLOUR_UP,
2932 SRV_STATS_COLOUR_NOLB,
2933 SRV_STATS_COLOUR_DRAINING,
2934 SRV_STATS_COLOUR_NO_CHECK,
2935
2936 SRV_STATS_COLOUR_COUNT, /* Must be last */
2937};
2938
2939static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
2940 [SRV_STATS_COLOUR_DOWN] = "down",
2941 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
2942 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
2943 [SRV_STATS_COLOUR_UP] = "up",
2944 [SRV_STATS_COLOUR_NOLB] = "nolb",
2945 [SRV_STATS_COLOUR_DRAINING] = "draining",
2946 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
2947};
2948
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002949/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2950 * from stream interface <si>, stats flags <flags>, and server state <state>.
2951 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09002952 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002953 */
Simon Horman4d2eab62015-04-23 14:51:26 +09002954static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09002955 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002957 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002958 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002959 char str[INET6_ADDRSTRLEN];
2960 struct chunk src;
2961 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002962
Willy Tarreau32091232014-05-16 13:52:00 +02002963 /* we have "via" which is the tracked server as described in the configuration,
2964 * and "ref" which is the checked server and the end of the chain.
2965 */
2966 via = sv->track ? sv->track : sv;
2967 ref = via;
2968 while (ref->track)
2969 ref = ref->track;
2970
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002971 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09002972 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2973 [SRV_STATS_STATE_DOWN] = "DOWN",
2974 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2975 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
2976 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
2977 [SRV_STATS_STATE_UP] = "UP",
2978 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
2979 [SRV_STATS_STATE_NOLB] = "NOLB",
2980 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
2981 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09002982 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09002983 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002984 };
Willy Tarreau91861262007-10-17 17:06:05 +02002985
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002986 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002987 chunk_appendf(&trash, "<tr class=\"maintain\">");
2988 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002989 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09002990 "<tr class=\"%s_%s\">",
2991 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02002992
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002993 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002994 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2996 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002997
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002998 chunk_appendf(&trash,
2999 "<td class=ac><a name=\"%s/%s\"></a>%s"
3000 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3001 "",
3002 px->id, sv->id,
3003 (flags & ST_SHLGNDS) ? "<u>" : "",
3004 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003005
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003007 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003008
3009 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3010 case AF_INET:
3011 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3012 break;
3013 case AF_INET6:
3014 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3015 break;
3016 case AF_UNIX:
3017 chunk_appendf(&trash, "unix, ");
3018 break;
3019 case -1:
3020 chunk_appendf(&trash, "(%s), ", strerror(errno));
3021 break;
3022 default: /* address family not supported */
3023 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003024 }
Willy Tarreau91861262007-10-17 17:06:05 +02003025
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003026 /* id */
3027 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003028
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 /* cookie */
3030 if (sv->cookie) {
3031 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003032
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003033 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3034 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003035
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003036 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003037 }
3038
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003039 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003040 }
Willy Tarreau91861262007-10-17 17:06:05 +02003041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003043 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003044 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 /* sessions rate : current, max, limit */
3046 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003047 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003048 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003049 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3050 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003051
Willy Tarreau466c9b52012-12-23 02:25:03 +01003052
3053 chunk_appendf(&trash,
3054 /* sessions: current, max, limit, total */
3055 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003056 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003057 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3058 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003059 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3060 U2H(sv->counters.cum_sess),
3061 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003062
Willy Tarreau466c9b52012-12-23 02:25:03 +01003063 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3064 if (px->mode == PR_MODE_HTTP) {
3065 unsigned long long tot;
3066 for (tot = i = 0; i < 6; i++)
3067 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003068
Willy Tarreau466c9b52012-12-23 02:25:03 +01003069 chunk_appendf(&trash,
3070 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3071 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3072 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3073 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3074 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3075 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3076 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3077 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003078 U2H(tot),
3079 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3080 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3081 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3082 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3083 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3084 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 +02003085 }
Willy Tarreau91861262007-10-17 17:06:05 +02003086
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003087 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3088 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)));
3089 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)));
3090 if (px->mode == PR_MODE_HTTP)
3091 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)));
3092 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)));
3093
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003094 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003095 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003096 /* sessions: lbtot, last */
3097 "<td>%s</td><td>%s</td>",
3098 U2H(sv->counters.cum_lbconn),
3099 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003100
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003101 chunk_appendf(&trash,
3102 /* bytes : in, out */
3103 "<td>%s</td><td>%s</td>"
3104 /* denied: req, resp */
3105 "<td></td><td>%s</td>"
3106 /* errors : request, connect */
3107 "<td></td><td>%s</td>"
3108 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003109 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 /* warnings: retries, redispatches */
3111 "<td>%lld</td><td>%lld</td>"
3112 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003113 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3114 U2H(sv->counters.failed_secu),
3115 U2H(sv->counters.failed_conns),
3116 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003117 sv->counters.cli_aborts,
3118 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 sv->counters.retries, sv->counters.redispatches);
3120
3121 /* status, lest check */
3122 chunk_appendf(&trash, "<td class=ac>");
3123
Willy Tarreau20125212014-05-13 19:44:56 +02003124 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3126 chunk_appendf(&trash, "MAINT");
3127 }
Simon Horman0766e442014-11-12 15:55:54 +09003128 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003129 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003130 /* DOWN (agent) */
3131 chunk_appendf(&trash, srv_hlt_st[1], "GCC: your -Werror=format-security is bogus, annoying, and hides real bugs, I don't thank you, really!");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003132 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003133 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003134 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3135 chunk_appendf(&trash,
3136 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003137 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3138 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003139 }
Willy Tarreau91861262007-10-17 17:06:05 +02003140
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003141 if ((sv->state == SRV_ST_STOPPED) &&
3142 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3143 chunk_appendf(&trash,
3144 "</td><td class=ac><u> %s%s",
3145 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3146 get_check_status_info(sv->agent.status));
3147
3148 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3149 chunk_appendf(&trash, "/%d", sv->agent.code);
3150
3151 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3152 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3153
3154 chunk_appendf(&trash, "<div class=tips>%s",
3155 get_check_status_description(sv->agent.status));
3156 if (*sv->agent.desc) {
3157 chunk_appendf(&trash, ": ");
3158 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3159 chunk_htmlencode(&trash, &src);
3160 }
3161 chunk_appendf(&trash, "</div></u>");
3162 }
3163 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003164 chunk_appendf(&trash,
3165 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003166 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167 get_check_status_info(sv->check.status));
3168
3169 if (sv->check.status >= HCHK_STATUS_L57DATA)
3170 chunk_appendf(&trash, "/%d", sv->check.code);
3171
3172 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003173 chunk_appendf(&trash, " in %lums", sv->check.duration);
3174
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003175 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003176 get_check_status_description(sv->check.status));
3177 if (*sv->check.desc) {
3178 chunk_appendf(&trash, ": ");
3179 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3180 chunk_htmlencode(&trash, &src);
3181 }
3182 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003183 }
3184 else
3185 chunk_appendf(&trash, "</td><td>");
3186
3187 chunk_appendf(&trash,
3188 /* weight */
3189 "</td><td class=ac>%d</td>"
3190 /* act, bck */
3191 "<td class=ac>%s</td><td class=ac>%s</td>"
3192 "",
3193 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003194 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3195 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003196
3197 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003198 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003199 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003200
3201 if (ref->observe)
3202 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3203
3204 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003205 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 "<td>%lld</td><td>%s</td>"
3207 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003208 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3210 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003211 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3212 /* tracking a server */
3213 chunk_appendf(&trash,
3214 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003215 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003216 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 else
3218 chunk_appendf(&trash, "<td colspan=3></td>");
3219
3220 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003221 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003222 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 else
3224 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3225 }
3226 else { /* CSV mode */
Simon Horman4d2eab62015-04-23 14:51:26 +09003227 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3228 [SRV_STATS_STATE_DOWN] = "DOWN,",
3229 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3230 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3231 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3232 [SRV_STATS_STATE_UP] = "UP,",
3233 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3234 [SRV_STATS_STATE_NOLB] = "NOLB,",
3235 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3236 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003237 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003238 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003239 };
3240
3241 chunk_appendf(&trash,
3242 /* pxid, name */
3243 "%s,%s,"
3244 /* queue : current, max */
3245 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003246 /* sessions : current, max, limit, total */
3247 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003248 /* bytes : in, out */
3249 "%lld,%lld,"
3250 /* denied: req, resp */
3251 ",%lld,"
3252 /* errors : request, connect, response */
3253 ",%lld,%lld,"
3254 /* warnings: retries, redispatches */
3255 "%lld,%lld,"
3256 "",
3257 px->id, sv->id,
3258 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003259 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003260 sv->counters.bytes_in, sv->counters.bytes_out,
3261 sv->counters.failed_secu,
3262 sv->counters.failed_conns, sv->counters.failed_resp,
3263 sv->counters.retries, sv->counters.redispatches);
3264
3265 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003266 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003267 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003268 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003269 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 else
3271 chunk_appendf(&trash,
3272 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003273 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3274 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003275
3276 chunk_appendf(&trash,
3277 /* weight, active, backup */
3278 "%d,%d,%d,"
3279 "",
3280 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003281 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3282 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283
3284 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003285 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003286 chunk_appendf(&trash,
3287 "%lld,%lld,%d,%d,",
3288 sv->counters.failed_checks, sv->counters.down_trans,
3289 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3290 else
3291 chunk_appendf(&trash, ",,,,");
3292
3293 /* queue limit, pid, iid, sid, */
3294 chunk_appendf(&trash,
3295 "%s,"
3296 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003297 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003298 relative_pid, px->uuid, sv->puid);
3299
3300 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003301 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003302 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303
3304 /* sessions: lbtot */
3305 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3306
3307 /* tracked */
3308 if (sv->track)
3309 chunk_appendf(&trash, "%s/%s,",
3310 sv->track->proxy->id, sv->track->id);
3311 else
3312 chunk_appendf(&trash, ",");
3313
3314 /* type */
3315 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3316
3317 /* rate */
3318 chunk_appendf(&trash, "%u,,%u,",
3319 read_freq_ctr(&sv->sess_per_sec),
3320 sv->counters.sps_max);
3321
Willy Tarreauff5ae352013-12-11 20:36:34 +01003322 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 /* check_status */
3324 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3325
3326 /* check_code */
3327 if (sv->check.status >= HCHK_STATUS_L57DATA)
3328 chunk_appendf(&trash, "%u,", sv->check.code);
3329 else
3330 chunk_appendf(&trash, ",");
3331
3332 /* check_duration */
3333 if (sv->check.status >= HCHK_STATUS_CHECKED)
3334 chunk_appendf(&trash, "%lu,", sv->check.duration);
3335 else
3336 chunk_appendf(&trash, ",");
3337
3338 }
3339 else
3340 chunk_appendf(&trash, ",,,");
3341
3342 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3343 if (px->mode == PR_MODE_HTTP) {
3344 for (i=1; i<6; i++)
3345 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3346
3347 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3348 }
3349 else
3350 chunk_appendf(&trash, ",,,,,,");
3351
3352 /* failed health analyses */
3353 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3354
3355 /* requests : req_rate, req_rate_max, req_tot, */
3356 chunk_appendf(&trash, ",,,");
3357
3358 /* errors: cli_aborts, srv_aborts */
3359 chunk_appendf(&trash, "%lld,%lld,",
3360 sv->counters.cli_aborts, sv->counters.srv_aborts);
3361
3362 /* compression: in, out, bypassed, comp_rsp */
3363 chunk_appendf(&trash, ",,,,");
3364
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003365 /* lastsess */
3366 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3367
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003368 /* capture of last check and agent statuses */
3369 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3370 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3371
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003372 /* qtime, ctime, rtime, ttime, */
3373 chunk_appendf(&trash, "%u,%u,%u,%u,",
3374 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3375 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3376 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3377 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3378
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 /* finish with EOL */
3380 chunk_appendf(&trash, "\n");
3381 }
3382 return 1;
3383}
3384
3385/* Dumps a line for backend <px> to the trash for and uses the state from stream
3386 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3387 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3388 */
3389static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3390{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003391 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392 struct chunk src;
3393 int i;
3394
3395 if (!(px->cap & PR_CAP_BE))
3396 return 0;
3397
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003398 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 return 0;
3400
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003401 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003403 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 /* Column sub-heading for Enable or Disable server */
3405 chunk_appendf(&trash, "<td></td>");
3406 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003407 chunk_appendf(&trash,
3408 "<td class=ac>"
3409 /* name */
3410 "%s<a name=\"%s/Backend\"></a>"
3411 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3412 "",
3413 (flags & ST_SHLGNDS)?"<u>":"",
3414 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415
3416 if (flags & ST_SHLGNDS) {
3417 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003418 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3420
3421 /* cookie */
3422 if (px->cookie_name) {
3423 chunk_appendf(&trash, ", cookie: '");
3424 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3425 chunk_htmlencode(&trash, &src);
3426 chunk_appendf(&trash, "'");
3427 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003428 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003429 }
3430
3431 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003432 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 /* queue : current, max */
3434 "<td>%s</td><td>%s</td><td></td>"
3435 /* sessions rate : current, max, limit */
3436 "<td>%s</td><td>%s</td><td></td>"
3437 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003439 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3440 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441
3442 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003443 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003445 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003446 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003447 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003448 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3449 U2H(px->be_counters.cum_conn),
3450 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451
Willy Tarreau466c9b52012-12-23 02:25:03 +01003452 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003453 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003454 chunk_appendf(&trash,
3455 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3456 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3457 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3458 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3459 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3460 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3461 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3462 "<tr><th>- other responses:</th><td>%s</td></tr>"
3463 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003464 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003465 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003466 U2H(px->be_counters.p.http.cum_req),
3467 U2H(px->be_counters.p.http.rsp[1]),
3468 U2H(px->be_counters.p.http.rsp[2]),
3469 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003471 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003472 U2H(px->be_counters.p.http.rsp[3]),
3473 U2H(px->be_counters.p.http.rsp[4]),
3474 U2H(px->be_counters.p.http.rsp[5]),
3475 U2H(px->be_counters.p.http.rsp[0]),
3476 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003477 }
3478
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003479 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES)));
3480 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES)));
3481 if (px->mode == PR_MODE_HTTP)
3482 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES)));
3483 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES)));
3484
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003486 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003487 /* sessions: lbtot, last */
3488 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003489 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003490 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003491 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003492 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003493 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003494 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003495
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003496 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003497 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003498 "<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 +01003499 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003500 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003501 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3502 px->be_counters.comp_in ?
3503 (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 +01003504 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3505
3506 chunk_appendf(&trash,
3507 /* denied: req, resp */
3508 "<td>%s</td><td>%s</td>"
3509 /* errors : request, connect */
3510 "<td></td><td>%s</td>"
3511 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003512 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003513 /* warnings: retries, redispatches */
3514 "<td>%lld</td><td>%lld</td>"
3515 /* backend status: reflect backend status (up/down): we display UP
3516 * if the backend has known working servers or if it has no server at
3517 * all (eg: for stats). Then we display the total weight, number of
3518 * active and backups. */
3519 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3520 "<td class=ac>%d</td><td class=ac>%d</td>"
3521 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003522 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3523 U2H(px->be_counters.failed_conns),
3524 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003525 px->be_counters.cli_aborts,
3526 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003527 px->be_counters.retries, px->be_counters.redispatches,
3528 human_time(now.tv_sec - px->last_change, 1),
3529 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3530 "<font color=\"red\"><b>DOWN</b></font>",
3531 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3532 px->srv_act, px->srv_bck);
3533
3534 chunk_appendf(&trash,
3535 /* rest of backend: nothing, down transitions, total downtime, throttle */
3536 "<td class=ac>&nbsp;</td><td>%d</td>"
3537 "<td>%s</td>"
3538 "<td></td>"
3539 "</tr>",
3540 px->down_trans,
3541 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3542 }
3543 else { /* CSV mode */
3544 chunk_appendf(&trash,
3545 /* pxid, name */
3546 "%s,BACKEND,"
3547 /* queue : current, max */
3548 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003549 /* sessions : current, max, limit, total */
3550 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551 /* bytes : in, out */
3552 "%lld,%lld,"
3553 /* denied: req, resp */
3554 "%lld,%lld,"
3555 /* errors : request, connect, response */
3556 ",%lld,%lld,"
3557 /* warnings: retries, redispatches */
3558 "%lld,%lld,"
3559 /* backend status: reflect backend status (up/down): we display UP
3560 * if the backend has known working servers or if it has no server at
3561 * all (eg: for stats). Then we display the total weight, number of
3562 * active and backups. */
3563 "%s,"
3564 "%d,%d,%d,"
3565 /* rest of backend: nothing, down transitions, last change, total downtime */
3566 ",%d,%d,%d,,"
3567 /* pid, iid, sid, throttle, lbtot, tracked, type */
3568 "%d,%d,0,,%lld,,%d,"
3569 /* rate, rate_lim, rate_max, */
3570 "%u,,%u,"
3571 /* check_status, check_code, check_duration */
3572 ",,,",
3573 px->id,
3574 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3575 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3576 px->be_counters.bytes_in, px->be_counters.bytes_out,
3577 px->be_counters.denied_req, px->be_counters.denied_resp,
3578 px->be_counters.failed_conns, px->be_counters.failed_resp,
3579 px->be_counters.retries, px->be_counters.redispatches,
3580 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3581 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3582 px->srv_act, px->srv_bck,
3583 px->down_trans, (int)(now.tv_sec - px->last_change),
3584 px->srv?be_downtime(px):0,
3585 relative_pid, px->uuid,
3586 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3587 read_freq_ctr(&px->be_sess_per_sec),
3588 px->be_counters.sps_max);
3589
3590 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3591 if (px->mode == PR_MODE_HTTP) {
3592 for (i=1; i<6; i++)
3593 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3594 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3595 }
3596 else
3597 chunk_appendf(&trash, ",,,,,,");
3598
3599 /* failed health analyses */
3600 chunk_appendf(&trash, ",");
3601
3602 /* requests : req_rate, req_rate_max, req_tot, */
3603 chunk_appendf(&trash, ",,,");
3604
3605 /* errors: cli_aborts, srv_aborts */
3606 chunk_appendf(&trash, "%lld,%lld,",
3607 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3608
3609 /* compression: in, out, bypassed */
3610 chunk_appendf(&trash, "%lld,%lld,%lld,",
3611 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3612
3613 /* compression: comp_rsp */
3614 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3615
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003616 /* lastsess, last_chk, last_agt, */
3617 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003618
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003619 /* qtime, ctime, rtime, ttime, */
3620 chunk_appendf(&trash, "%u,%u,%u,%u,",
3621 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3622 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3623 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3624 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3625
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003626 /* finish with EOL */
3627 chunk_appendf(&trash, "\n");
3628 }
3629 return 1;
3630}
3631
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003632/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003633 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003634 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003635 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003636static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003637{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003638 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003639 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3640
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003641 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003642 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003643
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003644 /* 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 +02003645 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003646 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003647 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003648 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003649 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003650 }
3651
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003652 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003653 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654 }
3655
3656 /* print a new table */
3657 chunk_appendf(&trash,
3658 "<table class=\"tbl\" width=\"100%%\">\n"
3659 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003660 "<th class=\"pxname\" width=\"10%%\">");
3661
3662 chunk_appendf(&trash,
3663 "<a name=\"%s\"></a>%s"
3664 "<a class=px href=\"#%s\">%s</a>",
3665 px->id,
3666 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3667 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003668
3669 if (uri->flags & ST_SHLGNDS) {
3670 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003671 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003672 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3673 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003674 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003675 }
3676
3677 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003678 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003679 "<th class=\"%s\" width=\"90%%\">%s</th>"
3680 "</tr>\n"
3681 "</table>\n"
3682 "<table class=\"tbl\" width=\"100%%\">\n"
3683 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3685 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3686
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003687 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003688 /* Column heading for Enable or Disable server */
3689 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003690 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003691
3692 chunk_appendf(&trash,
3693 "<th rowspan=2></th>"
3694 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003695 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3697 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3698 "<th colspan=9>Server</th>"
3699 "</tr>\n"
3700 "<tr class=\"titre\">"
3701 "<th>Cur</th><th>Max</th><th>Limit</th>"
3702 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003703 "<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 +01003704 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3705 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3706 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3707 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3708 "<th>Thrtle</th>\n"
3709 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003710}
3711
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003712/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003713 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003714 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003715static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003716{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003717 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003718 chunk_appendf(&trash, "</table>");
3719
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003720 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003721 /* close the form used to enable/disable this proxy servers */
3722 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003723 "Choose the action to perform on the checked servers : "
3724 "<select name=action>"
3725 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003726 "<option value=\"ready\">Set state to READY</option>"
3727 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003728 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003729 "<option value=\"dhlth\">Health: disable checks</option>"
3730 "<option value=\"ehlth\">Health: enable checks</option>"
3731 "<option value=\"hrunn\">Health: force UP</option>"
3732 "<option value=\"hnolb\">Health: force NOLB</option>"
3733 "<option value=\"hdown\">Health: force DOWN</option>"
3734 "<option value=\"dagent\">Agent: disable checks</option>"
3735 "<option value=\"eagent\">Agent: enable checks</option>"
3736 "<option value=\"arunn\">Agent: force UP</option>"
3737 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003738 "<option value=\"shutdown\">Kill Sessions</option>"
3739 "</select>"
3740 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3741 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3742 "</form>",
3743 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003744 }
3745
3746 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003747}
Willy Tarreau91861262007-10-17 17:06:05 +02003748
3749/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003750 * Dumps statistics for a proxy. The output is sent to the stream interface's
3751 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3752 * buffer space, or non-zero if everything completed. This function is used
3753 * both by the CLI and the HTTP entry points, and is able to dump the output
3754 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003755 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003756static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003757{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003758 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003759 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003760 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003761 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003762 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003763
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003764 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003765
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003766 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003767 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003768 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003769 if (uri && uri->scope) {
3770 /* we have a limited scope, we have to check the proxy name */
3771 struct stat_scope *scope;
3772 int len;
3773
3774 len = strlen(px->id);
3775 scope = uri->scope;
3776
3777 while (scope) {
3778 /* match exact proxy name */
3779 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3780 break;
3781
3782 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003783 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003784 break;
3785 scope = scope->next;
3786 }
3787
3788 /* proxy name not found : don't dump anything */
3789 if (scope == NULL)
3790 return 1;
3791 }
3792
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003793 /* if the user has requested a limited output and the proxy
3794 * name does not match, skip it.
3795 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003796 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003797 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003798 return 1;
3799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003800 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3801 (appctx->ctx.stats.iid != -1) &&
3802 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003803 return 1;
3804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003805 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003806 /* fall through */
3807
Willy Tarreau295a8372011-03-10 11:25:07 +01003808 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003809 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003810 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003811 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003812 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003813 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003814 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003815 }
Willy Tarreau91861262007-10-17 17:06:05 +02003816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003817 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003818 /* fall through */
3819
Willy Tarreau295a8372011-03-10 11:25:07 +01003820 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003821 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003822 if (stats_dump_fe_stats(si, px)) {
3823 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003824 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003825 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003826 }
3827 }
Willy Tarreau91861262007-10-17 17:06:05 +02003828
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003829 appctx->ctx.stats.l = px->conf.listeners.n;
3830 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003831 /* fall through */
3832
Willy Tarreau295a8372011-03-10 11:25:07 +01003833 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003834 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003835 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003836 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003837 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003838 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003839 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003840
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003841 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003842 if (!l->counters)
3843 continue;
3844
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003845 if (appctx->ctx.stats.flags & STAT_BOUND) {
3846 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003847 break;
3848
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003849 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003850 continue;
3851 }
3852
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003853 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003854 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3855 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003856 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003857 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003858 }
3859 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003860 }
3861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3863 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003864 /* fall through */
3865
Willy Tarreau295a8372011-03-10 11:25:07 +01003866 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003867 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003868 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003869 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09003870 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02003871
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003872 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003873 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003874 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003875 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003876
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003877 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003878
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003879 if (appctx->ctx.stats.flags & STAT_BOUND) {
3880 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003881 break;
3882
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003883 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003884 continue;
3885 }
3886
Willy Tarreau32091232014-05-16 13:52:00 +02003887 svs = sv;
3888 while (svs->track)
3889 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003890
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003891 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003892 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09003893 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003894 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003895 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
3896 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003897 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09003898 sv_colour = SRV_STATS_COLOUR_UP;
3899 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01003900
Simon Horman4463d192015-04-23 14:51:28 +09003901 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
3902 sv_colour = SRV_STATS_COLOUR_DRAINING;
3903
3904 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09003905 if (svs->agent.state & CHK_ST_ENABLED)
3906 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
3907 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09003908 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09003909 else
Simon Horman4d2eab62015-04-23 14:51:26 +09003910 sv_state = SRV_STATS_STATE_DRAIN;
3911 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003912
Simon Horman837bfa72015-04-23 14:51:27 +09003913 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003914 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09003915 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
3916 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01003917 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003918 else if (sv->state == SRV_ST_STOPPING) {
3919 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09003920 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003921 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09003922 sv_colour = SRV_STATS_COLOUR_NOLB;
3923 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003924 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003925 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
3926 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003927 }
3928 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09003929 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003930 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09003931 sv_colour = SRV_STATS_COLOUR_DOWN;
3932 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003933 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09003934 sv_colour = SRV_STATS_COLOUR_DOWN;
3935 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003936 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09003937 sv_colour = SRV_STATS_COLOUR_GOING_UP;
3938 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003939 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09003940 sv_colour = SRV_STATS_COLOUR_DOWN;
3941 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003942 }
Willy Tarreau91861262007-10-17 17:06:05 +02003943
Willy Tarreau9638efa2014-05-23 11:19:57 +02003944 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003945 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003946 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003947 continue;
3948 }
3949
Simon Horman837bfa72015-04-23 14:51:27 +09003950 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003951 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003952 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003953 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003954 }
3955 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003956 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003957
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003958 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003959 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003960
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003961 case STAT_PX_ST_BE:
3962 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003963 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
3964 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003965 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003967 }
3968 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003969
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003970 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003971 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003972
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003973 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003974 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003975 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003976 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003977 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003978 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003979 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003980 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003981
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003982 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003984
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 case STAT_PX_ST_FIN:
3986 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003987
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003988 default:
3989 /* unknown state, we should put an abort() here ! */
3990 return 1;
3991 }
3992}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003993
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003994/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3995 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003996 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003997static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003998{
3999 /* WARNING! This must fit in the first buffer !!! */
4000 chunk_appendf(&trash,
4001 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4002 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4003 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4004 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4005 "<style type=\"text/css\"><!--\n"
4006 "body {"
4007 " font-family: arial, helvetica, sans-serif;"
4008 " font-size: 12px;"
4009 " font-weight: normal;"
4010 " color: black;"
4011 " background: white;"
4012 "}\n"
4013 "th,td {"
4014 " font-size: 10px;"
4015 "}\n"
4016 "h1 {"
4017 " font-size: x-large;"
4018 " margin-bottom: 0.5em;"
4019 "}\n"
4020 "h2 {"
4021 " font-family: helvetica, arial;"
4022 " font-size: x-large;"
4023 " font-weight: bold;"
4024 " font-style: italic;"
4025 " color: #6020a0;"
4026 " margin-top: 0em;"
4027 " margin-bottom: 0em;"
4028 "}\n"
4029 "h3 {"
4030 " font-family: helvetica, arial;"
4031 " font-size: 16px;"
4032 " font-weight: bold;"
4033 " color: #b00040;"
4034 " background: #e8e8d0;"
4035 " margin-top: 0em;"
4036 " margin-bottom: 0em;"
4037 "}\n"
4038 "li {"
4039 " margin-top: 0.25em;"
4040 " margin-right: 2em;"
4041 "}\n"
4042 ".hr {margin-top: 0.25em;"
4043 " border-color: black;"
4044 " border-bottom-style: solid;"
4045 "}\n"
4046 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4047 ".total {background: #20D0D0;color: #ffff80;}\n"
4048 ".frontend {background: #e8e8d0;}\n"
4049 ".socket {background: #d0d0d0;}\n"
4050 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004051 ".active_down {background: #ff9090;}\n"
4052 ".active_going_up {background: #ffd020;}\n"
4053 ".active_going_down {background: #ffffa0;}\n"
4054 ".active_up {background: #c0ffc0;}\n"
4055 ".active_nolb {background: #20a0ff;}\n"
4056 ".active_draining {background: #20a0FF;}\n"
4057 ".active_no_check {background: #e0e0e0;}\n"
4058 ".backup_down {background: #ff9090;}\n"
4059 ".backup_going_up {background: #ff80ff;}\n"
4060 ".backup_going_down {background: #c060ff;}\n"
4061 ".backup_up {background: #b0d0ff;}\n"
4062 ".backup_nolb {background: #90b0e0;}\n"
4063 ".backup_draining {background: #cc9900;}\n"
4064 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004065 ".maintain {background: #c07820;}\n"
4066 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4067 "\n"
4068 "a.px:link {color: #ffff40; text-decoration: none;}"
4069 "a.px:visited {color: #ffff40; text-decoration: none;}"
4070 "a.px:hover {color: #ffffff; text-decoration: none;}"
4071 "a.lfsb:link {color: #000000; text-decoration: none;}"
4072 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4073 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4074 "\n"
4075 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4076 "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"
4077 "table.tbl td.ac { text-align: center;}\n"
4078 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4079 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4080 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4081 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4082 "\n"
4083 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4084 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4085 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004086 "table.det { border-collapse: collapse; border-style: none; }\n"
4087 "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 +01004088 "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 +01004089 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004090 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004091 " display:block;\n"
4092 " visibility:hidden;\n"
4093 " z-index:2147483647;\n"
4094 " position:absolute;\n"
4095 " padding:2px 4px 3px;\n"
4096 " background:#f0f060; color:#000000;\n"
4097 " border:1px solid #7040c0;\n"
4098 " white-space:nowrap;\n"
4099 " font-style:normal;font-size:11px;font-weight:normal;\n"
4100 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4101 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4102 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004103 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004104 "-->\n"
4105 "</style></head>\n",
4106 (uri->flags & ST_SHNODE) ? " on " : "",
4107 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4108 );
4109}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004110
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004111/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004112 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004113 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004114 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004115static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004116{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004117 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004118 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004119 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004120
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004121 /* WARNING! this has to fit the first packet too.
4122 * We are around 3.5 kB, add adding entries will
4123 * become tricky if we want to support 4kB buffers !
4124 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004125 chunk_appendf(&trash,
4126 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4127 PRODUCT_NAME "%s</a></h1>\n"
4128 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4129 "<hr width=\"100%%\" class=\"hr\">\n"
4130 "<h3>&gt; General process information</h3>\n"
4131 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4132 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4133 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4134 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4135 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4136 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4137 "Running tasks: %d/%d; idle = %d %%<br>\n"
4138 "</td><td align=\"center\" nowrap>\n"
4139 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004140 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4141 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004142 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004143 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4144 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004145 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004146 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4147 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004148 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004149 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4150 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004151 "</tr><tr>\n"
4152 "<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 -07004153 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004154 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004155 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004156 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004157 "</td>"
4158 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4159 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4160 "",
4161 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4162 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4163 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4164 (uri->flags & ST_SHDESC) ? ": " : "",
4165 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4166 pid, relative_pid, global.nbproc,
4167 up / 86400, (up % 86400) / 3600,
4168 (up % 3600) / 60, (up % 60),
4169 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4170 global.rlimit_memmax ? " MB" : "",
4171 global.rlimit_nofile,
4172 global.maxsock, global.maxconn, global.maxpipes,
4173 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4174 run_queue_cur, nb_tasks_cur, idle_pct
4175 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004177 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004178 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004179 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004180
4181 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004182 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004183 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004184 STAT_SCOPE_TXT_MAXLEN);
4185
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004186 /* 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 +02004187 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004188 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004189 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004190 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004191 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004192 }
4193
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004194 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004195 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004196 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004197 uri->uri_prefix,
4198 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004199 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004200 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 else
4202 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004203 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004204 uri->uri_prefix,
4205 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004206 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004207 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004208
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004210 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004211 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004212 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004213 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004214 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004215 "",
4216 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004217 else
4218 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004219 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004220 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004221 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004222 ";norefresh",
4223 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004224 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004225
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004226 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004227 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004228 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004229 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4230 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004231 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004232
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004233 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004234 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004235 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004236 (uri->refresh > 0) ? ";norefresh" : "",
4237 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004238
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004239 chunk_appendf(&trash,
4240 "</ul></td>"
4241 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4242 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4243 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4244 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4245 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4246 "</ul>"
4247 "</td>"
4248 "</tr></table>\n"
4249 ""
4250 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004251
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004252 if (appctx->ctx.stats.st_code) {
4253 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004254 case STAT_STATUS_DONE:
4255 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004256 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004257 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004258 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004259 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004260 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4261 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004262 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004263 break;
4264 case STAT_STATUS_NONE:
4265 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004266 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004267 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004268 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004269 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004270 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4271 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004272 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004273 break;
4274 case STAT_STATUS_PART:
4275 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004276 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004277 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004278 "Action partially processed.<br>"
4279 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004280 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004281 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4282 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004283 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004284 break;
4285 case STAT_STATUS_ERRP:
4286 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004287 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004288 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004289 "Action not processed because of invalid parameters."
4290 "<ul>"
4291 "<li>The action is maybe unknown.</li>"
4292 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4293 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4294 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004295 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004296 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4297 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004298 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004299 break;
4300 case STAT_STATUS_EXCD:
4301 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004302 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004303 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4305 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004306 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004307 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4308 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004309 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 break;
4311 case STAT_STATUS_DENY:
4312 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004313 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004314 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004316 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004317 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4318 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004319 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004320 break;
4321 default:
4322 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004323 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004324 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004326 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004327 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4328 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004329 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330 }
4331 chunk_appendf(&trash, "<p>\n");
4332 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004333}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004334
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004335/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4336 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004337 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004338static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004340 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004341}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004342
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004343/* This function dumps statistics onto the stream interface's read buffer in
4344 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004345 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4346 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
Willy Tarreau87b09662015-04-03 00:22:06 +02004347 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004348 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004349 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004350static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004351{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004352 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004353 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004354 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004355
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004358 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004359 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004360 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004362
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004363 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004364 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004365 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004366 else
4367 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004368
Willy Tarreaubc18da12015-03-13 14:00:47 +01004369 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004370 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004372 }
Willy Tarreauae526782010-03-04 20:34:23 +01004373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004374 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004376
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004377 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004378 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004379 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004380 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004381 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004382 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004383 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004384 }
Willy Tarreau91861262007-10-17 17:06:05 +02004385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004386 appctx->ctx.stats.px = proxy;
4387 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4388 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004389 /* fall through */
4390
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 case STAT_ST_LIST:
4392 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004393 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004394 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004395 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004396 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004397 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004398
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004399 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004400 /* skip the disabled proxies, global frontend and non-networked ones */
4401 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004402 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004403 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004404
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004405 appctx->ctx.stats.px = px->next;
4406 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004407 }
4408 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004409
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004410 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004411 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004412
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004413 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004414 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004415 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004416 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004417 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004418 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004419 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004420 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004421
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004422 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004423 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004425 case STAT_ST_FIN:
4426 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004428 default:
4429 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004430 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004431 return -1;
4432 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004433}
Willy Tarreauae526782010-03-04 20:34:23 +01004434
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004435/* We reached the stats page through a POST request. The appctx is
4436 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004437 * Parse the posted data and enable/disable servers if necessary.
4438 * Returns 1 if request was parsed or zero if it needs more data.
4439 */
4440static int stats_process_http_post(struct stream_interface *si)
4441{
Willy Tarreau87b09662015-04-03 00:22:06 +02004442 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004443 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004444
4445 struct proxy *px = NULL;
4446 struct server *sv = NULL;
4447
4448 char key[LINESIZE];
4449 int action = ST_ADM_ACTION_NONE;
4450 int reprocess = 0;
4451
4452 int total_servers = 0;
4453 int altered_servers = 0;
4454
4455 char *first_param, *cur_param, *next_param, *end_params;
4456 char *st_cur_param = NULL;
4457 char *st_next_param = NULL;
4458
4459 struct chunk *temp;
4460 int reql;
4461
4462 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004463 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004464 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004465 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004466 goto out;
4467 }
4468
Willy Tarreaueee5b512015-04-03 23:46:31 +02004469 reql = bo_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004470 if (reql <= 0) {
4471 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004472 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004473 return 0;
4474 }
4475
4476 first_param = temp->str;
4477 end_params = temp->str + reql;
4478 cur_param = next_param = end_params;
4479 *end_params = '\0';
4480
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004481 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004482
4483 /*
4484 * Parse the parameters in reverse order to only store the last value.
4485 * From the html form, the backend and the action are at the end.
4486 */
4487 while (cur_param > first_param) {
4488 char *value;
4489 int poffset, plen;
4490
4491 cur_param--;
4492
4493 if ((*cur_param == '&') || (cur_param == first_param)) {
4494 reprocess_servers:
4495 /* Parse the key */
4496 poffset = (cur_param != first_param ? 1 : 0);
4497 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4498 if ((plen > 0) && (plen <= sizeof(key))) {
4499 strncpy(key, cur_param + poffset, plen);
4500 key[plen - 1] = '\0';
4501 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004502 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004503 goto out;
4504 }
4505
4506 /* Parse the value */
4507 value = key;
4508 while (*value != '\0' && *value != '=') {
4509 value++;
4510 }
4511 if (*value == '=') {
4512 /* Ok, a value is found, we can mark the end of the key */
4513 *value++ = '\0';
4514 }
4515 if (url_decode(key) < 0 || url_decode(value) < 0)
4516 break;
4517
4518 /* Now we can check the key to see what to do */
4519 if (!px && (strcmp(key, "b") == 0)) {
4520 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4521 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004522 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004523 goto out;
4524 }
4525 }
4526 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004527 if (strcmp(value, "ready") == 0) {
4528 action = ST_ADM_ACTION_READY;
4529 }
4530 else if (strcmp(value, "drain") == 0) {
4531 action = ST_ADM_ACTION_DRAIN;
4532 }
4533 else if (strcmp(value, "maint") == 0) {
4534 action = ST_ADM_ACTION_MAINT;
4535 }
4536 else if (strcmp(value, "shutdown") == 0) {
4537 action = ST_ADM_ACTION_SHUTDOWN;
4538 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004539 else if (strcmp(value, "dhlth") == 0) {
4540 action = ST_ADM_ACTION_DHLTH;
4541 }
4542 else if (strcmp(value, "ehlth") == 0) {
4543 action = ST_ADM_ACTION_EHLTH;
4544 }
4545 else if (strcmp(value, "hrunn") == 0) {
4546 action = ST_ADM_ACTION_HRUNN;
4547 }
4548 else if (strcmp(value, "hnolb") == 0) {
4549 action = ST_ADM_ACTION_HNOLB;
4550 }
4551 else if (strcmp(value, "hdown") == 0) {
4552 action = ST_ADM_ACTION_HDOWN;
4553 }
4554 else if (strcmp(value, "dagent") == 0) {
4555 action = ST_ADM_ACTION_DAGENT;
4556 }
4557 else if (strcmp(value, "eagent") == 0) {
4558 action = ST_ADM_ACTION_EAGENT;
4559 }
4560 else if (strcmp(value, "arunn") == 0) {
4561 action = ST_ADM_ACTION_ARUNN;
4562 }
4563 else if (strcmp(value, "adown") == 0) {
4564 action = ST_ADM_ACTION_ADOWN;
4565 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004566 /* else these are the old supported methods */
4567 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004568 action = ST_ADM_ACTION_DISABLE;
4569 }
4570 else if (strcmp(value, "enable") == 0) {
4571 action = ST_ADM_ACTION_ENABLE;
4572 }
4573 else if (strcmp(value, "stop") == 0) {
4574 action = ST_ADM_ACTION_STOP;
4575 }
4576 else if (strcmp(value, "start") == 0) {
4577 action = ST_ADM_ACTION_START;
4578 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004579 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004580 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004581 goto out;
4582 }
4583 }
4584 else if (strcmp(key, "s") == 0) {
4585 if (!(px && action)) {
4586 /*
4587 * Indicates that we'll need to reprocess the parameters
4588 * as soon as backend and action are known
4589 */
4590 if (!reprocess) {
4591 st_cur_param = cur_param;
4592 st_next_param = next_param;
4593 }
4594 reprocess = 1;
4595 }
4596 else if ((sv = findserver(px, value)) != NULL) {
4597 switch (action) {
4598 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004599 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004600 altered_servers++;
4601 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004602 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004603 }
4604 break;
4605 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004606 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004607 altered_servers++;
4608 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004609 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004610 }
4611 break;
4612 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004613 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4614 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4615 altered_servers++;
4616 total_servers++;
4617 }
4618 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004619 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004620 if (sv->admin & SRV_ADMF_FDRAIN) {
4621 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4622 altered_servers++;
4623 total_servers++;
4624 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004625 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004626 case ST_ADM_ACTION_DHLTH:
4627 if (sv->check.state & CHK_ST_CONFIGURED) {
4628 sv->check.state &= ~CHK_ST_ENABLED;
4629 altered_servers++;
4630 total_servers++;
4631 }
4632 break;
4633 case ST_ADM_ACTION_EHLTH:
4634 if (sv->check.state & CHK_ST_CONFIGURED) {
4635 sv->check.state |= CHK_ST_ENABLED;
4636 altered_servers++;
4637 total_servers++;
4638 }
4639 break;
4640 case ST_ADM_ACTION_HRUNN:
4641 if (!(sv->track)) {
4642 sv->check.health = sv->check.rise + sv->check.fall - 1;
4643 srv_set_running(sv, "changed from Web interface");
4644 altered_servers++;
4645 total_servers++;
4646 }
4647 break;
4648 case ST_ADM_ACTION_HNOLB:
4649 if (!(sv->track)) {
4650 sv->check.health = sv->check.rise + sv->check.fall - 1;
4651 srv_set_stopping(sv, "changed from Web interface");
4652 altered_servers++;
4653 total_servers++;
4654 }
4655 break;
4656 case ST_ADM_ACTION_HDOWN:
4657 if (!(sv->track)) {
4658 sv->check.health = 0;
4659 srv_set_stopped(sv, "changed from Web interface");
4660 altered_servers++;
4661 total_servers++;
4662 }
4663 break;
4664 case ST_ADM_ACTION_DAGENT:
4665 if (sv->agent.state & CHK_ST_CONFIGURED) {
4666 sv->agent.state &= ~CHK_ST_ENABLED;
4667 altered_servers++;
4668 total_servers++;
4669 }
4670 break;
4671 case ST_ADM_ACTION_EAGENT:
4672 if (sv->agent.state & CHK_ST_CONFIGURED) {
4673 sv->agent.state |= CHK_ST_ENABLED;
4674 altered_servers++;
4675 total_servers++;
4676 }
4677 break;
4678 case ST_ADM_ACTION_ARUNN:
4679 if (sv->agent.state & CHK_ST_ENABLED) {
4680 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4681 srv_set_running(sv, "changed from Web interface");
4682 altered_servers++;
4683 total_servers++;
4684 }
4685 break;
4686 case ST_ADM_ACTION_ADOWN:
4687 if (sv->agent.state & CHK_ST_ENABLED) {
4688 sv->agent.health = 0;
4689 srv_set_stopped(sv, "changed from Web interface");
4690 altered_servers++;
4691 total_servers++;
4692 }
4693 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004694 case ST_ADM_ACTION_READY:
4695 srv_adm_set_ready(sv);
4696 altered_servers++;
4697 total_servers++;
4698 break;
4699 case ST_ADM_ACTION_DRAIN:
4700 srv_adm_set_drain(sv);
4701 altered_servers++;
4702 total_servers++;
4703 break;
4704 case ST_ADM_ACTION_MAINT:
4705 srv_adm_set_maint(sv);
4706 altered_servers++;
4707 total_servers++;
4708 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004709 case ST_ADM_ACTION_SHUTDOWN:
4710 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004711 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004712
4713 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4714 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004715 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004716
4717 altered_servers++;
4718 total_servers++;
4719 }
4720 break;
4721 }
4722 } else {
4723 /* the server name is unknown or ambiguous (duplicate names) */
4724 total_servers++;
4725 }
4726 }
4727 if (reprocess && px && action) {
4728 /* Now, we know the backend and the action chosen by the user.
4729 * We can safely restart from the first server parameter
4730 * to reprocess them
4731 */
4732 cur_param = st_cur_param;
4733 next_param = st_next_param;
4734 reprocess = 0;
4735 goto reprocess_servers;
4736 }
4737
4738 next_param = cur_param;
4739 }
4740 }
4741
4742 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004743 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004744 }
4745 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004746 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004747 }
4748 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004749 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004750 }
4751 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004752 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004753 }
4754 out:
4755 return 1;
4756}
4757
4758
4759static int stats_send_http_headers(struct stream_interface *si)
4760{
Willy Tarreau87b09662015-04-03 00:22:06 +02004761 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004762 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004763 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004764
4765 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004766 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004767 "Cache-Control: no-cache\r\n"
4768 "Connection: close\r\n"
4769 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004770 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004771
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004772 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004773 chunk_appendf(&trash, "Refresh: %d\r\n",
4774 uri->refresh);
4775
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004776 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4777
4778 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4779 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4780 else
4781 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004782
Willy Tarreaueee5b512015-04-03 23:46:31 +02004783 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004784 s->logs.tv_request = now;
4785
Willy Tarreaubc18da12015-03-13 14:00:47 +01004786 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004787 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004788 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004789 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004790
4791 return 1;
4792}
4793
4794static int stats_send_http_redirect(struct stream_interface *si)
4795{
4796 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004797 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004798 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004799 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004800
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004801 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004802 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004803 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004804 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004805 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004806 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004807 }
4808
4809 /* We don't want to land on the posted stats page because a refresh will
4810 * repost the data. We don't want this to happen on accident so we redirect
4811 * the browse to the stats page with a GET.
4812 */
4813 chunk_printf(&trash,
4814 "HTTP/1.1 303 See Other\r\n"
4815 "Cache-Control: no-cache\r\n"
4816 "Content-Type: text/plain\r\n"
4817 "Connection: close\r\n"
4818 "Location: %s;st=%s%s%s%s\r\n"
4819 "\r\n",
4820 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004821 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4822 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4823 stat_status_codes[appctx->ctx.stats.st_code]) ?
4824 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004825 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004826 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4827 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004828 scope_txt);
4829
Willy Tarreaueee5b512015-04-03 23:46:31 +02004830 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004831 s->logs.tv_request = now;
4832
Willy Tarreaubc18da12015-03-13 14:00:47 +01004833 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004834 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004835 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004836 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004837
4838 return 1;
4839}
4840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004841/* This I/O handler runs as an applet embedded in a stream interface. It is
4842 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004843 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004844 * automatically unregisters itself once transfer is complete.
4845 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02004846static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004847{
Willy Tarreau00a37f02015-04-13 12:05:19 +02004848 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02004849 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004850 struct channel *req = si_oc(si);
4851 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004852
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004853 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4854 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004855
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004856 /* check that the output is not closed */
4857 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004858 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004859
Willy Tarreau347a35d2013-11-22 17:51:09 +01004860 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004861 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004862 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02004863 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004864 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004865 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004866 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004867 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004868 }
4869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004870 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004871 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004872 unsigned int data_len;
4873 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004874 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004875
4876 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4877 /* One difficulty we're facing is that we must prevent
4878 * the input data from being automatically forwarded to
4879 * the output area. For this, we temporarily disable
4880 * forwarding on the channel.
4881 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004882 last_fwd = si_ic(si)->to_forward;
4883 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004884 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004885 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004886 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004887 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02004888 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004889 }
4890 }
4891
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004892 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004893 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004894 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004895
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004896 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004897
4898 /* Now we must either adjust or remove the chunk size. This is
4899 * not easy because the chunk size might wrap at the end of the
4900 * buffer, so we pretend we have nothing in the buffer, we write
4901 * the size, then restore the buffer's contents. Note that we can
4902 * only do that because no forwarding is scheduled on the stats
4903 * applet.
4904 */
4905 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004906 si_ic(si)->total -= (last_len - prev_len);
4907 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004908
4909 if (last_len != data_len) {
4910 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01004911 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02004912 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004913
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004914 si_ic(si)->total += (last_len - data_len);
4915 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004916 }
4917 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004918 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004919 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004920 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004921
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004922 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004923 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004924 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004925 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004926 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004927 }
4928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004929 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004930 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004931 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004932 }
4933
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004934 if (appctx->st0 == STAT_HTTP_DONE) {
4935 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4936 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01004937 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004938 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02004939 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004940 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004941 }
4942 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004943 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004944 res->flags |= CF_READ_NULL;
4945 si_shutr(si);
4946 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004947
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004948 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4949 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004950
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004951 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004952 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4953 si_shutr(si);
4954 res->flags |= CF_READ_NULL;
4955 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004956 }
Willy Tarreau828824a2015-04-19 17:20:03 +02004957 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02004958 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02004959}
4960
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004961
Willy Tarreau909d5172012-11-26 03:04:41 +01004962static inline const char *get_conn_ctrl_name(const struct connection *conn)
4963{
Willy Tarreau3c728722014-01-23 13:50:42 +01004964 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004965 return "NONE";
4966 return conn->ctrl->name;
4967}
4968
4969static inline const char *get_conn_xprt_name(const struct connection *conn)
4970{
4971 static char ptr[17];
4972
Willy Tarreauaad69382014-01-23 14:21:42 +01004973 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004974 return "NONE";
4975
4976 if (conn->xprt == &raw_sock)
4977 return "RAW";
4978
4979#ifdef USE_OPENSSL
4980 if (conn->xprt == &ssl_sock)
4981 return "SSL";
4982#endif
4983 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4984 return ptr;
4985}
4986
4987static inline const char *get_conn_data_name(const struct connection *conn)
4988{
4989 static char ptr[17];
4990
4991 if (!conn->data)
4992 return "NONE";
4993
4994 if (conn->data == &sess_conn_cb)
4995 return "SESS";
4996
4997 if (conn->data == &si_conn_cb)
4998 return "STRM";
4999
5000 if (conn->data == &check_conn_cb)
5001 return "CHCK";
5002
5003 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5004 return ptr;
5005}
5006
Willy Tarreau87b09662015-04-03 00:22:06 +02005007/* This function dumps a complete stream state onto the stream interface's
5008 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005009 * 0 if the output buffer is full and it needs to be called again, otherwise
5010 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005011 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005012static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005013{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005014 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005015 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005016 extern const char *monthname[12];
5017 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005018 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005019 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005020
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005021 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005022
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005023 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005024 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005025 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005026 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005027 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005028 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005029 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005030 appctx->ctx.sess.uid = 0;
5031 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005032 return 1;
5033 }
5034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005035 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005036 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 appctx->ctx.sess.uid = sess->uniq_id;
5038 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005039 /* fall through */
5040
5041 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005042 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005043 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005044 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005045 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005046 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5047 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005048 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005049 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005050
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005051 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005052 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005053 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005054 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005055 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005056 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005057 break;
5058 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005059 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005060 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005061 default:
5062 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005063 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005064 break;
5065 }
5066
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005067 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005068 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005069 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005070
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005071 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005072 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005073 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5074 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5075 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005076
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005077 if (conn)
5078 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005079
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005080 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005081 case AF_INET:
5082 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005083 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005084 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005085 break;
5086 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005087 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005088 break;
5089 default:
5090 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005091 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005092 break;
5093 }
5094
Willy Tarreau50943332011-09-02 17:33:05 +02005095 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005096 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005097 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005098 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005099 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5100 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005101 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005102
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005103 conn = objt_conn(sess->si[1].end);
5104 if (conn)
5105 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005106
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005107 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005108 case AF_INET:
5109 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005110 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005111 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005112 break;
5113 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005114 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005115 break;
5116 default:
5117 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005118 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005119 break;
5120 }
5121
5122 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005123 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005124 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005125 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5126 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005127 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005128 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005129
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005130 if (conn)
5131 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005132
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005133 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005134 case AF_INET:
5135 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005136 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005137 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005138 break;
5139 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005140 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005141 break;
5142 default:
5143 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005144 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005145 break;
5146 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005147
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005148 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005149 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005150 sess->task,
5151 sess->task->state,
5152 sess->task->nice, sess->task->calls,
5153 sess->task->expire ?
5154 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5155 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5156 TICKS_TO_MS(1000)) : "<NEVER>",
5157 task_in_rq(sess->task) ? ", running" : "");
5158
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005159 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005160 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005161 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5162
Willy Tarreaueee5b512015-04-03 23:46:31 +02005163 if (sess->txn)
5164 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005165 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
Willy Tarreaueee5b512015-04-03 23:46:31 +02005166 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5167 http_msg_state_str(sess->txn->req.msg_state), http_msg_state_str(sess->txn->rsp.msg_state), !LIST_ISEMPTY(&sess->buffer_wait));
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005168
5169 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005170 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005171 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005172 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005173 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005174 obj_type_name(sess->si[0].end),
5175 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005176 sess->si[0].exp ?
5177 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5178 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5179 TICKS_TO_MS(1000)) : "<NEVER>",
5180 sess->si[0].err_type);
5181
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005182 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005183 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005184 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005185 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005186 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005187 obj_type_name(sess->si[1].end),
5188 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005189 sess->si[1].exp ?
5190 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5191 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5192 TICKS_TO_MS(1000)) : "<NEVER>",
5193 sess->si[1].err_type);
5194
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005195 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005196 chunk_appendf(&trash,
5197 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005198 conn,
5199 get_conn_ctrl_name(conn),
5200 get_conn_xprt_name(conn),
5201 get_conn_data_name(conn),
5202 obj_type_name(conn->target),
5203 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005204
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005205 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005206 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005207 conn->flags,
5208 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005209 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005210 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005211 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005212 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005213 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5214 chunk_appendf(&trash,
5215 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5216 tmpctx,
5217 tmpctx->st0,
5218 tmpctx->st1,
5219 tmpctx->st2,
5220 tmpctx->applet->name);
5221 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005222
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005223 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005224 chunk_appendf(&trash,
5225 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005226 conn,
5227 get_conn_ctrl_name(conn),
5228 get_conn_xprt_name(conn),
5229 get_conn_data_name(conn),
5230 obj_type_name(conn->target),
5231 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005232
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005233 chunk_appendf(&trash,
5234 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005235 conn->flags,
5236 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005237 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005238 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005239 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005240 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005241 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5242 chunk_appendf(&trash,
5243 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5244 tmpctx,
5245 tmpctx->st0,
5246 tmpctx->st1,
5247 tmpctx->st2,
5248 tmpctx->applet->name);
5249 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005250
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005251 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005252 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005253 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005254 &sess->req,
5255 sess->req.flags, sess->req.analysers,
5256 sess->req.pipe ? sess->req.pipe->data : 0,
5257 sess->req.to_forward, sess->req.total,
5258 sess->req.analyse_exp ?
5259 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005260 TICKS_TO_MS(1000)) : "<NEVER>");
5261
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005262 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005263 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005264 sess->req.rex ?
5265 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005266 TICKS_TO_MS(1000)) : "<NEVER>");
5267
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005268 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005269 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005270 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005271 sess->req.wex ?
5272 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005273 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005274 sess->req.buf,
5275 sess->req.buf->data, sess->req.buf->o,
5276 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005277 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005278 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005279
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005280 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005281 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005282 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005283 &sess->res,
5284 sess->res.flags, sess->res.analysers,
5285 sess->res.pipe ? sess->res.pipe->data : 0,
5286 sess->res.to_forward, sess->res.total,
5287 sess->res.analyse_exp ?
5288 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005289 TICKS_TO_MS(1000)) : "<NEVER>");
5290
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005291 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005292 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005293 sess->res.rex ?
5294 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005295 TICKS_TO_MS(1000)) : "<NEVER>");
5296
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005297 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005298 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005299 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005300 sess->res.wex ?
5301 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005302 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005303 sess->res.buf,
5304 sess->res.buf->data, sess->res.buf->o,
5305 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005306 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005307 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005308
Willy Tarreaubc18da12015-03-13 14:00:47 +01005309 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005310 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005311 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005312 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005313
5314 /* use other states to dump the contents */
5315 }
5316 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005317 appctx->ctx.sess.uid = 0;
5318 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005319 return 1;
5320}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005321
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005322static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005323{
5324 struct appctx *appctx = __objt_appctx(si->end);
5325
5326 switch (appctx->st2) {
5327 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005328 /* Display the column headers. If the message cannot be sent,
5329 * quit the fucntion with returning 0. The function is called
5330 * later and restart at the state "STAT_ST_INIT".
5331 */
5332 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005333 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005334 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005335 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005336 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005337 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005338
5339 /* Now, we start the browsing of the references lists.
5340 * Note that the following call to LIST_ELEM return bad pointer. The only
5341 * avalaible field of this pointer is <list>. It is used with the function
5342 * pat_list_get_next() for retruning the first avalaible entry
5343 */
5344 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5345 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5346 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005347 appctx->st2 = STAT_ST_LIST;
5348 /* fall through */
5349
5350 case STAT_ST_LIST:
5351 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005352 chunk_reset(&trash);
5353
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005354 /* Build messages. If the reference is used by another category than
5355 * the listed categorie, display the information in the massage.
5356 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005357 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005358 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5359 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005360
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005361 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005362 /* let's try again later from this stream. We add ourselves into
5363 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005364 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005365 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005366 return 0;
5367 }
5368
5369 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005370 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5371 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005372 }
5373
5374 appctx->st2 = STAT_ST_FIN;
5375 /* fall through */
5376
5377 default:
5378 appctx->st2 = STAT_ST_FIN;
5379 return 1;
5380 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005381 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005382}
5383
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005384static int stats_map_lookup(struct stream_interface *si)
5385{
5386 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005387 struct sample sample;
5388 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005389 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005390
5391 switch (appctx->st2) {
5392 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005393 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005394 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005395 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005396 appctx->st2 = STAT_ST_LIST;
5397 /* fall through */
5398
5399 case STAT_ST_LIST:
5400 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005401 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005402 /* initialise chunk to build new message */
5403 chunk_reset(&trash);
5404
5405 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005406 sample.type = SMP_T_STR;
5407 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005408 sample.data.str.len = appctx->ctx.map.chunk.len;
5409 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005410 if (appctx->ctx.map.expr->pat_head->match &&
5411 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005412 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5413 else
5414 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005415
5416 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005417 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5418 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5419 break;
5420 if (match_method >= PAT_MATCH_NUM)
5421 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5422 else
5423 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005424
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005425 /* case sensitive */
5426 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5427 chunk_appendf(&trash, ", case=insensitive");
5428 else
5429 chunk_appendf(&trash, ", case=sensitive");
5430
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005431 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005432 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005433 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5434 chunk_appendf(&trash, ", found=no");
5435 else
5436 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005437 }
5438
5439 /* Display match and match info */
5440 else {
5441 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005442 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5443 chunk_appendf(&trash, ", found=yes");
5444 else
5445 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005446
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005447 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005448 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005449 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005450 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005451 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005452
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005453 /* display pattern */
5454 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5455 if (pat->ref && pat->ref->pattern)
5456 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5457 else
5458 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005459 }
5460 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005461 if (pat->ref && pat->ref->pattern)
5462 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5463 else
5464 chunk_appendf(&trash, ", pattern=unknown");
5465 }
5466
5467 /* display return value */
5468 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5469 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005470 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005471 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005472 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005473 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005474 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005475 }
5476
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005477 chunk_appendf(&trash, "\n");
5478
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005479 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005480 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005481 /* let's try again later from this stream. We add ourselves into
5482 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005483 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005484 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005485 return 0;
5486 }
5487
5488 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005489 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5490 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005491 }
5492
5493 appctx->st2 = STAT_ST_FIN;
5494 /* fall through */
5495
5496 default:
5497 appctx->st2 = STAT_ST_FIN;
5498 free(appctx->ctx.map.chunk.str);
5499 return 1;
5500 }
5501}
5502
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005503static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005504{
5505 struct appctx *appctx = __objt_appctx(si->end);
5506
5507 switch (appctx->st2) {
5508
5509 case STAT_ST_INIT:
5510 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005511 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5512 struct pat_ref_elt *, list);
5513 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5514 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005515 appctx->st2 = STAT_ST_LIST;
5516 /* fall through */
5517
5518 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005519 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005520 chunk_reset(&trash);
5521
5522 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005523 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005524 chunk_appendf(&trash, "%p %s %s\n",
5525 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5526 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005527 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005528 chunk_appendf(&trash, "%p %s\n",
5529 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005530
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005531 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005532 /* let's try again later from this stream. We add ourselves into
5533 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005534 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005535 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005536 return 0;
5537 }
5538
5539 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005540 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5541 struct pat_ref_elt *, list);
5542 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005543 break;
5544 }
5545
5546 appctx->st2 = STAT_ST_FIN;
5547 /* fall through */
5548
5549 default:
5550 appctx->st2 = STAT_ST_FIN;
5551 return 1;
5552 }
5553}
5554
Willy Tarreau87b09662015-04-03 00:22:06 +02005555/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005556 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005557 * to be called again, otherwise non-zero. It is designed to be called
5558 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005559 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005560static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005561{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005562 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005563 struct connection *conn;
5564
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005565 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005566 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005567 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005568 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 if (appctx->st2 == STAT_ST_LIST) {
5570 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5571 LIST_DEL(&appctx->ctx.sess.bref.users);
5572 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005573 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005574 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005575 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005576 }
5577
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005578 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005580 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005581 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005582 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005583 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005584 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005585 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005586 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005587 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005588 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005589 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005590 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005591 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005592 /* fall through */
5593
Willy Tarreau295a8372011-03-10 11:25:07 +01005594 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005595 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005596 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5597 LIST_DEL(&appctx->ctx.sess.bref.users);
5598 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005599 }
5600
5601 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005602 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005603 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005604 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005605
Willy Tarreau87b09662015-04-03 00:22:06 +02005606 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005607
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005608 if (appctx->ctx.sess.target) {
5609 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005610 goto next_sess;
5611
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005612 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005613 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005614 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005615 return 0;
5616
Willy Tarreau87b09662015-04-03 00:22:06 +02005617 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005618 LIST_DEL(&appctx->ctx.sess.bref.users);
5619 LIST_INIT(&appctx->ctx.sess.bref.users);
5620 if (appctx->ctx.sess.target != (void *)-1) {
5621 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005622 break;
5623 }
5624 else
5625 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005626 }
5627
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005628 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005629 "%p: proto=%s",
5630 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005631 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005632
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005633
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005634 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005635 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005636 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005637 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005638 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005639 " src=%s:%d fe=%s be=%s srv=%s",
5640 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005641 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005642 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005643 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005644 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005645 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005646 break;
5647 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005648 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005649 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005650 strm_li(curr_sess)->luid,
5651 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005652 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005653 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005654 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005655 break;
5656 }
5657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005658 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005659 " ts=%02x age=%s calls=%d",
5660 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005661 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5662 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005664 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005665 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005666 curr_sess->req.flags,
5667 curr_sess->req.buf->i,
5668 curr_sess->req.analysers,
5669 curr_sess->req.rex ?
5670 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005671 TICKS_TO_MS(1000)) : "");
5672
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005673 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005674 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005675 curr_sess->req.wex ?
5676 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005677 TICKS_TO_MS(1000)) : "");
5678
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005679 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005680 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005681 curr_sess->req.analyse_exp ?
5682 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005683 TICKS_TO_MS(1000)) : "");
5684
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005685 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005686 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005687 curr_sess->res.flags,
5688 curr_sess->res.buf->i,
5689 curr_sess->res.analysers,
5690 curr_sess->res.rex ?
5691 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005692 TICKS_TO_MS(1000)) : "");
5693
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005694 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005695 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005696 curr_sess->res.wex ?
5697 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005698 TICKS_TO_MS(1000)) : "");
5699
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005700 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005701 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005702 curr_sess->res.analyse_exp ?
5703 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005704 TICKS_TO_MS(1000)) : "");
5705
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005706 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005707 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005708 " s0=[%d,%1xh,fd=%d,ex=%s]",
5709 curr_sess->si[0].state,
5710 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005711 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005712 curr_sess->si[0].exp ?
5713 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5714 TICKS_TO_MS(1000)) : "");
5715
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005716 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005717 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005718 " s1=[%d,%1xh,fd=%d,ex=%s]",
5719 curr_sess->si[1].state,
5720 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005721 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005722 curr_sess->si[1].exp ?
5723 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5724 TICKS_TO_MS(1000)) : "");
5725
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005726 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005727 " exp=%s",
5728 curr_sess->task->expire ?
5729 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5730 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005731 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005732 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005733
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005734 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005735
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005736 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005737 /* let's try again later from this stream. We add ourselves into
5738 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005739 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005740 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005741 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005742 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005743 }
5744
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005745 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005746 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005747 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005749 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005750 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005751 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005752 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005753 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005754 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005755
Willy Tarreaubc18da12015-03-13 14:00:47 +01005756 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005757 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005758 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005759 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005760
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005761 appctx->ctx.sess.target = NULL;
5762 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005763 return 1;
5764 }
5765
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005766 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005767 /* fall through */
5768
5769 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005770 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005771 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005772 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005773}
5774
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005775/* This is called when the stream interface is closed. For instance, upon an
5776 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005777 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005778 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005779static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005780{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005781 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5782 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5783 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005784 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005785 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5786 free(appctx->ctx.cli.err);
5787 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005788 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5789 free(appctx->ctx.map.chunk.str);
5790 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005791}
5792
Willy Tarreau20e99322013-04-13 09:22:25 +02005793/* This function is used to either dump tables states (when action is set
5794 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005795 * It returns 0 if the output buffer is full and it needs to be called
5796 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005797 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005798static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005799{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005800 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02005801 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005802 struct ebmb_node *eb;
5803 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005804 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005805 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005806
5807 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005808 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005809 * - STAT_ST_INIT : the first call
5810 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005811 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005812 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005813 * and the entry pointer points to the next entry to be dumped,
5814 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005815 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005816 * data though.
5817 */
5818
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005819 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005820 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005821 if (appctx->st2 == STAT_ST_LIST) {
5822 appctx->ctx.table.entry->ref_cnt--;
5823 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005824 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005825 return 1;
5826 }
5827
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005828 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005830 while (appctx->st2 != STAT_ST_FIN) {
5831 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005832 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005833 appctx->ctx.table.proxy = appctx->ctx.table.target;
5834 if (!appctx->ctx.table.proxy)
5835 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005836
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005837 appctx->ctx.table.entry = NULL;
5838 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005839 break;
5840
Willy Tarreau295a8372011-03-10 11:25:07 +01005841 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005842 if (!appctx->ctx.table.proxy ||
5843 (appctx->ctx.table.target &&
5844 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5845 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005846 break;
5847 }
5848
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005849 if (appctx->ctx.table.proxy->table.size) {
5850 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5851 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005852 return 0;
5853
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005854 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005855 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005856 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005857 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005858 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005859 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5860 appctx->ctx.table.entry->ref_cnt++;
5861 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005862 break;
5863 }
5864 }
5865 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005866 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005867 break;
5868
Willy Tarreau295a8372011-03-10 11:25:07 +01005869 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005870 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005871
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005872 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005873 /* we're filtering on some data contents */
5874 void *ptr;
5875 long long data;
5876
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005877 dt = appctx->ctx.table.data_type;
5878 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5879 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005880 dt);
5881
5882 data = 0;
5883 switch (stktable_data_types[dt].std_type) {
5884 case STD_T_SINT:
5885 data = stktable_data_cast(ptr, std_t_sint);
5886 break;
5887 case STD_T_UINT:
5888 data = stktable_data_cast(ptr, std_t_uint);
5889 break;
5890 case STD_T_ULL:
5891 data = stktable_data_cast(ptr, std_t_ull);
5892 break;
5893 case STD_T_FRQP:
5894 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005895 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005896 break;
5897 }
5898
5899 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005900 if ((data < appctx->ctx.table.value &&
5901 (appctx->ctx.table.data_op == STD_OP_EQ ||
5902 appctx->ctx.table.data_op == STD_OP_GT ||
5903 appctx->ctx.table.data_op == STD_OP_GE)) ||
5904 (data == appctx->ctx.table.value &&
5905 (appctx->ctx.table.data_op == STD_OP_NE ||
5906 appctx->ctx.table.data_op == STD_OP_GT ||
5907 appctx->ctx.table.data_op == STD_OP_LT)) ||
5908 (data > appctx->ctx.table.value &&
5909 (appctx->ctx.table.data_op == STD_OP_EQ ||
5910 appctx->ctx.table.data_op == STD_OP_LT ||
5911 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005912 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005913 }
5914
Simon Hormanc88b8872011-06-15 15:18:49 +09005915 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005916 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5917 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005918 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005919
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005920 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005921
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005922 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005923 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005924 struct stksess *old = appctx->ctx.table.entry;
5925 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005926 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005927 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5928 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5929 stksess_kill(&appctx->ctx.table.proxy->table, old);
5930 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005931 break;
5932 }
5933
Simon Hormanc88b8872011-06-15 15:18:49 +09005934
5935 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005936 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5937 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5938 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005939
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005940 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5941 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005942 break;
5943
Willy Tarreau295a8372011-03-10 11:25:07 +01005944 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005945 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005946 break;
5947 }
5948 }
5949 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005950}
5951
Willy Tarreaud426a182010-03-05 14:58:26 +01005952/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005953 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5954 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5955 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5956 * lines are respected within the limit of 70 output chars. Lines that are
5957 * continuation of a previous truncated line begin with "+" instead of " "
5958 * after the offset. The new pointer is returned.
5959 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005960static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5961 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005962{
5963 int end;
5964 unsigned char c;
5965
5966 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005967 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005968 return ptr;
5969
Willy Tarreau77804732012-10-29 16:14:26 +01005970 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005971
Willy Tarreaud426a182010-03-05 14:58:26 +01005972 while (ptr < len && ptr < bsize) {
5973 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005974 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005975 if (out->len > end - 2)
5976 break;
5977 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005978 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005979 if (out->len > end - 3)
5980 break;
5981 out->str[out->len++] = '\\';
5982 switch (c) {
5983 case '\t': c = 't'; break;
5984 case '\n': c = 'n'; break;
5985 case '\r': c = 'r'; break;
5986 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005987 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005988 }
5989 out->str[out->len++] = c;
5990 } else {
5991 if (out->len > end - 5)
5992 break;
5993 out->str[out->len++] = '\\';
5994 out->str[out->len++] = 'x';
5995 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5996 out->str[out->len++] = hextab[c & 0xF];
5997 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005998 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005999 /* we had a line break, let's return now */
6000 out->str[out->len++] = '\n';
6001 *line = ptr;
6002 return ptr;
6003 }
6004 }
6005 /* we have an incomplete line, we return it as-is */
6006 out->str[out->len++] = '\n';
6007 return ptr;
6008}
6009
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006010/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006011 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006012 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006013 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006014static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006015{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006016 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006017 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006018
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006019 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006020 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006021
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006022 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006023
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006024 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006025 /* the function had not been called yet, let's prepare the
6026 * buffer for a response.
6027 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006028 struct tm tm;
6029
6030 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006031 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006032 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6033 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6034 error_snapshot_id);
6035
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006036 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006037 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006038 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006039 return 0;
6040 }
6041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006042 appctx->ctx.errors.px = proxy;
6043 appctx->ctx.errors.buf = 0;
6044 appctx->ctx.errors.bol = 0;
6045 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006046 }
6047
6048 /* we have two inner loops here, one for the proxy, the other one for
6049 * the buffer.
6050 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006051 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006052 struct error_snapshot *es;
6053
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006054 if (appctx->ctx.errors.buf == 0)
6055 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006056 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006057 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006058
6059 if (!es->when.tv_sec)
6060 goto next;
6061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006062 if (appctx->ctx.errors.iid >= 0 &&
6063 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6064 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006065 goto next;
6066
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006067 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006068 /* just print headers now */
6069
6070 char pn[INET6_ADDRSTRLEN];
6071 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006072 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006073
6074 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006075 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006076 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006077 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006078
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006079 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6080 case AF_INET:
6081 case AF_INET6:
6082 port = get_host_port(&es->src);
6083 break;
6084 default:
6085 port = 0;
6086 }
6087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006088 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006089 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006090 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006091 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006092 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006093 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006094 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6095 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006096 break;
6097 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006098 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006099 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006100 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006101 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006102 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006103 break;
6104 }
6105
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006106 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006107 ", server %s (#%d), event #%u\n"
6108 " src %s:%d, session #%d, session flags 0x%08x\n"
6109 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6110 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6111 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6112 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6113 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6114 es->ev_id,
6115 pn, port, es->sid, es->s_flags,
6116 es->state, es->m_flags, es->t_flags,
6117 es->m_clen, es->m_blen,
6118 es->b_flags, es->b_out, es->b_tot,
6119 es->len, es->b_wrap, es->pos);
6120
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006121 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006122 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006123 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006124 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006125 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006126 appctx->ctx.errors.ptr = 0;
6127 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006128 }
6129
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006130 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006131 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006132 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006133 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006134 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006135 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006136 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006137 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006138 goto next;
6139 }
6140
6141 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006142 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006143 int newptr;
6144 int newline;
6145
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006146 newline = appctx->ctx.errors.bol;
6147 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6148 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006149 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006150
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006151 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006152 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006153 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006154 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006155 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006156 appctx->ctx.errors.ptr = newptr;
6157 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006158 };
6159 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006160 appctx->ctx.errors.bol = 0;
6161 appctx->ctx.errors.ptr = -1;
6162 appctx->ctx.errors.buf++;
6163 if (appctx->ctx.errors.buf > 1) {
6164 appctx->ctx.errors.buf = 0;
6165 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006166 }
6167 }
6168
6169 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006170 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006171}
6172
Willy Tarreaud5781202012-09-22 19:32:35 +02006173/* parse the "level" argument on the bind lines */
6174static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6175{
6176 if (!*args[cur_arg + 1]) {
6177 memprintf(err, "'%s' : missing level", args[cur_arg]);
6178 return ERR_ALERT | ERR_FATAL;
6179 }
6180
6181 if (!strcmp(args[cur_arg+1], "user"))
6182 conf->level = ACCESS_LVL_USER;
6183 else if (!strcmp(args[cur_arg+1], "operator"))
6184 conf->level = ACCESS_LVL_OPER;
6185 else if (!strcmp(args[cur_arg+1], "admin"))
6186 conf->level = ACCESS_LVL_ADMIN;
6187 else {
6188 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6189 args[cur_arg], args[cur_arg+1]);
6190 return ERR_ALERT | ERR_FATAL;
6191 }
6192
6193 return 0;
6194}
6195
Willy Tarreau30576452015-04-13 13:50:30 +02006196struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006197 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006198 .name = "<STATS>", /* used for logging */
6199 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006200 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006201};
6202
Willy Tarreau30576452015-04-13 13:50:30 +02006203static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006204 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006205 .name = "<CLI>", /* used for logging */
6206 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006207 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006208};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006209
Willy Tarreaudc13c112013-06-21 23:16:39 +02006210static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006211 { CFG_GLOBAL, "stats", stats_parse_global },
6212 { 0, NULL, NULL },
6213}};
6214
Willy Tarreaud5781202012-09-22 19:32:35 +02006215static struct bind_kw_list bind_kws = { "STAT", { }, {
6216 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6217 { NULL, NULL, 0 },
6218}};
6219
Willy Tarreau10522fd2008-07-09 20:12:41 +02006220__attribute__((constructor))
6221static void __dumpstats_module_init(void)
6222{
6223 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006224 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006225}
6226
Willy Tarreau91861262007-10-17 17:06:05 +02006227/*
6228 * Local variables:
6229 * c-indent-level: 8
6230 * c-basic-offset: 8
6231 * End:
6232 */