blob: 09bc7f64e9a320f8001b28571f85d30fa72fc9c4 [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 Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010045#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010050#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010051#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020052#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010054#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020055#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020056#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010057#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020060#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010061#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010062#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020063
Willy Tarreau7a0169a2012-11-19 17:13:16 +010064#ifdef USE_OPENSSL
65#include <proto/ssl_sock.h>
66#endif
67
Willy Tarreau91b843d2014-01-28 16:27:17 +010068/* stats socket states */
69enum {
70 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
71 STAT_CLI_END, /* final state, let's close */
72 STAT_CLI_GETREQ, /* wait for a request */
73 STAT_CLI_OUTPUT, /* all states after this one are responses */
74 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
75 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010076 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010077 STAT_CLI_O_INFO, /* dump info */
78 STAT_CLI_O_SESS, /* dump sessions */
79 STAT_CLI_O_ERR, /* dump errors */
80 STAT_CLI_O_TAB, /* dump tables */
81 STAT_CLI_O_CLR, /* clear tables */
82 STAT_CLI_O_SET, /* set entries in tables */
83 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010084 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
85 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010086 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010087 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010088};
89
Willy Tarreaued7df902014-05-22 18:04:49 +020090/* Actions available for the stats admin forms */
91enum {
92 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020093
94 /* enable/disable health checks */
95 ST_ADM_ACTION_DHLTH,
96 ST_ADM_ACTION_EHLTH,
97
98 /* force health check status */
99 ST_ADM_ACTION_HRUNN,
100 ST_ADM_ACTION_HNOLB,
101 ST_ADM_ACTION_HDOWN,
102
103 /* enable/disable agent checks */
104 ST_ADM_ACTION_DAGENT,
105 ST_ADM_ACTION_EAGENT,
106
107 /* force agent check status */
108 ST_ADM_ACTION_ARUNN,
109 ST_ADM_ACTION_ADOWN,
110
111 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200112 ST_ADM_ACTION_READY,
113 ST_ADM_ACTION_DRAIN,
114 ST_ADM_ACTION_MAINT,
115 ST_ADM_ACTION_SHUTDOWN,
116 /* these are the ancient actions, still available for compatibility */
117 ST_ADM_ACTION_DISABLE,
118 ST_ADM_ACTION_ENABLE,
119 ST_ADM_ACTION_STOP,
120 ST_ADM_ACTION_START,
121};
122
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100123static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100124static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +0100125static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900126static int stats_dump_sess_to_buffer(struct stream_interface *si);
127static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100128static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100129static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
130static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100131static int stats_pats_list(struct stream_interface *si);
132static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100133static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900134
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100135/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100136 * cli_io_handler()
137 * -> stats_dump_sess_to_buffer() // "show sess"
138 * -> stats_dump_errors_to_buffer() // "show errors"
139 * -> stats_dump_info_to_buffer() // "show info"
140 * -> stats_dump_stat_to_buffer() // "show stat"
141 * -> stats_dump_csv_header()
142 * -> stats_dump_proxy_to_buffer()
143 * -> stats_dump_fe_stats()
144 * -> stats_dump_li_stats()
145 * -> stats_dump_sv_stats()
146 * -> stats_dump_be_stats()
147 *
148 * http_stats_io_handler()
149 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
150 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
151 * -> stats_dump_html_head() // emits the HTML headers
152 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
153 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
154 * -> stats_dump_html_px_hdr()
155 * -> stats_dump_fe_stats()
156 * -> stats_dump_li_stats()
157 * -> stats_dump_sv_stats()
158 * -> stats_dump_be_stats()
159 * -> stats_dump_html_px_end()
160 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100161 */
162
Simon Horman9bd2c732011-06-15 15:18:44 +0900163static struct si_applet cli_applet;
164
165static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200166 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200167 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200168 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200169 " help : this message\n"
170 " prompt : toggle interactive mode with prompt\n"
171 " quit : disconnect\n"
172 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100173 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200174 " show stat : report counters for each proxy and server\n"
175 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100176 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200177 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200178 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200179 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200180 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200181 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100182 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200183 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200184 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200185 " disable : put a server or frontend in maintenance mode\n"
186 " enable : re-enable a server or frontend which is in maintenance mode\n"
187 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100188 " show acl [id] : report avalaible acls or dump an acl's contents\n"
189 " get acl : reports the patterns matching a sample for an ACL\n"
190 " add acl : add acl entry\n"
191 " del acl : delete acl entry\n"
192 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100193 " show map [id] : report avalaible maps or dump a map's contents\n"
194 " get map : reports the keys and values matching a sample for a map\n"
195 " set map : modify map entry\n"
196 " add map : add map entry\n"
197 " del map : delete map entry\n"
198 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200199 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200200 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200201
Simon Horman9bd2c732011-06-15 15:18:44 +0900202static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200203 "Permission denied\n"
204 "";
205
Willy Tarreau295a8372011-03-10 11:25:07 +0100206/* data transmission states for the stats responses */
207enum {
208 STAT_ST_INIT = 0,
209 STAT_ST_HEAD,
210 STAT_ST_INFO,
211 STAT_ST_LIST,
212 STAT_ST_END,
213 STAT_ST_FIN,
214};
215
216/* data transmission states for the stats responses inside a proxy */
217enum {
218 STAT_PX_ST_INIT = 0,
219 STAT_PX_ST_TH,
220 STAT_PX_ST_FE,
221 STAT_PX_ST_LI,
222 STAT_PX_ST_SV,
223 STAT_PX_ST_BE,
224 STAT_PX_ST_END,
225 STAT_PX_ST_FIN,
226};
227
Cyril Bonté19979e12012-04-04 12:57:21 +0200228extern const char *stat_status_codes[];
229
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200230/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200231 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200232 * needs to be closed and ignored, or a negative value upon critical failure.
233 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900234static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200235{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100236 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100237 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200238
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200239 tv_zero(&s->logs.tv_request);
240 s->logs.t_queue = 0;
241 s->logs.t_connect = 0;
242 s->logs.t_data = 0;
243 s->logs.t_close = 0;
244 s->logs.bytes_in = s->logs.bytes_out = 0;
245 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
246 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200247
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200248 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200249
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200250 if (s->listener->timeout) {
251 s->req->rto = *s->listener->timeout;
252 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200253 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200254 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200255}
256
Willy Tarreau07e9e642010-08-17 21:48:17 +0200257/* allocate a new stats frontend named <name>, and return it
258 * (or NULL in case of lack of memory).
259 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200260static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200261{
262 struct proxy *fe;
263
264 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
265 if (!fe)
266 return NULL;
267
Willy Tarreau237250c2011-07-29 01:49:03 +0200268 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200269 fe->next = proxy;
270 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200271 fe->last_change = now.tv_sec;
272 fe->id = strdup("GLOBAL");
273 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200274 fe->maxconn = 10; /* default to 10 concurrent connections */
275 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200276 fe->conf.file = strdup(file);
277 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200278 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200279
280 /* the stats frontend is the only one able to assign ID #0 */
281 fe->conf.id.key = fe->uuid = 0;
282 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200283 return fe;
284}
285
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
288 * error message into the <err> buffer which will be preallocated. The trailing
289 * '\n' must not be written. The function must be called with <args> pointing to
290 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200292static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200293 struct proxy *defpx, const char *file, int line,
294 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200295{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200296 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200297 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200298
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 int cur_arg;
301
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200303 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 +0200304 return -1;
305 }
306
Willy Tarreau89a63132009-08-16 17:41:45 +0200307 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200308 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200309 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200310 return -1;
311 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200312 }
313
Willy Tarreau4348fad2012-09-20 16:48:07 +0200314 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200315 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200316
Willy Tarreauc53d4222012-09-20 20:19:28 +0200317 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
318 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
319 file, line, args[0], args[1], err && *err ? *err : "error");
320 return -1;
321 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200323 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200325 static int bind_dumped;
326 struct bind_kw *kw;
327
328 kw = bind_find_kw(args[cur_arg]);
329 if (kw) {
330 if (!kw->parse) {
331 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
332 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200333 return -1;
334 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200335
336 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
337 if (err && *err)
338 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
339 else
340 memprintf(err, "'%s %s' : error encountered while processing '%s'",
341 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200342 return -1;
343 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200344
345 cur_arg += 1 + kw->skip;
346 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200347 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200348
349 if (!bind_dumped) {
350 bind_dump_kws(err);
351 indent_msg(err, 4);
352 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200353 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200354
355 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
356 args[0], args[1], args[cur_arg],
357 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
358 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100360
Willy Tarreauc53d4222012-09-20 20:19:28 +0200361 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
362 l->maxconn = global.stats_fe->maxconn;
363 l->backlog = global.stats_fe->backlog;
364 l->timeout = &global.stats_fe->timeout.client;
365 l->accept = session_accept;
366 l->handler = process_session;
367 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
368 l->nice = -64; /* we want to boost priority for local stats */
369 global.maxsock += l->maxconn;
370 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200371 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200372 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100373 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200374 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100375
376 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200377 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100378 return -1;
379 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200380
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100381 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200382 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200383 return -1;
384 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200385 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200386 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200387 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200388 return -1;
389 }
390 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200391 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200392 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200393 else if (!strcmp(args[1], "maxconn")) {
394 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200395
396 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200397 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200398 return -1;
399 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200400
401 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200402 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200403 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200404 return -1;
405 }
406 }
407 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200408 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200409 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
410 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100411 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200412
Willy Tarreau91319572013-04-20 09:48:50 +0200413 if (!global.stats_fe) {
414 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
415 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
416 return -1;
417 }
418 }
419
Willy Tarreau35b7b162012-10-22 23:17:18 +0200420 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100421 unsigned int low, high;
422
Willy Tarreau35b7b162012-10-22 23:17:18 +0200423 if (strcmp(args[cur_arg], "all") == 0) {
424 set = 0;
425 break;
426 }
427 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100428 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200429 }
430 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100431 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200432 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100433 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100434 char *dash = strchr(args[cur_arg], '-');
435
436 low = high = str2uic(args[cur_arg]);
437 if (dash)
438 high = str2uic(dash + 1);
439
440 if (high < low) {
441 unsigned int swap = low;
442 low = high;
443 high = swap;
444 }
445
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100446 if (low < 1 || high > LONGBITS) {
447 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
448 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200449 return -1;
450 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100451 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100452 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100453 }
454 else {
455 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100456 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
457 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100458 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200459 }
460 cur_arg++;
461 }
462 global.stats_fe->bind_proc = set;
463 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200464 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200465 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200466 return -1;
467 }
468 return 0;
469}
470
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100471/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
472 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100473 * NOTE: Some tools happen to rely on the field position instead of its name,
474 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100475 */
476static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100477{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100478 chunk_appendf(&trash,
479 "# pxname,svname,"
480 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100481 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100482 "bin,bout,"
483 "dreq,dresp,"
484 "ereq,econ,eresp,"
485 "wretr,wredis,"
486 "status,weight,act,bck,"
487 "chkfail,chkdown,lastchg,downtime,qlimit,"
488 "pid,iid,sid,throttle,lbtot,tracked,type,"
489 "rate,rate_lim,rate_max,"
490 "check_status,check_code,check_duration,"
491 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
492 "req_rate,req_rate_max,req_tot,"
493 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200494 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100495 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100496}
497
Simon Hormand9366582011-06-15 15:18:45 +0900498/* print a string of text buffer to <out>. The format is :
499 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
500 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
501 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
502 */
503static int dump_text(struct chunk *out, const char *buf, int bsize)
504{
505 unsigned char c;
506 int ptr = 0;
507
508 while (buf[ptr] && ptr < bsize) {
509 c = buf[ptr];
510 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
511 if (out->len > out->size - 1)
512 break;
513 out->str[out->len++] = c;
514 }
515 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
516 if (out->len > out->size - 2)
517 break;
518 out->str[out->len++] = '\\';
519 switch (c) {
520 case ' ': c = ' '; break;
521 case '\t': c = 't'; break;
522 case '\n': c = 'n'; break;
523 case '\r': c = 'r'; break;
524 case '\e': c = 'e'; break;
525 case '\\': c = '\\'; break;
526 }
527 out->str[out->len++] = c;
528 }
529 else {
530 if (out->len > out->size - 4)
531 break;
532 out->str[out->len++] = '\\';
533 out->str[out->len++] = 'x';
534 out->str[out->len++] = hextab[(c >> 4) & 0xF];
535 out->str[out->len++] = hextab[c & 0xF];
536 }
537 ptr++;
538 }
539
540 return ptr;
541}
542
543/* print a buffer in hexa.
544 * Print stopped if <bsize> is reached, or if no more place in the chunk.
545 */
546static int dump_binary(struct chunk *out, const char *buf, int bsize)
547{
548 unsigned char c;
549 int ptr = 0;
550
551 while (ptr < bsize) {
552 c = buf[ptr];
553
554 if (out->len > out->size - 2)
555 break;
556 out->str[out->len++] = hextab[(c >> 4) & 0xF];
557 out->str[out->len++] = hextab[c & 0xF];
558
559 ptr++;
560 }
561 return ptr;
562}
563
564/* Dump the status of a table to a stream interface's
565 * read buffer. It returns 0 if the output buffer is full
566 * and needs to be called again, otherwise non-zero.
567 */
568static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
569 struct proxy *proxy, struct proxy *target)
570{
Willy Tarreau306f8302013-07-08 15:53:06 +0200571 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900572
Willy Tarreau77804732012-10-29 16:14:26 +0100573 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900574 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
575
576 /* any other information should be dumped here */
577
Willy Tarreau290e63a2012-09-20 18:07:14 +0200578 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100579 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900580
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200581 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900582 return 0;
583
584 return 1;
585}
586
587/* Dump the a table entry to a stream interface's
588 * read buffer. It returns 0 if the output buffer is full
589 * and needs to be called again, otherwise non-zero.
590 */
591static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
592 struct proxy *proxy, struct stksess *entry)
593{
594 int dt;
595
Willy Tarreau77804732012-10-29 16:14:26 +0100596 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900597
598 if (proxy->table.type == STKTABLE_TYPE_IP) {
599 char addr[INET_ADDRSTRLEN];
600 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100601 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900602 }
603 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
604 char addr[INET6_ADDRSTRLEN];
605 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100606 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900607 }
608 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100609 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900610 }
611 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100612 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900613 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
614 }
615 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100616 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900617 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
618 }
619
Willy Tarreau77804732012-10-29 16:14:26 +0100620 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900621
622 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
623 void *ptr;
624
625 if (proxy->table.data_ofs[dt] == 0)
626 continue;
627 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100628 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900629 else
Willy Tarreau77804732012-10-29 16:14:26 +0100630 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900631
632 ptr = stktable_data_ptr(&proxy->table, entry, dt);
633 switch (stktable_data_types[dt].std_type) {
634 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100635 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900636 break;
637 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100638 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900639 break;
640 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100641 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900642 break;
643 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100644 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900645 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
646 proxy->table.data_arg[dt].u));
647 break;
648 }
649 }
Willy Tarreau77804732012-10-29 16:14:26 +0100650 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900651
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200652 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900653 return 0;
654
655 return 1;
656}
657
Willy Tarreaudec98142012-06-06 23:37:08 +0200658static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900659{
Willy Tarreau306f8302013-07-08 15:53:06 +0200660 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100661 struct appctx *appctx = __objt_appctx(si->end);
662 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900663 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900664 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900665 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200666 long long value;
667 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200668 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200669 void *ptr;
670 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900671
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100672 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900673
674 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100675 appctx->ctx.cli.msg = "Key value expected\n";
676 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900677 return;
678 }
679
Simon Hormanc5b89f62011-06-15 15:18:50 +0900680 switch (px->table.type) {
681 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900682 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100683 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900684 break;
685 case STKTABLE_TYPE_IPV6:
686 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100687 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900688 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900689 case STKTABLE_TYPE_INTEGER:
690 {
691 char *endptr;
692 unsigned long val;
693 errno = 0;
694 val = strtoul(args[4], &endptr, 10);
695 if ((errno == ERANGE && val == ULONG_MAX) ||
696 (errno != 0 && val == 0) || endptr == args[4] ||
697 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100698 appctx->ctx.cli.msg = "Invalid key\n";
699 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900700 return;
701 }
702 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100703 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900704 break;
705 }
706 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900707 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100708 static_table_key->key = args[4];
709 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900710 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900711 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200712 switch (action) {
713 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100714 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 +0200715 break;
716 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100717 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 +0200718 break;
719 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100720 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200721 break;
722 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100723 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900724 return;
725 }
726
727 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200728 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100729 appctx->ctx.cli.msg = stats_permission_denied_msg;
730 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900731 return;
732 }
733
Willy Tarreau07115412012-10-29 21:56:59 +0100734 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900735
Willy Tarreaudec98142012-06-06 23:37:08 +0200736 switch (action) {
737 case STAT_CLI_O_TAB:
738 if (!ts)
739 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100740 chunk_reset(&trash);
741 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900742 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100743 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900744 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200745
746 case STAT_CLI_O_CLR:
747 if (!ts)
748 return;
749 if (ts->ref_cnt) {
750 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100751 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
752 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200753 return;
754 }
755 stksess_kill(&px->table, ts);
756 break;
Simon Horman17bce342011-06-15 15:18:47 +0900757
Willy Tarreau654694e2012-06-07 01:03:16 +0200758 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200759 if (ts)
760 stktable_touch(&px->table, ts, 1);
761 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100762 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200763 if (!ts) {
764 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100765 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
766 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200767 return;
768 }
769 stktable_store(&px->table, ts, 1);
770 }
771
Willy Tarreau47060b62013-08-01 21:11:42 +0200772 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
773 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100774 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
775 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200776 return;
777 }
778
779 data_type = stktable_get_data_type(args[cur_arg] + 5);
780 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100781 appctx->ctx.cli.msg = "Unknown data type\n";
782 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200783 return;
784 }
785
786 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100787 appctx->ctx.cli.msg = "Data type not stored in this table\n";
788 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200789 return;
790 }
791
792 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100793 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
794 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200795 return;
796 }
797
798 ptr = stktable_data_ptr(&px->table, ts, data_type);
799
800 switch (stktable_data_types[data_type].std_type) {
801 case STD_T_SINT:
802 stktable_data_cast(ptr, std_t_sint) = value;
803 break;
804 case STD_T_UINT:
805 stktable_data_cast(ptr, std_t_uint) = value;
806 break;
807 case STD_T_ULL:
808 stktable_data_cast(ptr, std_t_ull) = value;
809 break;
810 case STD_T_FRQP:
811 /* We set both the current and previous values. That way
812 * the reported frequency is stable during all the period
813 * then slowly fades out. This allows external tools to
814 * push measures without having to update them too often.
815 */
816 frqp = &stktable_data_cast(ptr, std_t_frqp);
817 frqp->curr_tick = now_ms;
818 frqp->prev_ctr = 0;
819 frqp->curr_ctr = value;
820 break;
821 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200822 }
823 break;
824
Willy Tarreaudec98142012-06-06 23:37:08 +0200825 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100826 appctx->ctx.cli.msg = "Unknown action\n";
827 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200828 break;
Simon Horman121f3052011-06-15 15:18:46 +0900829 }
Simon Horman121f3052011-06-15 15:18:46 +0900830}
831
Willy Tarreau654694e2012-06-07 01:03:16 +0200832static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900833{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100834 struct appctx *appctx = __objt_appctx(si->end);
835
Willy Tarreau04b3a192013-04-13 09:41:37 +0200836 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100837 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
838 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200839 return;
840 }
841
Simon Hormand5b9fd92011-06-15 15:18:48 +0900842 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100843 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
844 if (appctx->ctx.table.data_type < 0) {
845 appctx->ctx.cli.msg = "Unknown data type\n";
846 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900847 return;
848 }
849
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100850 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
851 appctx->ctx.cli.msg = "Data type not stored in this table\n";
852 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900853 return;
854 }
855
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100856 appctx->ctx.table.data_op = get_std_op(args[4]);
857 if (appctx->ctx.table.data_op < 0) {
858 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
859 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900860 return;
861 }
862
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100863 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
864 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
865 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900866 return;
867 }
868}
869
Willy Tarreaudec98142012-06-06 23:37:08 +0200870static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900871{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100872 struct appctx *appctx = __objt_appctx(si->end);
873
874 appctx->ctx.table.data_type = -1;
875 appctx->st2 = STAT_ST_INIT;
876 appctx->ctx.table.target = NULL;
877 appctx->ctx.table.proxy = NULL;
878 appctx->ctx.table.entry = NULL;
879 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900880
881 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100882 appctx->ctx.table.target = find_stktable(args[2]);
883 if (!appctx->ctx.table.target) {
884 appctx->ctx.cli.msg = "No such table\n";
885 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900886 return;
887 }
888 }
889 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200890 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900891 goto err_args;
892 return;
893 }
894
895 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200896 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900897 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200898 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900899 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900900 goto err_args;
901
902 return;
903
904err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200905 switch (action) {
906 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 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 +0200908 break;
909 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100910 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 +0200911 break;
912 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100913 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200914 break;
915 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100916 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900917}
918
Willy Tarreau532a4502011-09-07 22:37:44 +0200919/* Expects to find a frontend named <arg> and returns it, otherwise displays various
920 * adequate error messages and returns NULL. This function also expects the session
921 * level to be admin.
922 */
923static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
924{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100925 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200926 struct proxy *px;
927
Willy Tarreau290e63a2012-09-20 18:07:14 +0200928 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100929 appctx->ctx.cli.msg = stats_permission_denied_msg;
930 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200931 return NULL;
932 }
933
934 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100935 appctx->ctx.cli.msg = "A frontend name is expected.\n";
936 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200937 return NULL;
938 }
939
940 px = findproxy(arg, PR_CAP_FE);
941 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100942 appctx->ctx.cli.msg = "No such frontend.\n";
943 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200944 return NULL;
945 }
946 return px;
947}
948
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200949/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
950 * and returns the pointer to the server. Otherwise, display adequate error messages
951 * and returns NULL. This function also expects the session level to be admin. Note:
952 * the <arg> is modified to remove the '/'.
953 */
954static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
955{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100956 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200957 struct proxy *px;
958 struct server *sv;
959 char *line;
960
Willy Tarreau290e63a2012-09-20 18:07:14 +0200961 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100962 appctx->ctx.cli.msg = stats_permission_denied_msg;
963 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200964 return NULL;
965 }
966
967 /* split "backend/server" and make <line> point to server */
968 for (line = arg; *line; line++)
969 if (*line == '/') {
970 *line++ = '\0';
971 break;
972 }
973
974 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100975 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
976 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200977 return NULL;
978 }
979
980 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100981 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
982 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200983 return NULL;
984 }
985
986 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100987 appctx->ctx.cli.msg = "Proxy is disabled.\n";
988 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200989 return NULL;
990 }
991
992 return sv;
993}
994
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100995/* This function is used with map and acl management. It permits to browse
996 * each reference. The variable <getnext> must contain the current node,
997 * <end> point to the root node and the <flags> permit to filter required
998 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100999 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001000static inline
1001struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1002 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001003{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001004 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001005
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001006 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001007
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001008 /* Get next list entry. */
1009 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001010
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001011 /* If the entry is the last of the list, return NULL. */
1012 if (&ref->list == end)
1013 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001014
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001015 /* If the entry match the flag, return it. */
1016 if (ref->flags & flags)
1017 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001018 }
1019}
1020
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001021static inline
1022struct pat_ref *pat_ref_lookup_ref(const char *reference)
1023{
1024 int id;
1025 char *error;
1026
1027 /* If the reference starts by a '#', this is numeric id. */
1028 if (reference[0] == '#') {
1029 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1030 id = strtol(reference + 1, &error, 10);
1031 if (*error != '\0')
1032 return NULL;
1033
1034 /* Perform the unique id lookup. */
1035 return pat_ref_lookupid(id);
1036 }
1037
1038 /* Perform the string lookup. */
1039 return pat_ref_lookup(reference);
1040}
1041
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001042/* This function is used with map and acl management. It permits to browse
1043 * each reference.
1044 */
1045static inline
1046struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1047{
1048 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001049 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1050 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001051 return NULL;
1052 return expr;
1053}
1054
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001055/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001056 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001057 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001058 * designating the function which will have to process the request, which can
1059 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001060 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001061static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001062{
Willy Tarreau306f8302013-07-08 15:53:06 +02001063 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001064 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001065 char *args[MAX_STATS_ARGS + 1];
1066 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001067 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001068
1069 while (isspace((unsigned char)*line))
1070 line++;
1071
1072 arg = 0;
1073 args[arg] = line;
1074
1075 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001076 if (*line == '\\') {
1077 line++;
1078 if (*line == '\0')
1079 break;
1080 }
1081 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001082 *line++ = '\0';
1083
1084 while (isspace((unsigned char)*line))
1085 line++;
1086
1087 args[++arg] = line;
1088 continue;
1089 }
1090
1091 line++;
1092 }
1093
1094 while (++arg <= MAX_STATS_ARGS)
1095 args[arg] = line;
1096
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001097 /* remove \ */
1098 arg = 0;
1099 while (*args[arg] != '\0') {
1100 j = 0;
1101 for (i=0; args[arg][i] != '\0'; i++) {
1102 if (args[arg][i] == '\\')
1103 continue;
1104 args[arg][j] = args[arg][i];
1105 j++;
1106 }
1107 args[arg][j] = '\0';
1108 arg++;
1109 }
1110
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001111 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001112 if (strcmp(args[0], "show") == 0) {
1113 if (strcmp(args[1], "stat") == 0) {
1114 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->ctx.stats.flags |= STAT_BOUND;
1116 appctx->ctx.stats.iid = atoi(args[2]);
1117 appctx->ctx.stats.type = atoi(args[3]);
1118 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001119 }
1120
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001121 appctx->st2 = STAT_ST_INIT;
1122 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001123 }
1124 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001125 appctx->st2 = STAT_ST_INIT;
1126 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001127 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001128 else if (strcmp(args[1], "pools") == 0) {
1129 appctx->st2 = STAT_ST_INIT;
1130 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1131 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001132 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001134 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001135 appctx->ctx.cli.msg = stats_permission_denied_msg;
1136 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001137 return 1;
1138 }
Willy Tarreau76153662012-11-26 01:16:39 +01001139 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001140 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001141 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001142 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001143 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001144 appctx->ctx.sess.target = NULL;
1145 appctx->ctx.sess.section = 0; /* start with session status */
1146 appctx->ctx.sess.pos = 0;
1147 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001148 }
1149 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001150 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001151 appctx->ctx.cli.msg = stats_permission_denied_msg;
1152 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001153 return 1;
1154 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001155 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001156 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001157 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001158 appctx->ctx.errors.iid = -1;
1159 appctx->ctx.errors.px = NULL;
1160 appctx->st2 = STAT_ST_INIT;
1161 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001162 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001163 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001164 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001165 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001166 else if (strcmp(args[1], "map") == 0 ||
1167 strcmp(args[1], "acl") == 0) {
1168
1169 /* Set ACL or MAP flags. */
1170 if (args[1][0] == 'm')
1171 appctx->ctx.map.display_flags = PAT_REF_MAP;
1172 else
1173 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001174
1175 /* no parameter: display all map avalaible */
1176 if (!*args[2]) {
1177 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001178 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001179 return 1;
1180 }
1181
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001182 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001183 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001184 if (!appctx->ctx.map.ref ||
1185 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1186 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001187 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001188 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001189 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001190 appctx->st0 = STAT_CLI_PRINT;
1191 return 1;
1192 }
1193 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001194 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001195 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001196 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001197 return 0;
1198 }
1199 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001200 else if (strcmp(args[0], "clear") == 0) {
1201 if (strcmp(args[1], "counters") == 0) {
1202 struct proxy *px;
1203 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001204 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001205 int clrall = 0;
1206
1207 if (strcmp(args[2], "all") == 0)
1208 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001209
Willy Tarreau6162db22009-10-10 17:13:00 +02001210 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001211 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1212 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001213 appctx->ctx.cli.msg = stats_permission_denied_msg;
1214 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001215 return 1;
1216 }
1217
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001218 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001219 if (clrall) {
1220 memset(&px->be_counters, 0, sizeof(px->be_counters));
1221 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1222 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001223 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001224 px->be_counters.conn_max = 0;
1225 px->be_counters.p.http.rps_max = 0;
1226 px->be_counters.sps_max = 0;
1227 px->be_counters.cps_max = 0;
1228 px->be_counters.nbpend_max = 0;
1229
1230 px->fe_counters.conn_max = 0;
1231 px->fe_counters.p.http.rps_max = 0;
1232 px->fe_counters.sps_max = 0;
1233 px->fe_counters.cps_max = 0;
1234 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001235 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001236
1237 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001238 if (clrall)
1239 memset(&sv->counters, 0, sizeof(sv->counters));
1240 else {
1241 sv->counters.cur_sess_max = 0;
1242 sv->counters.nbpend_max = 0;
1243 sv->counters.sps_max = 0;
1244 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001245
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001246 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001247 if (li->counters) {
1248 if (clrall)
1249 memset(li->counters, 0, sizeof(*li->counters));
1250 else
1251 li->counters->conn_max = 0;
1252 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001253 }
1254
Willy Tarreau81c25d02011-09-07 15:17:21 +02001255 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001256 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001257 return 1;
1258 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001259 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001260 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001261 /* end of processing */
1262 return 1;
1263 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001264 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1265 /* Set ACL or MAP flags. */
1266 if (args[1][0] == 'm')
1267 appctx->ctx.map.display_flags = PAT_REF_MAP;
1268 else
1269 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001270
1271 /* no parameter */
1272 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001273 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1274 appctx->ctx.cli.msg = "Missing map identifier.\n";
1275 else
1276 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001277 appctx->st0 = STAT_CLI_PRINT;
1278 return 1;
1279 }
1280
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001281 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001282 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001283 if (!appctx->ctx.map.ref ||
1284 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1285 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001286 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001287 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001288 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001289 appctx->st0 = STAT_CLI_PRINT;
1290 return 1;
1291 }
1292
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001293 /* Clear all. */
1294 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001295
1296 /* return response */
1297 appctx->ctx.cli.msg = "Done.\n";
1298 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001299 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001300 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001301 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001302 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001303 return 0;
1304 }
1305 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001306 else if (strcmp(args[0], "get") == 0) {
1307 if (strcmp(args[1], "weight") == 0) {
1308 struct proxy *px;
1309 struct server *sv;
1310
1311 /* split "backend/server" and make <line> point to server */
1312 for (line = args[2]; *line; line++)
1313 if (*line == '/') {
1314 *line++ = '\0';
1315 break;
1316 }
1317
1318 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001319 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1320 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001321 return 1;
1322 }
1323
1324 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001325 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1326 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001327 return 1;
1328 }
1329
1330 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001331 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1332 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001333 return 1;
1334 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001335 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1336 /* Set flags. */
1337 if (args[1][0] == 'm')
1338 appctx->ctx.map.display_flags = PAT_REF_MAP;
1339 else
1340 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001341
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001342 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001343 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001344 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1345 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1346 else
1347 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001348 appctx->st0 = STAT_CLI_PRINT;
1349 return 1;
1350 }
1351
1352 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001353 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001354 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001355 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001356 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001357 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001358 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001359 appctx->st0 = STAT_CLI_PRINT;
1360 return 1;
1361 }
1362
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001363 /* copy input string. The string must be allocated because
1364 * it may be used over multiple iterations. It's released
1365 * at the end and upon abort anyway.
1366 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001367 appctx->ctx.map.chunk.len = strlen(args[3]);
1368 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1369 appctx->ctx.map.chunk.str = strdup(args[3]);
1370 if (!appctx->ctx.map.chunk.str) {
1371 appctx->ctx.cli.msg = "Out of memory error.\n";
1372 appctx->st0 = STAT_CLI_PRINT;
1373 return 1;
1374 }
1375
1376 /* prepare response */
1377 appctx->st2 = STAT_ST_INIT;
1378 appctx->st0 = STAT_CLI_O_MLOOK;
1379 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001380 else { /* not "get weight" */
1381 return 0;
1382 }
1383 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001384 else if (strcmp(args[0], "set") == 0) {
1385 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001386 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001387 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001388
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001389 sv = expect_server_admin(s, si, args[2]);
1390 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001391 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001392
Simon Horman7d09b9a2013-02-12 10:45:51 +09001393 warning = server_parse_weight_change_request(sv, args[3]);
1394 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001395 appctx->ctx.cli.msg = warning;
1396 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001397 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001398 return 1;
1399 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001400 else if (strcmp(args[1], "server") == 0) {
1401 struct server *sv;
1402 const char *warning;
1403
1404 sv = expect_server_admin(s, si, args[2]);
1405 if (!sv)
1406 return 1;
1407
1408 if (strcmp(args[3], "weight") == 0) {
1409 warning = server_parse_weight_change_request(sv, args[4]);
1410 if (warning) {
1411 appctx->ctx.cli.msg = warning;
1412 appctx->st0 = STAT_CLI_PRINT;
1413 }
1414 }
1415 else if (strcmp(args[3], "state") == 0) {
1416 if (strcmp(args[4], "ready") == 0)
1417 srv_adm_set_ready(sv);
1418 else if (strcmp(args[4], "drain") == 0)
1419 srv_adm_set_drain(sv);
1420 else if (strcmp(args[4], "maint") == 0)
1421 srv_adm_set_maint(sv);
1422 else {
1423 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1424 appctx->st0 = STAT_CLI_PRINT;
1425 }
1426 }
1427 else if (strcmp(args[3], "health") == 0) {
1428 if (sv->track) {
1429 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1430 appctx->st0 = STAT_CLI_PRINT;
1431 }
1432 else if (strcmp(args[4], "up") == 0) {
1433 sv->check.health = sv->check.rise + sv->check.fall - 1;
1434 srv_set_running(sv, "changed from CLI");
1435 }
1436 else if (strcmp(args[4], "stopping") == 0) {
1437 sv->check.health = sv->check.rise + sv->check.fall - 1;
1438 srv_set_stopping(sv, "changed from CLI");
1439 }
1440 else if (strcmp(args[4], "down") == 0) {
1441 sv->check.health = 0;
1442 srv_set_stopped(sv, "changed from CLI");
1443 }
1444 else {
1445 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1446 appctx->st0 = STAT_CLI_PRINT;
1447 }
1448 }
1449 else if (strcmp(args[3], "agent") == 0) {
1450 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1451 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1452 appctx->st0 = STAT_CLI_PRINT;
1453 }
1454 else if (strcmp(args[4], "up") == 0) {
1455 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1456 srv_set_running(sv, "changed from CLI");
1457 }
1458 else if (strcmp(args[4], "down") == 0) {
1459 sv->agent.health = 0;
1460 srv_set_stopped(sv, "changed from CLI");
1461 }
1462 else {
1463 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1464 appctx->st0 = STAT_CLI_PRINT;
1465 }
1466 }
1467 else {
1468 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1469 appctx->st0 = STAT_CLI_PRINT;
1470 }
1471 return 1;
1472 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001473 else if (strcmp(args[1], "timeout") == 0) {
1474 if (strcmp(args[2], "cli") == 0) {
1475 unsigned timeout;
1476 const char *res;
1477
1478 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001479 appctx->ctx.cli.msg = "Expects an integer value.\n";
1480 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001481 return 1;
1482 }
1483
1484 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1485 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001486 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1487 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001488 return 1;
1489 }
1490
1491 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1492 return 1;
1493 }
1494 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001495 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1496 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001497 return 1;
1498 }
1499 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001500 else if (strcmp(args[1], "maxconn") == 0) {
1501 if (strcmp(args[2], "frontend") == 0) {
1502 struct proxy *px;
1503 struct listener *l;
1504 int v;
1505
Willy Tarreau532a4502011-09-07 22:37:44 +02001506 px = expect_frontend_admin(s, si, args[3]);
1507 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001508 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001509
1510 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001511 appctx->ctx.cli.msg = "Integer value expected.\n";
1512 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001513 return 1;
1514 }
1515
1516 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001517 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001518 appctx->ctx.cli.msg = "Value out of range.\n";
1519 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001520 return 1;
1521 }
1522
1523 /* OK, the value is fine, so we assign it to the proxy and to all of
1524 * its listeners. The blocked ones will be dequeued.
1525 */
1526 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001527 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001528 l->maxconn = v;
1529 if (l->state == LI_FULL)
1530 resume_listener(l);
1531 }
1532
1533 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1534 dequeue_all_listeners(&s->fe->listener_queue);
1535
1536 return 1;
1537 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001538 else if (strcmp(args[2], "global") == 0) {
1539 int v;
1540
Willy Tarreau290e63a2012-09-20 18:07:14 +02001541 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001542 appctx->ctx.cli.msg = stats_permission_denied_msg;
1543 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001544 return 1;
1545 }
1546
1547 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001548 appctx->ctx.cli.msg = "Expects an integer value.\n";
1549 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001550 return 1;
1551 }
1552
1553 v = atoi(args[3]);
1554 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 appctx->ctx.cli.msg = "Value out of range.\n";
1556 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001557 return 1;
1558 }
1559
1560 /* check for unlimited values */
1561 if (v <= 0)
1562 v = global.hardmaxconn;
1563
1564 global.maxconn = v;
1565
1566 /* Dequeues all of the listeners waiting for a resource */
1567 if (!LIST_ISEMPTY(&global_listener_queue))
1568 dequeue_all_listeners(&global_listener_queue);
1569
1570 return 1;
1571 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001572 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001573 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1574 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001575 return 1;
1576 }
1577 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001578 else if (strcmp(args[1], "rate-limit") == 0) {
1579 if (strcmp(args[2], "connections") == 0) {
1580 if (strcmp(args[3], "global") == 0) {
1581 int v;
1582
Willy Tarreau290e63a2012-09-20 18:07:14 +02001583 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001584 appctx->ctx.cli.msg = stats_permission_denied_msg;
1585 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001586 return 1;
1587 }
1588
1589 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 appctx->ctx.cli.msg = "Expects an integer value.\n";
1591 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001592 return 1;
1593 }
1594
1595 v = atoi(args[4]);
1596 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001597 appctx->ctx.cli.msg = "Value out of range.\n";
1598 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001599 return 1;
1600 }
1601
1602 global.cps_lim = v;
1603
1604 /* Dequeues all of the listeners waiting for a resource */
1605 if (!LIST_ISEMPTY(&global_listener_queue))
1606 dequeue_all_listeners(&global_listener_queue);
1607
1608 return 1;
1609 }
1610 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1612 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001613 return 1;
1614 }
1615 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001616 else if (strcmp(args[2], "sessions") == 0) {
1617 if (strcmp(args[3], "global") == 0) {
1618 int v;
1619
1620 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1621 appctx->ctx.cli.msg = stats_permission_denied_msg;
1622 appctx->st0 = STAT_CLI_PRINT;
1623 return 1;
1624 }
1625
1626 if (!*args[4]) {
1627 appctx->ctx.cli.msg = "Expects an integer value.\n";
1628 appctx->st0 = STAT_CLI_PRINT;
1629 return 1;
1630 }
1631
1632 v = atoi(args[4]);
1633 if (v < 0) {
1634 appctx->ctx.cli.msg = "Value out of range.\n";
1635 appctx->st0 = STAT_CLI_PRINT;
1636 return 1;
1637 }
1638
1639 global.sps_lim = v;
1640
1641 /* Dequeues all of the listeners waiting for a resource */
1642 if (!LIST_ISEMPTY(&global_listener_queue))
1643 dequeue_all_listeners(&global_listener_queue);
1644
1645 return 1;
1646 }
1647 else {
1648 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1649 appctx->st0 = STAT_CLI_PRINT;
1650 return 1;
1651 }
1652 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001653#ifdef USE_OPENSSL
1654 else if (strcmp(args[2], "ssl-sessions") == 0) {
1655 if (strcmp(args[3], "global") == 0) {
1656 int v;
1657
1658 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1659 appctx->ctx.cli.msg = stats_permission_denied_msg;
1660 appctx->st0 = STAT_CLI_PRINT;
1661 return 1;
1662 }
1663
1664 if (!*args[4]) {
1665 appctx->ctx.cli.msg = "Expects an integer value.\n";
1666 appctx->st0 = STAT_CLI_PRINT;
1667 return 1;
1668 }
1669
1670 v = atoi(args[4]);
1671 if (v < 0) {
1672 appctx->ctx.cli.msg = "Value out of range.\n";
1673 appctx->st0 = STAT_CLI_PRINT;
1674 return 1;
1675 }
1676
1677 global.ssl_lim = v;
1678
1679 /* Dequeues all of the listeners waiting for a resource */
1680 if (!LIST_ISEMPTY(&global_listener_queue))
1681 dequeue_all_listeners(&global_listener_queue);
1682
1683 return 1;
1684 }
1685 else {
1686 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1687 appctx->st0 = STAT_CLI_PRINT;
1688 return 1;
1689 }
1690 }
1691#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001692 else if (strcmp(args[2], "http-compression") == 0) {
1693 if (strcmp(args[3], "global") == 0) {
1694 int v;
1695
Willy Tarreau85d47f92012-11-21 00:29:50 +01001696 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001697 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1698 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001699 return 1;
1700 }
1701
William Lallemandd85f9172012-11-09 17:05:39 +01001702 v = atoi(args[4]);
1703 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1704 }
1705 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001706 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1707 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001708 return 1;
1709 }
1710 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001711 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001712 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001713 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001714 return 1;
1715 }
1716 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001717 else if (strcmp(args[1], "table") == 0) {
1718 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1719 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001720 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001721 char *err;
1722
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001723 /* Set flags. */
1724 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001725
1726 /* Expect three parameters: map name, key and new value. */
1727 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001728 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001729 appctx->st0 = STAT_CLI_PRINT;
1730 return 1;
1731 }
1732
1733 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001734 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001735 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001736 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001737 appctx->st0 = STAT_CLI_PRINT;
1738 return 1;
1739 }
1740
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001741 /* If the entry identifier start with a '#', it is considered as
1742 * pointer id
1743 */
1744 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1745 struct pat_ref_elt *ref;
1746 long long int conv;
1747 char *error;
1748
1749 /* Convert argument to integer value. */
1750 conv = strtoll(&args[3][1], &error, 16);
1751 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001752 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001753 appctx->st0 = STAT_CLI_PRINT;
1754 return 1;
1755 }
1756
1757 /* Convert and check integer to pointer. */
1758 ref = (struct pat_ref_elt *)(long)conv;
1759 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001760 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001761 appctx->st0 = STAT_CLI_PRINT;
1762 return 1;
1763 }
1764
1765 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001766 err = NULL;
1767 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1768 if (err)
1769 memprintf(&err, "%s.\n", err);
1770 appctx->ctx.cli.err = err;
1771 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001772 return 1;
1773 }
1774 }
1775 else {
1776 /* Else, use the entry identifier as pattern
1777 * string, and update the value.
1778 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001779 err = NULL;
1780 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1781 if (err)
1782 memprintf(&err, "%s.\n", err);
1783 appctx->ctx.cli.err = err;
1784 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001785 return 1;
1786 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001787 }
1788
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001789 /* The set is done, send message. */
1790 appctx->ctx.cli.msg = "Done.\n";
1791 appctx->st0 = STAT_CLI_PRINT;
1792 return 1;
1793 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001794#ifdef USE_OPENSSL
1795 else if (strcmp(args[1], "ssl") == 0) {
1796 if (strcmp(args[2], "ocsp-response") == 0) {
1797#ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB
1798 char *err = NULL;
1799
Emeric Brunaf4ef742014-06-19 14:10:45 +02001800 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001801 if (!*args[3]) {
1802 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1803 appctx->st0 = STAT_CLI_PRINT;
1804 return 1;
1805 }
1806
1807 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1808 if (trash.len < 0) {
1809 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1810 appctx->st0 = STAT_CLI_PRINT;
1811 return 1;
1812 }
1813
1814 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1815 if (err) {
1816 memprintf(&err, "%s.\n", err);
1817 appctx->ctx.cli.err = err;
1818 appctx->st0 = STAT_CLI_PRINT_FREE;
1819 }
1820 return 1;
1821 }
1822 appctx->ctx.cli.msg = "OCSP Response updated!";
1823 appctx->st0 = STAT_CLI_PRINT;
1824 return 1;
1825#else
1826 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1827 appctx->st0 = STAT_CLI_PRINT;
1828 return 1;
1829#endif
1830 }
1831 else {
1832 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1833 appctx->st0 = STAT_CLI_PRINT;
1834 return 1;
1835 }
1836 }
1837#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001838 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001839 return 0;
1840 }
1841 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001842 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001843 if (strcmp(args[1], "agent") == 0) {
1844 struct server *sv;
1845
1846 sv = expect_server_admin(s, si, args[2]);
1847 if (!sv)
1848 return 1;
1849
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001850 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1851 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1852 appctx->st0 = STAT_CLI_PRINT;
1853 return 1;
1854 }
1855
1856 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001857 return 1;
1858 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001859 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001860 struct server *sv;
1861
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001862 sv = expect_server_admin(s, si, args[2]);
1863 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001864 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001865
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001866 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1867 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1868 appctx->st0 = STAT_CLI_PRINT;
1869 return 1;
1870 }
1871
1872 sv->check.state |= CHK_ST_ENABLED;
1873 return 1;
1874 }
1875 else if (strcmp(args[1], "server") == 0) {
1876 struct server *sv;
1877
1878 sv = expect_server_admin(s, si, args[2]);
1879 if (!sv)
1880 return 1;
1881
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001882 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001883 return 1;
1884 }
1885 else if (strcmp(args[1], "frontend") == 0) {
1886 struct proxy *px;
1887
1888 px = expect_frontend_admin(s, si, args[2]);
1889 if (!px)
1890 return 1;
1891
1892 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001893 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1894 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001895 return 1;
1896 }
1897
1898 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001899 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1900 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001901 return 1;
1902 }
1903
1904 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001905 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1906 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001907 return 1;
1908 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001909 return 1;
1910 }
1911 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001912 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001913 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001914 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001915 }
1916 }
1917 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001918 if (strcmp(args[1], "agent") == 0) {
1919 struct server *sv;
1920
1921 sv = expect_server_admin(s, si, args[2]);
1922 if (!sv)
1923 return 1;
1924
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001925 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001926 return 1;
1927 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001928 else if (strcmp(args[1], "health") == 0) {
1929 struct server *sv;
1930
1931 sv = expect_server_admin(s, si, args[2]);
1932 if (!sv)
1933 return 1;
1934
1935 sv->check.state &= ~CHK_ST_ENABLED;
1936 return 1;
1937 }
Simon Horman671b6f02013-11-25 10:46:39 +09001938 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001939 struct server *sv;
1940
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001941 sv = expect_server_admin(s, si, args[2]);
1942 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001943 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001944
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001945 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001946 return 1;
1947 }
1948 else if (strcmp(args[1], "frontend") == 0) {
1949 struct proxy *px;
1950
1951 px = expect_frontend_admin(s, si, args[2]);
1952 if (!px)
1953 return 1;
1954
1955 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001956 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1957 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001958 return 1;
1959 }
1960
1961 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001962 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1963 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001964 return 1;
1965 }
1966
1967 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001968 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1969 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001970 return 1;
1971 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001972 return 1;
1973 }
1974 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001975 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001976 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001977 return 1;
1978 }
1979 }
1980 else if (strcmp(args[0], "shutdown") == 0) {
1981 if (strcmp(args[1], "frontend") == 0) {
1982 struct proxy *px;
1983
1984 px = expect_frontend_admin(s, si, args[2]);
1985 if (!px)
1986 return 1;
1987
1988 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001989 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1990 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001991 return 1;
1992 }
1993
1994 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1995 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1996 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1997 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1998 stop_proxy(px);
1999 return 1;
2000 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002001 else if (strcmp(args[1], "session") == 0) {
2002 struct session *sess, *ptr;
2003
Willy Tarreau290e63a2012-09-20 18:07:14 +02002004 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002005 appctx->ctx.cli.msg = stats_permission_denied_msg;
2006 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002007 return 1;
2008 }
2009
2010 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002011 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2012 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002013 return 1;
2014 }
2015
2016 ptr = (void *)strtoul(args[2], NULL, 0);
2017
2018 /* first, look for the requested session in the session table */
2019 list_for_each_entry(sess, &sessions, list) {
2020 if (sess == ptr)
2021 break;
2022 }
2023
2024 /* do we have the session ? */
2025 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002026 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2027 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002028 return 1;
2029 }
2030
2031 session_shutdown(sess, SN_ERR_KILLED);
2032 return 1;
2033 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002034 else if (strcmp(args[1], "sessions") == 0) {
2035 if (strcmp(args[2], "server") == 0) {
2036 struct server *sv;
2037 struct session *sess, *sess_bck;
2038
2039 sv = expect_server_admin(s, si, args[3]);
2040 if (!sv)
2041 return 1;
2042
2043 /* kill all the session that are on this server */
2044 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2045 if (sess->srv_conn == sv)
2046 session_shutdown(sess, SN_ERR_KILLED);
2047
2048 return 1;
2049 }
2050 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002051 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2052 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002053 return 1;
2054 }
2055 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002056 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002057 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2058 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002059 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002060 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002061 }
2062 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002063 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2064 if (args[1][0] == 'm')
2065 appctx->ctx.map.display_flags = PAT_REF_MAP;
2066 else
2067 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002068
2069 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002070 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2071 if (!*args[2] || !*args[3]) {
2072 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2073 appctx->st0 = STAT_CLI_PRINT;
2074 return 1;
2075 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002076 }
2077
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002078 else {
2079 if (!*args[2] || !*args[3]) {
2080 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2081 appctx->st0 = STAT_CLI_PRINT;
2082 return 1;
2083 }
2084 }
2085
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002086 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002087 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002088 if (!appctx->ctx.map.ref ||
2089 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002090 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002091 appctx->st0 = STAT_CLI_PRINT;
2092 return 1;
2093 }
2094
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002095 /* If the entry identifier start with a '#', it is considered as
2096 * pointer id
2097 */
2098 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2099 struct pat_ref_elt *ref;
2100 long long int conv;
2101 char *error;
2102
2103 /* Convert argument to integer value. */
2104 conv = strtoll(&args[3][1], &error, 16);
2105 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002106 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002107 appctx->st0 = STAT_CLI_PRINT;
2108 return 1;
2109 }
2110
2111 /* Convert and check integer to pointer. */
2112 ref = (struct pat_ref_elt *)(long)conv;
2113 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002114 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002115 appctx->st0 = STAT_CLI_PRINT;
2116 return 1;
2117 }
2118
2119 /* Try to delete the entry. */
2120 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2121 /* The entry is not found, send message. */
2122 appctx->ctx.cli.msg = "Key not found.\n";
2123 appctx->st0 = STAT_CLI_PRINT;
2124 return 1;
2125 }
2126 }
2127 else {
2128 /* Else, use the entry identifier as pattern
2129 * string and try to delete the entry.
2130 */
2131 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2132 /* The entry is not found, send message. */
2133 appctx->ctx.cli.msg = "Key not found.\n";
2134 appctx->st0 = STAT_CLI_PRINT;
2135 return 1;
2136 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002137 }
2138
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002139 /* The deletion is done, send message. */
2140 appctx->ctx.cli.msg = "Done.\n";
2141 appctx->st0 = STAT_CLI_PRINT;
2142 return 1;
2143 }
2144 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002145 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002146 appctx->st0 = STAT_CLI_PRINT;
2147 return 1;
2148 }
2149 }
2150 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151 if (strcmp(args[1], "map") == 0 ||
2152 strcmp(args[1], "acl") == 0) {
2153 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002154 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002155
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002156 /* Set flags. */
2157 if (args[1][0] == 'm')
2158 appctx->ctx.map.display_flags = PAT_REF_MAP;
2159 else
2160 appctx->ctx.map.display_flags = PAT_REF_ACL;
2161
2162 /* If the keywork is "map", we expect three parameters, if it
2163 * is "acl", we expect only two parameters
2164 */
2165 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2166 if (!*args[2] || !*args[3] || !*args[4]) {
2167 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2168 appctx->st0 = STAT_CLI_PRINT;
2169 return 1;
2170 }
2171 }
2172 else {
2173 if (!*args[2] || !*args[3]) {
2174 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2175 appctx->st0 = STAT_CLI_PRINT;
2176 return 1;
2177 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002178 }
2179
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002180 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002181 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002182 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002183 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002184 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002186 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002187 appctx->st0 = STAT_CLI_PRINT;
2188 return 1;
2189 }
2190
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002191 /* The command "add acl" is prohibited if the reference
2192 * use samples.
2193 */
2194 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2195 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2196 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2197 "You must use the command 'add map' to add values.\n";
2198 appctx->st0 = STAT_CLI_PRINT;
2199 return 1;
2200 }
2201
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002203 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002204 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002205 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002206 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002207 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002208 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002209 if (err)
2210 memprintf(&err, "%s.\n", err);
2211 appctx->ctx.cli.err = err;
2212 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002213 return 1;
2214 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002215
2216 /* The add is done, send message. */
2217 appctx->ctx.cli.msg = "Done.\n";
2218 appctx->st0 = STAT_CLI_PRINT;
2219 return 1;
2220 }
2221 else { /* unknown "del" parameter */
2222 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2223 appctx->st0 = STAT_CLI_PRINT;
2224 return 1;
2225 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002226 }
2227 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002228 return 0;
2229 }
2230 return 1;
2231}
2232
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002233/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002234 * used to processes I/O from/to the stats unix socket. The system relies on a
2235 * state machine handling requests and various responses. We read a request,
2236 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002237 * Then we can read again. The state is stored in appctx->st0 and is one of the
2238 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002239 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002240 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002241static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002242{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002243 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002244 struct channel *req = si->ob;
2245 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002246 int reql;
2247 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002248
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002249 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2250 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002251
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002252 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002253 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002254 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002255 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2256 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002257 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002258 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002259 /* Let's close for real now. We just close the request
2260 * side, the conditions below will complete if needed.
2261 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002262 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002263 break;
2264 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002265 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002266 /* ensure we have some output room left in the event we
2267 * would want to return some info right after parsing.
2268 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002269 if (buffer_almost_full(si->ib->buf)) {
2270 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002271 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002272 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002273
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002274 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002275 if (reql <= 0) { /* closed or EOL not found */
2276 if (reql == 0)
2277 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002278 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002279 continue;
2280 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002281
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002282 /* seek for a possible semi-colon. If we find one, we
2283 * replace it with an LF and skip only this part.
2284 */
2285 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002286 if (trash.str[len] == ';') {
2287 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002288 reql = len + 1;
2289 break;
2290 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002291
Willy Tarreau816fc222009-10-04 07:36:58 +02002292 /* now it is time to check that we have a full line,
2293 * remove the trailing \n and possibly \r, then cut the
2294 * line.
2295 */
2296 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002297 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002298 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002299 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002300 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002301
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002302 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002303 len--;
2304
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002305 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002306
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002308 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002309 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002310 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002311 continue;
2312 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002313 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002314 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002315 else if (strcmp(trash.str, "help") == 0 ||
2316 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.cli.msg = stats_sock_usage_msg;
2318 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002319 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002320 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002321 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002322 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002323 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002325 /* if prompt is disabled, print help on empty lines,
2326 * so that the user at least knows how to enable
2327 * prompt and find help.
2328 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002329 appctx->ctx.cli.msg = stats_sock_usage_msg;
2330 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002331 }
2332
2333 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002334 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002335 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002336 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002337 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002338 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002339 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002340 continue;
2341 }
2342
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002343 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002344 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002345 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2346 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002347 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002348 case STAT_CLI_PRINT_FREE:
2349 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2350 free(appctx->ctx.cli.err);
2351 appctx->st0 = STAT_CLI_PROMPT;
2352 }
2353 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002354 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002355 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002357 break;
2358 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002359 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002360 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002361 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002362 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002363 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002364 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002365 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002366 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002367 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002368 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002369 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002370 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002371 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002372 if (stats_table_request(si, appctx->st0))
2373 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002374 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002375 case STAT_CLI_O_PATS:
2376 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002377 appctx->st0 = STAT_CLI_PROMPT;
2378 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002379 case STAT_CLI_O_PAT:
2380 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002381 appctx->st0 = STAT_CLI_PROMPT;
2382 break;
2383 case STAT_CLI_O_MLOOK:
2384 if (stats_map_lookup(si))
2385 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002386 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002387 case STAT_CLI_O_POOLS:
2388 if (stats_dump_pools_to_buffer(si))
2389 appctx->st0 = STAT_CLI_PROMPT;
2390 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002391 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002392 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002393 break;
2394 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002395
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002396 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002397 if (appctx->st0 == STAT_CLI_PROMPT) {
2398 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2399 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002400 }
2401
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002402 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002404 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002405
2406 /* Now we close the output if one of the writers did so,
2407 * or if we're not in interactive mode and the request
2408 * buffer is empty. This still allows pipelined requests
2409 * to be sent in non-interactive mode.
2410 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002411 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2412 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002413 continue;
2414 }
2415
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002416 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002417 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002418 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002419 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002420
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002421 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002422 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2423 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002424 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002425 * and nothing more to consume. This is comparable to a broken pipe, so
2426 * we forward the close to the request side so that it flows upstream to
2427 * the client.
2428 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002429 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002430 }
2431
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002432 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002433 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2434 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2435 /* We have no more processing to do, and nothing more to send, and
2436 * the client side has closed. So we'll forward this state downstream
2437 * on the response buffer.
2438 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002439 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002440 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002441 }
2442
2443 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002444 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002445
2446 /* we don't want to expire timeouts while we're processing requests */
2447 si->ib->rex = TICK_ETERNITY;
2448 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002449
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002450 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002451 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 +02002452 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002453 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002454
2455 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2456 /* check that we have released everything then unregister */
2457 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002458 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002459}
2460
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002461/* This function dumps information onto the stream interface's read buffer.
2462 * It returns 0 as long as it does not complete, non-zero upon completion.
2463 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002464 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002465static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002466{
2467 unsigned int up = (now.tv_sec - start_date.tv_sec);
2468
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002469#ifdef USE_OPENSSL
2470 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2471 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2472 int ssl_reuse = 0;
2473
2474 if (ssl_key_rate < ssl_sess_rate) {
2475 /* count the ssl reuse ratio and avoid overflows in both directions */
2476 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2477 }
2478#endif
2479
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002480 chunk_printf(&trash,
2481 "Name: " PRODUCT_NAME "\n"
2482 "Version: " HAPROXY_VERSION "\n"
2483 "Release_date: " HAPROXY_DATE "\n"
2484 "Nbproc: %d\n"
2485 "Process_num: %d\n"
2486 "Pid: %d\n"
2487 "Uptime: %dd %dh%02dm%02ds\n"
2488 "Uptime_sec: %d\n"
2489 "Memmax_MB: %d\n"
2490 "Ulimit-n: %d\n"
2491 "Maxsock: %d\n"
2492 "Maxconn: %d\n"
2493 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002494 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002495 "CumConns: %d\n"
2496 "CumReq: %d\n"
2497#ifdef USE_OPENSSL
2498 "MaxSslConns: %d\n"
2499 "CurrSslConns: %d\n"
2500 "CumSslConns: %d\n"
2501#endif
2502 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002503 "PipesUsed: %d\n"
2504 "PipesFree: %d\n"
2505 "ConnRate: %d\n"
2506 "ConnRateLimit: %d\n"
2507 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002508 "SessRate: %d\n"
2509 "SessRateLimit: %d\n"
2510 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002511#ifdef USE_OPENSSL
2512 "SslRate: %d\n"
2513 "SslRateLimit: %d\n"
2514 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002515 "SslFrontendKeyRate: %d\n"
2516 "SslFrontendMaxKeyRate: %d\n"
2517 "SslFrontendSessionReuse_pct: %d\n"
2518 "SslBackendKeyRate: %d\n"
2519 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002520 "SslCacheLookups: %u\n"
2521 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002522#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002523 "CompressBpsIn: %u\n"
2524 "CompressBpsOut: %u\n"
2525 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002526#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002527 "ZlibMemUsage: %ld\n"
2528 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002529#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002530 "Tasks: %d\n"
2531 "Run_queue: %d\n"
2532 "Idle_pct: %d\n"
2533 "node: %s\n"
2534 "description: %s\n"
2535 "",
2536 global.nbproc,
2537 relative_pid,
2538 pid,
2539 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2540 up,
2541 global.rlimit_memmax,
2542 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002543 global.maxsock, global.maxconn, global.hardmaxconn,
2544 actconn, totalconn, global.req_count,
2545#ifdef USE_OPENSSL
2546 global.maxsslconn, sslconns, totalsslconns,
2547#endif
2548 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002549 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002550 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002551#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002552 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2553 ssl_key_rate, global.ssl_fe_keys_max,
2554 ssl_reuse,
2555 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002556 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002557#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002558 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2559 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002560#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002561 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002562#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002563 nb_tasks_cur, run_queue_cur, idle_pct,
2564 global.node, global.desc ? global.desc : ""
2565 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002566
2567 if (bi_putchk(si->ib, &trash) == -1)
2568 return 0;
2569
2570 return 1;
2571}
2572
Willy Tarreau12833bb2014-01-28 16:49:56 +01002573/* This function dumps memory usage information onto the stream interface's
2574 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2575 * completion. No state is used.
2576 */
2577static int stats_dump_pools_to_buffer(struct stream_interface *si)
2578{
2579 dump_pools_to_trash();
2580 if (bi_putchk(si->ib, &trash) == -1)
2581 return 0;
2582 return 1;
2583}
2584
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002585/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2586 * the state from stream interface <si>. The caller is responsible for clearing
2587 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002588 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002590{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002591 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002592 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 if (!(px->cap & PR_CAP_FE))
2595 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002597 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002598 return 0;
2599
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002600 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002601 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002602 /* name, queue */
2603 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002604
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002605 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002606 /* Column sub-heading for Enable or Disable server */
2607 chunk_appendf(&trash, "<td></td>");
2608 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610 chunk_appendf(&trash,
2611 "<td class=ac>"
2612 "<a name=\"%s/Frontend\"></a>"
2613 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2614 "<td colspan=3></td>"
2615 "",
2616 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002617
Willy Tarreau466c9b52012-12-23 02:25:03 +01002618 chunk_appendf(&trash,
2619 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002620 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002621 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2622 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2623 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002624 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2625 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2626 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002627
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002628 if (px->mode == PR_MODE_HTTP)
2629 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002630 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002631 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002632
2633 chunk_appendf(&trash,
2634 "</table></div></u></td>"
2635 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002636 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002637 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2638 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2639 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002640 U2H(px->fe_counters.sps_max),
2641 U2H(px->fe_counters.cps_max),
2642 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002643
2644 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002646 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002647 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002648
2649 chunk_appendf(&trash,
2650 "</table></div></u></td>"
2651 /* sessions rate : limit */
2652 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002653 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002654
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002655 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002656 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002657 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002658 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002659 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2660 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002661 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002662 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2663 U2H(px->fe_counters.cum_sess),
2664 U2H(px->fe_counters.cum_conn),
2665 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002666
Willy Tarreau466c9b52012-12-23 02:25:03 +01002667 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002668 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002669 chunk_appendf(&trash,
2670 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2671 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2672 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2673 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2674 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2675 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2676 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2677 "<tr><th>- other responses:</th><td>%s</td></tr>"
2678 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2679 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002680 U2H(px->fe_counters.p.http.cum_req),
2681 U2H(px->fe_counters.p.http.rsp[1]),
2682 U2H(px->fe_counters.p.http.rsp[2]),
2683 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002684 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002685 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002686 U2H(px->fe_counters.p.http.rsp[3]),
2687 U2H(px->fe_counters.p.http.rsp[4]),
2688 U2H(px->fe_counters.p.http.rsp[5]),
2689 U2H(px->fe_counters.p.http.rsp[0]),
2690 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002691 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002692
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002694 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002695 /* sessions: lbtot, lastsess */
2696 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002697 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002698 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002699 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002700 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002701
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002702 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002703 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002704 "<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 +01002705 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002706 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002707 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2708 px->fe_counters.comp_in ?
2709 (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 +01002710 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002711
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002712 chunk_appendf(&trash,
2713 /* denied: req, resp */
2714 "<td>%s</td><td>%s</td>"
2715 /* errors : request, connect, response */
2716 "<td>%s</td><td></td><td></td>"
2717 /* warnings: retries, redispatches */
2718 "<td></td><td></td>"
2719 /* server status : reflect frontend status */
2720 "<td class=ac>%s</td>"
2721 /* rest of server: nothing */
2722 "<td class=ac colspan=8></td></tr>"
2723 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002724 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2725 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002726 px->state == PR_STREADY ? "OPEN" :
2727 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002728 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002729 else { /* CSV mode */
2730 chunk_appendf(&trash,
2731 /* pxid, name, queue cur, queue max, */
2732 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002733 /* sessions : current, max, limit, total */
2734 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002735 /* bytes : in, out */
2736 "%lld,%lld,"
2737 /* denied: req, resp */
2738 "%lld,%lld,"
2739 /* errors : request, connect, response */
2740 "%lld,,,"
2741 /* warnings: retries, redispatches */
2742 ",,"
2743 /* server status : reflect frontend status */
2744 "%s,"
2745 /* rest of server: nothing */
2746 ",,,,,,,,"
2747 /* pid, iid, sid, throttle, lbtot, tracked, type */
2748 "%d,%d,0,,,,%d,"
2749 /* rate, rate_lim, rate_max */
2750 "%u,%u,%u,"
2751 /* check_status, check_code, check_duration */
2752 ",,,",
2753 px->id,
2754 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2755 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2756 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2757 px->fe_counters.failed_req,
2758 px->state == PR_STREADY ? "OPEN" :
2759 px->state == PR_STFULL ? "FULL" : "STOP",
2760 relative_pid, px->uuid, STATS_TYPE_FE,
2761 read_freq_ctr(&px->fe_sess_per_sec),
2762 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002763
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2765 if (px->mode == PR_MODE_HTTP) {
2766 for (i=1; i<6; i++)
2767 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2768 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2769 }
2770 else
2771 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002772
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002773 /* failed health analyses */
2774 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 /* requests : req_rate, req_rate_max, req_tot, */
2777 chunk_appendf(&trash, "%u,%u,%lld,",
2778 read_freq_ctr(&px->fe_req_per_sec),
2779 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2780
2781 /* errors: cli_aborts, srv_aborts */
2782 chunk_appendf(&trash, ",,");
2783
2784 /* compression: in, out, bypassed */
2785 chunk_appendf(&trash, "%lld,%lld,%lld,",
2786 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2787
2788 /* compression: comp_rsp */
2789 chunk_appendf(&trash, "%lld,",
2790 px->fe_counters.p.http.comp_rsp);
2791
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002792 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2793 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002794
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002795 /* finish with EOL */
2796 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002797 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002799}
2800
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2802 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2803 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2804 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002805 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002806static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002807{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002808 struct appctx *appctx = __objt_appctx(si->end);
2809
2810 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002811 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002812 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002813 /* Column sub-heading for Enable or Disable server */
2814 chunk_appendf(&trash, "<td></td>");
2815 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002816 chunk_appendf(&trash,
2817 /* frontend name, listener name */
2818 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2819 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2820 "",
2821 px->id, l->name,
2822 (flags & ST_SHLGNDS)?"<u>":"",
2823 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002824
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002825 if (flags & ST_SHLGNDS) {
2826 char str[INET6_ADDRSTRLEN];
2827 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002828
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002829 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002830
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002831 port = get_host_port(&l->addr);
2832 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2833 case AF_INET:
2834 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2835 break;
2836 case AF_INET6:
2837 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2838 break;
2839 case AF_UNIX:
2840 chunk_appendf(&trash, "unix, ");
2841 break;
2842 case -1:
2843 chunk_appendf(&trash, "(%s), ", strerror(errno));
2844 break;
2845 }
Willy Tarreau91861262007-10-17 17:06:05 +02002846
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002847 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002848 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 }
Willy Tarreau91861262007-10-17 17:06:05 +02002850
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002852 /* queue */
2853 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002854 /* sessions rate: current, max, limit */
2855 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002856 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002857 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002858 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002859 /* bytes: in, out */
2860 "<td>%s</td><td>%s</td>"
2861 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002863 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2864 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002865
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002866 chunk_appendf(&trash,
2867 /* denied: req, resp */
2868 "<td>%s</td><td>%s</td>"
2869 /* errors: request, connect, response */
2870 "<td>%s</td><td></td><td></td>"
2871 /* warnings: retries, redispatches */
2872 "<td></td><td></td>"
2873 /* server status: reflect listener status */
2874 "<td class=ac>%s</td>"
2875 /* rest of server: nothing */
2876 "<td class=ac colspan=8></td></tr>"
2877 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002878 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2879 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002880 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2881 }
2882 else { /* CSV mode */
2883 chunk_appendf(&trash,
2884 /* pxid, name, queue cur, queue max, */
2885 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002886 /* sessions: current, max, limit, total */
2887 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002888 /* bytes: in, out */
2889 "%lld,%lld,"
2890 /* denied: req, resp */
2891 "%lld,%lld,"
2892 /* errors: request, connect, response */
2893 "%lld,,,"
2894 /* warnings: retries, redispatches */
2895 ",,"
2896 /* server status: reflect listener status */
2897 "%s,"
2898 /* rest of server: nothing */
2899 ",,,,,,,,"
2900 /* pid, iid, sid, throttle, lbtot, tracked, type */
2901 "%d,%d,%d,,,,%d,"
2902 /* rate, rate_lim, rate_max */
2903 ",,,"
2904 /* check_status, check_code, check_duration */
2905 ",,,"
2906 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2907 ",,,,,,"
2908 /* failed health analyses */
2909 ","
2910 /* requests : req_rate, req_rate_max, req_tot, */
2911 ",,,"
2912 /* errors: cli_aborts, srv_aborts */
2913 ",,"
2914 /* compression: in, out, bypassed, comp_rsp */
2915 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002916 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2917 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002918 "\n",
2919 px->id, l->name,
2920 l->nbconn, l->counters->conn_max,
2921 l->maxconn, l->counters->cum_conn,
2922 l->counters->bytes_in, l->counters->bytes_out,
2923 l->counters->denied_req, l->counters->denied_resp,
2924 l->counters->failed_req,
2925 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2926 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2927 }
2928 return 1;
2929}
Willy Tarreau91861262007-10-17 17:06:05 +02002930
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002931/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2932 * from stream interface <si>, stats flags <flags>, and server state <state>.
2933 * The caller is responsible for clearing the trash if needed. Returns non-zero
2934 * if it emits anything, zero otherwise. The <state> parameter can take the
Willy Tarreau9638efa2014-05-23 11:19:57 +02002935 * following values : 0=DOWN, 1=DOWN(agent) 2=going up, 3=going down, 4=UP, 5,6=NOLB,
2936 * 7,8=DRAIN, 9=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002937 */
2938static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2939{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002940 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002941 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002942 char str[INET6_ADDRSTRLEN];
2943 struct chunk src;
2944 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002945
Willy Tarreau32091232014-05-16 13:52:00 +02002946 /* we have "via" which is the tracked server as described in the configuration,
2947 * and "ref" which is the checked server and the end of the chain.
2948 */
2949 via = sv->track ? sv->track : sv;
2950 ref = via;
2951 while (ref->track)
2952 ref = ref->track;
2953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002954 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02002955 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956 "DOWN",
Willy Tarreau9638efa2014-05-23 11:19:57 +02002957 "DOWN (agent)",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002958 "DN %d/%d &uarr;",
2959 "UP %d/%d &darr;",
2960 "UP",
2961 "NOLB %d/%d &darr;",
2962 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002963 "DRAIN %d/%d &darr;",
2964 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 "<i>no check</i>"
2966 };
Willy Tarreau91861262007-10-17 17:06:05 +02002967
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002968 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002969 chunk_appendf(&trash, "<tr class=\"maintain\">");
2970 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002971 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002972 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002973 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002974
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002975 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002976 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2978 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002979
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002980 chunk_appendf(&trash,
2981 "<td class=ac><a name=\"%s/%s\"></a>%s"
2982 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2983 "",
2984 px->id, sv->id,
2985 (flags & ST_SHLGNDS) ? "<u>" : "",
2986 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002987
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002989 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990
2991 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2992 case AF_INET:
2993 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2994 break;
2995 case AF_INET6:
2996 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2997 break;
2998 case AF_UNIX:
2999 chunk_appendf(&trash, "unix, ");
3000 break;
3001 case -1:
3002 chunk_appendf(&trash, "(%s), ", strerror(errno));
3003 break;
3004 default: /* address family not supported */
3005 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003006 }
Willy Tarreau91861262007-10-17 17:06:05 +02003007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003008 /* id */
3009 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003010
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 /* cookie */
3012 if (sv->cookie) {
3013 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003015 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3016 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003017
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003019 }
3020
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003021 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003022 }
Willy Tarreau91861262007-10-17 17:06:05 +02003023
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003024 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003026 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027 /* sessions rate : current, max, limit */
3028 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003031 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3032 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003033
Willy Tarreau466c9b52012-12-23 02:25:03 +01003034
3035 chunk_appendf(&trash,
3036 /* sessions: current, max, limit, total */
3037 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003038 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003039 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3040 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003041 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3042 U2H(sv->counters.cum_sess),
3043 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003044
Willy Tarreau466c9b52012-12-23 02:25:03 +01003045 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3046 if (px->mode == PR_MODE_HTTP) {
3047 unsigned long long tot;
3048 for (tot = i = 0; i < 6; i++)
3049 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003050
Willy Tarreau466c9b52012-12-23 02:25:03 +01003051 chunk_appendf(&trash,
3052 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3053 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3054 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3055 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3056 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3057 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3058 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3059 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003060 U2H(tot),
3061 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3062 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3063 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3064 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3065 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3066 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 +02003067 }
Willy Tarreau91861262007-10-17 17:06:05 +02003068
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003069 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3070 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)));
3071 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)));
3072 if (px->mode == PR_MODE_HTTP)
3073 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)));
3074 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)));
3075
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003076 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003077 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003078 /* sessions: lbtot, last */
3079 "<td>%s</td><td>%s</td>",
3080 U2H(sv->counters.cum_lbconn),
3081 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003082
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003083 chunk_appendf(&trash,
3084 /* bytes : in, out */
3085 "<td>%s</td><td>%s</td>"
3086 /* denied: req, resp */
3087 "<td></td><td>%s</td>"
3088 /* errors : request, connect */
3089 "<td></td><td>%s</td>"
3090 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003091 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003092 /* warnings: retries, redispatches */
3093 "<td>%lld</td><td>%lld</td>"
3094 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003095 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3096 U2H(sv->counters.failed_secu),
3097 U2H(sv->counters.failed_conns),
3098 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003099 sv->counters.cli_aborts,
3100 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003101 sv->counters.retries, sv->counters.redispatches);
3102
3103 /* status, lest check */
3104 chunk_appendf(&trash, "<td class=ac>");
3105
Willy Tarreau20125212014-05-13 19:44:56 +02003106 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3108 chunk_appendf(&trash, "MAINT");
3109 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003110 else if ((ref->agent.state & CHK_ST_ENABLED) && (ref->state == SRV_ST_STOPPED)) {
3111 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003112 /* DOWN (agent) */
3113 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 +02003114 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003115 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003116 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3117 chunk_appendf(&trash,
3118 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003119 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3120 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003121 }
Willy Tarreau91861262007-10-17 17:06:05 +02003122
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003123 if ((sv->state == SRV_ST_STOPPED) &&
3124 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3125 chunk_appendf(&trash,
3126 "</td><td class=ac><u> %s%s",
3127 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3128 get_check_status_info(sv->agent.status));
3129
3130 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3131 chunk_appendf(&trash, "/%d", sv->agent.code);
3132
3133 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3134 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3135
3136 chunk_appendf(&trash, "<div class=tips>%s",
3137 get_check_status_description(sv->agent.status));
3138 if (*sv->agent.desc) {
3139 chunk_appendf(&trash, ": ");
3140 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3141 chunk_htmlencode(&trash, &src);
3142 }
3143 chunk_appendf(&trash, "</div></u>");
3144 }
3145 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003146 chunk_appendf(&trash,
3147 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003148 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003149 get_check_status_info(sv->check.status));
3150
3151 if (sv->check.status >= HCHK_STATUS_L57DATA)
3152 chunk_appendf(&trash, "/%d", sv->check.code);
3153
3154 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003155 chunk_appendf(&trash, " in %lums", sv->check.duration);
3156
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003157 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003158 get_check_status_description(sv->check.status));
3159 if (*sv->check.desc) {
3160 chunk_appendf(&trash, ": ");
3161 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3162 chunk_htmlencode(&trash, &src);
3163 }
3164 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003165 }
3166 else
3167 chunk_appendf(&trash, "</td><td>");
3168
3169 chunk_appendf(&trash,
3170 /* weight */
3171 "</td><td class=ac>%d</td>"
3172 /* act, bck */
3173 "<td class=ac>%s</td><td class=ac>%s</td>"
3174 "",
3175 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003176 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3177 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003178
3179 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003180 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003181 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003182
3183 if (ref->observe)
3184 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3185
3186 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003187 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003188 "<td>%lld</td><td>%s</td>"
3189 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003190 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003191 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3192 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003193 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3194 /* tracking a server */
3195 chunk_appendf(&trash,
3196 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003197 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003198 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003199 else
3200 chunk_appendf(&trash, "<td colspan=3></td>");
3201
3202 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003203 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003204 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003205 else
3206 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3207 }
3208 else { /* CSV mode */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003209 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003210 "DOWN,",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003211 "DOWN (agent),",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212 "DOWN %d/%d,",
3213 "UP %d/%d,",
3214 "UP,",
3215 "NOLB %d/%d,",
3216 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003217 "DRAIN %d/%d,",
3218 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 "no check,"
3220 };
3221
3222 chunk_appendf(&trash,
3223 /* pxid, name */
3224 "%s,%s,"
3225 /* queue : current, max */
3226 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003227 /* sessions : current, max, limit, total */
3228 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 /* bytes : in, out */
3230 "%lld,%lld,"
3231 /* denied: req, resp */
3232 ",%lld,"
3233 /* errors : request, connect, response */
3234 ",%lld,%lld,"
3235 /* warnings: retries, redispatches */
3236 "%lld,%lld,"
3237 "",
3238 px->id, sv->id,
3239 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003240 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003241 sv->counters.bytes_in, sv->counters.bytes_out,
3242 sv->counters.failed_secu,
3243 sv->counters.failed_conns, sv->counters.failed_resp,
3244 sv->counters.retries, sv->counters.redispatches);
3245
3246 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003247 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003248 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003249 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003250 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 else
3252 chunk_appendf(&trash,
3253 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003254 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3255 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256
3257 chunk_appendf(&trash,
3258 /* weight, active, backup */
3259 "%d,%d,%d,"
3260 "",
3261 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003262 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3263 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003264
3265 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003266 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267 chunk_appendf(&trash,
3268 "%lld,%lld,%d,%d,",
3269 sv->counters.failed_checks, sv->counters.down_trans,
3270 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3271 else
3272 chunk_appendf(&trash, ",,,,");
3273
3274 /* queue limit, pid, iid, sid, */
3275 chunk_appendf(&trash,
3276 "%s,"
3277 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003278 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279 relative_pid, px->uuid, sv->puid);
3280
3281 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003282 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003283 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003284
3285 /* sessions: lbtot */
3286 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3287
3288 /* tracked */
3289 if (sv->track)
3290 chunk_appendf(&trash, "%s/%s,",
3291 sv->track->proxy->id, sv->track->id);
3292 else
3293 chunk_appendf(&trash, ",");
3294
3295 /* type */
3296 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3297
3298 /* rate */
3299 chunk_appendf(&trash, "%u,,%u,",
3300 read_freq_ctr(&sv->sess_per_sec),
3301 sv->counters.sps_max);
3302
Willy Tarreauff5ae352013-12-11 20:36:34 +01003303 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003304 /* check_status */
3305 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3306
3307 /* check_code */
3308 if (sv->check.status >= HCHK_STATUS_L57DATA)
3309 chunk_appendf(&trash, "%u,", sv->check.code);
3310 else
3311 chunk_appendf(&trash, ",");
3312
3313 /* check_duration */
3314 if (sv->check.status >= HCHK_STATUS_CHECKED)
3315 chunk_appendf(&trash, "%lu,", sv->check.duration);
3316 else
3317 chunk_appendf(&trash, ",");
3318
3319 }
3320 else
3321 chunk_appendf(&trash, ",,,");
3322
3323 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3324 if (px->mode == PR_MODE_HTTP) {
3325 for (i=1; i<6; i++)
3326 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3327
3328 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3329 }
3330 else
3331 chunk_appendf(&trash, ",,,,,,");
3332
3333 /* failed health analyses */
3334 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3335
3336 /* requests : req_rate, req_rate_max, req_tot, */
3337 chunk_appendf(&trash, ",,,");
3338
3339 /* errors: cli_aborts, srv_aborts */
3340 chunk_appendf(&trash, "%lld,%lld,",
3341 sv->counters.cli_aborts, sv->counters.srv_aborts);
3342
3343 /* compression: in, out, bypassed, comp_rsp */
3344 chunk_appendf(&trash, ",,,,");
3345
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003346 /* lastsess */
3347 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3348
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003349 /* capture of last check and agent statuses */
3350 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3351 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3352
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003353 /* qtime, ctime, rtime, ttime, */
3354 chunk_appendf(&trash, "%u,%u,%u,%u,",
3355 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3356 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3357 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3358 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3359
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 /* finish with EOL */
3361 chunk_appendf(&trash, "\n");
3362 }
3363 return 1;
3364}
3365
3366/* Dumps a line for backend <px> to the trash for and uses the state from stream
3367 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3368 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3369 */
3370static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3371{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003372 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 struct chunk src;
3374 int i;
3375
3376 if (!(px->cap & PR_CAP_BE))
3377 return 0;
3378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003379 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003380 return 0;
3381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003383 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003384 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 /* Column sub-heading for Enable or Disable server */
3386 chunk_appendf(&trash, "<td></td>");
3387 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003388 chunk_appendf(&trash,
3389 "<td class=ac>"
3390 /* name */
3391 "%s<a name=\"%s/Backend\"></a>"
3392 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3393 "",
3394 (flags & ST_SHLGNDS)?"<u>":"",
3395 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396
3397 if (flags & ST_SHLGNDS) {
3398 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003399 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3401
3402 /* cookie */
3403 if (px->cookie_name) {
3404 chunk_appendf(&trash, ", cookie: '");
3405 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3406 chunk_htmlencode(&trash, &src);
3407 chunk_appendf(&trash, "'");
3408 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003409 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410 }
3411
3412 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003413 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 /* queue : current, max */
3415 "<td>%s</td><td>%s</td><td></td>"
3416 /* sessions rate : current, max, limit */
3417 "<td>%s</td><td>%s</td><td></td>"
3418 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003420 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3421 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422
3423 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003424 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003426 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003427 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003429 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3430 U2H(px->be_counters.cum_conn),
3431 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432
Willy Tarreau466c9b52012-12-23 02:25:03 +01003433 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003434 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003435 chunk_appendf(&trash,
3436 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3437 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3438 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3439 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3440 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3441 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3442 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3443 "<tr><th>- other responses:</th><td>%s</td></tr>"
3444 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003445 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003446 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003447 U2H(px->be_counters.p.http.cum_req),
3448 U2H(px->be_counters.p.http.rsp[1]),
3449 U2H(px->be_counters.p.http.rsp[2]),
3450 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003452 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003453 U2H(px->be_counters.p.http.rsp[3]),
3454 U2H(px->be_counters.p.http.rsp[4]),
3455 U2H(px->be_counters.p.http.rsp[5]),
3456 U2H(px->be_counters.p.http.rsp[0]),
3457 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003458 }
3459
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003460 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)));
3461 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)));
3462 if (px->mode == PR_MODE_HTTP)
3463 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)));
3464 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)));
3465
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003466 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003467 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003468 /* sessions: lbtot, last */
3469 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003471 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003472 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003473 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003474 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003475 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003477 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003478 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003479 "<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 +01003480 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003481 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003482 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3483 px->be_counters.comp_in ?
3484 (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 +01003485 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3486
3487 chunk_appendf(&trash,
3488 /* denied: req, resp */
3489 "<td>%s</td><td>%s</td>"
3490 /* errors : request, connect */
3491 "<td></td><td>%s</td>"
3492 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003493 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 /* warnings: retries, redispatches */
3495 "<td>%lld</td><td>%lld</td>"
3496 /* backend status: reflect backend status (up/down): we display UP
3497 * if the backend has known working servers or if it has no server at
3498 * all (eg: for stats). Then we display the total weight, number of
3499 * active and backups. */
3500 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3501 "<td class=ac>%d</td><td class=ac>%d</td>"
3502 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003503 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3504 U2H(px->be_counters.failed_conns),
3505 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003506 px->be_counters.cli_aborts,
3507 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003508 px->be_counters.retries, px->be_counters.redispatches,
3509 human_time(now.tv_sec - px->last_change, 1),
3510 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3511 "<font color=\"red\"><b>DOWN</b></font>",
3512 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3513 px->srv_act, px->srv_bck);
3514
3515 chunk_appendf(&trash,
3516 /* rest of backend: nothing, down transitions, total downtime, throttle */
3517 "<td class=ac>&nbsp;</td><td>%d</td>"
3518 "<td>%s</td>"
3519 "<td></td>"
3520 "</tr>",
3521 px->down_trans,
3522 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3523 }
3524 else { /* CSV mode */
3525 chunk_appendf(&trash,
3526 /* pxid, name */
3527 "%s,BACKEND,"
3528 /* queue : current, max */
3529 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003530 /* sessions : current, max, limit, total */
3531 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003532 /* bytes : in, out */
3533 "%lld,%lld,"
3534 /* denied: req, resp */
3535 "%lld,%lld,"
3536 /* errors : request, connect, response */
3537 ",%lld,%lld,"
3538 /* warnings: retries, redispatches */
3539 "%lld,%lld,"
3540 /* backend status: reflect backend status (up/down): we display UP
3541 * if the backend has known working servers or if it has no server at
3542 * all (eg: for stats). Then we display the total weight, number of
3543 * active and backups. */
3544 "%s,"
3545 "%d,%d,%d,"
3546 /* rest of backend: nothing, down transitions, last change, total downtime */
3547 ",%d,%d,%d,,"
3548 /* pid, iid, sid, throttle, lbtot, tracked, type */
3549 "%d,%d,0,,%lld,,%d,"
3550 /* rate, rate_lim, rate_max, */
3551 "%u,,%u,"
3552 /* check_status, check_code, check_duration */
3553 ",,,",
3554 px->id,
3555 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3556 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3557 px->be_counters.bytes_in, px->be_counters.bytes_out,
3558 px->be_counters.denied_req, px->be_counters.denied_resp,
3559 px->be_counters.failed_conns, px->be_counters.failed_resp,
3560 px->be_counters.retries, px->be_counters.redispatches,
3561 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3562 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3563 px->srv_act, px->srv_bck,
3564 px->down_trans, (int)(now.tv_sec - px->last_change),
3565 px->srv?be_downtime(px):0,
3566 relative_pid, px->uuid,
3567 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3568 read_freq_ctr(&px->be_sess_per_sec),
3569 px->be_counters.sps_max);
3570
3571 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3572 if (px->mode == PR_MODE_HTTP) {
3573 for (i=1; i<6; i++)
3574 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3575 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3576 }
3577 else
3578 chunk_appendf(&trash, ",,,,,,");
3579
3580 /* failed health analyses */
3581 chunk_appendf(&trash, ",");
3582
3583 /* requests : req_rate, req_rate_max, req_tot, */
3584 chunk_appendf(&trash, ",,,");
3585
3586 /* errors: cli_aborts, srv_aborts */
3587 chunk_appendf(&trash, "%lld,%lld,",
3588 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3589
3590 /* compression: in, out, bypassed */
3591 chunk_appendf(&trash, "%lld,%lld,%lld,",
3592 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3593
3594 /* compression: comp_rsp */
3595 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3596
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003597 /* lastsess, last_chk, last_agt, */
3598 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003599
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003600 /* qtime, ctime, rtime, ttime, */
3601 chunk_appendf(&trash, "%u,%u,%u,%u,",
3602 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3603 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3604 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3605 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3606
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003607 /* finish with EOL */
3608 chunk_appendf(&trash, "\n");
3609 }
3610 return 1;
3611}
3612
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003613/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003614 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003615 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003616 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003617static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003618{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003619 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003620 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3621
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003622 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003623 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003624
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003625 /* 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 +02003626 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003627 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003628 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003629 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3630 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003631 }
3632
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003633 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003634 "<form action=\"%s%s%s%s\" method=\"post\">",
3635 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003636 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3637 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003638 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003639 }
3640
3641 /* print a new table */
3642 chunk_appendf(&trash,
3643 "<table class=\"tbl\" width=\"100%%\">\n"
3644 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003645 "<th class=\"pxname\" width=\"10%%\">");
3646
3647 chunk_appendf(&trash,
3648 "<a name=\"%s\"></a>%s"
3649 "<a class=px href=\"#%s\">%s</a>",
3650 px->id,
3651 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3652 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003653
3654 if (uri->flags & ST_SHLGNDS) {
3655 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003656 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003657 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3658 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003659 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660 }
3661
3662 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003663 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003664 "<th class=\"%s\" width=\"90%%\">%s</th>"
3665 "</tr>\n"
3666 "</table>\n"
3667 "<table class=\"tbl\" width=\"100%%\">\n"
3668 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003669 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3670 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3671
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003672 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003673 /* Column heading for Enable or Disable server */
3674 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003675 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003676
3677 chunk_appendf(&trash,
3678 "<th rowspan=2></th>"
3679 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003680 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003681 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3682 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3683 "<th colspan=9>Server</th>"
3684 "</tr>\n"
3685 "<tr class=\"titre\">"
3686 "<th>Cur</th><th>Max</th><th>Limit</th>"
3687 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003688 "<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 +01003689 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3690 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3691 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3692 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3693 "<th>Thrtle</th>\n"
3694 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003695}
3696
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003697/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003698 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003699 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003700static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003701{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003702 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003703 chunk_appendf(&trash, "</table>");
3704
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003705 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003706 /* close the form used to enable/disable this proxy servers */
3707 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003708 "Choose the action to perform on the checked servers : "
3709 "<select name=action>"
3710 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003711 "<option value=\"ready\">Set state to READY</option>"
3712 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corted38f5c02014-07-02 17:49:34 +02003713 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003714 "<option value=\"dhlth\">Health: disable checks</option>"
3715 "<option value=\"ehlth\">Health: enable checks</option>"
3716 "<option value=\"hrunn\">Health: force UP</option>"
3717 "<option value=\"hnolb\">Health: force NOLB</option>"
3718 "<option value=\"hdown\">Health: force DOWN</option>"
3719 "<option value=\"dagent\">Agent: disable checks</option>"
3720 "<option value=\"eagent\">Agent: enable checks</option>"
3721 "<option value=\"arunn\">Agent: force UP</option>"
3722 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003723 "<option value=\"shutdown\">Kill Sessions</option>"
3724 "</select>"
3725 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3726 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3727 "</form>",
3728 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003729 }
3730
3731 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003732}
Willy Tarreau91861262007-10-17 17:06:05 +02003733
3734/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003735 * Dumps statistics for a proxy. The output is sent to the stream interface's
3736 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3737 * buffer space, or non-zero if everything completed. This function is used
3738 * both by the CLI and the HTTP entry points, and is able to dump the output
3739 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003740 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003741static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003742{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003743 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003744 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003745 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003746 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003747 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003748
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003749 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003750
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003751 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003752 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003753 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003754 if (uri && uri->scope) {
3755 /* we have a limited scope, we have to check the proxy name */
3756 struct stat_scope *scope;
3757 int len;
3758
3759 len = strlen(px->id);
3760 scope = uri->scope;
3761
3762 while (scope) {
3763 /* match exact proxy name */
3764 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3765 break;
3766
3767 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003768 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003769 break;
3770 scope = scope->next;
3771 }
3772
3773 /* proxy name not found : don't dump anything */
3774 if (scope == NULL)
3775 return 1;
3776 }
3777
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003778 /* if the user has requested a limited output and the proxy
3779 * name does not match, skip it.
3780 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003781 if (appctx->ctx.stats.scope_len &&
3782 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003783 return 1;
3784
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003785 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3786 (appctx->ctx.stats.iid != -1) &&
3787 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003788 return 1;
3789
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003790 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003791 /* fall through */
3792
Willy Tarreau295a8372011-03-10 11:25:07 +01003793 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003794 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003795 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003796 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003797 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003798 }
Willy Tarreau91861262007-10-17 17:06:05 +02003799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003800 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003801 /* fall through */
3802
Willy Tarreau295a8372011-03-10 11:25:07 +01003803 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003804 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003805 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003806 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003807 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003808
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003809 appctx->ctx.stats.l = px->conf.listeners.n;
3810 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003811 /* fall through */
3812
Willy Tarreau295a8372011-03-10 11:25:07 +01003813 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003814 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003815 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003816 if (buffer_almost_full(rep->buf)) {
3817 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003818 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003819 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003820
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003821 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003822 if (!l->counters)
3823 continue;
3824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003825 if (appctx->ctx.stats.flags & STAT_BOUND) {
3826 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003827 break;
3828
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003829 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003830 continue;
3831 }
3832
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833 /* print the frontend */
3834 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3835 if (bi_putchk(rep, &trash) == -1)
3836 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003837 }
3838
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003839 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3840 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003841 /* fall through */
3842
Willy Tarreau295a8372011-03-10 11:25:07 +01003843 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003844 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003845 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003846 int sv_state;
Willy Tarreau91861262007-10-17 17:06:05 +02003847
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003848 if (buffer_almost_full(rep->buf)) {
3849 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003850 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003851 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003852
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003854
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003855 if (appctx->ctx.stats.flags & STAT_BOUND) {
3856 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003857 break;
3858
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003859 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003860 continue;
3861 }
3862
Willy Tarreau32091232014-05-16 13:52:00 +02003863 svs = sv;
3864 while (svs->track)
3865 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003866
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003867 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3868 /* server is UP. The possibilities are :
Willy Tarreau9638efa2014-05-23 11:19:57 +02003869 * - UP, draining, going down => state = 7
3870 * - UP, going down => state = 3
3871 * - UP, draining => state = 8
3872 * - UP, checked => state = 4
3873 * - UP, not checked nor tracked => state = 9
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003874 */
3875
3876 if ((svs->check.state & CHK_ST_ENABLED) &&
3877 (svs->check.health < svs->check.rise + svs->check.fall - 1))
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003878 sv_state = 3;
Willy Tarreau9638efa2014-05-23 11:19:57 +02003879 else
3880 sv_state = 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003881
Willy Tarreauefe28222014-05-21 17:13:13 +02003882 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003883 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003884
Willy Tarreau9638efa2014-05-23 11:19:57 +02003885 if (sv_state == 4 && !(svs->check.state & CHK_ST_ENABLED))
3886 sv_state = 9; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003887 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003888 else if (sv->state == SRV_ST_STOPPING) {
3889 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3890 (svs->check.health == svs->check.rise + svs->check.fall - 1))
Willy Tarreau9638efa2014-05-23 11:19:57 +02003891 sv_state = 6; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003892 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003893 sv_state = 5; /* NOLB going down */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003894 }
3895 else { /* stopped */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003896 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health)
3897 sv_state = 1; /* DOWN (agent) */
3898 else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003899 sv_state = 0; /* DOWN */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003900 else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED))
3901 sv_state = 2; /* going up */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003902 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003903 sv_state = 0; /* DOWN, unchecked */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003904 }
Willy Tarreau91861262007-10-17 17:06:05 +02003905
Willy Tarreau9638efa2014-05-23 11:19:57 +02003906 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003907 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003908 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003909 continue;
3910 }
3911
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3913 if (bi_putchk(rep, &trash) == -1)
3914 return 0;
3915 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003916
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003917 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003918 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003919
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003920 case STAT_PX_ST_BE:
3921 /* print the backend */
3922 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3923 if (bi_putchk(rep, &trash) == -1)
3924 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003925
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003926 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003927 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003928
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003929 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003930 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003931 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003932 if (bi_putchk(rep, &trash) == -1)
3933 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003934 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003935
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003936 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003937 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003938
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003939 case STAT_PX_ST_FIN:
3940 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003941
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003942 default:
3943 /* unknown state, we should put an abort() here ! */
3944 return 1;
3945 }
3946}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003947
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003948/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3949 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003950 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003951static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003952{
3953 /* WARNING! This must fit in the first buffer !!! */
3954 chunk_appendf(&trash,
3955 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3956 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3957 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3958 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3959 "<style type=\"text/css\"><!--\n"
3960 "body {"
3961 " font-family: arial, helvetica, sans-serif;"
3962 " font-size: 12px;"
3963 " font-weight: normal;"
3964 " color: black;"
3965 " background: white;"
3966 "}\n"
3967 "th,td {"
3968 " font-size: 10px;"
3969 "}\n"
3970 "h1 {"
3971 " font-size: x-large;"
3972 " margin-bottom: 0.5em;"
3973 "}\n"
3974 "h2 {"
3975 " font-family: helvetica, arial;"
3976 " font-size: x-large;"
3977 " font-weight: bold;"
3978 " font-style: italic;"
3979 " color: #6020a0;"
3980 " margin-top: 0em;"
3981 " margin-bottom: 0em;"
3982 "}\n"
3983 "h3 {"
3984 " font-family: helvetica, arial;"
3985 " font-size: 16px;"
3986 " font-weight: bold;"
3987 " color: #b00040;"
3988 " background: #e8e8d0;"
3989 " margin-top: 0em;"
3990 " margin-bottom: 0em;"
3991 "}\n"
3992 "li {"
3993 " margin-top: 0.25em;"
3994 " margin-right: 2em;"
3995 "}\n"
3996 ".hr {margin-top: 0.25em;"
3997 " border-color: black;"
3998 " border-bottom-style: solid;"
3999 "}\n"
4000 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4001 ".total {background: #20D0D0;color: #ffff80;}\n"
4002 ".frontend {background: #e8e8d0;}\n"
4003 ".socket {background: #d0d0d0;}\n"
4004 ".backend {background: #e8e8d0;}\n"
4005 ".active0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004006 ".active1 {background: #ff9090;}\n"
4007 ".active2 {background: #ffd020;}\n"
4008 ".active3 {background: #ffffa0;}\n"
4009 ".active4 {background: #c0ffc0;}\n"
4010 ".active5 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
4011 ".active6 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
4012 ".active7 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
4013 ".active8 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
4014 ".active9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004015 ".backup0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004016 ".backup1 {background: #ff9090;}\n"
4017 ".backup2 {background: #ff80ff;}\n"
4018 ".backup3 {background: #c060ff;}\n"
4019 ".backup4 {background: #b0d0ff;}\n"
4020 ".backup5 {background: #c060ff;}\n" /* NOLB state shows same as going down */
4021 ".backup6 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
4022 ".backup7 {background: #c060ff;}\n"
4023 ".backup8 {background: #cc9900;}\n"
4024 ".backup9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004025 ".maintain {background: #c07820;}\n"
4026 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4027 "\n"
4028 "a.px:link {color: #ffff40; text-decoration: none;}"
4029 "a.px:visited {color: #ffff40; text-decoration: none;}"
4030 "a.px:hover {color: #ffffff; text-decoration: none;}"
4031 "a.lfsb:link {color: #000000; text-decoration: none;}"
4032 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4033 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4034 "\n"
4035 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4036 "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"
4037 "table.tbl td.ac { text-align: center;}\n"
4038 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4039 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4040 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4041 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4042 "\n"
4043 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4044 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4045 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004046 "table.det { border-collapse: collapse; border-style: none; }\n"
4047 "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 +01004048 "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 +01004049 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004050 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004051 " display:block;\n"
4052 " visibility:hidden;\n"
4053 " z-index:2147483647;\n"
4054 " position:absolute;\n"
4055 " padding:2px 4px 3px;\n"
4056 " background:#f0f060; color:#000000;\n"
4057 " border:1px solid #7040c0;\n"
4058 " white-space:nowrap;\n"
4059 " font-style:normal;font-size:11px;font-weight:normal;\n"
4060 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4061 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4062 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004063 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004064 "-->\n"
4065 "</style></head>\n",
4066 (uri->flags & ST_SHNODE) ? " on " : "",
4067 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4068 );
4069}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004070
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004071/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004072 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004073 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004075static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004076{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004077 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004078 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004079 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004080
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004081 /* WARNING! this has to fit the first packet too.
4082 * We are around 3.5 kB, add adding entries will
4083 * become tricky if we want to support 4kB buffers !
4084 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004085 chunk_appendf(&trash,
4086 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4087 PRODUCT_NAME "%s</a></h1>\n"
4088 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4089 "<hr width=\"100%%\" class=\"hr\">\n"
4090 "<h3>&gt; General process information</h3>\n"
4091 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4092 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4093 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4094 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4095 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4096 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4097 "Running tasks: %d/%d; idle = %d %%<br>\n"
4098 "</td><td align=\"center\" nowrap>\n"
4099 "<table class=\"lgd\"><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004100 "<td class=\"active4\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4101 "<td class=\"backup4\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004102 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004103 "<td class=\"active3\"></td><td class=\"noborder\">active UP, going down </td>"
4104 "<td class=\"backup3\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004106 "<td class=\"active2\"></td><td class=\"noborder\">active DOWN, going up </td>"
4107 "<td class=\"backup2\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 "</tr><tr>\n"
4109 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004110 "<td class=\"active9\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111 "</tr><tr>\n"
4112 "<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 -07004113 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004114 "<td class=\"active8\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004115 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004116 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 "</td>"
4118 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4119 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4120 "",
4121 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4122 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4123 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4124 (uri->flags & ST_SHDESC) ? ": " : "",
4125 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4126 pid, relative_pid, global.nbproc,
4127 up / 86400, (up % 86400) / 3600,
4128 (up % 3600) / 60, (up % 60),
4129 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4130 global.rlimit_memmax ? " MB" : "",
4131 global.rlimit_nofile,
4132 global.maxsock, global.maxconn, global.maxpipes,
4133 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4134 run_queue_cur, nb_tasks_cur, idle_pct
4135 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004136
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004137 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4138 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4139 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004140
4141 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02004142 "<li><form method=\"GET\" action=\"%s%s%s\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004143 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004144 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4145 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4146 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004147 STAT_SCOPE_TXT_MAXLEN);
4148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004149 /* 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 +02004150 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004151 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004152 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004153 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4154 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004155 }
4156
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004157 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004158 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004159 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004160 uri->uri_prefix,
4161 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004162 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004163 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004164 else
4165 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004166 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004167 uri->uri_prefix,
4168 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004169 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004170 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004171
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004172 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004173 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004174 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004175 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004176 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004177 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004178 "",
4179 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004180 else
4181 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004182 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004183 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004184 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004185 ";norefresh",
4186 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004187 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004188
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004189 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004190 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004191 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004192 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4193 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004194 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004195
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004196 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004197 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004198 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004199 (uri->refresh > 0) ? ";norefresh" : "",
4200 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004201
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004202 chunk_appendf(&trash,
4203 "</ul></td>"
4204 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4205 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4206 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4207 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4208 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4209 "</ul>"
4210 "</td>"
4211 "</tr></table>\n"
4212 ""
4213 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004214
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004215 if (appctx->ctx.stats.st_code) {
4216 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004217 case STAT_STATUS_DONE:
4218 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004219 "<p><div class=active4>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004220 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004221 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004222 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004223 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4224 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004225 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004226 break;
4227 case STAT_STATUS_NONE:
4228 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004229 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004230 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004231 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004232 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004233 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4234 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004235 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004236 break;
4237 case STAT_STATUS_PART:
4238 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004239 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004240 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004241 "Action partially processed.<br>"
4242 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004243 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004244 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4245 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004246 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004247 break;
4248 case STAT_STATUS_ERRP:
4249 chunk_appendf(&trash,
4250 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004251 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004252 "Action not processed because of invalid parameters."
4253 "<ul>"
4254 "<li>The action is maybe unknown.</li>"
4255 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4256 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4257 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004258 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004259 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4260 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004261 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004262 break;
4263 case STAT_STATUS_EXCD:
4264 chunk_appendf(&trash,
4265 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004266 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004267 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4268 "You should retry with less servers at a time.</b>"
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_DENY:
4275 chunk_appendf(&trash,
4276 "<p><div class=active0>"
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 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004279 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004280 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4281 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004282 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004283 break;
4284 default:
4285 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004286 "<p><div class=active9>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004287 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004288 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004289 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004290 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4291 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004292 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004293 }
4294 chunk_appendf(&trash, "<p>\n");
4295 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004296}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004297
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004298/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4299 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004300 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004301static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004302{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004303 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004305
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004306/* This function dumps statistics onto the stream interface's read buffer in
4307 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004308 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4309 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4310 * and the session must be closed, or -1 in case of any error. This function is
4311 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004312 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004313static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004314{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004315 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 struct channel *rep = si->ib;
4317 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004318
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004319 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004320
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004321 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004323 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004324 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004325
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004326 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004327 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004328 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004329 else
4330 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004331
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332 if (bi_putchk(rep, &trash) == -1)
4333 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004334
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004335 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004337
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004338 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004339 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004340 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004341 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004342 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004343 }
Willy Tarreau91861262007-10-17 17:06:05 +02004344
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004345 appctx->ctx.stats.px = proxy;
4346 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4347 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004348 /* fall through */
4349
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004350 case STAT_ST_LIST:
4351 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004352 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004353 if (buffer_almost_full(rep->buf)) {
4354 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004355 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004356 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004358 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004359 /* skip the disabled proxies, global frontend and non-networked ones */
4360 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004361 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004363
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004364 appctx->ctx.stats.px = px->next;
4365 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366 }
4367 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004368
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004371
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004372 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004373 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004374 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 if (bi_putchk(rep, &trash) == -1)
4376 return 0;
4377 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004379 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004381
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382 case STAT_ST_FIN:
4383 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004385 default:
4386 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004388 return -1;
4389 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390}
Willy Tarreauae526782010-03-04 20:34:23 +01004391
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392/* We reached the stats page through a POST request. The appctx is
4393 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004394 * Parse the posted data and enable/disable servers if necessary.
4395 * Returns 1 if request was parsed or zero if it needs more data.
4396 */
4397static int stats_process_http_post(struct stream_interface *si)
4398{
4399 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004400 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004401
4402 struct proxy *px = NULL;
4403 struct server *sv = NULL;
4404
4405 char key[LINESIZE];
4406 int action = ST_ADM_ACTION_NONE;
4407 int reprocess = 0;
4408
4409 int total_servers = 0;
4410 int altered_servers = 0;
4411
4412 char *first_param, *cur_param, *next_param, *end_params;
4413 char *st_cur_param = NULL;
4414 char *st_next_param = NULL;
4415
4416 struct chunk *temp;
4417 int reql;
4418
4419 temp = get_trash_chunk();
4420 if (temp->size < s->txn.req.body_len) {
4421 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004422 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004423 goto out;
4424 }
4425
4426 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4427 if (reql <= 0) {
4428 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004429 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004430 return 0;
4431 }
4432
4433 first_param = temp->str;
4434 end_params = temp->str + reql;
4435 cur_param = next_param = end_params;
4436 *end_params = '\0';
4437
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004438 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004439
4440 /*
4441 * Parse the parameters in reverse order to only store the last value.
4442 * From the html form, the backend and the action are at the end.
4443 */
4444 while (cur_param > first_param) {
4445 char *value;
4446 int poffset, plen;
4447
4448 cur_param--;
4449
4450 if ((*cur_param == '&') || (cur_param == first_param)) {
4451 reprocess_servers:
4452 /* Parse the key */
4453 poffset = (cur_param != first_param ? 1 : 0);
4454 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4455 if ((plen > 0) && (plen <= sizeof(key))) {
4456 strncpy(key, cur_param + poffset, plen);
4457 key[plen - 1] = '\0';
4458 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004459 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004460 goto out;
4461 }
4462
4463 /* Parse the value */
4464 value = key;
4465 while (*value != '\0' && *value != '=') {
4466 value++;
4467 }
4468 if (*value == '=') {
4469 /* Ok, a value is found, we can mark the end of the key */
4470 *value++ = '\0';
4471 }
4472 if (url_decode(key) < 0 || url_decode(value) < 0)
4473 break;
4474
4475 /* Now we can check the key to see what to do */
4476 if (!px && (strcmp(key, "b") == 0)) {
4477 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4478 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004479 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004480 goto out;
4481 }
4482 }
4483 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004484 if (strcmp(value, "ready") == 0) {
4485 action = ST_ADM_ACTION_READY;
4486 }
4487 else if (strcmp(value, "drain") == 0) {
4488 action = ST_ADM_ACTION_DRAIN;
4489 }
4490 else if (strcmp(value, "maint") == 0) {
4491 action = ST_ADM_ACTION_MAINT;
4492 }
4493 else if (strcmp(value, "shutdown") == 0) {
4494 action = ST_ADM_ACTION_SHUTDOWN;
4495 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004496 else if (strcmp(value, "dhlth") == 0) {
4497 action = ST_ADM_ACTION_DHLTH;
4498 }
4499 else if (strcmp(value, "ehlth") == 0) {
4500 action = ST_ADM_ACTION_EHLTH;
4501 }
4502 else if (strcmp(value, "hrunn") == 0) {
4503 action = ST_ADM_ACTION_HRUNN;
4504 }
4505 else if (strcmp(value, "hnolb") == 0) {
4506 action = ST_ADM_ACTION_HNOLB;
4507 }
4508 else if (strcmp(value, "hdown") == 0) {
4509 action = ST_ADM_ACTION_HDOWN;
4510 }
4511 else if (strcmp(value, "dagent") == 0) {
4512 action = ST_ADM_ACTION_DAGENT;
4513 }
4514 else if (strcmp(value, "eagent") == 0) {
4515 action = ST_ADM_ACTION_EAGENT;
4516 }
4517 else if (strcmp(value, "arunn") == 0) {
4518 action = ST_ADM_ACTION_ARUNN;
4519 }
4520 else if (strcmp(value, "adown") == 0) {
4521 action = ST_ADM_ACTION_ADOWN;
4522 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004523 /* else these are the old supported methods */
4524 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004525 action = ST_ADM_ACTION_DISABLE;
4526 }
4527 else if (strcmp(value, "enable") == 0) {
4528 action = ST_ADM_ACTION_ENABLE;
4529 }
4530 else if (strcmp(value, "stop") == 0) {
4531 action = ST_ADM_ACTION_STOP;
4532 }
4533 else if (strcmp(value, "start") == 0) {
4534 action = ST_ADM_ACTION_START;
4535 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004536 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004537 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004538 goto out;
4539 }
4540 }
4541 else if (strcmp(key, "s") == 0) {
4542 if (!(px && action)) {
4543 /*
4544 * Indicates that we'll need to reprocess the parameters
4545 * as soon as backend and action are known
4546 */
4547 if (!reprocess) {
4548 st_cur_param = cur_param;
4549 st_next_param = next_param;
4550 }
4551 reprocess = 1;
4552 }
4553 else if ((sv = findserver(px, value)) != NULL) {
4554 switch (action) {
4555 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004556 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004557 altered_servers++;
4558 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004559 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004560 }
4561 break;
4562 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004563 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004564 altered_servers++;
4565 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004566 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004567 }
4568 break;
4569 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004570 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4571 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4572 altered_servers++;
4573 total_servers++;
4574 }
4575 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004576 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004577 if (sv->admin & SRV_ADMF_FDRAIN) {
4578 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4579 altered_servers++;
4580 total_servers++;
4581 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004582 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004583 case ST_ADM_ACTION_DHLTH:
4584 if (sv->check.state & CHK_ST_CONFIGURED) {
4585 sv->check.state &= ~CHK_ST_ENABLED;
4586 altered_servers++;
4587 total_servers++;
4588 }
4589 break;
4590 case ST_ADM_ACTION_EHLTH:
4591 if (sv->check.state & CHK_ST_CONFIGURED) {
4592 sv->check.state |= CHK_ST_ENABLED;
4593 altered_servers++;
4594 total_servers++;
4595 }
4596 break;
4597 case ST_ADM_ACTION_HRUNN:
4598 if (!(sv->track)) {
4599 sv->check.health = sv->check.rise + sv->check.fall - 1;
4600 srv_set_running(sv, "changed from Web interface");
4601 altered_servers++;
4602 total_servers++;
4603 }
4604 break;
4605 case ST_ADM_ACTION_HNOLB:
4606 if (!(sv->track)) {
4607 sv->check.health = sv->check.rise + sv->check.fall - 1;
4608 srv_set_stopping(sv, "changed from Web interface");
4609 altered_servers++;
4610 total_servers++;
4611 }
4612 break;
4613 case ST_ADM_ACTION_HDOWN:
4614 if (!(sv->track)) {
4615 sv->check.health = 0;
4616 srv_set_stopped(sv, "changed from Web interface");
4617 altered_servers++;
4618 total_servers++;
4619 }
4620 break;
4621 case ST_ADM_ACTION_DAGENT:
4622 if (sv->agent.state & CHK_ST_CONFIGURED) {
4623 sv->agent.state &= ~CHK_ST_ENABLED;
4624 altered_servers++;
4625 total_servers++;
4626 }
4627 break;
4628 case ST_ADM_ACTION_EAGENT:
4629 if (sv->agent.state & CHK_ST_CONFIGURED) {
4630 sv->agent.state |= CHK_ST_ENABLED;
4631 altered_servers++;
4632 total_servers++;
4633 }
4634 break;
4635 case ST_ADM_ACTION_ARUNN:
4636 if (sv->agent.state & CHK_ST_ENABLED) {
4637 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4638 srv_set_running(sv, "changed from Web interface");
4639 altered_servers++;
4640 total_servers++;
4641 }
4642 break;
4643 case ST_ADM_ACTION_ADOWN:
4644 if (sv->agent.state & CHK_ST_ENABLED) {
4645 sv->agent.health = 0;
4646 srv_set_stopped(sv, "changed from Web interface");
4647 altered_servers++;
4648 total_servers++;
4649 }
4650 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004651 case ST_ADM_ACTION_READY:
4652 srv_adm_set_ready(sv);
4653 altered_servers++;
4654 total_servers++;
4655 break;
4656 case ST_ADM_ACTION_DRAIN:
4657 srv_adm_set_drain(sv);
4658 altered_servers++;
4659 total_servers++;
4660 break;
4661 case ST_ADM_ACTION_MAINT:
4662 srv_adm_set_maint(sv);
4663 altered_servers++;
4664 total_servers++;
4665 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004666 case ST_ADM_ACTION_SHUTDOWN:
4667 if (px->state != PR_STSTOPPED) {
4668 struct session *sess, *sess_bck;
4669
4670 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4671 if (sess->srv_conn == sv)
4672 session_shutdown(sess, SN_ERR_KILLED);
4673
4674 altered_servers++;
4675 total_servers++;
4676 }
4677 break;
4678 }
4679 } else {
4680 /* the server name is unknown or ambiguous (duplicate names) */
4681 total_servers++;
4682 }
4683 }
4684 if (reprocess && px && action) {
4685 /* Now, we know the backend and the action chosen by the user.
4686 * We can safely restart from the first server parameter
4687 * to reprocess them
4688 */
4689 cur_param = st_cur_param;
4690 next_param = st_next_param;
4691 reprocess = 0;
4692 goto reprocess_servers;
4693 }
4694
4695 next_param = cur_param;
4696 }
4697 }
4698
4699 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004700 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004701 }
4702 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004703 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004704 }
4705 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004706 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004707 }
4708 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004709 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004710 }
4711 out:
4712 return 1;
4713}
4714
4715
4716static int stats_send_http_headers(struct stream_interface *si)
4717{
4718 struct session *s = session_from_task(si->owner);
4719 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004720 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004721
4722 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004723 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004724 "Cache-Control: no-cache\r\n"
4725 "Connection: close\r\n"
4726 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004727 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004728
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004729 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004730 chunk_appendf(&trash, "Refresh: %d\r\n",
4731 uri->refresh);
4732
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004733 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4734
4735 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4736 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4737 else
4738 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004739
4740 s->txn.status = 200;
4741 s->logs.tv_request = now;
4742
4743 if (bi_putchk(si->ib, &trash) == -1)
4744 return 0;
4745
4746 return 1;
4747}
4748
4749static int stats_send_http_redirect(struct stream_interface *si)
4750{
4751 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4752 struct session *s = session_from_task(si->owner);
4753 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004754 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004755
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004756 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004757 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004758 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004759 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004760 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4761 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004762 }
4763
4764 /* We don't want to land on the posted stats page because a refresh will
4765 * repost the data. We don't want this to happen on accident so we redirect
4766 * the browse to the stats page with a GET.
4767 */
4768 chunk_printf(&trash,
4769 "HTTP/1.1 303 See Other\r\n"
4770 "Cache-Control: no-cache\r\n"
4771 "Content-Type: text/plain\r\n"
4772 "Connection: close\r\n"
4773 "Location: %s;st=%s%s%s%s\r\n"
4774 "\r\n",
4775 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004776 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4777 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4778 stat_status_codes[appctx->ctx.stats.st_code]) ?
4779 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004780 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004781 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4782 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004783 scope_txt);
4784
4785 s->txn.status = 303;
4786 s->logs.tv_request = now;
4787
4788 if (bi_putchk(si->ib, &trash) == -1)
4789 return 0;
4790
4791 return 1;
4792}
4793
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004794/* This I/O handler runs as an applet embedded in a stream interface. It is
4795 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004796 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004797 * automatically unregisters itself once transfer is complete.
4798 */
4799static void http_stats_io_handler(struct stream_interface *si)
4800{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004801 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004802 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004803 struct channel *req = si->ob;
4804 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004805
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4807 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004808
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004809 /* check that the output is not closed */
4810 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004811 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004812
Willy Tarreau347a35d2013-11-22 17:51:09 +01004813 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004814 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004815 if (stats_send_http_headers(si)) {
4816 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004817 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004818 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004819 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004820 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004821 }
4822
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004823 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004824 unsigned int prev_len = si->ib->buf->i;
4825 unsigned int data_len;
4826 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004827 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004828
4829 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4830 /* One difficulty we're facing is that we must prevent
4831 * the input data from being automatically forwarded to
4832 * the output area. For this, we temporarily disable
4833 * forwarding on the channel.
4834 */
4835 last_fwd = si->ib->to_forward;
4836 si->ib->to_forward = 0;
4837 chunk_printf(&trash, "\r\n000000\r\n");
4838 if (bi_putchk(si->ib, &trash) == -1) {
4839 si->ib->to_forward = last_fwd;
4840 goto fail;
4841 }
4842 }
4843
4844 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004845 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004846 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004847
4848 last_len = si->ib->buf->i;
4849
4850 /* Now we must either adjust or remove the chunk size. This is
4851 * not easy because the chunk size might wrap at the end of the
4852 * buffer, so we pretend we have nothing in the buffer, we write
4853 * the size, then restore the buffer's contents. Note that we can
4854 * only do that because no forwarding is scheduled on the stats
4855 * applet.
4856 */
4857 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4858 si->ib->total -= (last_len - prev_len);
4859 si->ib->buf->i -= (last_len - prev_len);
4860
4861 if (last_len != data_len) {
4862 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4863 bi_putchk(si->ib, &trash);
4864
4865 si->ib->total += (last_len - data_len);
4866 si->ib->buf->i += (last_len - data_len);
4867 }
4868 /* now re-enable forwarding */
4869 channel_forward(si->ib, last_fwd);
4870 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004871 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004872
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004873 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004874 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004875 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004876 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004877 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004878 }
4879
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004880 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004881 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004882 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004883 }
4884
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004885 if (appctx->st0 == STAT_HTTP_DONE) {
4886 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4887 chunk_printf(&trash, "\r\n0\r\n\r\n");
4888 if (bi_putchk(si->ib, &trash) == -1)
4889 goto fail;
4890 }
4891 /* eat the whole request */
4892 bo_skip(si->ob, si->ob->buf->o);
4893 res->flags |= CF_READ_NULL;
4894 si_shutr(si);
4895 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004896
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004897 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4898 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004899
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004900 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004901 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4902 si_shutr(si);
4903 res->flags |= CF_READ_NULL;
4904 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004905 }
Willy Tarreau91861262007-10-17 17:06:05 +02004906
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004907 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004908 /* update all other flags and resync with the other side */
4909 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004910
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004911 /* we don't want to expire timeouts while we're processing requests */
4912 si->ib->rex = TICK_ETERNITY;
4913 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004914
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004915 out:
4916 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4917 /* check that we have released everything then unregister */
4918 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004919 }
4920}
4921
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004922
Willy Tarreau909d5172012-11-26 03:04:41 +01004923static inline const char *get_conn_ctrl_name(const struct connection *conn)
4924{
Willy Tarreau3c728722014-01-23 13:50:42 +01004925 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004926 return "NONE";
4927 return conn->ctrl->name;
4928}
4929
4930static inline const char *get_conn_xprt_name(const struct connection *conn)
4931{
4932 static char ptr[17];
4933
Willy Tarreauaad69382014-01-23 14:21:42 +01004934 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004935 return "NONE";
4936
4937 if (conn->xprt == &raw_sock)
4938 return "RAW";
4939
4940#ifdef USE_OPENSSL
4941 if (conn->xprt == &ssl_sock)
4942 return "SSL";
4943#endif
4944 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4945 return ptr;
4946}
4947
4948static inline const char *get_conn_data_name(const struct connection *conn)
4949{
4950 static char ptr[17];
4951
4952 if (!conn->data)
4953 return "NONE";
4954
4955 if (conn->data == &sess_conn_cb)
4956 return "SESS";
4957
4958 if (conn->data == &si_conn_cb)
4959 return "STRM";
4960
4961 if (conn->data == &check_conn_cb)
4962 return "CHCK";
4963
4964 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4965 return ptr;
4966}
4967
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004968/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004969 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004970 * 0 if the output buffer is full and it needs to be called again, otherwise
4971 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004972 */
Willy Tarreau76153662012-11-26 01:16:39 +01004973static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004974{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004975 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004976 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004977 extern const char *monthname[12];
4978 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004979 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004980 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004981
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004982 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004983
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004984 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004985 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004986 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4987 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004988 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004989 appctx->ctx.sess.uid = 0;
4990 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004991 return 1;
4992 }
4993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004994 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004995 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004996 appctx->ctx.sess.uid = sess->uniq_id;
4997 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004998 /* fall through */
4999
5000 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005001 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005002 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005003 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005004 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005005 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5006 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005007 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01005008 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005009
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005010 conn = objt_conn(sess->si[0].end);
5011 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005012 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005013 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005014 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005015 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005016 break;
5017 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005018 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005019 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005020 default:
5021 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005022 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005023 break;
5024 }
5025
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005026 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005027 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005028 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005029
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005030 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005031 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005032 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
5033 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
5034 sess->listener ? sess->listener->luid : 0);
5035
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005036 if (conn)
5037 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005038
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005039 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005040 case AF_INET:
5041 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005042 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005043 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005044 break;
5045 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005046 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005047 break;
5048 default:
5049 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005050 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005051 break;
5052 }
5053
Willy Tarreau50943332011-09-02 17:33:05 +02005054 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005055 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005056 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005057 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005058 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5059 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005060 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005061
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005062 conn = objt_conn(sess->si[1].end);
5063 if (conn)
5064 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005065
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005066 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005067 case AF_INET:
5068 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005069 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005070 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005071 break;
5072 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005073 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005074 break;
5075 default:
5076 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005077 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005078 break;
5079 }
5080
5081 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005082 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005083 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005084 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5085 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005086 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005087 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005088
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005089 if (conn)
5090 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005091
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005092 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005093 case AF_INET:
5094 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005095 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005096 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005097 break;
5098 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005099 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005100 break;
5101 default:
5102 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005103 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005104 break;
5105 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005106
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005107 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005108 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005109 sess->task,
5110 sess->task->state,
5111 sess->task->nice, sess->task->calls,
5112 sess->task->expire ?
5113 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5114 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5115 TICKS_TO_MS(1000)) : "<NEVER>",
5116 task_in_rq(sess->task) ? ", running" : "");
5117
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005118 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005119 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005120 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005122 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005123 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
5124 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
5125 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
5126
5127 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005128 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005129 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005130 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005131 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005132 obj_type_name(sess->si[0].end),
5133 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005134 sess->si[0].exp ?
5135 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5136 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5137 TICKS_TO_MS(1000)) : "<NEVER>",
5138 sess->si[0].err_type);
5139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005140 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005141 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005142 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005143 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005144 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005145 obj_type_name(sess->si[1].end),
5146 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005147 sess->si[1].exp ?
5148 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5149 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5150 TICKS_TO_MS(1000)) : "<NEVER>",
5151 sess->si[1].err_type);
5152
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005153 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005154 chunk_appendf(&trash,
5155 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005156 conn,
5157 get_conn_ctrl_name(conn),
5158 get_conn_xprt_name(conn),
5159 get_conn_data_name(conn),
5160 obj_type_name(conn->target),
5161 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005162
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005163 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005164 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005165 conn->flags,
5166 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005167 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005168 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005169 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005170 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005171 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5172 chunk_appendf(&trash,
5173 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5174 tmpctx,
5175 tmpctx->st0,
5176 tmpctx->st1,
5177 tmpctx->st2,
5178 tmpctx->applet->name);
5179 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005180
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005181 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005182 chunk_appendf(&trash,
5183 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005184 conn,
5185 get_conn_ctrl_name(conn),
5186 get_conn_xprt_name(conn),
5187 get_conn_data_name(conn),
5188 obj_type_name(conn->target),
5189 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005190
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005191 chunk_appendf(&trash,
5192 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005193 conn->flags,
5194 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005195 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005196 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005197 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005198 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005199 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5200 chunk_appendf(&trash,
5201 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5202 tmpctx,
5203 tmpctx->st0,
5204 tmpctx->st1,
5205 tmpctx->st2,
5206 tmpctx->applet->name);
5207 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005208
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005209 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005210 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005211 " an_exp=%s",
5212 sess->req,
5213 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005214 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005215 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005216 sess->req->analyse_exp ?
5217 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
5218 TICKS_TO_MS(1000)) : "<NEVER>");
5219
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005220 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005221 " rex=%s",
5222 sess->req->rex ?
5223 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
5224 TICKS_TO_MS(1000)) : "<NEVER>");
5225
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005226 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005227 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005228 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005229 sess->req->wex ?
5230 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
5231 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005232 sess->req->buf,
5233 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005234 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005235 sess->txn.req.next, sess->req->buf->i,
5236 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005237
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005238 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005239 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005240 " an_exp=%s",
5241 sess->rep,
5242 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005243 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005244 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005245 sess->rep->analyse_exp ?
5246 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
5247 TICKS_TO_MS(1000)) : "<NEVER>");
5248
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005249 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005250 " rex=%s",
5251 sess->rep->rex ?
5252 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
5253 TICKS_TO_MS(1000)) : "<NEVER>");
5254
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005255 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005256 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005257 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005258 sess->rep->wex ?
5259 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
5260 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005261 sess->rep->buf,
5262 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005263 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005264 sess->txn.rsp.next, sess->rep->buf->i,
5265 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005266
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005267 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005268 return 0;
5269
5270 /* use other states to dump the contents */
5271 }
5272 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005273 appctx->ctx.sess.uid = 0;
5274 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005275 return 1;
5276}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005277
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005278static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005279{
5280 struct appctx *appctx = __objt_appctx(si->end);
5281
5282 switch (appctx->st2) {
5283 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005284 /* Display the column headers. If the message cannot be sent,
5285 * quit the fucntion with returning 0. The function is called
5286 * later and restart at the state "STAT_ST_INIT".
5287 */
5288 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005289 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005290 if (bi_putchk(si->ib, &trash) == -1)
5291 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005292
5293 /* Now, we start the browsing of the references lists.
5294 * Note that the following call to LIST_ELEM return bad pointer. The only
5295 * avalaible field of this pointer is <list>. It is used with the function
5296 * pat_list_get_next() for retruning the first avalaible entry
5297 */
5298 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5299 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5300 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005301 appctx->st2 = STAT_ST_LIST;
5302 /* fall through */
5303
5304 case STAT_ST_LIST:
5305 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005306 chunk_reset(&trash);
5307
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005308 /* Build messages. If the reference is used by another category than
5309 * the listed categorie, display the information in the massage.
5310 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005311 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005312 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5313 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005314
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005315 if (bi_putchk(si->ib, &trash) == -1) {
5316 /* let's try again later from this session. We add ourselves into
5317 * this session's users so that it can remove us upon termination.
5318 */
5319 return 0;
5320 }
5321
5322 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005323 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5324 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005325 }
5326
5327 appctx->st2 = STAT_ST_FIN;
5328 /* fall through */
5329
5330 default:
5331 appctx->st2 = STAT_ST_FIN;
5332 return 1;
5333 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005334 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005335}
5336
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005337static int stats_map_lookup(struct stream_interface *si)
5338{
5339 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005340 struct sample sample;
5341 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005342 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005343
5344 switch (appctx->st2) {
5345 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005346 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005347 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005348 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005349 appctx->st2 = STAT_ST_LIST;
5350 /* fall through */
5351
5352 case STAT_ST_LIST:
5353 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005354 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005355 /* initialise chunk to build new message */
5356 chunk_reset(&trash);
5357
5358 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005359 sample.type = SMP_T_STR;
5360 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005361 sample.data.str.len = appctx->ctx.map.chunk.len;
5362 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005363 if (appctx->ctx.map.expr->pat_head->match &&
5364 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005365 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5366 else
5367 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005368
5369 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005370 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5371 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5372 break;
5373 if (match_method >= PAT_MATCH_NUM)
5374 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5375 else
5376 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005377
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005378 /* case sensitive */
5379 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5380 chunk_appendf(&trash, ", case=insensitive");
5381 else
5382 chunk_appendf(&trash, ", case=sensitive");
5383
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005384 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005385 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005386 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5387 chunk_appendf(&trash, ", found=no");
5388 else
5389 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005390 }
5391
5392 /* Display match and match info */
5393 else {
5394 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005395 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5396 chunk_appendf(&trash, ", found=yes");
5397 else
5398 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005399
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005400 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005401 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005402 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005403 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005404 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005405
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005406 /* display pattern */
5407 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5408 if (pat->ref && pat->ref->pattern)
5409 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5410 else
5411 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005412 }
5413 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005414 if (pat->ref && pat->ref->pattern)
5415 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5416 else
5417 chunk_appendf(&trash, ", pattern=unknown");
5418 }
5419
5420 /* display return value */
5421 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5422 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005423 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005424 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005425 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005426 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005427 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005428 }
5429
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005430 chunk_appendf(&trash, "\n");
5431
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005432 /* display response */
5433 if (bi_putchk(si->ib, &trash) == -1) {
5434 /* let's try again later from this session. We add ourselves into
5435 * this session's users so that it can remove us upon termination.
5436 */
5437 return 0;
5438 }
5439
5440 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005441 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5442 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005443 }
5444
5445 appctx->st2 = STAT_ST_FIN;
5446 /* fall through */
5447
5448 default:
5449 appctx->st2 = STAT_ST_FIN;
5450 free(appctx->ctx.map.chunk.str);
5451 return 1;
5452 }
5453}
5454
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005455static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005456{
5457 struct appctx *appctx = __objt_appctx(si->end);
5458
5459 switch (appctx->st2) {
5460
5461 case STAT_ST_INIT:
5462 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005463 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5464 struct pat_ref_elt *, list);
5465 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5466 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005467 appctx->st2 = STAT_ST_LIST;
5468 /* fall through */
5469
5470 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005471 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005472 chunk_reset(&trash);
5473
5474 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005475 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005476 chunk_appendf(&trash, "%p %s %s\n",
5477 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5478 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005479 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005480 chunk_appendf(&trash, "%p %s\n",
5481 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005482
5483 if (bi_putchk(si->ib, &trash) == -1) {
5484 /* let's try again later from this session. We add ourselves into
5485 * this session's users so that it can remove us upon termination.
5486 */
5487 return 0;
5488 }
5489
5490 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005491 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5492 struct pat_ref_elt *, list);
5493 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005494 break;
5495 }
5496
5497 appctx->st2 = STAT_ST_FIN;
5498 /* fall through */
5499
5500 default:
5501 appctx->st2 = STAT_ST_FIN;
5502 return 1;
5503 }
5504}
5505
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005506/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005507 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005508 * to be called again, otherwise non-zero. It is designed to be called
5509 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005510 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005511static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005512{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005513 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005514 struct connection *conn;
5515
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005516 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005517 /* If we're forced to shut down, we might have to remove our
5518 * reference to the last session being dumped.
5519 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005520 if (appctx->st2 == STAT_ST_LIST) {
5521 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5522 LIST_DEL(&appctx->ctx.sess.bref.users);
5523 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005524 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005525 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005526 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005527 }
5528
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005529 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005530
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005531 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005532 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005533 /* the function had not been called yet, let's prepare the
5534 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005535 * pointer to the first in the global list. When a target
5536 * session is being destroyed, it is responsible for updating
5537 * this pointer. We know we have reached the end when this
5538 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005539 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005540 LIST_INIT(&appctx->ctx.sess.bref.users);
5541 appctx->ctx.sess.bref.ref = sessions.n;
5542 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005543 /* fall through */
5544
Willy Tarreau295a8372011-03-10 11:25:07 +01005545 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005546 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005547 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5548 LIST_DEL(&appctx->ctx.sess.bref.users);
5549 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005550 }
5551
5552 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005553 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005554 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005555 struct session *curr_sess;
5556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005557 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005558
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005559 if (appctx->ctx.sess.target) {
5560 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005561 goto next_sess;
5562
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005563 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005564 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005565 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005566 return 0;
5567
5568 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 LIST_DEL(&appctx->ctx.sess.bref.users);
5570 LIST_INIT(&appctx->ctx.sess.bref.users);
5571 if (appctx->ctx.sess.target != (void *)-1) {
5572 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005573 break;
5574 }
5575 else
5576 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005577 }
5578
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005579 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005580 "%p: proto=%s",
5581 curr_sess,
5582 curr_sess->listener->proto->name);
5583
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005584
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005585 conn = objt_conn(curr_sess->si[0].end);
5586 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005587 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005588 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005589 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005590 " src=%s:%d fe=%s be=%s srv=%s",
5591 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005592 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005593 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005594 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005595 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005596 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005597 break;
5598 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005600 " src=unix:%d fe=%s be=%s srv=%s",
5601 curr_sess->listener->luid,
5602 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005603 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005604 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005605 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005606 break;
5607 }
5608
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005609 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005610 " ts=%02x age=%s calls=%d",
5611 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005612 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5613 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005615 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005616 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005617 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005618 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005619 curr_sess->req->analysers,
5620 curr_sess->req->rex ?
5621 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5622 TICKS_TO_MS(1000)) : "");
5623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005624 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005625 ",wx=%s",
5626 curr_sess->req->wex ?
5627 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5628 TICKS_TO_MS(1000)) : "");
5629
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005630 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005631 ",ax=%s]",
5632 curr_sess->req->analyse_exp ?
5633 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5634 TICKS_TO_MS(1000)) : "");
5635
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005636 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005637 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005638 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005639 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005640 curr_sess->rep->analysers,
5641 curr_sess->rep->rex ?
5642 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5643 TICKS_TO_MS(1000)) : "");
5644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005645 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005646 ",wx=%s",
5647 curr_sess->rep->wex ?
5648 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5649 TICKS_TO_MS(1000)) : "");
5650
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005651 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005652 ",ax=%s]",
5653 curr_sess->rep->analyse_exp ?
5654 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5655 TICKS_TO_MS(1000)) : "");
5656
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005657 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005658 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005659 " s0=[%d,%1xh,fd=%d,ex=%s]",
5660 curr_sess->si[0].state,
5661 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005662 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005663 curr_sess->si[0].exp ?
5664 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5665 TICKS_TO_MS(1000)) : "");
5666
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005667 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005668 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005669 " s1=[%d,%1xh,fd=%d,ex=%s]",
5670 curr_sess->si[1].state,
5671 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005672 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005673 curr_sess->si[1].exp ?
5674 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5675 TICKS_TO_MS(1000)) : "");
5676
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005677 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005678 " exp=%s",
5679 curr_sess->task->expire ?
5680 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5681 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005682 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005683 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005684
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005685 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005686
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005687 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005688 /* let's try again later from this session. We add ourselves into
5689 * this session's users so that it can remove us upon termination.
5690 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005691 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005692 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005693 }
5694
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005695 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005696 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005697 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005698
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005699 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005700 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005701 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005702 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005703 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005704 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005705
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005706 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005707 return 0;
5708
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005709 appctx->ctx.sess.target = NULL;
5710 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005711 return 1;
5712 }
5713
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005714 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005715 /* fall through */
5716
5717 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005718 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005719 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005720 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005721}
5722
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005723/* This is called when the stream interface is closed. For instance, upon an
5724 * external abort, we won't call the i/o handler anymore so we may need to
5725 * remove back references to the session currently being dumped.
5726 */
Simon Horman74d88312013-02-12 10:45:50 +09005727static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005728{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005729 struct appctx *appctx = __objt_appctx(si->end);
5730
5731 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5732 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5733 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005734 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005735 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5736 free(appctx->ctx.cli.err);
5737 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005738 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5739 free(appctx->ctx.map.chunk.str);
5740 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005741}
5742
Willy Tarreau20e99322013-04-13 09:22:25 +02005743/* This function is used to either dump tables states (when action is set
5744 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005745 * It returns 0 if the output buffer is full and it needs to be called
5746 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005747 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005748static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005749{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005750 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005751 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005752 struct ebmb_node *eb;
5753 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005754 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005755 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005756
5757 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005758 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005759 * - STAT_ST_INIT : the first call
5760 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005761 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005762 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005763 * and the entry pointer points to the next entry to be dumped,
5764 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005765 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005766 * data though.
5767 */
5768
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005769 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005770 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005771 if (appctx->st2 == STAT_ST_LIST) {
5772 appctx->ctx.table.entry->ref_cnt--;
5773 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005774 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005775 return 1;
5776 }
5777
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005778 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005779
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005780 while (appctx->st2 != STAT_ST_FIN) {
5781 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005782 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 appctx->ctx.table.proxy = appctx->ctx.table.target;
5784 if (!appctx->ctx.table.proxy)
5785 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005787 appctx->ctx.table.entry = NULL;
5788 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005789 break;
5790
Willy Tarreau295a8372011-03-10 11:25:07 +01005791 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005792 if (!appctx->ctx.table.proxy ||
5793 (appctx->ctx.table.target &&
5794 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5795 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005796 break;
5797 }
5798
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005799 if (appctx->ctx.table.proxy->table.size) {
5800 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5801 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005802 return 0;
5803
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005804 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005805 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005806 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005807 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005808 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005809 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5810 appctx->ctx.table.entry->ref_cnt++;
5811 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005812 break;
5813 }
5814 }
5815 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005816 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005817 break;
5818
Willy Tarreau295a8372011-03-10 11:25:07 +01005819 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005820 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005822 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005823 /* we're filtering on some data contents */
5824 void *ptr;
5825 long long data;
5826
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005827 dt = appctx->ctx.table.data_type;
5828 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5829 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005830 dt);
5831
5832 data = 0;
5833 switch (stktable_data_types[dt].std_type) {
5834 case STD_T_SINT:
5835 data = stktable_data_cast(ptr, std_t_sint);
5836 break;
5837 case STD_T_UINT:
5838 data = stktable_data_cast(ptr, std_t_uint);
5839 break;
5840 case STD_T_ULL:
5841 data = stktable_data_cast(ptr, std_t_ull);
5842 break;
5843 case STD_T_FRQP:
5844 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005845 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005846 break;
5847 }
5848
5849 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005850 if ((data < appctx->ctx.table.value &&
5851 (appctx->ctx.table.data_op == STD_OP_EQ ||
5852 appctx->ctx.table.data_op == STD_OP_GT ||
5853 appctx->ctx.table.data_op == STD_OP_GE)) ||
5854 (data == appctx->ctx.table.value &&
5855 (appctx->ctx.table.data_op == STD_OP_NE ||
5856 appctx->ctx.table.data_op == STD_OP_GT ||
5857 appctx->ctx.table.data_op == STD_OP_LT)) ||
5858 (data > appctx->ctx.table.value &&
5859 (appctx->ctx.table.data_op == STD_OP_EQ ||
5860 appctx->ctx.table.data_op == STD_OP_LT ||
5861 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005862 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005863 }
5864
Simon Hormanc88b8872011-06-15 15:18:49 +09005865 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005866 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5867 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005868 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005870 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005871
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005872 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005873 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005874 struct stksess *old = appctx->ctx.table.entry;
5875 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005876 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005877 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5878 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5879 stksess_kill(&appctx->ctx.table.proxy->table, old);
5880 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005881 break;
5882 }
5883
Simon Hormanc88b8872011-06-15 15:18:49 +09005884
5885 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005886 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5887 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5888 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005889
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005890 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5891 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005892 break;
5893
Willy Tarreau295a8372011-03-10 11:25:07 +01005894 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005895 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005896 break;
5897 }
5898 }
5899 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005900}
5901
Willy Tarreaud426a182010-03-05 14:58:26 +01005902/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005903 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5904 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5905 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5906 * lines are respected within the limit of 70 output chars. Lines that are
5907 * continuation of a previous truncated line begin with "+" instead of " "
5908 * after the offset. The new pointer is returned.
5909 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005910static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5911 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005912{
5913 int end;
5914 unsigned char c;
5915
5916 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005917 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005918 return ptr;
5919
Willy Tarreau77804732012-10-29 16:14:26 +01005920 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005921
Willy Tarreaud426a182010-03-05 14:58:26 +01005922 while (ptr < len && ptr < bsize) {
5923 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005924 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005925 if (out->len > end - 2)
5926 break;
5927 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005928 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005929 if (out->len > end - 3)
5930 break;
5931 out->str[out->len++] = '\\';
5932 switch (c) {
5933 case '\t': c = 't'; break;
5934 case '\n': c = 'n'; break;
5935 case '\r': c = 'r'; break;
5936 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005937 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005938 }
5939 out->str[out->len++] = c;
5940 } else {
5941 if (out->len > end - 5)
5942 break;
5943 out->str[out->len++] = '\\';
5944 out->str[out->len++] = 'x';
5945 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5946 out->str[out->len++] = hextab[c & 0xF];
5947 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005948 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005949 /* we had a line break, let's return now */
5950 out->str[out->len++] = '\n';
5951 *line = ptr;
5952 return ptr;
5953 }
5954 }
5955 /* we have an incomplete line, we return it as-is */
5956 out->str[out->len++] = '\n';
5957 return ptr;
5958}
5959
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005960/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005961 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005962 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005963 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005964static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005965{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005966 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005967 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005968
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005969 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005970 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005971
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005972 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005973
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005974 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005975 /* the function had not been called yet, let's prepare the
5976 * buffer for a response.
5977 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005978 struct tm tm;
5979
5980 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005981 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005982 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5983 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5984 error_snapshot_id);
5985
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005986 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005987 /* Socket buffer full. Let's try again later from the same point */
5988 return 0;
5989 }
5990
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005991 appctx->ctx.errors.px = proxy;
5992 appctx->ctx.errors.buf = 0;
5993 appctx->ctx.errors.bol = 0;
5994 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005995 }
5996
5997 /* we have two inner loops here, one for the proxy, the other one for
5998 * the buffer.
5999 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006000 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006001 struct error_snapshot *es;
6002
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006003 if (appctx->ctx.errors.buf == 0)
6004 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006005 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006006 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006007
6008 if (!es->when.tv_sec)
6009 goto next;
6010
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006011 if (appctx->ctx.errors.iid >= 0 &&
6012 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6013 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006014 goto next;
6015
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006016 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006017 /* just print headers now */
6018
6019 char pn[INET6_ADDRSTRLEN];
6020 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006021 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006022
6023 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006024 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006025 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006026 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006027
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006028 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6029 case AF_INET:
6030 case AF_INET6:
6031 port = get_host_port(&es->src);
6032 break;
6033 default:
6034 port = 0;
6035 }
6036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006037 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006038 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006039 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006040 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006041 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006042 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006043 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6044 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006045 break;
6046 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006047 chunk_appendf(&trash,
Olivier Doucet815d7d52014-09-08 11:23:00 +02006048 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006049 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006050 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006051 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006052 break;
6053 }
6054
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006055 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006056 ", server %s (#%d), event #%u\n"
6057 " src %s:%d, session #%d, session flags 0x%08x\n"
6058 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6059 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6060 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6061 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6062 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6063 es->ev_id,
6064 pn, port, es->sid, es->s_flags,
6065 es->state, es->m_flags, es->t_flags,
6066 es->m_clen, es->m_blen,
6067 es->b_flags, es->b_out, es->b_tot,
6068 es->len, es->b_wrap, es->pos);
6069
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006070 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006071 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006072 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006073 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006074 appctx->ctx.errors.ptr = 0;
6075 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006076 }
6077
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006078 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006079 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006080 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006081 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006082 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02006083 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006084 goto next;
6085 }
6086
6087 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006088 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006089 int newptr;
6090 int newline;
6091
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006092 newline = appctx->ctx.errors.bol;
6093 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6094 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006095 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006096
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006097 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006098 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006099 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006100 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006101 appctx->ctx.errors.ptr = newptr;
6102 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006103 };
6104 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006105 appctx->ctx.errors.bol = 0;
6106 appctx->ctx.errors.ptr = -1;
6107 appctx->ctx.errors.buf++;
6108 if (appctx->ctx.errors.buf > 1) {
6109 appctx->ctx.errors.buf = 0;
6110 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006111 }
6112 }
6113
6114 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006115 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006116}
6117
Willy Tarreaud5781202012-09-22 19:32:35 +02006118/* parse the "level" argument on the bind lines */
6119static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6120{
6121 if (!*args[cur_arg + 1]) {
6122 memprintf(err, "'%s' : missing level", args[cur_arg]);
6123 return ERR_ALERT | ERR_FATAL;
6124 }
6125
6126 if (!strcmp(args[cur_arg+1], "user"))
6127 conf->level = ACCESS_LVL_USER;
6128 else if (!strcmp(args[cur_arg+1], "operator"))
6129 conf->level = ACCESS_LVL_OPER;
6130 else if (!strcmp(args[cur_arg+1], "admin"))
6131 conf->level = ACCESS_LVL_ADMIN;
6132 else {
6133 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6134 args[cur_arg], args[cur_arg+1]);
6135 return ERR_ALERT | ERR_FATAL;
6136 }
6137
6138 return 0;
6139}
6140
Willy Tarreaub24281b2011-02-13 13:16:36 +01006141struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006142 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006143 .name = "<STATS>", /* used for logging */
6144 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006145 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006146};
6147
Simon Horman9bd2c732011-06-15 15:18:44 +09006148static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006149 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006150 .name = "<CLI>", /* used for logging */
6151 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006152 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006153};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006154
Willy Tarreaudc13c112013-06-21 23:16:39 +02006155static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006156 { CFG_GLOBAL, "stats", stats_parse_global },
6157 { 0, NULL, NULL },
6158}};
6159
Willy Tarreaud5781202012-09-22 19:32:35 +02006160static struct bind_kw_list bind_kws = { "STAT", { }, {
6161 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6162 { NULL, NULL, 0 },
6163}};
6164
Willy Tarreau10522fd2008-07-09 20:12:41 +02006165__attribute__((constructor))
6166static void __dumpstats_module_init(void)
6167{
6168 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006169 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006170}
6171
Willy Tarreau91861262007-10-17 17:06:05 +02006172/*
6173 * Local variables:
6174 * c-indent-level: 8
6175 * c-basic-offset: 8
6176 * End:
6177 */