blob: c2dfe1f9ad4859c6dc1ef1abf30ee7ca8ebffc15 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010049#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010053#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020054#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020055#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020056#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020058#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010059#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010060#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020061
Willy Tarreau7a0169a2012-11-19 17:13:16 +010062#ifdef USE_OPENSSL
63#include <proto/ssl_sock.h>
64#endif
65
Willy Tarreau91b843d2014-01-28 16:27:17 +010066/* stats socket states */
67enum {
68 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
69 STAT_CLI_END, /* final state, let's close */
70 STAT_CLI_GETREQ, /* wait for a request */
71 STAT_CLI_OUTPUT, /* all states after this one are responses */
72 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
73 STAT_CLI_PRINT, /* display message in cli->msg */
74 STAT_CLI_O_INFO, /* dump info */
75 STAT_CLI_O_SESS, /* dump sessions */
76 STAT_CLI_O_ERR, /* dump errors */
77 STAT_CLI_O_TAB, /* dump tables */
78 STAT_CLI_O_CLR, /* clear tables */
79 STAT_CLI_O_SET, /* set entries in tables */
80 STAT_CLI_O_STAT, /* dump stats */
81 STAT_CLI_O_MAPS, /* list all maps */
82 STAT_CLI_O_MAP, /* list all map entries of a map */
83 STAT_CLI_O_MLOOK, /* lookup a map entry */
84};
85
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010086static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010087static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090088static int stats_dump_sess_to_buffer(struct stream_interface *si);
89static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010090static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010091static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
92static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010093static int stats_maps_list(struct stream_interface *si);
94static int stats_map_list(struct stream_interface *si);
95static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +090096
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010097/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010098 * cli_io_handler()
99 * -> stats_dump_sess_to_buffer() // "show sess"
100 * -> stats_dump_errors_to_buffer() // "show errors"
101 * -> stats_dump_info_to_buffer() // "show info"
102 * -> stats_dump_stat_to_buffer() // "show stat"
103 * -> stats_dump_csv_header()
104 * -> stats_dump_proxy_to_buffer()
105 * -> stats_dump_fe_stats()
106 * -> stats_dump_li_stats()
107 * -> stats_dump_sv_stats()
108 * -> stats_dump_be_stats()
109 *
110 * http_stats_io_handler()
111 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
112 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
113 * -> stats_dump_html_head() // emits the HTML headers
114 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
115 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
116 * -> stats_dump_html_px_hdr()
117 * -> stats_dump_fe_stats()
118 * -> stats_dump_li_stats()
119 * -> stats_dump_sv_stats()
120 * -> stats_dump_be_stats()
121 * -> stats_dump_html_px_end()
122 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100123 */
124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static struct si_applet cli_applet;
126
127static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200128 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200129 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200130 " clear table : remove an entry from a table\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100131 " clear map [id] : clear the content of this map\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200132 " help : this message\n"
133 " prompt : toggle interactive mode with prompt\n"
134 " quit : disconnect\n"
135 " show info : report information about the running process\n"
136 " show stat : report counters for each proxy and server\n"
137 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100138 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200139 " show table [id]: report table usage stats or dump this table's contents\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100140 " show map [id] : report avalaible maps or dump this map's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200141 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200142 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200143 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100144 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200145 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200146 " set rate-limit : change a rate limiting value\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100147 " set map [id] [key] [value] : modify map entry\n"
148 " add map [id] [key] [value] : add map entry\n"
149 " del map [id] [key] : delete map entry\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200150 " disable : put a server or frontend in maintenance mode\n"
151 " enable : re-enable a server or frontend which is in maintenance mode\n"
152 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200153 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200154
Simon Horman9bd2c732011-06-15 15:18:44 +0900155static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200156 "Permission denied\n"
157 "";
158
Willy Tarreau295a8372011-03-10 11:25:07 +0100159/* data transmission states for the stats responses */
160enum {
161 STAT_ST_INIT = 0,
162 STAT_ST_HEAD,
163 STAT_ST_INFO,
164 STAT_ST_LIST,
165 STAT_ST_END,
166 STAT_ST_FIN,
167};
168
169/* data transmission states for the stats responses inside a proxy */
170enum {
171 STAT_PX_ST_INIT = 0,
172 STAT_PX_ST_TH,
173 STAT_PX_ST_FE,
174 STAT_PX_ST_LI,
175 STAT_PX_ST_SV,
176 STAT_PX_ST_BE,
177 STAT_PX_ST_END,
178 STAT_PX_ST_FIN,
179};
180
Cyril Bonté19979e12012-04-04 12:57:21 +0200181extern const char *stat_status_codes[];
182
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200183/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200184 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200185 * needs to be closed and ignored, or a negative value upon critical failure.
186 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900187static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200188{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100189 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100190 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200191
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200192 tv_zero(&s->logs.tv_request);
193 s->logs.t_queue = 0;
194 s->logs.t_connect = 0;
195 s->logs.t_data = 0;
196 s->logs.t_close = 0;
197 s->logs.bytes_in = s->logs.bytes_out = 0;
198 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
199 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200200
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200201 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200202
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200203 if (s->listener->timeout) {
204 s->req->rto = *s->listener->timeout;
205 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200206 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200207 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200208}
209
Willy Tarreau07e9e642010-08-17 21:48:17 +0200210/* allocate a new stats frontend named <name>, and return it
211 * (or NULL in case of lack of memory).
212 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200213static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200214{
215 struct proxy *fe;
216
217 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
218 if (!fe)
219 return NULL;
220
Willy Tarreau237250c2011-07-29 01:49:03 +0200221 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200222 fe->next = proxy;
223 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200224 fe->last_change = now.tv_sec;
225 fe->id = strdup("GLOBAL");
226 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200227 fe->maxconn = 10; /* default to 10 concurrent connections */
228 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200229 fe->conf.file = strdup(file);
230 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200231 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200232
233 /* the stats frontend is the only one able to assign ID #0 */
234 fe->conf.id.key = fe->uuid = 0;
235 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200236 return fe;
237}
238
Willy Tarreaufbee7132007-10-18 13:53:22 +0200239/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200240 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
241 * error message into the <err> buffer which will be preallocated. The trailing
242 * '\n' must not be written. The function must be called with <args> pointing to
243 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200244 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200245static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200246 struct proxy *defpx, const char *file, int line,
247 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200249 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200250 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200251
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200252 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253 int cur_arg;
254
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200255 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200256 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 +0200257 return -1;
258 }
259
Willy Tarreau89a63132009-08-16 17:41:45 +0200260 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200261 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200262 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200263 return -1;
264 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200265 }
266
Willy Tarreau4348fad2012-09-20 16:48:07 +0200267 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200268 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200269
Willy Tarreauc53d4222012-09-20 20:19:28 +0200270 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
271 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
272 file, line, args[0], args[1], err && *err ? *err : "error");
273 return -1;
274 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200275
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200278 static int bind_dumped;
279 struct bind_kw *kw;
280
281 kw = bind_find_kw(args[cur_arg]);
282 if (kw) {
283 if (!kw->parse) {
284 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
285 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 return -1;
287 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200288
289 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
290 if (err && *err)
291 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
292 else
293 memprintf(err, "'%s %s' : error encountered while processing '%s'",
294 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200295 return -1;
296 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200297
298 cur_arg += 1 + kw->skip;
299 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200300 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200301
302 if (!bind_dumped) {
303 bind_dump_kws(err);
304 indent_msg(err, 4);
305 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200307
308 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
309 args[0], args[1], args[cur_arg],
310 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
311 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200312 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100313
Willy Tarreauc53d4222012-09-20 20:19:28 +0200314 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
315 l->maxconn = global.stats_fe->maxconn;
316 l->backlog = global.stats_fe->backlog;
317 l->timeout = &global.stats_fe->timeout.client;
318 l->accept = session_accept;
319 l->handler = process_session;
320 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
321 l->nice = -64; /* we want to boost priority for local stats */
322 global.maxsock += l->maxconn;
323 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200325 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100326 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200327 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100328
329 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200330 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100331 return -1;
332 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200333
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100334 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200335 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200336 return -1;
337 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200338 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200339 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200340 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200341 return -1;
342 }
343 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200344 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200345 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200346 else if (!strcmp(args[1], "maxconn")) {
347 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200348
349 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200351 return -1;
352 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200353
354 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200355 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200356 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200357 return -1;
358 }
359 }
360 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200361 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200362 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
363 int cur_arg = 2;
364 unsigned int set = 0;
365
Willy Tarreau91319572013-04-20 09:48:50 +0200366 if (!global.stats_fe) {
367 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
368 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
369 return -1;
370 }
371 }
372
Willy Tarreau35b7b162012-10-22 23:17:18 +0200373 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100374 unsigned int low, high;
375
Willy Tarreau35b7b162012-10-22 23:17:18 +0200376 if (strcmp(args[cur_arg], "all") == 0) {
377 set = 0;
378 break;
379 }
380 else if (strcmp(args[cur_arg], "odd") == 0) {
381 set |= 0x55555555;
382 }
383 else if (strcmp(args[cur_arg], "even") == 0) {
384 set |= 0xAAAAAAAA;
385 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100386 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100387 char *dash = strchr(args[cur_arg], '-');
388
389 low = high = str2uic(args[cur_arg]);
390 if (dash)
391 high = str2uic(dash + 1);
392
393 if (high < low) {
394 unsigned int swap = low;
395 low = high;
396 high = swap;
397 }
398
399 if (low < 1 || high > 32) {
400 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
401 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200402 return -1;
403 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100404
405 while (low <= high)
406 set |= 1 << (low++ - 1);
407 }
408 else {
409 memprintf(err,
410 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
411 args[0], args[1]);
412 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200413 }
414 cur_arg++;
415 }
416 global.stats_fe->bind_proc = set;
417 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200418 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200419 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200420 return -1;
421 }
422 return 0;
423}
424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100425/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
426 * for clearing it if needed.
427 */
428static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100429{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100430 chunk_appendf(&trash,
431 "# pxname,svname,"
432 "qcur,qmax,"
433 "scur,smax,slim,stot,"
434 "bin,bout,"
435 "dreq,dresp,"
436 "ereq,econ,eresp,"
437 "wretr,wredis,"
438 "status,weight,act,bck,"
439 "chkfail,chkdown,lastchg,downtime,qlimit,"
440 "pid,iid,sid,throttle,lbtot,tracked,type,"
441 "rate,rate_lim,rate_max,"
442 "check_status,check_code,check_duration,"
443 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
444 "req_rate,req_rate_max,req_tot,"
445 "cli_abrt,srv_abrt,"
446 "comp_in,comp_out,comp_byp,comp_rsp,"
447 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100448}
449
Simon Hormand9366582011-06-15 15:18:45 +0900450/* print a string of text buffer to <out>. The format is :
451 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
452 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
453 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
454 */
455static int dump_text(struct chunk *out, const char *buf, int bsize)
456{
457 unsigned char c;
458 int ptr = 0;
459
460 while (buf[ptr] && ptr < bsize) {
461 c = buf[ptr];
462 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
463 if (out->len > out->size - 1)
464 break;
465 out->str[out->len++] = c;
466 }
467 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
468 if (out->len > out->size - 2)
469 break;
470 out->str[out->len++] = '\\';
471 switch (c) {
472 case ' ': c = ' '; break;
473 case '\t': c = 't'; break;
474 case '\n': c = 'n'; break;
475 case '\r': c = 'r'; break;
476 case '\e': c = 'e'; break;
477 case '\\': c = '\\'; break;
478 }
479 out->str[out->len++] = c;
480 }
481 else {
482 if (out->len > out->size - 4)
483 break;
484 out->str[out->len++] = '\\';
485 out->str[out->len++] = 'x';
486 out->str[out->len++] = hextab[(c >> 4) & 0xF];
487 out->str[out->len++] = hextab[c & 0xF];
488 }
489 ptr++;
490 }
491
492 return ptr;
493}
494
495/* print a buffer in hexa.
496 * Print stopped if <bsize> is reached, or if no more place in the chunk.
497 */
498static int dump_binary(struct chunk *out, const char *buf, int bsize)
499{
500 unsigned char c;
501 int ptr = 0;
502
503 while (ptr < bsize) {
504 c = buf[ptr];
505
506 if (out->len > out->size - 2)
507 break;
508 out->str[out->len++] = hextab[(c >> 4) & 0xF];
509 out->str[out->len++] = hextab[c & 0xF];
510
511 ptr++;
512 }
513 return ptr;
514}
515
516/* Dump the status of a table to a stream interface's
517 * read buffer. It returns 0 if the output buffer is full
518 * and needs to be called again, otherwise non-zero.
519 */
520static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
521 struct proxy *proxy, struct proxy *target)
522{
Willy Tarreau306f8302013-07-08 15:53:06 +0200523 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900524
Willy Tarreau77804732012-10-29 16:14:26 +0100525 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900526 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
527
528 /* any other information should be dumped here */
529
Willy Tarreau290e63a2012-09-20 18:07:14 +0200530 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900532
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200533 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900534 return 0;
535
536 return 1;
537}
538
539/* Dump the a table entry to a stream interface's
540 * read buffer. It returns 0 if the output buffer is full
541 * and needs to be called again, otherwise non-zero.
542 */
543static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
544 struct proxy *proxy, struct stksess *entry)
545{
546 int dt;
547
Willy Tarreau77804732012-10-29 16:14:26 +0100548 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900549
550 if (proxy->table.type == STKTABLE_TYPE_IP) {
551 char addr[INET_ADDRSTRLEN];
552 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100553 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900554 }
555 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
556 char addr[INET6_ADDRSTRLEN];
557 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900559 }
560 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100561 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900562 }
563 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100564 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900565 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
566 }
567 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100568 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900569 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
570 }
571
Willy Tarreau77804732012-10-29 16:14:26 +0100572 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900573
574 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
575 void *ptr;
576
577 if (proxy->table.data_ofs[dt] == 0)
578 continue;
579 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100580 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900581 else
Willy Tarreau77804732012-10-29 16:14:26 +0100582 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900583
584 ptr = stktable_data_ptr(&proxy->table, entry, dt);
585 switch (stktable_data_types[dt].std_type) {
586 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100587 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900588 break;
589 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100590 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900591 break;
592 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100593 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900594 break;
595 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100596 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900597 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
598 proxy->table.data_arg[dt].u));
599 break;
600 }
601 }
Willy Tarreau77804732012-10-29 16:14:26 +0100602 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900603
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200604 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900605 return 0;
606
607 return 1;
608}
609
Willy Tarreaudec98142012-06-06 23:37:08 +0200610static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900611{
Willy Tarreau306f8302013-07-08 15:53:06 +0200612 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100613 struct appctx *appctx = __objt_appctx(si->end);
614 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900615 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900616 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900617 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200618 long long value;
619 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200620 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200621 void *ptr;
622 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900623
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100624 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900625
626 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100627 appctx->ctx.cli.msg = "Key value expected\n";
628 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900629 return;
630 }
631
Simon Hormanc5b89f62011-06-15 15:18:50 +0900632 switch (px->table.type) {
633 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900634 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100635 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900636 break;
637 case STKTABLE_TYPE_IPV6:
638 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100639 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900640 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900641 case STKTABLE_TYPE_INTEGER:
642 {
643 char *endptr;
644 unsigned long val;
645 errno = 0;
646 val = strtoul(args[4], &endptr, 10);
647 if ((errno == ERANGE && val == ULONG_MAX) ||
648 (errno != 0 && val == 0) || endptr == args[4] ||
649 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100650 appctx->ctx.cli.msg = "Invalid key\n";
651 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900652 return;
653 }
654 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100655 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900656 break;
657 }
658 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900659 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100660 static_table_key->key = args[4];
661 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900662 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900663 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200664 switch (action) {
665 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100666 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 +0200667 break;
668 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100669 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 +0200670 break;
671 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100672 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200673 break;
674 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100675 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900676 return;
677 }
678
679 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200680 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100681 appctx->ctx.cli.msg = stats_permission_denied_msg;
682 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900683 return;
684 }
685
Willy Tarreau07115412012-10-29 21:56:59 +0100686 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900687
Willy Tarreaudec98142012-06-06 23:37:08 +0200688 switch (action) {
689 case STAT_CLI_O_TAB:
690 if (!ts)
691 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100692 chunk_reset(&trash);
693 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900694 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100695 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900696 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200697
698 case STAT_CLI_O_CLR:
699 if (!ts)
700 return;
701 if (ts->ref_cnt) {
702 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100703 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
704 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200705 return;
706 }
707 stksess_kill(&px->table, ts);
708 break;
Simon Horman17bce342011-06-15 15:18:47 +0900709
Willy Tarreau654694e2012-06-07 01:03:16 +0200710 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200711 if (ts)
712 stktable_touch(&px->table, ts, 1);
713 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100714 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200715 if (!ts) {
716 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100717 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
718 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200719 return;
720 }
721 stktable_store(&px->table, ts, 1);
722 }
723
Willy Tarreau47060b62013-08-01 21:11:42 +0200724 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
725 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100726 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
727 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200728 return;
729 }
730
731 data_type = stktable_get_data_type(args[cur_arg] + 5);
732 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100733 appctx->ctx.cli.msg = "Unknown data type\n";
734 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200735 return;
736 }
737
738 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100739 appctx->ctx.cli.msg = "Data type not stored in this table\n";
740 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200741 return;
742 }
743
744 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100745 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
746 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200747 return;
748 }
749
750 ptr = stktable_data_ptr(&px->table, ts, data_type);
751
752 switch (stktable_data_types[data_type].std_type) {
753 case STD_T_SINT:
754 stktable_data_cast(ptr, std_t_sint) = value;
755 break;
756 case STD_T_UINT:
757 stktable_data_cast(ptr, std_t_uint) = value;
758 break;
759 case STD_T_ULL:
760 stktable_data_cast(ptr, std_t_ull) = value;
761 break;
762 case STD_T_FRQP:
763 /* We set both the current and previous values. That way
764 * the reported frequency is stable during all the period
765 * then slowly fades out. This allows external tools to
766 * push measures without having to update them too often.
767 */
768 frqp = &stktable_data_cast(ptr, std_t_frqp);
769 frqp->curr_tick = now_ms;
770 frqp->prev_ctr = 0;
771 frqp->curr_ctr = value;
772 break;
773 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200774 }
775 break;
776
Willy Tarreaudec98142012-06-06 23:37:08 +0200777 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100778 appctx->ctx.cli.msg = "Unknown action\n";
779 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200780 break;
Simon Horman121f3052011-06-15 15:18:46 +0900781 }
Simon Horman121f3052011-06-15 15:18:46 +0900782}
783
Willy Tarreau654694e2012-06-07 01:03:16 +0200784static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900785{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100786 struct appctx *appctx = __objt_appctx(si->end);
787
Willy Tarreau04b3a192013-04-13 09:41:37 +0200788 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100789 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
790 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200791 return;
792 }
793
Simon Hormand5b9fd92011-06-15 15:18:48 +0900794 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100795 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
796 if (appctx->ctx.table.data_type < 0) {
797 appctx->ctx.cli.msg = "Unknown data type\n";
798 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900799 return;
800 }
801
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100802 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
803 appctx->ctx.cli.msg = "Data type not stored in this table\n";
804 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900805 return;
806 }
807
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 appctx->ctx.table.data_op = get_std_op(args[4]);
809 if (appctx->ctx.table.data_op < 0) {
810 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
811 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812 return;
813 }
814
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100815 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
816 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
817 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900818 return;
819 }
820}
821
Willy Tarreaudec98142012-06-06 23:37:08 +0200822static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900823{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 struct appctx *appctx = __objt_appctx(si->end);
825
826 appctx->ctx.table.data_type = -1;
827 appctx->st2 = STAT_ST_INIT;
828 appctx->ctx.table.target = NULL;
829 appctx->ctx.table.proxy = NULL;
830 appctx->ctx.table.entry = NULL;
831 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900832
833 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100834 appctx->ctx.table.target = find_stktable(args[2]);
835 if (!appctx->ctx.table.target) {
836 appctx->ctx.cli.msg = "No such table\n";
837 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900838 return;
839 }
840 }
841 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200842 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900843 goto err_args;
844 return;
845 }
846
847 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200848 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900849 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200850 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900851 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900852 goto err_args;
853
854 return;
855
856err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200857 switch (action) {
858 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100859 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 +0200860 break;
861 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100862 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 +0200863 break;
864 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100865 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200866 break;
867 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100868 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900869}
870
Willy Tarreau532a4502011-09-07 22:37:44 +0200871/* Expects to find a frontend named <arg> and returns it, otherwise displays various
872 * adequate error messages and returns NULL. This function also expects the session
873 * level to be admin.
874 */
875static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
876{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100877 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200878 struct proxy *px;
879
Willy Tarreau290e63a2012-09-20 18:07:14 +0200880 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100881 appctx->ctx.cli.msg = stats_permission_denied_msg;
882 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200883 return NULL;
884 }
885
886 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100887 appctx->ctx.cli.msg = "A frontend name is expected.\n";
888 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200889 return NULL;
890 }
891
892 px = findproxy(arg, PR_CAP_FE);
893 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = "No such frontend.\n";
895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200896 return NULL;
897 }
898 return px;
899}
900
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200901/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
902 * and returns the pointer to the server. Otherwise, display adequate error messages
903 * and returns NULL. This function also expects the session level to be admin. Note:
904 * the <arg> is modified to remove the '/'.
905 */
906static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
907{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100908 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200909 struct proxy *px;
910 struct server *sv;
911 char *line;
912
Willy Tarreau290e63a2012-09-20 18:07:14 +0200913 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 appctx->ctx.cli.msg = stats_permission_denied_msg;
915 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200916 return NULL;
917 }
918
919 /* split "backend/server" and make <line> point to server */
920 for (line = arg; *line; line++)
921 if (*line == '/') {
922 *line++ = '\0';
923 break;
924 }
925
926 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100927 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
928 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200929 return NULL;
930 }
931
932 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200935 return NULL;
936 }
937
938 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = "Proxy is disabled.\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200941 return NULL;
942 }
943
944 return sv;
945}
946
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100947/* This function is used with map management. It permits to browse each
948 * really allocated descriptors of one map reference. The variable
949 * <appctx->ctx.map.ref> must contain the map reference to browse.
950 * The variable <appctx->ctx.map.desc> contain the descriptor of the
951 * current allocated map descriptor. This variable must be initialized
952 * to NULL.
953 */
954static inline void stats_map_lookup_next(struct stream_interface *si)
955{
956 struct appctx *appctx = __objt_appctx(si->end);
957
958 /* search the next allocated map */
959 while (1) {
960 /* get next descriptor */
961 if (!appctx->ctx.map.desc)
962 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.ref->maps,
963 struct map_descriptor *, list);
964 else
965 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.desc->list,
966 struct map_descriptor *, list);
967
968 /* detect end of list */
969 if (&appctx->ctx.map.desc->list == &appctx->ctx.map.ref->maps) {
970 appctx->ctx.map.desc = NULL;
971 return;
972 }
973
974 /* do not lookup this entry */
975 if (!appctx->ctx.map.desc->do_free)
976 continue;
977
978 /* avalaible descriptor */
979 return;
980 }
981}
982
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200983/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200984 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100985 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200986 * designating the function which will have to process the request, which can
987 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200988 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900989static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200990{
Willy Tarreau306f8302013-07-08 15:53:06 +0200991 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100992 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200993 char *args[MAX_STATS_ARGS + 1];
994 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100995 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200996
997 while (isspace((unsigned char)*line))
998 line++;
999
1000 arg = 0;
1001 args[arg] = line;
1002
1003 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001004 if (*line == '\\') {
1005 line++;
1006 if (*line == '\0')
1007 break;
1008 }
1009 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001010 *line++ = '\0';
1011
1012 while (isspace((unsigned char)*line))
1013 line++;
1014
1015 args[++arg] = line;
1016 continue;
1017 }
1018
1019 line++;
1020 }
1021
1022 while (++arg <= MAX_STATS_ARGS)
1023 args[arg] = line;
1024
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001025 /* remove \ */
1026 arg = 0;
1027 while (*args[arg] != '\0') {
1028 j = 0;
1029 for (i=0; args[arg][i] != '\0'; i++) {
1030 if (args[arg][i] == '\\')
1031 continue;
1032 args[arg][j] = args[arg][i];
1033 j++;
1034 }
1035 args[arg][j] = '\0';
1036 arg++;
1037 }
1038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001039 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001040 if (strcmp(args[0], "show") == 0) {
1041 if (strcmp(args[1], "stat") == 0) {
1042 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001043 appctx->ctx.stats.flags |= STAT_BOUND;
1044 appctx->ctx.stats.iid = atoi(args[2]);
1045 appctx->ctx.stats.type = atoi(args[3]);
1046 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047 }
1048
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001049 appctx->st2 = STAT_ST_INIT;
1050 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001051 }
1052 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001053 appctx->st2 = STAT_ST_INIT;
1054 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001055 }
1056 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001057 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001058 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001059 appctx->ctx.cli.msg = stats_permission_denied_msg;
1060 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001061 return 1;
1062 }
Willy Tarreau76153662012-11-26 01:16:39 +01001063 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001064 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001065 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001066 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001067 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001068 appctx->ctx.sess.target = NULL;
1069 appctx->ctx.sess.section = 0; /* start with session status */
1070 appctx->ctx.sess.pos = 0;
1071 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001072 }
1073 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001074 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001075 appctx->ctx.cli.msg = stats_permission_denied_msg;
1076 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001077 return 1;
1078 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001079 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001080 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001081 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001082 appctx->ctx.errors.iid = -1;
1083 appctx->ctx.errors.px = NULL;
1084 appctx->st2 = STAT_ST_INIT;
1085 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001086 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001087 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001088 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001089 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001090 else if (strcmp(args[1], "map") == 0) {
1091
1092 /* no parameter: display all map avalaible */
1093 if (!*args[2]) {
1094 appctx->st2 = STAT_ST_INIT;
1095 appctx->st0 = STAT_CLI_O_MAPS;
1096 return 1;
1097 }
1098
1099 /* lookup into the maps */
1100 appctx->ctx.map.ref = map_get_reference(args[2]);
1101 if (!appctx->ctx.map.ref) {
1102 appctx->ctx.cli.msg = "Unknown map reference.\n";
1103 appctx->st0 = STAT_CLI_PRINT;
1104 return 1;
1105 }
1106 appctx->st2 = STAT_ST_INIT;
1107 appctx->st0 = STAT_CLI_O_MAP;
1108 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001109 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001110 return 0;
1111 }
1112 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001113 else if (strcmp(args[0], "clear") == 0) {
1114 if (strcmp(args[1], "counters") == 0) {
1115 struct proxy *px;
1116 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001117 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001118 int clrall = 0;
1119
1120 if (strcmp(args[2], "all") == 0)
1121 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001122
Willy Tarreau6162db22009-10-10 17:13:00 +02001123 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001124 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1125 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->ctx.cli.msg = stats_permission_denied_msg;
1127 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001128 return 1;
1129 }
1130
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001131 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001132 if (clrall) {
1133 memset(&px->be_counters, 0, sizeof(px->be_counters));
1134 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1135 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001136 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001137 px->be_counters.conn_max = 0;
1138 px->be_counters.p.http.rps_max = 0;
1139 px->be_counters.sps_max = 0;
1140 px->be_counters.cps_max = 0;
1141 px->be_counters.nbpend_max = 0;
1142
1143 px->fe_counters.conn_max = 0;
1144 px->fe_counters.p.http.rps_max = 0;
1145 px->fe_counters.sps_max = 0;
1146 px->fe_counters.cps_max = 0;
1147 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001148 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001149
1150 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001151 if (clrall)
1152 memset(&sv->counters, 0, sizeof(sv->counters));
1153 else {
1154 sv->counters.cur_sess_max = 0;
1155 sv->counters.nbpend_max = 0;
1156 sv->counters.sps_max = 0;
1157 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001158
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001159 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001160 if (li->counters) {
1161 if (clrall)
1162 memset(li->counters, 0, sizeof(*li->counters));
1163 else
1164 li->counters->conn_max = 0;
1165 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001166 }
1167
Willy Tarreau81c25d02011-09-07 15:17:21 +02001168 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001169 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001170 return 1;
1171 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001172 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001173 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001174 /* end of processing */
1175 return 1;
1176 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001177 else if (strcmp(args[1], "map") == 0) {
1178 struct map_reference *mref;
1179 struct map_descriptor *mdesc;
1180 struct map_entry *ent, *nent;
1181
1182 /* no parameter */
1183 if (!*args[2]) {
1184 appctx->ctx.cli.msg = "Expect map reference.\n";
1185 appctx->st0 = STAT_CLI_PRINT;
1186 return 1;
1187 }
1188
1189 /* lookup into the maps */
1190 mref = map_get_reference(args[2]);
1191 if (!mref) {
1192 appctx->ctx.cli.msg = "Unknown map reference.\n";
1193 appctx->st0 = STAT_CLI_PRINT;
1194 return 1;
1195 }
1196
1197 /* clear all maps */
1198 list_for_each_entry(mdesc, &mref->maps, list)
1199 if (mdesc->do_free)
1200 pattern_prune_expr(mdesc->pat);
1201
1202 /* clear map reference */
1203 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1204 LIST_DEL(&ent->list);
1205 free(ent->key);
1206 free(ent->value);
1207 free(ent);
1208 }
1209
1210 /* return response */
1211 appctx->ctx.cli.msg = "Done.\n";
1212 appctx->st0 = STAT_CLI_PRINT;
1213 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001214 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001215 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001216 return 0;
1217 }
1218 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001219 else if (strcmp(args[0], "get") == 0) {
1220 if (strcmp(args[1], "weight") == 0) {
1221 struct proxy *px;
1222 struct server *sv;
1223
1224 /* split "backend/server" and make <line> point to server */
1225 for (line = args[2]; *line; line++)
1226 if (*line == '/') {
1227 *line++ = '\0';
1228 break;
1229 }
1230
1231 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001232 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1233 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001234 return 1;
1235 }
1236
1237 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001238 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1239 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001240 return 1;
1241 }
1242
1243 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001244 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1245 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001246 return 1;
1247 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001248 else if (strcmp(args[1], "map") == 0) {
1249
1250 /* no parameter */
1251 if (!*args[2] || !*args[3]) {
1252 appctx->ctx.cli.msg = "Expect map reference and required key.\n";
1253 appctx->st0 = STAT_CLI_PRINT;
1254 return 1;
1255 }
1256
1257 /* lookup into the maps */
1258 appctx->ctx.map.ref = map_get_reference(args[2]);
1259 if (!appctx->ctx.map.ref) {
1260 appctx->ctx.cli.msg = "Unknown map reference.\n";
1261 appctx->st0 = STAT_CLI_PRINT;
1262 return 1;
1263 }
1264
1265 /* copy input string */
1266 appctx->ctx.map.chunk.len = strlen(args[3]);
1267 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1268 appctx->ctx.map.chunk.str = strdup(args[3]);
1269 if (!appctx->ctx.map.chunk.str) {
1270 appctx->ctx.cli.msg = "Out of memory error.\n";
1271 appctx->st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274
1275 /* prepare response */
1276 appctx->st2 = STAT_ST_INIT;
1277 appctx->st0 = STAT_CLI_O_MLOOK;
1278 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001279 else { /* not "get weight" */
1280 return 0;
1281 }
1282 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001283 else if (strcmp(args[0], "set") == 0) {
1284 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001285 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001286 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001287
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001288 sv = expect_server_admin(s, si, args[2]);
1289 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001290 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001291
Simon Horman7d09b9a2013-02-12 10:45:51 +09001292 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001293 /*
1294 * The user-weight may now be zero and thus
1295 * the server considered to be draining.
1296 * Update the server's drain state as necessary.
1297 */
1298 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001299 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001300 appctx->ctx.cli.msg = warning;
1301 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001302 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001303 return 1;
1304 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001305 else if (strcmp(args[1], "timeout") == 0) {
1306 if (strcmp(args[2], "cli") == 0) {
1307 unsigned timeout;
1308 const char *res;
1309
1310 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001311 appctx->ctx.cli.msg = "Expects an integer value.\n";
1312 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001313 return 1;
1314 }
1315
1316 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1317 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001318 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1319 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001320 return 1;
1321 }
1322
1323 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1324 return 1;
1325 }
1326 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001327 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1328 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001329 return 1;
1330 }
1331 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001332 else if (strcmp(args[1], "maxconn") == 0) {
1333 if (strcmp(args[2], "frontend") == 0) {
1334 struct proxy *px;
1335 struct listener *l;
1336 int v;
1337
Willy Tarreau532a4502011-09-07 22:37:44 +02001338 px = expect_frontend_admin(s, si, args[3]);
1339 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001340 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001341
1342 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001343 appctx->ctx.cli.msg = "Integer value expected.\n";
1344 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001345 return 1;
1346 }
1347
1348 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001349 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001350 appctx->ctx.cli.msg = "Value out of range.\n";
1351 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001352 return 1;
1353 }
1354
1355 /* OK, the value is fine, so we assign it to the proxy and to all of
1356 * its listeners. The blocked ones will be dequeued.
1357 */
1358 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001359 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001360 l->maxconn = v;
1361 if (l->state == LI_FULL)
1362 resume_listener(l);
1363 }
1364
1365 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1366 dequeue_all_listeners(&s->fe->listener_queue);
1367
1368 return 1;
1369 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001370 else if (strcmp(args[2], "global") == 0) {
1371 int v;
1372
Willy Tarreau290e63a2012-09-20 18:07:14 +02001373 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001374 appctx->ctx.cli.msg = stats_permission_denied_msg;
1375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001376 return 1;
1377 }
1378
1379 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001380 appctx->ctx.cli.msg = "Expects an integer value.\n";
1381 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001382 return 1;
1383 }
1384
1385 v = atoi(args[3]);
1386 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001387 appctx->ctx.cli.msg = "Value out of range.\n";
1388 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001389 return 1;
1390 }
1391
1392 /* check for unlimited values */
1393 if (v <= 0)
1394 v = global.hardmaxconn;
1395
1396 global.maxconn = v;
1397
1398 /* Dequeues all of the listeners waiting for a resource */
1399 if (!LIST_ISEMPTY(&global_listener_queue))
1400 dequeue_all_listeners(&global_listener_queue);
1401
1402 return 1;
1403 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001404 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001405 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1406 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001407 return 1;
1408 }
1409 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001410 else if (strcmp(args[1], "rate-limit") == 0) {
1411 if (strcmp(args[2], "connections") == 0) {
1412 if (strcmp(args[3], "global") == 0) {
1413 int v;
1414
Willy Tarreau290e63a2012-09-20 18:07:14 +02001415 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->ctx.cli.msg = stats_permission_denied_msg;
1417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001418 return 1;
1419 }
1420
1421 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001422 appctx->ctx.cli.msg = "Expects an integer value.\n";
1423 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001424 return 1;
1425 }
1426
1427 v = atoi(args[4]);
1428 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001429 appctx->ctx.cli.msg = "Value out of range.\n";
1430 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001431 return 1;
1432 }
1433
1434 global.cps_lim = v;
1435
1436 /* Dequeues all of the listeners waiting for a resource */
1437 if (!LIST_ISEMPTY(&global_listener_queue))
1438 dequeue_all_listeners(&global_listener_queue);
1439
1440 return 1;
1441 }
1442 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001443 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1444 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001445 return 1;
1446 }
1447 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001448 else if (strcmp(args[2], "sessions") == 0) {
1449 if (strcmp(args[3], "global") == 0) {
1450 int v;
1451
1452 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1453 appctx->ctx.cli.msg = stats_permission_denied_msg;
1454 appctx->st0 = STAT_CLI_PRINT;
1455 return 1;
1456 }
1457
1458 if (!*args[4]) {
1459 appctx->ctx.cli.msg = "Expects an integer value.\n";
1460 appctx->st0 = STAT_CLI_PRINT;
1461 return 1;
1462 }
1463
1464 v = atoi(args[4]);
1465 if (v < 0) {
1466 appctx->ctx.cli.msg = "Value out of range.\n";
1467 appctx->st0 = STAT_CLI_PRINT;
1468 return 1;
1469 }
1470
1471 global.sps_lim = v;
1472
1473 /* Dequeues all of the listeners waiting for a resource */
1474 if (!LIST_ISEMPTY(&global_listener_queue))
1475 dequeue_all_listeners(&global_listener_queue);
1476
1477 return 1;
1478 }
1479 else {
1480 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1481 appctx->st0 = STAT_CLI_PRINT;
1482 return 1;
1483 }
1484 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001485#ifdef USE_OPENSSL
1486 else if (strcmp(args[2], "ssl-sessions") == 0) {
1487 if (strcmp(args[3], "global") == 0) {
1488 int v;
1489
1490 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1491 appctx->ctx.cli.msg = stats_permission_denied_msg;
1492 appctx->st0 = STAT_CLI_PRINT;
1493 return 1;
1494 }
1495
1496 if (!*args[4]) {
1497 appctx->ctx.cli.msg = "Expects an integer value.\n";
1498 appctx->st0 = STAT_CLI_PRINT;
1499 return 1;
1500 }
1501
1502 v = atoi(args[4]);
1503 if (v < 0) {
1504 appctx->ctx.cli.msg = "Value out of range.\n";
1505 appctx->st0 = STAT_CLI_PRINT;
1506 return 1;
1507 }
1508
1509 global.ssl_lim = v;
1510
1511 /* Dequeues all of the listeners waiting for a resource */
1512 if (!LIST_ISEMPTY(&global_listener_queue))
1513 dequeue_all_listeners(&global_listener_queue);
1514
1515 return 1;
1516 }
1517 else {
1518 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1519 appctx->st0 = STAT_CLI_PRINT;
1520 return 1;
1521 }
1522 }
1523#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001524 else if (strcmp(args[2], "http-compression") == 0) {
1525 if (strcmp(args[3], "global") == 0) {
1526 int v;
1527
Willy Tarreau85d47f92012-11-21 00:29:50 +01001528 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1530 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001531 return 1;
1532 }
1533
William Lallemandd85f9172012-11-09 17:05:39 +01001534 v = atoi(args[4]);
1535 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1536 }
1537 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001538 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1539 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001540 return 1;
1541 }
1542 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001543 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001544 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001546 return 1;
1547 }
1548 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001549 else if (strcmp(args[1], "table") == 0) {
1550 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1551 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001552 else if (strcmp(args[1], "map") == 0) {
1553 struct pattern *pat_elt;
1554 struct pat_idx_elt *idx_elt;
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001555 char *value = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001556
1557 /* Expect three parameters: map name, key and new value. */
1558 if (!*args[2] || !*args[3] || !*args[4]) {
1559 appctx->ctx.cli.msg = "'set map' expect three parameters: map name, key and value.\n";
1560 appctx->st0 = STAT_CLI_PRINT;
1561 return 1;
1562 }
1563
1564 /* Lookup the reference in the maps. */
1565 appctx->ctx.map.ref = map_get_reference(args[2]);
1566 if (!appctx->ctx.map.ref) {
1567 appctx->ctx.cli.msg = "Unknown map reference.\n";
1568 appctx->st0 = STAT_CLI_PRINT;
1569 return 1;
1570 }
1571
1572 /* Lookup the entry in the reference values. */
1573 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1574 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1575 break;
1576
1577 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1578 appctx->ctx.cli.msg = "Entry not found.\n";
1579 appctx->st0 = STAT_CLI_PRINT;
1580 return 1;
1581 }
1582
1583 /* Update each reference entries. */
1584 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1585 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1586 value = strdup(args[4]);
1587 if (!value) {
1588 appctx->ctx.cli.msg = "Out of memory error.\n";
1589 appctx->st0 = STAT_CLI_PRINT;
1590 return 1;
1591 }
1592 free(appctx->ctx.map.ent->value);
1593 appctx->ctx.map.ent->value = value;
1594 }
1595 }
1596
1597 /* Change the sample. The lookup juste return the first entry, other
1598 * entries are not changed, but are never matched.
1599 */
1600 appctx->ctx.map.desc = NULL;
1601 for (stats_map_lookup_next(si);
1602 appctx->ctx.map.desc;
1603 stats_map_lookup_next(si)) {
1604 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1605 if (pat_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001606 appctx->ctx.map.desc->parse(value, pat_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001607 if (idx_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001608 appctx->ctx.map.desc->parse(value, idx_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001609 }
1610
1611 /* The set is done, send message. */
1612 appctx->ctx.cli.msg = "Done.\n";
1613 appctx->st0 = STAT_CLI_PRINT;
1614 return 1;
1615 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001616 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001617 return 0;
1618 }
1619 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001620 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001621 if (strcmp(args[1], "agent") == 0) {
1622 struct server *sv;
1623
1624 sv = expect_server_admin(s, si, args[2]);
1625 if (!sv)
1626 return 1;
1627
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001628 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1629 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1630 appctx->st0 = STAT_CLI_PRINT;
1631 return 1;
1632 }
1633
1634 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001635 return 1;
1636 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001637 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001638 struct server *sv;
1639
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001640 sv = expect_server_admin(s, si, args[2]);
1641 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001642 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001643
Cyril Bontécd19e512010-01-31 22:34:03 +01001644 if (sv->state & SRV_MAINTAIN) {
1645 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001646 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001647 /* If this server tracks the status of another one,
1648 * we must restore the good status.
1649 */
Willy Tarreau44267702011-10-28 15:35:33 +02001650 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001651 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001652 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001653 } else {
1654 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001655 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001656 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001657 }
1658 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001659 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001660 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001661 }
1662 }
1663
Willy Tarreau532a4502011-09-07 22:37:44 +02001664 return 1;
1665 }
1666 else if (strcmp(args[1], "frontend") == 0) {
1667 struct proxy *px;
1668
1669 px = expect_frontend_admin(s, si, args[2]);
1670 if (!px)
1671 return 1;
1672
1673 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001674 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1675 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001676 return 1;
1677 }
1678
1679 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001680 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001682 return 1;
1683 }
1684
1685 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001686 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1687 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001688 return 1;
1689 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001690 return 1;
1691 }
1692 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001693 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1694 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001695 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001696 }
1697 }
1698 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001699 if (strcmp(args[1], "agent") == 0) {
1700 struct server *sv;
1701
1702 sv = expect_server_admin(s, si, args[2]);
1703 if (!sv)
1704 return 1;
1705
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001706 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001707 return 1;
1708 }
1709 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001710 struct server *sv;
1711
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001712 sv = expect_server_admin(s, si, args[2]);
1713 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001714 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001715
Cyril Bontécd19e512010-01-31 22:34:03 +01001716 if (! (sv->state & SRV_MAINTAIN)) {
1717 /* Not already in maintenance, we can change the server state */
1718 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001719 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001720 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001721 }
1722
Willy Tarreau532a4502011-09-07 22:37:44 +02001723 return 1;
1724 }
1725 else if (strcmp(args[1], "frontend") == 0) {
1726 struct proxy *px;
1727
1728 px = expect_frontend_admin(s, si, args[2]);
1729 if (!px)
1730 return 1;
1731
1732 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001733 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1734 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001735 return 1;
1736 }
1737
1738 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001739 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1740 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001741 return 1;
1742 }
1743
1744 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001745 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1746 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001747 return 1;
1748 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001749 return 1;
1750 }
1751 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001752 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001754 return 1;
1755 }
1756 }
1757 else if (strcmp(args[0], "shutdown") == 0) {
1758 if (strcmp(args[1], "frontend") == 0) {
1759 struct proxy *px;
1760
1761 px = expect_frontend_admin(s, si, args[2]);
1762 if (!px)
1763 return 1;
1764
1765 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001766 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1767 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001768 return 1;
1769 }
1770
1771 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1772 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1773 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1774 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1775 stop_proxy(px);
1776 return 1;
1777 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001778 else if (strcmp(args[1], "session") == 0) {
1779 struct session *sess, *ptr;
1780
Willy Tarreau290e63a2012-09-20 18:07:14 +02001781 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001782 appctx->ctx.cli.msg = stats_permission_denied_msg;
1783 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001784 return 1;
1785 }
1786
1787 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001788 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1789 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001790 return 1;
1791 }
1792
1793 ptr = (void *)strtoul(args[2], NULL, 0);
1794
1795 /* first, look for the requested session in the session table */
1796 list_for_each_entry(sess, &sessions, list) {
1797 if (sess == ptr)
1798 break;
1799 }
1800
1801 /* do we have the session ? */
1802 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001803 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1804 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001805 return 1;
1806 }
1807
1808 session_shutdown(sess, SN_ERR_KILLED);
1809 return 1;
1810 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001811 else if (strcmp(args[1], "sessions") == 0) {
1812 if (strcmp(args[2], "server") == 0) {
1813 struct server *sv;
1814 struct session *sess, *sess_bck;
1815
1816 sv = expect_server_admin(s, si, args[3]);
1817 if (!sv)
1818 return 1;
1819
1820 /* kill all the session that are on this server */
1821 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1822 if (sess->srv_conn == sv)
1823 session_shutdown(sess, SN_ERR_KILLED);
1824
1825 return 1;
1826 }
1827 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001828 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1829 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001830 return 1;
1831 }
1832 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001833 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001834 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1835 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001836 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001837 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001838 }
1839 else if (strcmp(args[0], "del") == 0) {
1840 if (strcmp(args[1], "map") == 0) {
1841 struct pattern *pat_elt;
1842 struct pat_idx_elt *idx_elt;
1843 struct map_entry *ent;
1844
1845 /* Expect two parameters: map name and key. */
1846 if (!*args[2] || !*args[3]) {
1847 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1848 appctx->st0 = STAT_CLI_PRINT;
1849 return 1;
1850 }
1851
1852 /* Lookup the reference in the maps. */
1853 appctx->ctx.map.ref = map_get_reference(args[2]);
1854 if (!appctx->ctx.map.ref) {
1855 appctx->ctx.cli.msg = "Unknown map reference.\n";
1856 appctx->st0 = STAT_CLI_PRINT;
1857 return 1;
1858 }
1859
1860 /* Lookup the entry in the reference values.
1861 * If the entry is not found in the reference, return error message.
1862 */
1863 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1864 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1865 break;
1866
1867 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1868 appctx->ctx.cli.msg = "Entry not found.\n";
1869 appctx->st0 = STAT_CLI_PRINT;
1870 return 1;
1871 }
1872
1873 /* Delete each enties from reference. */
1874 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1875 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1876 LIST_DEL(&appctx->ctx.map.ent->list);
1877 free(appctx->ctx.map.ent->key);
1878 free(appctx->ctx.map.ent->value);
1879 free(appctx->ctx.map.ent);
1880 }
1881 }
1882
1883 /* Delete all matching entries for each map descritor. */
1884 appctx->ctx.map.desc = NULL;
1885 stats_map_lookup_next(si);
1886 while (appctx->ctx.map.desc) {
1887 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1888 if (pat_elt != NULL) {
1889 LIST_DEL(&pat_elt->list);
1890 pattern_free(pat_elt);
1891 }
1892 if (idx_elt != NULL) {
1893 ebmb_delete(&idx_elt->node);
1894 free(idx_elt);
1895 }
1896 }
1897 stats_map_lookup_next(si);
1898 }
1899
1900 /* The deletion is done, send message. */
1901 appctx->ctx.cli.msg = "Done.\n";
1902 appctx->st0 = STAT_CLI_PRINT;
1903 return 1;
1904 }
1905 else { /* unknown "del" parameter */
1906 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1907 appctx->st0 = STAT_CLI_PRINT;
1908 return 1;
1909 }
1910 }
1911 else if (strcmp(args[0], "add") == 0) {
1912 if (strcmp(args[1], "map") == 0) {
1913 const char *params[2];
1914 struct pattern *pat;
1915 struct map_entry *ent;
1916 struct sample_storage *smp;
1917
1918 /* Expect three parameters: map name, key and new value. */
1919 if (!*args[2] || !*args[3] || !*args[4]) {
1920 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1921 appctx->st0 = STAT_CLI_PRINT;
1922 return 1;
1923 }
1924
1925 params[0] = args[3];
1926 params[1] = "";
1927
1928 /* Lookup the reference in the maps. */
1929 appctx->ctx.map.ref = map_get_reference(args[2]);
1930 if (!appctx->ctx.map.ref) {
1931 appctx->ctx.cli.msg = "Unknown map reference.\n";
1932 appctx->st0 = STAT_CLI_PRINT;
1933 return 1;
1934 }
1935
1936 /* Prepare and link the new map_entry element. If out of memory
1937 * error the action is cancelled and the descriptor are left
1938 * coherents.
1939 */
1940 ent = malloc(sizeof(*ent));
1941 if (!ent) {
1942 appctx->ctx.cli.msg = "Out of memory error.\n";
1943 appctx->st0 = STAT_CLI_PRINT;
1944 return 1;
1945 }
1946 ent->key = strdup(args[3]);
1947 if (!ent->key) {
1948 free(ent);
1949 appctx->ctx.cli.msg = "Out of memory error.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
1951 return 1;
1952 }
1953 ent->value = strdup(args[4]);
1954 if (!ent->value) {
1955 free(ent->key);
1956 free(ent);
1957 appctx->ctx.cli.msg = "Out of memory error.\n";
1958 appctx->st0 = STAT_CLI_PRINT;
1959 return 1;
1960 }
1961 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1962
1963 /* Browse each map descritor and try to insert this new value. */
1964 appctx->ctx.map.desc = NULL;
1965 for (stats_map_lookup_next(si);
1966 appctx->ctx.map.desc;
1967 stats_map_lookup_next(si)) {
1968
1969 /* Create new sample. Return out of memory error
1970 * if the memory cannot be allocated. The 'add' process
1971 * is aborted, but the already inserted entries are not
1972 * deleted.
1973 */
1974 smp = calloc(1, sizeof(*smp));
1975 if (!smp) {
1976 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1977 appctx->st0 = STAT_CLI_PRINT;
1978 return 1;
1979 }
1980
1981 /* Create sample. If this function fails, the insertion
1982 * is canceled for this 'descriptor', but continue, for
1983 * the other descriptors.
1984 */
1985 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1986 free(smp);
1987 continue;
1988 }
1989
1990 /* If the value can be indexed, get the first pattern. If
1991 * the return entry is not the indexed entry, new 'pattern' is
1992 * created by the function pattern_register(). If the 'pattern'
1993 * is NULL, new entry is created. This is ugly because the
1994 * following code interfers with the own code of the function
1995 * pattern_register().
1996 */
1997 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
1998 appctx->ctx.map.desc->pat->match == pat_match_ip) {
1999 pat = LIST_NEXT(&appctx->ctx.map.desc->pat->patterns, struct pattern *, list);
2000 if (&pat->list == &appctx->ctx.map.desc->pat->patterns)
2001 pat = NULL;
2002 }
2003 else
2004 pat = NULL;
2005
2006 if (!pattern_register(appctx->ctx.map.desc->pat, params, smp, &pat, 0, NULL)) {
2007 free(smp);
2008 continue;
2009 }
2010 }
2011
2012 /* The add is done, send message. */
2013 appctx->ctx.cli.msg = "Done.\n";
2014 appctx->st0 = STAT_CLI_PRINT;
2015 return 1;
2016 }
2017 else { /* unknown "del" parameter */
2018 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2019 appctx->st0 = STAT_CLI_PRINT;
2020 return 1;
2021 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002022 }
2023 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002024 return 0;
2025 }
2026 return 1;
2027}
2028
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002029/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002030 * used to processes I/O from/to the stats unix socket. The system relies on a
2031 * state machine handling requests and various responses. We read a request,
2032 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002033 * Then we can read again. The state is stored in appctx->st0 and is one of the
2034 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002035 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002036 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002037static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002038{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002039 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002040 struct channel *req = si->ob;
2041 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002042 int reql;
2043 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002044
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002045 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2046 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002047
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002048 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002049 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002050 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002051 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2052 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002053 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002054 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002055 /* Let's close for real now. We just close the request
2056 * side, the conditions below will complete if needed.
2057 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002058 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002059 break;
2060 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002061 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002062 /* ensure we have some output room left in the event we
2063 * would want to return some info right after parsing.
2064 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002065 if (buffer_almost_full(si->ib->buf)) {
2066 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002067 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002068 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002069
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002070 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002071 if (reql <= 0) { /* closed or EOL not found */
2072 if (reql == 0)
2073 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002074 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002075 continue;
2076 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002077
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002078 /* seek for a possible semi-colon. If we find one, we
2079 * replace it with an LF and skip only this part.
2080 */
2081 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002082 if (trash.str[len] == ';') {
2083 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 reql = len + 1;
2085 break;
2086 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002087
Willy Tarreau816fc222009-10-04 07:36:58 +02002088 /* now it is time to check that we have a full line,
2089 * remove the trailing \n and possibly \r, then cut the
2090 * line.
2091 */
2092 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002093 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002094 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002095 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002096 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002097
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002098 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002099 len--;
2100
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002101 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002103 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002104 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002105 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002106 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002107 continue;
2108 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002109 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002110 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002111 else if (strcmp(trash.str, "help") == 0 ||
2112 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002113 appctx->ctx.cli.msg = stats_sock_usage_msg;
2114 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002115 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002116 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002117 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002118 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002119 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002120 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002121 /* if prompt is disabled, print help on empty lines,
2122 * so that the user at least knows how to enable
2123 * prompt and find help.
2124 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002125 appctx->ctx.cli.msg = stats_sock_usage_msg;
2126 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002127 }
2128
2129 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002130 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002131 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002132 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002133 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002134 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002135 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002136 continue;
2137 }
2138
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002139 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002140 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002141 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2142 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002143 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002144 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002145 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002147 break;
2148 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002149 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002150 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002151 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002152 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002153 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002154 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002155 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002156 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002157 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002158 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002159 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002160 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002161 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002162 if (stats_table_request(si, appctx->st0))
2163 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002164 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002165 case STAT_CLI_O_MAPS:
2166 if (stats_maps_list(si))
2167 appctx->st0 = STAT_CLI_PROMPT;
2168 break;
2169 case STAT_CLI_O_MAP:
2170 if (stats_map_list(si))
2171 appctx->st0 = STAT_CLI_PROMPT;
2172 break;
2173 case STAT_CLI_O_MLOOK:
2174 if (stats_map_lookup(si))
2175 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002176 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002177 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002178 break;
2179 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002180
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002181 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002182 if (appctx->st0 == STAT_CLI_PROMPT) {
2183 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2184 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002185 }
2186
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002187 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002188 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002189 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002190
2191 /* Now we close the output if one of the writers did so,
2192 * or if we're not in interactive mode and the request
2193 * buffer is empty. This still allows pipelined requests
2194 * to be sent in non-interactive mode.
2195 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002196 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2197 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002198 continue;
2199 }
2200
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002201 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002202 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002203 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002204 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002205
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002206 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002207 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2208 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002209 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002210 * and nothing more to consume. This is comparable to a broken pipe, so
2211 * we forward the close to the request side so that it flows upstream to
2212 * the client.
2213 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002214 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002215 }
2216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002217 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002218 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2219 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2220 /* We have no more processing to do, and nothing more to send, and
2221 * the client side has closed. So we'll forward this state downstream
2222 * on the response buffer.
2223 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002224 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002225 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002226 }
2227
2228 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002229 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002230
2231 /* we don't want to expire timeouts while we're processing requests */
2232 si->ib->rex = TICK_ETERNITY;
2233 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002234
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002235 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002236 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 +02002237 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002238 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 +02002239
2240 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2241 /* check that we have released everything then unregister */
2242 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002243 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002244}
2245
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002246/* This function dumps information onto the stream interface's read buffer.
2247 * It returns 0 as long as it does not complete, non-zero upon completion.
2248 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002249 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002250static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002251{
2252 unsigned int up = (now.tv_sec - start_date.tv_sec);
2253
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002254 chunk_printf(&trash,
2255 "Name: " PRODUCT_NAME "\n"
2256 "Version: " HAPROXY_VERSION "\n"
2257 "Release_date: " HAPROXY_DATE "\n"
2258 "Nbproc: %d\n"
2259 "Process_num: %d\n"
2260 "Pid: %d\n"
2261 "Uptime: %dd %dh%02dm%02ds\n"
2262 "Uptime_sec: %d\n"
2263 "Memmax_MB: %d\n"
2264 "Ulimit-n: %d\n"
2265 "Maxsock: %d\n"
2266 "Maxconn: %d\n"
2267 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002268 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002269 "CumConns: %d\n"
2270 "CumReq: %d\n"
2271#ifdef USE_OPENSSL
2272 "MaxSslConns: %d\n"
2273 "CurrSslConns: %d\n"
2274 "CumSslConns: %d\n"
2275#endif
2276 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002277 "PipesUsed: %d\n"
2278 "PipesFree: %d\n"
2279 "ConnRate: %d\n"
2280 "ConnRateLimit: %d\n"
2281 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002282 "SessRate: %d\n"
2283 "SessRateLimit: %d\n"
2284 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002285#ifdef USE_OPENSSL
2286 "SslRate: %d\n"
2287 "SslRateLimit: %d\n"
2288 "MaxSslRate: %d\n"
2289#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002290 "CompressBpsIn: %u\n"
2291 "CompressBpsOut: %u\n"
2292 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002293#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002294 "ZlibMemUsage: %ld\n"
2295 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002296#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002297 "Tasks: %d\n"
2298 "Run_queue: %d\n"
2299 "Idle_pct: %d\n"
2300 "node: %s\n"
2301 "description: %s\n"
2302 "",
2303 global.nbproc,
2304 relative_pid,
2305 pid,
2306 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2307 up,
2308 global.rlimit_memmax,
2309 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002310 global.maxsock, global.maxconn, global.hardmaxconn,
2311 actconn, totalconn, global.req_count,
2312#ifdef USE_OPENSSL
2313 global.maxsslconn, sslconns, totalsslconns,
2314#endif
2315 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002316 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002317 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002318#ifdef USE_OPENSSL
2319 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2320#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002321 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2322 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002323#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002324 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002325#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002326 nb_tasks_cur, run_queue_cur, idle_pct,
2327 global.node, global.desc ? global.desc : ""
2328 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002329
2330 if (bi_putchk(si->ib, &trash) == -1)
2331 return 0;
2332
2333 return 1;
2334}
2335
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002336/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2337 * the state from stream interface <si>. The caller is responsible for clearing
2338 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002339 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002340static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002341{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002342 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002343 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002344
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002345 if (!(px->cap & PR_CAP_FE))
2346 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002348 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002349 return 0;
2350
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002351 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002352 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002353 /* name, queue */
2354 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002355
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002357 /* Column sub-heading for Enable or Disable server */
2358 chunk_appendf(&trash, "<td></td>");
2359 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002360
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002361 chunk_appendf(&trash,
2362 "<td class=ac>"
2363 "<a name=\"%s/Frontend\"></a>"
2364 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2365 "<td colspan=3></td>"
2366 "",
2367 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002368
Willy Tarreau466c9b52012-12-23 02:25:03 +01002369 chunk_appendf(&trash,
2370 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002371 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002372 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2373 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2374 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002375 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2376 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2377 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002378
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002379 if (px->mode == PR_MODE_HTTP)
2380 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002381 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002382 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002383
2384 chunk_appendf(&trash,
2385 "</table></div></u></td>"
2386 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002387 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002388 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2389 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2390 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002391 U2H(px->fe_counters.sps_max),
2392 U2H(px->fe_counters.cps_max),
2393 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002394
2395 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002396 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002397 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002398 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002399
2400 chunk_appendf(&trash,
2401 "</table></div></u></td>"
2402 /* sessions rate : limit */
2403 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002404 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002405
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002406 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002407 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002408 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002409 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002410 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2411 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002412 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002413 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2414 U2H(px->fe_counters.cum_sess),
2415 U2H(px->fe_counters.cum_conn),
2416 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002417
Willy Tarreau466c9b52012-12-23 02:25:03 +01002418 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002419 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002420 chunk_appendf(&trash,
2421 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2422 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2423 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2424 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2425 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2426 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2427 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2428 "<tr><th>- other responses:</th><td>%s</td></tr>"
2429 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2430 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002431 U2H(px->fe_counters.p.http.cum_req),
2432 U2H(px->fe_counters.p.http.rsp[1]),
2433 U2H(px->fe_counters.p.http.rsp[2]),
2434 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002435 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002436 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002437 U2H(px->fe_counters.p.http.rsp[3]),
2438 U2H(px->fe_counters.p.http.rsp[4]),
2439 U2H(px->fe_counters.p.http.rsp[5]),
2440 U2H(px->fe_counters.p.http.rsp[0]),
2441 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002442 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002443
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002444 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002445 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002446 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002447 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002448 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002449 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002450 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002451 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002452
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002453 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002454 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002455 "<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 +01002456 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002457 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002458 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2459 px->fe_counters.comp_in ?
2460 (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 +01002461 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002462
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002463 chunk_appendf(&trash,
2464 /* denied: req, resp */
2465 "<td>%s</td><td>%s</td>"
2466 /* errors : request, connect, response */
2467 "<td>%s</td><td></td><td></td>"
2468 /* warnings: retries, redispatches */
2469 "<td></td><td></td>"
2470 /* server status : reflect frontend status */
2471 "<td class=ac>%s</td>"
2472 /* rest of server: nothing */
2473 "<td class=ac colspan=8></td></tr>"
2474 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002475 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2476 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002477 px->state == PR_STREADY ? "OPEN" :
2478 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002479 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002480 else { /* CSV mode */
2481 chunk_appendf(&trash,
2482 /* pxid, name, queue cur, queue max, */
2483 "%s,FRONTEND,,,"
2484 /* sessions : current, max, limit, total */
2485 "%d,%d,%d,%lld,"
2486 /* bytes : in, out */
2487 "%lld,%lld,"
2488 /* denied: req, resp */
2489 "%lld,%lld,"
2490 /* errors : request, connect, response */
2491 "%lld,,,"
2492 /* warnings: retries, redispatches */
2493 ",,"
2494 /* server status : reflect frontend status */
2495 "%s,"
2496 /* rest of server: nothing */
2497 ",,,,,,,,"
2498 /* pid, iid, sid, throttle, lbtot, tracked, type */
2499 "%d,%d,0,,,,%d,"
2500 /* rate, rate_lim, rate_max */
2501 "%u,%u,%u,"
2502 /* check_status, check_code, check_duration */
2503 ",,,",
2504 px->id,
2505 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2506 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2507 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2508 px->fe_counters.failed_req,
2509 px->state == PR_STREADY ? "OPEN" :
2510 px->state == PR_STFULL ? "FULL" : "STOP",
2511 relative_pid, px->uuid, STATS_TYPE_FE,
2512 read_freq_ctr(&px->fe_sess_per_sec),
2513 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002514
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2516 if (px->mode == PR_MODE_HTTP) {
2517 for (i=1; i<6; i++)
2518 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2519 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2520 }
2521 else
2522 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002523
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002524 /* failed health analyses */
2525 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002526
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002527 /* requests : req_rate, req_rate_max, req_tot, */
2528 chunk_appendf(&trash, "%u,%u,%lld,",
2529 read_freq_ctr(&px->fe_req_per_sec),
2530 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2531
2532 /* errors: cli_aborts, srv_aborts */
2533 chunk_appendf(&trash, ",,");
2534
2535 /* compression: in, out, bypassed */
2536 chunk_appendf(&trash, "%lld,%lld,%lld,",
2537 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2538
2539 /* compression: comp_rsp */
2540 chunk_appendf(&trash, "%lld,",
2541 px->fe_counters.p.http.comp_rsp);
2542
2543 /* finish with EOL */
2544 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002545 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002546 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002547}
2548
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002549/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2550 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2551 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2552 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002553 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002554static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002555{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002556 struct appctx *appctx = __objt_appctx(si->end);
2557
2558 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002559 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002560 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002561 /* Column sub-heading for Enable or Disable server */
2562 chunk_appendf(&trash, "<td></td>");
2563 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002564 chunk_appendf(&trash,
2565 /* frontend name, listener name */
2566 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2567 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2568 "",
2569 px->id, l->name,
2570 (flags & ST_SHLGNDS)?"<u>":"",
2571 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002572
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002573 if (flags & ST_SHLGNDS) {
2574 char str[INET6_ADDRSTRLEN];
2575 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002576
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002577 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002578
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579 port = get_host_port(&l->addr);
2580 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2581 case AF_INET:
2582 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2583 break;
2584 case AF_INET6:
2585 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2586 break;
2587 case AF_UNIX:
2588 chunk_appendf(&trash, "unix, ");
2589 break;
2590 case -1:
2591 chunk_appendf(&trash, "(%s), ", strerror(errno));
2592 break;
2593 }
Willy Tarreau91861262007-10-17 17:06:05 +02002594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002595 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002596 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597 }
Willy Tarreau91861262007-10-17 17:06:05 +02002598
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002599 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002600 /* queue */
2601 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002602 /* sessions rate: current, max, limit */
2603 "<td colspan=3>&nbsp;</td>"
2604 /* sessions: current, max, limit, total, lbtot */
2605 "<td>%s</td><td>%s</td><td>%s</td>"
2606 "<td>%s</td><td>&nbsp;</td>"
2607 /* bytes: in, out */
2608 "<td>%s</td><td>%s</td>"
2609 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002611 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2612 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002613
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614 chunk_appendf(&trash,
2615 /* denied: req, resp */
2616 "<td>%s</td><td>%s</td>"
2617 /* errors: request, connect, response */
2618 "<td>%s</td><td></td><td></td>"
2619 /* warnings: retries, redispatches */
2620 "<td></td><td></td>"
2621 /* server status: reflect listener status */
2622 "<td class=ac>%s</td>"
2623 /* rest of server: nothing */
2624 "<td class=ac colspan=8></td></tr>"
2625 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002626 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2627 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002628 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2629 }
2630 else { /* CSV mode */
2631 chunk_appendf(&trash,
2632 /* pxid, name, queue cur, queue max, */
2633 "%s,%s,,,"
2634 /* sessions: current, max, limit, total */
2635 "%d,%d,%d,%lld,"
2636 /* bytes: in, out */
2637 "%lld,%lld,"
2638 /* denied: req, resp */
2639 "%lld,%lld,"
2640 /* errors: request, connect, response */
2641 "%lld,,,"
2642 /* warnings: retries, redispatches */
2643 ",,"
2644 /* server status: reflect listener status */
2645 "%s,"
2646 /* rest of server: nothing */
2647 ",,,,,,,,"
2648 /* pid, iid, sid, throttle, lbtot, tracked, type */
2649 "%d,%d,%d,,,,%d,"
2650 /* rate, rate_lim, rate_max */
2651 ",,,"
2652 /* check_status, check_code, check_duration */
2653 ",,,"
2654 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2655 ",,,,,,"
2656 /* failed health analyses */
2657 ","
2658 /* requests : req_rate, req_rate_max, req_tot, */
2659 ",,,"
2660 /* errors: cli_aborts, srv_aborts */
2661 ",,"
2662 /* compression: in, out, bypassed, comp_rsp */
2663 ",,,,"
2664 "\n",
2665 px->id, l->name,
2666 l->nbconn, l->counters->conn_max,
2667 l->maxconn, l->counters->cum_conn,
2668 l->counters->bytes_in, l->counters->bytes_out,
2669 l->counters->denied_req, l->counters->denied_resp,
2670 l->counters->failed_req,
2671 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2672 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2673 }
2674 return 1;
2675}
Willy Tarreau91861262007-10-17 17:06:05 +02002676
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002677/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2678 * from stream interface <si>, stats flags <flags>, and server state <state>.
2679 * The caller is responsible for clearing the trash if needed. Returns non-zero
2680 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002681 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2682 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002683 */
2684static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2685{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002686 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002687 struct server *ref = sv->track ? sv->track : sv;
2688 char str[INET6_ADDRSTRLEN];
2689 struct chunk src;
2690 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002691
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002692 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002693 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002694 "DOWN",
2695 "DN %d/%d &uarr;",
2696 "UP %d/%d &darr;",
2697 "UP",
2698 "NOLB %d/%d &darr;",
2699 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002700 "DRAIN %d/%d &darr;",
2701 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002702 "<i>no check</i>"
2703 };
Willy Tarreau91861262007-10-17 17:06:05 +02002704
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002705 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2706 chunk_appendf(&trash, "<tr class=\"maintain\">");
2707 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002708 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002709 "<tr class=\"%s%d\">",
2710 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002711
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002712 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002713 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002714 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2715 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002716
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002717 chunk_appendf(&trash,
2718 "<td class=ac><a name=\"%s/%s\"></a>%s"
2719 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2720 "",
2721 px->id, sv->id,
2722 (flags & ST_SHLGNDS) ? "<u>" : "",
2723 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002724
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002726 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002727
2728 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2729 case AF_INET:
2730 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2731 break;
2732 case AF_INET6:
2733 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2734 break;
2735 case AF_UNIX:
2736 chunk_appendf(&trash, "unix, ");
2737 break;
2738 case -1:
2739 chunk_appendf(&trash, "(%s), ", strerror(errno));
2740 break;
2741 default: /* address family not supported */
2742 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002743 }
Willy Tarreau91861262007-10-17 17:06:05 +02002744
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002745 /* id */
2746 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002747
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002748 /* cookie */
2749 if (sv->cookie) {
2750 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002751
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002752 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2753 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002756 }
2757
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002758 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002759 }
Willy Tarreau91861262007-10-17 17:06:05 +02002760
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002761 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002762 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002763 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 /* sessions rate : current, max, limit */
2765 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002766 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002768 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2769 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002770
Willy Tarreau466c9b52012-12-23 02:25:03 +01002771
2772 chunk_appendf(&trash,
2773 /* sessions: current, max, limit, total */
2774 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002775 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002776 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2777 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002778 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2779 U2H(sv->counters.cum_sess),
2780 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002781
Willy Tarreau466c9b52012-12-23 02:25:03 +01002782 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2783 if (px->mode == PR_MODE_HTTP) {
2784 unsigned long long tot;
2785 for (tot = i = 0; i < 6; i++)
2786 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787
Willy Tarreau466c9b52012-12-23 02:25:03 +01002788 chunk_appendf(&trash,
2789 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2790 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2791 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2792 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2793 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2794 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2795 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2796 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002797 U2H(tot),
2798 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2799 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2800 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2801 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2802 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2803 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 +02002804 }
Willy Tarreau91861262007-10-17 17:06:05 +02002805
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002806 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002807 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002808 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002809 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002810 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002811
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002812 chunk_appendf(&trash,
2813 /* bytes : in, out */
2814 "<td>%s</td><td>%s</td>"
2815 /* denied: req, resp */
2816 "<td></td><td>%s</td>"
2817 /* errors : request, connect */
2818 "<td></td><td>%s</td>"
2819 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002820 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002821 /* warnings: retries, redispatches */
2822 "<td>%lld</td><td>%lld</td>"
2823 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002824 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2825 U2H(sv->counters.failed_secu),
2826 U2H(sv->counters.failed_conns),
2827 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002828 sv->counters.cli_aborts,
2829 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002830 sv->counters.retries, sv->counters.redispatches);
2831
2832 /* status, lest check */
2833 chunk_appendf(&trash, "<td class=ac>");
2834
2835 if (sv->state & SRV_MAINTAIN) {
2836 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2837 chunk_appendf(&trash, "MAINT");
2838 }
2839 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2840 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2841 chunk_appendf(&trash, "MAINT(via)");
2842 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002843 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002844 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2845 chunk_appendf(&trash,
2846 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002847 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2848 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002849 }
Willy Tarreau91861262007-10-17 17:06:05 +02002850
Willy Tarreauff5ae352013-12-11 20:36:34 +01002851 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002852 chunk_appendf(&trash,
2853 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002854 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002855 get_check_status_info(sv->check.status));
2856
2857 if (sv->check.status >= HCHK_STATUS_L57DATA)
2858 chunk_appendf(&trash, "/%d", sv->check.code);
2859
2860 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002861 chunk_appendf(&trash, " in %lums", sv->check.duration);
2862
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002863 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002864 get_check_status_description(sv->check.status));
2865 if (*sv->check.desc) {
2866 chunk_appendf(&trash, ": ");
2867 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2868 chunk_htmlencode(&trash, &src);
2869 }
2870 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002871 }
2872 else
2873 chunk_appendf(&trash, "</td><td>");
2874
2875 chunk_appendf(&trash,
2876 /* weight */
2877 "</td><td class=ac>%d</td>"
2878 /* act, bck */
2879 "<td class=ac>%s</td><td class=ac>%s</td>"
2880 "",
2881 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2882 (sv->state & SRV_BACKUP) ? "-" : "Y",
2883 (sv->state & SRV_BACKUP) ? "Y" : "-");
2884
2885 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002886 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002887 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002888
2889 if (ref->observe)
2890 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2891
2892 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002893 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002894 "<td>%lld</td><td>%s</td>"
2895 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002896 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002897 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2898 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002899 else if (sv != ref) {
2900 if (sv->state & SRV_MAINTAIN)
2901 chunk_appendf(&trash,
2902 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2903 ref->proxy->id, ref->id);
2904 else
2905 chunk_appendf(&trash,
2906 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2907 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2908 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002909 else
2910 chunk_appendf(&trash, "<td colspan=3></td>");
2911
2912 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002913 if (sv->state & SRV_WARMINGUP)
2914 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002915 else
2916 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2917 }
2918 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002919 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002920 "DOWN,",
2921 "DOWN %d/%d,",
2922 "UP %d/%d,",
2923 "UP,",
2924 "NOLB %d/%d,",
2925 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002926 "DRAIN %d/%d,",
2927 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002928 "no check,"
2929 };
2930
2931 chunk_appendf(&trash,
2932 /* pxid, name */
2933 "%s,%s,"
2934 /* queue : current, max */
2935 "%d,%d,"
2936 /* sessions : current, max, limit, total */
2937 "%d,%d,%s,%lld,"
2938 /* bytes : in, out */
2939 "%lld,%lld,"
2940 /* denied: req, resp */
2941 ",%lld,"
2942 /* errors : request, connect, response */
2943 ",%lld,%lld,"
2944 /* warnings: retries, redispatches */
2945 "%lld,%lld,"
2946 "",
2947 px->id, sv->id,
2948 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002949 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002950 sv->counters.bytes_in, sv->counters.bytes_out,
2951 sv->counters.failed_secu,
2952 sv->counters.failed_conns, sv->counters.failed_resp,
2953 sv->counters.retries, sv->counters.redispatches);
2954
2955 /* status */
2956 if (sv->state & SRV_MAINTAIN)
2957 chunk_appendf(&trash, "MAINT,");
2958 else if (ref != sv && ref->state & SRV_MAINTAIN)
2959 chunk_appendf(&trash, "MAINT(via),");
2960 else
2961 chunk_appendf(&trash,
2962 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002963 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2964 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965
2966 chunk_appendf(&trash,
2967 /* weight, active, backup */
2968 "%d,%d,%d,"
2969 "",
2970 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2971 (sv->state & SRV_BACKUP) ? 0 : 1,
2972 (sv->state & SRV_BACKUP) ? 1 : 0);
2973
2974 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002975 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002976 chunk_appendf(&trash,
2977 "%lld,%lld,%d,%d,",
2978 sv->counters.failed_checks, sv->counters.down_trans,
2979 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2980 else
2981 chunk_appendf(&trash, ",,,,");
2982
2983 /* queue limit, pid, iid, sid, */
2984 chunk_appendf(&trash,
2985 "%s,"
2986 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002987 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988 relative_pid, px->uuid, sv->puid);
2989
2990 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002991 if (sv->state & SRV_WARMINGUP)
2992 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993
2994 /* sessions: lbtot */
2995 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2996
2997 /* tracked */
2998 if (sv->track)
2999 chunk_appendf(&trash, "%s/%s,",
3000 sv->track->proxy->id, sv->track->id);
3001 else
3002 chunk_appendf(&trash, ",");
3003
3004 /* type */
3005 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3006
3007 /* rate */
3008 chunk_appendf(&trash, "%u,,%u,",
3009 read_freq_ctr(&sv->sess_per_sec),
3010 sv->counters.sps_max);
3011
Willy Tarreauff5ae352013-12-11 20:36:34 +01003012 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 /* check_status */
3014 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3015
3016 /* check_code */
3017 if (sv->check.status >= HCHK_STATUS_L57DATA)
3018 chunk_appendf(&trash, "%u,", sv->check.code);
3019 else
3020 chunk_appendf(&trash, ",");
3021
3022 /* check_duration */
3023 if (sv->check.status >= HCHK_STATUS_CHECKED)
3024 chunk_appendf(&trash, "%lu,", sv->check.duration);
3025 else
3026 chunk_appendf(&trash, ",");
3027
3028 }
3029 else
3030 chunk_appendf(&trash, ",,,");
3031
3032 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3033 if (px->mode == PR_MODE_HTTP) {
3034 for (i=1; i<6; i++)
3035 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3036
3037 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3038 }
3039 else
3040 chunk_appendf(&trash, ",,,,,,");
3041
3042 /* failed health analyses */
3043 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3044
3045 /* requests : req_rate, req_rate_max, req_tot, */
3046 chunk_appendf(&trash, ",,,");
3047
3048 /* errors: cli_aborts, srv_aborts */
3049 chunk_appendf(&trash, "%lld,%lld,",
3050 sv->counters.cli_aborts, sv->counters.srv_aborts);
3051
3052 /* compression: in, out, bypassed, comp_rsp */
3053 chunk_appendf(&trash, ",,,,");
3054
3055 /* finish with EOL */
3056 chunk_appendf(&trash, "\n");
3057 }
3058 return 1;
3059}
3060
3061/* Dumps a line for backend <px> to the trash for and uses the state from stream
3062 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3063 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3064 */
3065static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3066{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003067 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003068 struct chunk src;
3069 int i;
3070
3071 if (!(px->cap & PR_CAP_BE))
3072 return 0;
3073
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003074 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003075 return 0;
3076
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003077 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003078 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003079 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003080 /* Column sub-heading for Enable or Disable server */
3081 chunk_appendf(&trash, "<td></td>");
3082 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003083 chunk_appendf(&trash,
3084 "<td class=ac>"
3085 /* name */
3086 "%s<a name=\"%s/Backend\"></a>"
3087 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3088 "",
3089 (flags & ST_SHLGNDS)?"<u>":"",
3090 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003091
3092 if (flags & ST_SHLGNDS) {
3093 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003094 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003095 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3096
3097 /* cookie */
3098 if (px->cookie_name) {
3099 chunk_appendf(&trash, ", cookie: '");
3100 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3101 chunk_htmlencode(&trash, &src);
3102 chunk_appendf(&trash, "'");
3103 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003104 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003105 }
3106
3107 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003108 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003109 /* queue : current, max */
3110 "<td>%s</td><td>%s</td><td></td>"
3111 /* sessions rate : current, max, limit */
3112 "<td>%s</td><td>%s</td><td></td>"
3113 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003114 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003115 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3116 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003117
3118 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003119 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003121 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003122 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003123 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003124 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3125 U2H(px->be_counters.cum_conn),
3126 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003127
Willy Tarreau466c9b52012-12-23 02:25:03 +01003128 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003129 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003130 chunk_appendf(&trash,
3131 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3132 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3133 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3134 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3135 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3136 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3137 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3138 "<tr><th>- other responses:</th><td>%s</td></tr>"
3139 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3140 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003141 U2H(px->be_counters.p.http.cum_req),
3142 U2H(px->be_counters.p.http.rsp[1]),
3143 U2H(px->be_counters.p.http.rsp[2]),
3144 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003146 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003147 U2H(px->be_counters.p.http.rsp[3]),
3148 U2H(px->be_counters.p.http.rsp[4]),
3149 U2H(px->be_counters.p.http.rsp[5]),
3150 U2H(px->be_counters.p.http.rsp[0]),
3151 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 }
3153
3154 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003155 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003156 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01003157 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003158 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003159 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003160 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003161 U2H(px->be_counters.cum_lbconn),
3162 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003163
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003164 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003165 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003166 "<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 +01003167 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003168 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003169 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3170 px->be_counters.comp_in ?
3171 (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 +01003172 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3173
3174 chunk_appendf(&trash,
3175 /* denied: req, resp */
3176 "<td>%s</td><td>%s</td>"
3177 /* errors : request, connect */
3178 "<td></td><td>%s</td>"
3179 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003180 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003181 /* warnings: retries, redispatches */
3182 "<td>%lld</td><td>%lld</td>"
3183 /* backend status: reflect backend status (up/down): we display UP
3184 * if the backend has known working servers or if it has no server at
3185 * all (eg: for stats). Then we display the total weight, number of
3186 * active and backups. */
3187 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3188 "<td class=ac>%d</td><td class=ac>%d</td>"
3189 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003190 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3191 U2H(px->be_counters.failed_conns),
3192 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003193 px->be_counters.cli_aborts,
3194 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003195 px->be_counters.retries, px->be_counters.redispatches,
3196 human_time(now.tv_sec - px->last_change, 1),
3197 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3198 "<font color=\"red\"><b>DOWN</b></font>",
3199 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3200 px->srv_act, px->srv_bck);
3201
3202 chunk_appendf(&trash,
3203 /* rest of backend: nothing, down transitions, total downtime, throttle */
3204 "<td class=ac>&nbsp;</td><td>%d</td>"
3205 "<td>%s</td>"
3206 "<td></td>"
3207 "</tr>",
3208 px->down_trans,
3209 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3210 }
3211 else { /* CSV mode */
3212 chunk_appendf(&trash,
3213 /* pxid, name */
3214 "%s,BACKEND,"
3215 /* queue : current, max */
3216 "%d,%d,"
3217 /* sessions : current, max, limit, total */
3218 "%d,%d,%d,%lld,"
3219 /* bytes : in, out */
3220 "%lld,%lld,"
3221 /* denied: req, resp */
3222 "%lld,%lld,"
3223 /* errors : request, connect, response */
3224 ",%lld,%lld,"
3225 /* warnings: retries, redispatches */
3226 "%lld,%lld,"
3227 /* backend status: reflect backend status (up/down): we display UP
3228 * if the backend has known working servers or if it has no server at
3229 * all (eg: for stats). Then we display the total weight, number of
3230 * active and backups. */
3231 "%s,"
3232 "%d,%d,%d,"
3233 /* rest of backend: nothing, down transitions, last change, total downtime */
3234 ",%d,%d,%d,,"
3235 /* pid, iid, sid, throttle, lbtot, tracked, type */
3236 "%d,%d,0,,%lld,,%d,"
3237 /* rate, rate_lim, rate_max, */
3238 "%u,,%u,"
3239 /* check_status, check_code, check_duration */
3240 ",,,",
3241 px->id,
3242 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3243 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3244 px->be_counters.bytes_in, px->be_counters.bytes_out,
3245 px->be_counters.denied_req, px->be_counters.denied_resp,
3246 px->be_counters.failed_conns, px->be_counters.failed_resp,
3247 px->be_counters.retries, px->be_counters.redispatches,
3248 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3249 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3250 px->srv_act, px->srv_bck,
3251 px->down_trans, (int)(now.tv_sec - px->last_change),
3252 px->srv?be_downtime(px):0,
3253 relative_pid, px->uuid,
3254 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3255 read_freq_ctr(&px->be_sess_per_sec),
3256 px->be_counters.sps_max);
3257
3258 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3259 if (px->mode == PR_MODE_HTTP) {
3260 for (i=1; i<6; i++)
3261 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3262 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3263 }
3264 else
3265 chunk_appendf(&trash, ",,,,,,");
3266
3267 /* failed health analyses */
3268 chunk_appendf(&trash, ",");
3269
3270 /* requests : req_rate, req_rate_max, req_tot, */
3271 chunk_appendf(&trash, ",,,");
3272
3273 /* errors: cli_aborts, srv_aborts */
3274 chunk_appendf(&trash, "%lld,%lld,",
3275 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3276
3277 /* compression: in, out, bypassed */
3278 chunk_appendf(&trash, "%lld,%lld,%lld,",
3279 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3280
3281 /* compression: comp_rsp */
3282 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3283
3284 /* finish with EOL */
3285 chunk_appendf(&trash, "\n");
3286 }
3287 return 1;
3288}
3289
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003290/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003291 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003292 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003293 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003294static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003295{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003296 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003297 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003299 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003300 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003301
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003302 /* 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 +02003303 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003304 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003305 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003306 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3307 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003308 }
3309
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003311 "<form action=\"%s%s%s%s\" method=\"post\">",
3312 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003313 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3314 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003315 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316 }
3317
3318 /* print a new table */
3319 chunk_appendf(&trash,
3320 "<table class=\"tbl\" width=\"100%%\">\n"
3321 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003322 "<th class=\"pxname\" width=\"10%%\">");
3323
3324 chunk_appendf(&trash,
3325 "<a name=\"%s\"></a>%s"
3326 "<a class=px href=\"#%s\">%s</a>",
3327 px->id,
3328 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3329 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003330
3331 if (uri->flags & ST_SHLGNDS) {
3332 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003333 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003334 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3335 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003336 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 }
3338
3339 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003340 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 "<th class=\"%s\" width=\"90%%\">%s</th>"
3342 "</tr>\n"
3343 "</table>\n"
3344 "<table class=\"tbl\" width=\"100%%\">\n"
3345 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003346 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3347 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3348
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003349 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 /* Column heading for Enable or Disable server */
3351 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003352 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353
3354 chunk_appendf(&trash,
3355 "<th rowspan=2></th>"
3356 "<th colspan=3>Queue</th>"
3357 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
3358 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3359 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3360 "<th colspan=9>Server</th>"
3361 "</tr>\n"
3362 "<tr class=\"titre\">"
3363 "<th>Cur</th><th>Max</th><th>Limit</th>"
3364 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3365 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
3366 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3367 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3368 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3369 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3370 "<th>Thrtle</th>\n"
3371 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003372}
3373
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003374/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003377static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003379 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003380 chunk_appendf(&trash, "</table>");
3381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003383 /* close the form used to enable/disable this proxy servers */
3384 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003385 "Choose the action to perform on the checked servers : "
3386 "<select name=action>"
3387 "<option value=\"\"></option>"
3388 "<option value=\"disable\">Disable</option>"
3389 "<option value=\"enable\">Enable</option>"
3390 "<option value=\"stop\">Soft Stop</option>"
3391 "<option value=\"start\">Soft Start</option>"
3392 "<option value=\"shutdown\">Kill Sessions</option>"
3393 "</select>"
3394 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3395 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3396 "</form>",
3397 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398 }
3399
3400 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003401}
Willy Tarreau91861262007-10-17 17:06:05 +02003402
3403/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003404 * Dumps statistics for a proxy. The output is sent to the stream interface's
3405 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3406 * buffer space, or non-zero if everything completed. This function is used
3407 * both by the CLI and the HTTP entry points, and is able to dump the output
3408 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003409 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003410static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003411{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003412 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003413 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003414 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003415 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003416 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003417
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003418 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003419
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003420 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003421 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003422 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003423 if (uri && uri->scope) {
3424 /* we have a limited scope, we have to check the proxy name */
3425 struct stat_scope *scope;
3426 int len;
3427
3428 len = strlen(px->id);
3429 scope = uri->scope;
3430
3431 while (scope) {
3432 /* match exact proxy name */
3433 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3434 break;
3435
3436 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003437 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003438 break;
3439 scope = scope->next;
3440 }
3441
3442 /* proxy name not found : don't dump anything */
3443 if (scope == NULL)
3444 return 1;
3445 }
3446
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003447 /* if the user has requested a limited output and the proxy
3448 * name does not match, skip it.
3449 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003450 if (appctx->ctx.stats.scope_len &&
3451 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 +02003452 return 1;
3453
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003454 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3455 (appctx->ctx.stats.iid != -1) &&
3456 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003457 return 1;
3458
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003459 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003460 /* fall through */
3461
Willy Tarreau295a8372011-03-10 11:25:07 +01003462 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003463 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003464 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003465 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003466 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003467 }
Willy Tarreau91861262007-10-17 17:06:05 +02003468
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003469 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003470 /* fall through */
3471
Willy Tarreau295a8372011-03-10 11:25:07 +01003472 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003473 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003474 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003475 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003476 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003477
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003478 appctx->ctx.stats.l = px->conf.listeners.n;
3479 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003480 /* fall through */
3481
Willy Tarreau295a8372011-03-10 11:25:07 +01003482 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003483 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003484 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003485 if (buffer_almost_full(rep->buf)) {
3486 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003487 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003488 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003489
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003490 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003491 if (!l->counters)
3492 continue;
3493
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003494 if (appctx->ctx.stats.flags & STAT_BOUND) {
3495 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003496 break;
3497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003498 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003499 continue;
3500 }
3501
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003502 /* print the frontend */
3503 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3504 if (bi_putchk(rep, &trash) == -1)
3505 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003506 }
3507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003508 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3509 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003510 /* fall through */
3511
Willy Tarreau295a8372011-03-10 11:25:07 +01003512 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003513 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003514 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003515 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked */
Willy Tarreau91861262007-10-17 17:06:05 +02003516
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003517 if (buffer_almost_full(rep->buf)) {
3518 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003519 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003520 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003521
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003522 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003523
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003524 if (appctx->ctx.stats.flags & STAT_BOUND) {
3525 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003526 break;
3527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003528 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003529 continue;
3530 }
3531
Willy Tarreau44267702011-10-28 15:35:33 +02003532 if (sv->track)
3533 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003534 else
3535 svs = sv;
3536
Willy Tarreau91861262007-10-17 17:06:05 +02003537 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003538 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003539 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003540 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003541 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003542 sv_state = 3; /* UP */
3543 else
3544 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003545
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003546 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003547 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003548 else if (svs->state & SRV_GOINGDOWN)
3549 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003550 }
Willy Tarreau91861262007-10-17 17:06:05 +02003551 else
Simon Horman125d0992013-02-24 17:23:38 +09003552 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003553 sv_state = 1; /* going up */
3554 else
3555 sv_state = 0; /* DOWN */
3556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003557 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003558 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003559 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003560 continue;
3561 }
3562
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003563 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3564 if (bi_putchk(rep, &trash) == -1)
3565 return 0;
3566 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003568 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003569 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003570
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003571 case STAT_PX_ST_BE:
3572 /* print the backend */
3573 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3574 if (bi_putchk(rep, &trash) == -1)
3575 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003577 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003578 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003579
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003580 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003581 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003582 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003583 if (bi_putchk(rep, &trash) == -1)
3584 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003585 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003587 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003588 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003589
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590 case STAT_PX_ST_FIN:
3591 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003592
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003593 default:
3594 /* unknown state, we should put an abort() here ! */
3595 return 1;
3596 }
3597}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003598
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003599/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3600 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003602static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003603{
3604 /* WARNING! This must fit in the first buffer !!! */
3605 chunk_appendf(&trash,
3606 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3607 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3608 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3609 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3610 "<style type=\"text/css\"><!--\n"
3611 "body {"
3612 " font-family: arial, helvetica, sans-serif;"
3613 " font-size: 12px;"
3614 " font-weight: normal;"
3615 " color: black;"
3616 " background: white;"
3617 "}\n"
3618 "th,td {"
3619 " font-size: 10px;"
3620 "}\n"
3621 "h1 {"
3622 " font-size: x-large;"
3623 " margin-bottom: 0.5em;"
3624 "}\n"
3625 "h2 {"
3626 " font-family: helvetica, arial;"
3627 " font-size: x-large;"
3628 " font-weight: bold;"
3629 " font-style: italic;"
3630 " color: #6020a0;"
3631 " margin-top: 0em;"
3632 " margin-bottom: 0em;"
3633 "}\n"
3634 "h3 {"
3635 " font-family: helvetica, arial;"
3636 " font-size: 16px;"
3637 " font-weight: bold;"
3638 " color: #b00040;"
3639 " background: #e8e8d0;"
3640 " margin-top: 0em;"
3641 " margin-bottom: 0em;"
3642 "}\n"
3643 "li {"
3644 " margin-top: 0.25em;"
3645 " margin-right: 2em;"
3646 "}\n"
3647 ".hr {margin-top: 0.25em;"
3648 " border-color: black;"
3649 " border-bottom-style: solid;"
3650 "}\n"
3651 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3652 ".total {background: #20D0D0;color: #ffff80;}\n"
3653 ".frontend {background: #e8e8d0;}\n"
3654 ".socket {background: #d0d0d0;}\n"
3655 ".backend {background: #e8e8d0;}\n"
3656 ".active0 {background: #ff9090;}\n"
3657 ".active1 {background: #ffd020;}\n"
3658 ".active2 {background: #ffffa0;}\n"
3659 ".active3 {background: #c0ffc0;}\n"
3660 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003661 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3662 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3663 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003664 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003665 ".backup0 {background: #ff9090;}\n"
3666 ".backup1 {background: #ff80ff;}\n"
3667 ".backup2 {background: #c060ff;}\n"
3668 ".backup3 {background: #b0d0ff;}\n"
3669 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3670 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003671 ".backup6 {background: #c060ff;}\n"
3672 ".backup7 {background: #cc9900;}\n"
3673 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003674 ".maintain {background: #c07820;}\n"
3675 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3676 "\n"
3677 "a.px:link {color: #ffff40; text-decoration: none;}"
3678 "a.px:visited {color: #ffff40; text-decoration: none;}"
3679 "a.px:hover {color: #ffffff; text-decoration: none;}"
3680 "a.lfsb:link {color: #000000; text-decoration: none;}"
3681 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3682 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3683 "\n"
3684 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3685 "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"
3686 "table.tbl td.ac { text-align: center;}\n"
3687 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3688 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3689 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3690 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3691 "\n"
3692 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3693 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3694 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003695 "table.det { border-collapse: collapse; border-style: none; }\n"
3696 "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 +01003697 "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 +01003698 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003699 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003700 " display:block;\n"
3701 " visibility:hidden;\n"
3702 " z-index:2147483647;\n"
3703 " position:absolute;\n"
3704 " padding:2px 4px 3px;\n"
3705 " background:#f0f060; color:#000000;\n"
3706 " border:1px solid #7040c0;\n"
3707 " white-space:nowrap;\n"
3708 " font-style:normal;font-size:11px;font-weight:normal;\n"
3709 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3710 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3711 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003712 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003713 "-->\n"
3714 "</style></head>\n",
3715 (uri->flags & ST_SHNODE) ? " on " : "",
3716 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3717 );
3718}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003719
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003720/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003721 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003722 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003723 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003724static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003725{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003726 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003727 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003728 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003729
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003730 /* WARNING! this has to fit the first packet too.
3731 * We are around 3.5 kB, add adding entries will
3732 * become tricky if we want to support 4kB buffers !
3733 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003734 chunk_appendf(&trash,
3735 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3736 PRODUCT_NAME "%s</a></h1>\n"
3737 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3738 "<hr width=\"100%%\" class=\"hr\">\n"
3739 "<h3>&gt; General process information</h3>\n"
3740 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3741 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3742 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3743 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3744 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3745 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3746 "Running tasks: %d/%d; idle = %d %%<br>\n"
3747 "</td><td align=\"center\" nowrap>\n"
3748 "<table class=\"lgd\"><tr>\n"
3749 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3750 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3751 "</tr><tr>\n"
3752 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3753 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3754 "</tr><tr>\n"
3755 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3756 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3757 "</tr><tr>\n"
3758 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003759 "</tr><tr>\n"
3760 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003761 "</tr><tr>\n"
3762 "<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 -07003763 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003764 "<td class=\"active7\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003765 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003766 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003767 "</td>"
3768 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3769 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3770 "",
3771 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3772 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3773 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3774 (uri->flags & ST_SHDESC) ? ": " : "",
3775 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3776 pid, relative_pid, global.nbproc,
3777 up / 86400, (up % 86400) / 3600,
3778 (up % 3600) / 60, (up % 60),
3779 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3780 global.rlimit_memmax ? " MB" : "",
3781 global.rlimit_nofile,
3782 global.maxsock, global.maxconn, global.maxpipes,
3783 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3784 run_queue_cur, nb_tasks_cur, idle_pct
3785 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003787 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3788 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3789 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003790
3791 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003792 "<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 +02003793 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003794 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3795 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3796 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003797 STAT_SCOPE_TXT_MAXLEN);
3798
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003799 /* 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 +02003800 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003801 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003802 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003803 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3804 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003805 }
3806
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003807 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003809 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 uri->uri_prefix,
3811 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003812 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003813 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003814 else
3815 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003816 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 uri->uri_prefix,
3818 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003819 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003820 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003821
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003822 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003823 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003824 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003825 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003827 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003828 "",
3829 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003830 else
3831 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003832 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003834 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003835 ";norefresh",
3836 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003837 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003838
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003839 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003840 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003841 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003842 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3843 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003844 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003845
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003847 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003848 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003849 (uri->refresh > 0) ? ";norefresh" : "",
3850 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003851
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003852 chunk_appendf(&trash,
3853 "</ul></td>"
3854 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3855 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3856 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3857 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3858 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3859 "</ul>"
3860 "</td>"
3861 "</tr></table>\n"
3862 ""
3863 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003864
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003865 if (appctx->ctx.stats.st_code) {
3866 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003867 case STAT_STATUS_DONE:
3868 chunk_appendf(&trash,
3869 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003870 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003871 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003872 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003873 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3874 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003875 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003876 break;
3877 case STAT_STATUS_NONE:
3878 chunk_appendf(&trash,
3879 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003880 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003881 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003882 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003883 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3884 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003885 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 break;
3887 case STAT_STATUS_PART:
3888 chunk_appendf(&trash,
3889 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003890 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003891 "Action partially processed.<br>"
3892 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003893 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003894 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3895 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003896 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003897 break;
3898 case STAT_STATUS_ERRP:
3899 chunk_appendf(&trash,
3900 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003901 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003902 "Action not processed because of invalid parameters."
3903 "<ul>"
3904 "<li>The action is maybe unknown.</li>"
3905 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3906 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3907 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003908 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003909 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3910 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003911 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912 break;
3913 case STAT_STATUS_EXCD:
3914 chunk_appendf(&trash,
3915 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003916 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003917 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3918 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003919 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003920 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3921 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003922 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923 break;
3924 case STAT_STATUS_DENY:
3925 chunk_appendf(&trash,
3926 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003927 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003928 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003929 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003930 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3931 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003932 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003933 break;
3934 default:
3935 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003936 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003937 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003938 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003939 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003940 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3941 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003942 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003943 }
3944 chunk_appendf(&trash, "<p>\n");
3945 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003946}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003947
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003948/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3949 * 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_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003952{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003953 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003954}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003955
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003956/* This function dumps statistics onto the stream interface's read buffer in
3957 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003958 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3959 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3960 * and the session must be closed, or -1 in case of any error. This function is
3961 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003963static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003964{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003965 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 struct channel *rep = si->ib;
3967 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003968
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003969 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003970
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003971 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003973 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003974 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003975
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003976 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003977 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003978 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003979 else
3980 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003981
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003982 if (bi_putchk(rep, &trash) == -1)
3983 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003984
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003985 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003986 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003987
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003988 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003989 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003990 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003992 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993 }
Willy Tarreau91861262007-10-17 17:06:05 +02003994
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003995 appctx->ctx.stats.px = proxy;
3996 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3997 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003998 /* fall through */
3999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 case STAT_ST_LIST:
4001 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004002 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004003 if (buffer_almost_full(rep->buf)) {
4004 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004006 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004008 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004009 /* skip the disabled proxies, global frontend and non-networked ones */
4010 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004011 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004012 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004013
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004014 appctx->ctx.stats.px = px->next;
4015 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016 }
4017 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004019 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004020 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004021
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004022 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004023 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004024 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004025 if (bi_putchk(rep, &trash) == -1)
4026 return 0;
4027 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004028
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004029 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004030 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004031
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032 case STAT_ST_FIN:
4033 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004035 default:
4036 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004037 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004038 return -1;
4039 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004040}
Willy Tarreauae526782010-03-04 20:34:23 +01004041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004042/* We reached the stats page through a POST request. The appctx is
4043 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004044 * Parse the posted data and enable/disable servers if necessary.
4045 * Returns 1 if request was parsed or zero if it needs more data.
4046 */
4047static int stats_process_http_post(struct stream_interface *si)
4048{
4049 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004050 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004051
4052 struct proxy *px = NULL;
4053 struct server *sv = NULL;
4054
4055 char key[LINESIZE];
4056 int action = ST_ADM_ACTION_NONE;
4057 int reprocess = 0;
4058
4059 int total_servers = 0;
4060 int altered_servers = 0;
4061
4062 char *first_param, *cur_param, *next_param, *end_params;
4063 char *st_cur_param = NULL;
4064 char *st_next_param = NULL;
4065
4066 struct chunk *temp;
4067 int reql;
4068
4069 temp = get_trash_chunk();
4070 if (temp->size < s->txn.req.body_len) {
4071 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004072 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004073 goto out;
4074 }
4075
4076 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4077 if (reql <= 0) {
4078 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004079 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004080 return 0;
4081 }
4082
4083 first_param = temp->str;
4084 end_params = temp->str + reql;
4085 cur_param = next_param = end_params;
4086 *end_params = '\0';
4087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004088 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004089
4090 /*
4091 * Parse the parameters in reverse order to only store the last value.
4092 * From the html form, the backend and the action are at the end.
4093 */
4094 while (cur_param > first_param) {
4095 char *value;
4096 int poffset, plen;
4097
4098 cur_param--;
4099
4100 if ((*cur_param == '&') || (cur_param == first_param)) {
4101 reprocess_servers:
4102 /* Parse the key */
4103 poffset = (cur_param != first_param ? 1 : 0);
4104 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4105 if ((plen > 0) && (plen <= sizeof(key))) {
4106 strncpy(key, cur_param + poffset, plen);
4107 key[plen - 1] = '\0';
4108 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004109 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004110 goto out;
4111 }
4112
4113 /* Parse the value */
4114 value = key;
4115 while (*value != '\0' && *value != '=') {
4116 value++;
4117 }
4118 if (*value == '=') {
4119 /* Ok, a value is found, we can mark the end of the key */
4120 *value++ = '\0';
4121 }
4122 if (url_decode(key) < 0 || url_decode(value) < 0)
4123 break;
4124
4125 /* Now we can check the key to see what to do */
4126 if (!px && (strcmp(key, "b") == 0)) {
4127 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4128 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004129 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004130 goto out;
4131 }
4132 }
4133 else if (!action && (strcmp(key, "action") == 0)) {
4134 if (strcmp(value, "disable") == 0) {
4135 action = ST_ADM_ACTION_DISABLE;
4136 }
4137 else if (strcmp(value, "enable") == 0) {
4138 action = ST_ADM_ACTION_ENABLE;
4139 }
4140 else if (strcmp(value, "stop") == 0) {
4141 action = ST_ADM_ACTION_STOP;
4142 }
4143 else if (strcmp(value, "start") == 0) {
4144 action = ST_ADM_ACTION_START;
4145 }
4146 else if (strcmp(value, "shutdown") == 0) {
4147 action = ST_ADM_ACTION_SHUTDOWN;
4148 }
4149 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004150 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004151 goto out;
4152 }
4153 }
4154 else if (strcmp(key, "s") == 0) {
4155 if (!(px && action)) {
4156 /*
4157 * Indicates that we'll need to reprocess the parameters
4158 * as soon as backend and action are known
4159 */
4160 if (!reprocess) {
4161 st_cur_param = cur_param;
4162 st_next_param = next_param;
4163 }
4164 reprocess = 1;
4165 }
4166 else if ((sv = findserver(px, value)) != NULL) {
4167 switch (action) {
4168 case ST_ADM_ACTION_DISABLE:
4169 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4170 /* Not already in maintenance, we can change the server state */
4171 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004172 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004173 set_server_down(&sv->check);
4174 altered_servers++;
4175 total_servers++;
4176 }
4177 break;
4178 case ST_ADM_ACTION_ENABLE:
4179 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004180 /* Already in maintenance, we can change the server state.
4181 * If this server tracks the status of another one,
4182 * we must restore the good status.
4183 */
4184 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4185 set_server_up(&sv->check);
4186 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4187 }
4188 else {
4189 sv->state &= ~SRV_MAINTAIN;
4190 sv->check.state &= ~CHK_ST_PAUSED;
4191 set_server_down(&sv->check);
4192 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004193 altered_servers++;
4194 total_servers++;
4195 }
4196 break;
4197 case ST_ADM_ACTION_STOP:
4198 case ST_ADM_ACTION_START:
4199 if (action == ST_ADM_ACTION_START)
4200 sv->uweight = sv->iweight;
4201 else
4202 sv->uweight = 0;
4203
4204 server_recalc_eweight(sv);
4205 set_server_drain_state(sv);
4206
4207 altered_servers++;
4208 total_servers++;
4209 break;
4210 case ST_ADM_ACTION_SHUTDOWN:
4211 if (px->state != PR_STSTOPPED) {
4212 struct session *sess, *sess_bck;
4213
4214 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4215 if (sess->srv_conn == sv)
4216 session_shutdown(sess, SN_ERR_KILLED);
4217
4218 altered_servers++;
4219 total_servers++;
4220 }
4221 break;
4222 }
4223 } else {
4224 /* the server name is unknown or ambiguous (duplicate names) */
4225 total_servers++;
4226 }
4227 }
4228 if (reprocess && px && action) {
4229 /* Now, we know the backend and the action chosen by the user.
4230 * We can safely restart from the first server parameter
4231 * to reprocess them
4232 */
4233 cur_param = st_cur_param;
4234 next_param = st_next_param;
4235 reprocess = 0;
4236 goto reprocess_servers;
4237 }
4238
4239 next_param = cur_param;
4240 }
4241 }
4242
4243 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004244 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004245 }
4246 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004247 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004248 }
4249 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004250 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004251 }
4252 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004253 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004254 }
4255 out:
4256 return 1;
4257}
4258
4259
4260static int stats_send_http_headers(struct stream_interface *si)
4261{
4262 struct session *s = session_from_task(si->owner);
4263 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004264 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004265
4266 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004267 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004268 "Cache-Control: no-cache\r\n"
4269 "Connection: close\r\n"
4270 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004271 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004272
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004273 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004274 chunk_appendf(&trash, "Refresh: %d\r\n",
4275 uri->refresh);
4276
Willy Tarreau51437d22013-12-29 00:43:40 +01004277 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004278
4279 s->txn.status = 200;
4280 s->logs.tv_request = now;
4281
4282 if (bi_putchk(si->ib, &trash) == -1)
4283 return 0;
4284
4285 return 1;
4286}
4287
4288static int stats_send_http_redirect(struct stream_interface *si)
4289{
4290 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4291 struct session *s = session_from_task(si->owner);
4292 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004293 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004294
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004295 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004296 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004297 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004298 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004299 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4300 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004301 }
4302
4303 /* We don't want to land on the posted stats page because a refresh will
4304 * repost the data. We don't want this to happen on accident so we redirect
4305 * the browse to the stats page with a GET.
4306 */
4307 chunk_printf(&trash,
4308 "HTTP/1.1 303 See Other\r\n"
4309 "Cache-Control: no-cache\r\n"
4310 "Content-Type: text/plain\r\n"
4311 "Connection: close\r\n"
4312 "Location: %s;st=%s%s%s%s\r\n"
4313 "\r\n",
4314 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004315 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4316 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4317 stat_status_codes[appctx->ctx.stats.st_code]) ?
4318 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004319 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004320 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4321 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004322 scope_txt);
4323
4324 s->txn.status = 303;
4325 s->logs.tv_request = now;
4326
4327 if (bi_putchk(si->ib, &trash) == -1)
4328 return 0;
4329
4330 return 1;
4331}
4332
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004333/* This I/O handler runs as an applet embedded in a stream interface. It is
4334 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004335 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 * automatically unregisters itself once transfer is complete.
4337 */
4338static void http_stats_io_handler(struct stream_interface *si)
4339{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004340 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004341 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004342 struct channel *req = si->ob;
4343 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004344
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004345 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4346 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004347
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004348 /* check that the output is not closed */
4349 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004350 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004351
Willy Tarreau347a35d2013-11-22 17:51:09 +01004352 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004353 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004354 if (stats_send_http_headers(si)) {
4355 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004356 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004357 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004358 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004359 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004360 }
4361
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004362 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004363 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004364 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004366
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004367 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004368 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004370 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004371 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004372 }
4373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004374 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004375 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004377 }
4378
Willy Tarreau51437d22013-12-29 00:43:40 +01004379 if (appctx->st0 == STAT_HTTP_DONE)
4380 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004381
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4383 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004384
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004385 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004386 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4387 si_shutr(si);
4388 res->flags |= CF_READ_NULL;
4389 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390 }
Willy Tarreau91861262007-10-17 17:06:05 +02004391
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004392 /* update all other flags and resync with the other side */
4393 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004394
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004395 /* we don't want to expire timeouts while we're processing requests */
4396 si->ib->rex = TICK_ETERNITY;
4397 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004398
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004399 out:
4400 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4401 /* check that we have released everything then unregister */
4402 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004403 }
4404}
4405
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004406
Willy Tarreau909d5172012-11-26 03:04:41 +01004407static inline const char *get_conn_ctrl_name(const struct connection *conn)
4408{
Willy Tarreau3c728722014-01-23 13:50:42 +01004409 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004410 return "NONE";
4411 return conn->ctrl->name;
4412}
4413
4414static inline const char *get_conn_xprt_name(const struct connection *conn)
4415{
4416 static char ptr[17];
4417
Willy Tarreauaad69382014-01-23 14:21:42 +01004418 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004419 return "NONE";
4420
4421 if (conn->xprt == &raw_sock)
4422 return "RAW";
4423
4424#ifdef USE_OPENSSL
4425 if (conn->xprt == &ssl_sock)
4426 return "SSL";
4427#endif
4428 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4429 return ptr;
4430}
4431
4432static inline const char *get_conn_data_name(const struct connection *conn)
4433{
4434 static char ptr[17];
4435
4436 if (!conn->data)
4437 return "NONE";
4438
4439 if (conn->data == &sess_conn_cb)
4440 return "SESS";
4441
4442 if (conn->data == &si_conn_cb)
4443 return "STRM";
4444
4445 if (conn->data == &check_conn_cb)
4446 return "CHCK";
4447
4448 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4449 return ptr;
4450}
4451
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004452/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004453 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004454 * 0 if the output buffer is full and it needs to be called again, otherwise
4455 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004456 */
Willy Tarreau76153662012-11-26 01:16:39 +01004457static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004458{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004459 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004460 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004461 extern const char *monthname[12];
4462 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004463 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004464 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004465
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004466 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004467
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004468 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004469 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004470 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4471 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004472 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004473 appctx->ctx.sess.uid = 0;
4474 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004475 return 1;
4476 }
4477
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004478 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004479 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004480 appctx->ctx.sess.uid = sess->uniq_id;
4481 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004482 /* fall through */
4483
4484 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004485 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004486 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004487 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004488 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004489 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4490 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004491 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004492 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004493
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004494 conn = objt_conn(sess->si[0].end);
4495 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004496 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004497 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004498 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004499 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004500 break;
4501 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004502 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004503 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004504 default:
4505 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004506 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004507 break;
4508 }
4509
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004511 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004512 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004513
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004514 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004515 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004516 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4517 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4518 sess->listener ? sess->listener->luid : 0);
4519
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004520 if (conn)
4521 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004522
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004523 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004524 case AF_INET:
4525 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004526 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004527 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004528 break;
4529 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004530 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004531 break;
4532 default:
4533 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004534 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004535 break;
4536 }
4537
Willy Tarreau50943332011-09-02 17:33:05 +02004538 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004539 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004540 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004541 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004542 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4543 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004544 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004545
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004546 conn = objt_conn(sess->si[1].end);
4547 if (conn)
4548 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004549
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004550 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004551 case AF_INET:
4552 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004553 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004554 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004555 break;
4556 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004557 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004558 break;
4559 default:
4560 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004561 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004562 break;
4563 }
4564
4565 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004566 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004567 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004568 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4569 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004570 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004571 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004572
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004573 if (conn)
4574 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004575
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004576 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004577 case AF_INET:
4578 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004579 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004580 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004581 break;
4582 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004583 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004584 break;
4585 default:
4586 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004587 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004588 break;
4589 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004590
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004591 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004592 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004593 sess->task,
4594 sess->task->state,
4595 sess->task->nice, sess->task->calls,
4596 sess->task->expire ?
4597 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4598 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4599 TICKS_TO_MS(1000)) : "<NEVER>",
4600 task_in_rq(sess->task) ? ", running" : "");
4601
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004602 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004603 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004604 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4605
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004606 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004607 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4608 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4609 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4610
4611 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004612 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004613 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004614 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004615 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004616 obj_type_name(sess->si[0].end),
4617 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004618 sess->si[0].exp ?
4619 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4620 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4621 TICKS_TO_MS(1000)) : "<NEVER>",
4622 sess->si[0].err_type);
4623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004624 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004625 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004626 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004627 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004628 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004629 obj_type_name(sess->si[1].end),
4630 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004631 sess->si[1].exp ?
4632 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4633 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4634 TICKS_TO_MS(1000)) : "<NEVER>",
4635 sess->si[1].err_type);
4636
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004637 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004638 chunk_appendf(&trash,
4639 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004640 conn,
4641 get_conn_ctrl_name(conn),
4642 get_conn_xprt_name(conn),
4643 get_conn_data_name(conn),
4644 obj_type_name(conn->target),
4645 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004646
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004647 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004648 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004649 conn->flags,
4650 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004651 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004652 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004653 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004654 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004655 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4656 chunk_appendf(&trash,
4657 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4658 tmpctx,
4659 tmpctx->st0,
4660 tmpctx->st1,
4661 tmpctx->st2,
4662 tmpctx->applet->name);
4663 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004664
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004665 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004666 chunk_appendf(&trash,
4667 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 conn,
4669 get_conn_ctrl_name(conn),
4670 get_conn_xprt_name(conn),
4671 get_conn_data_name(conn),
4672 obj_type_name(conn->target),
4673 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004674
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004675 chunk_appendf(&trash,
4676 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004677 conn->flags,
4678 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004679 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004680 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004682 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004683 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4684 chunk_appendf(&trash,
4685 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4686 tmpctx,
4687 tmpctx->st0,
4688 tmpctx->st1,
4689 tmpctx->st2,
4690 tmpctx->applet->name);
4691 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004692
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004693 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004694 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004695 " an_exp=%s",
4696 sess->req,
4697 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004698 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004699 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004700 sess->req->analyse_exp ?
4701 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4702 TICKS_TO_MS(1000)) : "<NEVER>");
4703
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004704 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004705 " rex=%s",
4706 sess->req->rex ?
4707 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4708 TICKS_TO_MS(1000)) : "<NEVER>");
4709
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004710 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004711 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004712 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004713 sess->req->wex ?
4714 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4715 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004716 sess->req->buf,
4717 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004718 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004719 sess->txn.req.next, sess->req->buf->i,
4720 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004721
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004722 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004723 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004724 " an_exp=%s",
4725 sess->rep,
4726 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004727 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004728 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004729 sess->rep->analyse_exp ?
4730 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4731 TICKS_TO_MS(1000)) : "<NEVER>");
4732
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004733 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004734 " rex=%s",
4735 sess->rep->rex ?
4736 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4737 TICKS_TO_MS(1000)) : "<NEVER>");
4738
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004739 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004740 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004741 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004742 sess->rep->wex ?
4743 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4744 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004745 sess->rep->buf,
4746 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004747 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004748 sess->txn.rsp.next, sess->rep->buf->i,
4749 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004750
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004751 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004752 return 0;
4753
4754 /* use other states to dump the contents */
4755 }
4756 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004757 appctx->ctx.sess.uid = 0;
4758 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004759 return 1;
4760}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004761
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004762static int stats_maps_list(struct stream_interface *si)
4763{
4764 struct appctx *appctx = __objt_appctx(si->end);
4765
4766 switch (appctx->st2) {
4767 case STAT_ST_INIT:
4768 /* Init to the first entry. The list cannot be change */
4769 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4770 appctx->st2 = STAT_ST_LIST;
4771 /* fall through */
4772
4773 case STAT_ST_LIST:
4774 while (appctx->ctx.map.ref) {
4775
4776 chunk_reset(&trash);
4777
4778 /* build messages */
4779 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4780
4781 if (bi_putchk(si->ib, &trash) == -1) {
4782 /* let's try again later from this session. We add ourselves into
4783 * this session's users so that it can remove us upon termination.
4784 */
4785 return 0;
4786 }
4787
4788 /* get next list entry and check the end of the list */
4789 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4790 struct map_reference *, list);
4791 if (&appctx->ctx.map.ref->list == &maps)
4792 break;
4793 }
4794
4795 appctx->st2 = STAT_ST_FIN;
4796 /* fall through */
4797
4798 default:
4799 appctx->st2 = STAT_ST_FIN;
4800 return 1;
4801 }
4802}
4803
4804static const char *smp_to_type[SMP_TYPES] = {
4805 [SMP_T_BOOL] = "bool",
4806 [SMP_T_UINT] = "uint",
4807 [SMP_T_SINT] = "sint",
4808 [SMP_T_ADDR] = "addr",
4809 [SMP_T_IPV4] = "ipv4",
4810 [SMP_T_IPV6] = "ipv6",
4811 [SMP_T_STR] = "str",
4812 [SMP_T_BIN] = "bin",
4813 [SMP_T_CSTR] = "cstr",
4814 [SMP_T_CBIN] = "cbin",
4815};
4816
4817static int stats_map_lookup(struct stream_interface *si)
4818{
4819 struct appctx *appctx = __objt_appctx(si->end);
4820 struct sample_storage *smp;
4821 struct sample sample;
4822 struct pattern *pat;
4823 struct pat_idx_elt *elt;
4824 enum pat_match_res res;
4825 struct sockaddr_in addr;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004826 char addr_str[INET_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004827
4828 switch (appctx->st2) {
4829 case STAT_ST_INIT:
4830 appctx->ctx.map.desc = NULL;
4831 stats_map_lookup_next(si);
4832 appctx->st2 = STAT_ST_LIST;
4833 /* fall through */
4834
4835 case STAT_ST_LIST:
4836 /* for each lookup type */
4837 while (appctx->ctx.map.desc) {
4838 /* initialise chunk to build new message */
4839 chunk_reset(&trash);
4840
4841 /* execute pattern matching */
4842 sample.type = SMP_T_CSTR;
4843 sample.data.str.len = appctx->ctx.map.chunk.len;
4844 sample.data.str.str = appctx->ctx.map.chunk.str;
4845 pat = NULL;
4846 elt = NULL;
4847 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4848
4849 /* build return message: set type of match */
4850 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4851 chunk_appendf(&trash, "found, ");
4852 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4853 chunk_appendf(&trash, "bool, ");
4854 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4855 chunk_appendf(&trash, "int, ");
4856 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4857 chunk_appendf(&trash, "ip, ");
4858 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4859 chunk_appendf(&trash, "bin, ");
4860 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4861 chunk_appendf(&trash, "len, ");
4862 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4863 chunk_appendf(&trash, "str, ");
4864 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4865 chunk_appendf(&trash, "beg, ");
4866 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4867 chunk_appendf(&trash, "sub, ");
4868 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4869 chunk_appendf(&trash, "dir, ");
4870 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4871 chunk_appendf(&trash, "dom, ");
4872 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4873 chunk_appendf(&trash, "end, ");
4874 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4875 chunk_appendf(&trash, "reg, ");
4876 else /* The never appens case */
4877 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4878
4879 /* Display no match, and set default value */
4880 if (res == PAT_NOMATCH) {
4881 chunk_appendf(&trash, "no-match, ");
4882 smp = appctx->ctx.map.desc->def;
4883 }
4884
4885 /* Display match and match info */
4886 else {
4887 /* display match */
4888 chunk_appendf(&trash, "match, ");
4889
4890 /* display search mode */
4891 if (elt)
4892 chunk_appendf(&trash, "tree, ");
4893 else
4894 chunk_appendf(&trash, "list, ");
4895
4896 /* display search options */
4897 if (pat) {
4898 /* case sensitive */
4899 if (pat->flags & PAT_F_IGNORE_CASE)
4900 chunk_appendf(&trash, "case-insensitive, ");
4901 else
4902 chunk_appendf(&trash, "case-sensitive, ");
4903
4904 /* display source */
4905 if (pat->flags & PAT_F_FROM_FILE)
4906 chunk_appendf(&trash, "from-file, ");
4907 }
4908
4909 /* display match expresion */
4910 if (elt) {
4911 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4912 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4913 }
4914 /* only IPv4 */
4915 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4916 /* convert ip */
4917 memcpy(&addr.sin_addr, elt->node.key, 4);
4918 addr.sin_family = AF_INET;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004919 if (addr_to_str((struct sockaddr_storage *)&addr, addr_str, INET_ADDRSTRLEN))
4920 chunk_appendf(&trash, "match=\"%s/%d\", ", addr_str, elt->node.node.pfx);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004921 }
4922 }
4923 }
4924
4925 /* display return value */
4926 if (!smp) {
4927 chunk_appendf(&trash, "return=nothing\n");
4928 }
4929 else {
4930 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4931 sample.type = smp->type;
4932 if (sample_casts[sample.type][SMP_T_CSTR] &&
4933 sample_casts[sample.type][SMP_T_CSTR](&sample))
4934 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4935 sample.data.str.str, smp_to_type[smp->type]);
4936 else
4937 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4938 smp_to_type[smp->type]);
4939 }
4940
4941 /* display response */
4942 if (bi_putchk(si->ib, &trash) == -1) {
4943 /* let's try again later from this session. We add ourselves into
4944 * this session's users so that it can remove us upon termination.
4945 */
4946 return 0;
4947 }
4948
4949 /* get next entry */
4950 stats_map_lookup_next(si);
4951 }
4952
4953 appctx->st2 = STAT_ST_FIN;
4954 /* fall through */
4955
4956 default:
4957 appctx->st2 = STAT_ST_FIN;
4958 free(appctx->ctx.map.chunk.str);
4959 return 1;
4960 }
4961}
4962
4963static int stats_map_list(struct stream_interface *si)
4964{
4965 struct appctx *appctx = __objt_appctx(si->end);
4966
4967 switch (appctx->st2) {
4968
4969 case STAT_ST_INIT:
4970 /* Init to the first entry. The list cannot be change */
4971 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
4972 struct map_entry *, list);
4973 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4974 appctx->ctx.map.ent = NULL;
4975 appctx->st2 = STAT_ST_LIST;
4976 /* fall through */
4977
4978 case STAT_ST_LIST:
4979 while (appctx->ctx.map.ent) {
4980 chunk_reset(&trash);
4981
4982 /* build messages */
4983 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
4984
4985 if (bi_putchk(si->ib, &trash) == -1) {
4986 /* let's try again later from this session. We add ourselves into
4987 * this session's users so that it can remove us upon termination.
4988 */
4989 return 0;
4990 }
4991
4992 /* get next list entry and check the end of the list */
4993 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
4994 struct map_entry *, list);
4995 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4996 break;
4997 }
4998
4999 appctx->st2 = STAT_ST_FIN;
5000 /* fall through */
5001
5002 default:
5003 appctx->st2 = STAT_ST_FIN;
5004 return 1;
5005 }
5006}
5007
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005008/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005009 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005010 * to be called again, otherwise non-zero. It is designed to be called
5011 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005012 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005013static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005014{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005015 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005016 struct connection *conn;
5017
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005018 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005019 /* If we're forced to shut down, we might have to remove our
5020 * reference to the last session being dumped.
5021 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005022 if (appctx->st2 == STAT_ST_LIST) {
5023 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5024 LIST_DEL(&appctx->ctx.sess.bref.users);
5025 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005026 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005027 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005028 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005029 }
5030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005031 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005032
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005033 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005034 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005035 /* the function had not been called yet, let's prepare the
5036 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005037 * pointer to the first in the global list. When a target
5038 * session is being destroyed, it is responsible for updating
5039 * this pointer. We know we have reached the end when this
5040 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005041 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005042 LIST_INIT(&appctx->ctx.sess.bref.users);
5043 appctx->ctx.sess.bref.ref = sessions.n;
5044 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005045 /* fall through */
5046
Willy Tarreau295a8372011-03-10 11:25:07 +01005047 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005048 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005049 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5050 LIST_DEL(&appctx->ctx.sess.bref.users);
5051 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005052 }
5053
5054 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005055 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005056 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005057 struct session *curr_sess;
5058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005059 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005060
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 if (appctx->ctx.sess.target) {
5062 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005063 goto next_sess;
5064
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005065 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005066 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005067 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005068 return 0;
5069
5070 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 LIST_DEL(&appctx->ctx.sess.bref.users);
5072 LIST_INIT(&appctx->ctx.sess.bref.users);
5073 if (appctx->ctx.sess.target != (void *)-1) {
5074 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005075 break;
5076 }
5077 else
5078 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005079 }
5080
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005081 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005082 "%p: proto=%s",
5083 curr_sess,
5084 curr_sess->listener->proto->name);
5085
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005086
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005087 conn = objt_conn(curr_sess->si[0].end);
5088 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005089 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005090 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005091 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005092 " src=%s:%d fe=%s be=%s srv=%s",
5093 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005094 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005095 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005096 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005097 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005098 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005099 break;
5100 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005101 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005102 " src=unix:%d fe=%s be=%s srv=%s",
5103 curr_sess->listener->luid,
5104 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005105 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005106 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005107 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005108 break;
5109 }
5110
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005111 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005112 " ts=%02x age=%s calls=%d",
5113 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005114 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5115 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005116
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005117 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005118 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005119 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005120 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005121 curr_sess->req->analysers,
5122 curr_sess->req->rex ?
5123 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5124 TICKS_TO_MS(1000)) : "");
5125
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005126 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005127 ",wx=%s",
5128 curr_sess->req->wex ?
5129 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5130 TICKS_TO_MS(1000)) : "");
5131
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005132 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005133 ",ax=%s]",
5134 curr_sess->req->analyse_exp ?
5135 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5136 TICKS_TO_MS(1000)) : "");
5137
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005138 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005139 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005140 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005141 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005142 curr_sess->rep->analysers,
5143 curr_sess->rep->rex ?
5144 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5145 TICKS_TO_MS(1000)) : "");
5146
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005147 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005148 ",wx=%s",
5149 curr_sess->rep->wex ?
5150 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5151 TICKS_TO_MS(1000)) : "");
5152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005153 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005154 ",ax=%s]",
5155 curr_sess->rep->analyse_exp ?
5156 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5157 TICKS_TO_MS(1000)) : "");
5158
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005159 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005160 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005161 " s0=[%d,%1xh,fd=%d,ex=%s]",
5162 curr_sess->si[0].state,
5163 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005164 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005165 curr_sess->si[0].exp ?
5166 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5167 TICKS_TO_MS(1000)) : "");
5168
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005169 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005170 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005171 " s1=[%d,%1xh,fd=%d,ex=%s]",
5172 curr_sess->si[1].state,
5173 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005174 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005175 curr_sess->si[1].exp ?
5176 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5177 TICKS_TO_MS(1000)) : "");
5178
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005179 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005180 " exp=%s",
5181 curr_sess->task->expire ?
5182 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5183 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005184 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005185 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005186
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005187 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005188
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005189 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005190 /* let's try again later from this session. We add ourselves into
5191 * this session's users so that it can remove us upon termination.
5192 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005193 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005194 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005195 }
5196
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005197 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005198 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005199 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005200
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005201 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005202 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005203 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005204 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005205 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005206 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005207
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005208 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005209 return 0;
5210
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005211 appctx->ctx.sess.target = NULL;
5212 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005213 return 1;
5214 }
5215
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005216 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005217 /* fall through */
5218
5219 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005220 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005221 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005222 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005223}
5224
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005225/* This is called when the stream interface is closed. For instance, upon an
5226 * external abort, we won't call the i/o handler anymore so we may need to
5227 * remove back references to the session currently being dumped.
5228 */
Simon Horman74d88312013-02-12 10:45:50 +09005229static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005230{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005231 struct appctx *appctx = __objt_appctx(si->end);
5232
5233 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5234 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5235 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005236 }
5237}
5238
Willy Tarreau20e99322013-04-13 09:22:25 +02005239/* This function is used to either dump tables states (when action is set
5240 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005241 * It returns 0 if the output buffer is full and it needs to be called
5242 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005243 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005244static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005245{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005246 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005247 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005248 struct ebmb_node *eb;
5249 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005250 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005251 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005252
5253 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005255 * - STAT_ST_INIT : the first call
5256 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005257 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005258 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005259 * and the entry pointer points to the next entry to be dumped,
5260 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005261 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005262 * data though.
5263 */
5264
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005265 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005266 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005267 if (appctx->st2 == STAT_ST_LIST) {
5268 appctx->ctx.table.entry->ref_cnt--;
5269 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005270 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005271 return 1;
5272 }
5273
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005274 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005276 while (appctx->st2 != STAT_ST_FIN) {
5277 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005278 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005279 appctx->ctx.table.proxy = appctx->ctx.table.target;
5280 if (!appctx->ctx.table.proxy)
5281 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 appctx->ctx.table.entry = NULL;
5284 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005285 break;
5286
Willy Tarreau295a8372011-03-10 11:25:07 +01005287 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005288 if (!appctx->ctx.table.proxy ||
5289 (appctx->ctx.table.target &&
5290 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5291 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005292 break;
5293 }
5294
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005295 if (appctx->ctx.table.proxy->table.size) {
5296 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5297 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005298 return 0;
5299
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005300 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005301 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005302 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005303 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005304 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005305 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5306 appctx->ctx.table.entry->ref_cnt++;
5307 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005308 break;
5309 }
5310 }
5311 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005312 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005313 break;
5314
Willy Tarreau295a8372011-03-10 11:25:07 +01005315 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005316 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005319 /* we're filtering on some data contents */
5320 void *ptr;
5321 long long data;
5322
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005323 dt = appctx->ctx.table.data_type;
5324 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5325 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005326 dt);
5327
5328 data = 0;
5329 switch (stktable_data_types[dt].std_type) {
5330 case STD_T_SINT:
5331 data = stktable_data_cast(ptr, std_t_sint);
5332 break;
5333 case STD_T_UINT:
5334 data = stktable_data_cast(ptr, std_t_uint);
5335 break;
5336 case STD_T_ULL:
5337 data = stktable_data_cast(ptr, std_t_ull);
5338 break;
5339 case STD_T_FRQP:
5340 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005341 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005342 break;
5343 }
5344
5345 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005346 if ((data < appctx->ctx.table.value &&
5347 (appctx->ctx.table.data_op == STD_OP_EQ ||
5348 appctx->ctx.table.data_op == STD_OP_GT ||
5349 appctx->ctx.table.data_op == STD_OP_GE)) ||
5350 (data == appctx->ctx.table.value &&
5351 (appctx->ctx.table.data_op == STD_OP_NE ||
5352 appctx->ctx.table.data_op == STD_OP_GT ||
5353 appctx->ctx.table.data_op == STD_OP_LT)) ||
5354 (data > appctx->ctx.table.value &&
5355 (appctx->ctx.table.data_op == STD_OP_EQ ||
5356 appctx->ctx.table.data_op == STD_OP_LT ||
5357 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005358 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005359 }
5360
Simon Hormanc88b8872011-06-15 15:18:49 +09005361 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005362 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5363 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005364 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005365
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005366 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005367
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005368 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005369 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005370 struct stksess *old = appctx->ctx.table.entry;
5371 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005372 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005373 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5374 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5375 stksess_kill(&appctx->ctx.table.proxy->table, old);
5376 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005377 break;
5378 }
5379
Simon Hormanc88b8872011-06-15 15:18:49 +09005380
5381 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005382 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5383 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5384 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005386 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5387 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005388 break;
5389
Willy Tarreau295a8372011-03-10 11:25:07 +01005390 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005391 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005392 break;
5393 }
5394 }
5395 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005396}
5397
Willy Tarreaud426a182010-03-05 14:58:26 +01005398/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005399 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5400 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5401 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5402 * lines are respected within the limit of 70 output chars. Lines that are
5403 * continuation of a previous truncated line begin with "+" instead of " "
5404 * after the offset. The new pointer is returned.
5405 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005406static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5407 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005408{
5409 int end;
5410 unsigned char c;
5411
5412 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005413 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005414 return ptr;
5415
Willy Tarreau77804732012-10-29 16:14:26 +01005416 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005417
Willy Tarreaud426a182010-03-05 14:58:26 +01005418 while (ptr < len && ptr < bsize) {
5419 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005420 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005421 if (out->len > end - 2)
5422 break;
5423 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005424 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005425 if (out->len > end - 3)
5426 break;
5427 out->str[out->len++] = '\\';
5428 switch (c) {
5429 case '\t': c = 't'; break;
5430 case '\n': c = 'n'; break;
5431 case '\r': c = 'r'; break;
5432 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005433 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005434 }
5435 out->str[out->len++] = c;
5436 } else {
5437 if (out->len > end - 5)
5438 break;
5439 out->str[out->len++] = '\\';
5440 out->str[out->len++] = 'x';
5441 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5442 out->str[out->len++] = hextab[c & 0xF];
5443 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005444 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005445 /* we had a line break, let's return now */
5446 out->str[out->len++] = '\n';
5447 *line = ptr;
5448 return ptr;
5449 }
5450 }
5451 /* we have an incomplete line, we return it as-is */
5452 out->str[out->len++] = '\n';
5453 return ptr;
5454}
5455
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005456/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005457 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005458 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005459 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005460static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005461{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005462 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005463 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005464
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005465 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005466 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005467
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005468 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005469
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005470 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005471 /* the function had not been called yet, let's prepare the
5472 * buffer for a response.
5473 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005474 struct tm tm;
5475
5476 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005477 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005478 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5479 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5480 error_snapshot_id);
5481
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005482 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005483 /* Socket buffer full. Let's try again later from the same point */
5484 return 0;
5485 }
5486
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005487 appctx->ctx.errors.px = proxy;
5488 appctx->ctx.errors.buf = 0;
5489 appctx->ctx.errors.bol = 0;
5490 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005491 }
5492
5493 /* we have two inner loops here, one for the proxy, the other one for
5494 * the buffer.
5495 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005496 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005497 struct error_snapshot *es;
5498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005499 if (appctx->ctx.errors.buf == 0)
5500 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005501 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005502 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005503
5504 if (!es->when.tv_sec)
5505 goto next;
5506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005507 if (appctx->ctx.errors.iid >= 0 &&
5508 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5509 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005510 goto next;
5511
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005512 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005513 /* just print headers now */
5514
5515 char pn[INET6_ADDRSTRLEN];
5516 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005517 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005518
5519 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005520 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005521 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005522 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005523
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005524 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5525 case AF_INET:
5526 case AF_INET6:
5527 port = get_host_port(&es->src);
5528 break;
5529 default:
5530 port = 0;
5531 }
5532
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005533 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005534 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005535 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005536 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005537 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005538 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005539 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5540 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005541 break;
5542 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005543 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005544 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005545 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005546 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005547 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005548 break;
5549 }
5550
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005551 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005552 ", server %s (#%d), event #%u\n"
5553 " src %s:%d, session #%d, session flags 0x%08x\n"
5554 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5555 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5556 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5557 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5558 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5559 es->ev_id,
5560 pn, port, es->sid, es->s_flags,
5561 es->state, es->m_flags, es->t_flags,
5562 es->m_clen, es->m_blen,
5563 es->b_flags, es->b_out, es->b_tot,
5564 es->len, es->b_wrap, es->pos);
5565
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005566 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005567 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005568 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005569 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005570 appctx->ctx.errors.ptr = 0;
5571 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005572 }
5573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005575 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005576 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005577 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005578 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005579 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005580 goto next;
5581 }
5582
5583 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005584 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005585 int newptr;
5586 int newline;
5587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005588 newline = appctx->ctx.errors.bol;
5589 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5590 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005591 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005592
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005593 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005594 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005595 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005596 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005597 appctx->ctx.errors.ptr = newptr;
5598 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005599 };
5600 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005601 appctx->ctx.errors.bol = 0;
5602 appctx->ctx.errors.ptr = -1;
5603 appctx->ctx.errors.buf++;
5604 if (appctx->ctx.errors.buf > 1) {
5605 appctx->ctx.errors.buf = 0;
5606 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005607 }
5608 }
5609
5610 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005611 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005612}
5613
Willy Tarreaud5781202012-09-22 19:32:35 +02005614/* parse the "level" argument on the bind lines */
5615static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5616{
5617 if (!*args[cur_arg + 1]) {
5618 memprintf(err, "'%s' : missing level", args[cur_arg]);
5619 return ERR_ALERT | ERR_FATAL;
5620 }
5621
5622 if (!strcmp(args[cur_arg+1], "user"))
5623 conf->level = ACCESS_LVL_USER;
5624 else if (!strcmp(args[cur_arg+1], "operator"))
5625 conf->level = ACCESS_LVL_OPER;
5626 else if (!strcmp(args[cur_arg+1], "admin"))
5627 conf->level = ACCESS_LVL_ADMIN;
5628 else {
5629 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5630 args[cur_arg], args[cur_arg+1]);
5631 return ERR_ALERT | ERR_FATAL;
5632 }
5633
5634 return 0;
5635}
5636
Willy Tarreaub24281b2011-02-13 13:16:36 +01005637struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005638 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005639 .name = "<STATS>", /* used for logging */
5640 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005641 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005642};
5643
Simon Horman9bd2c732011-06-15 15:18:44 +09005644static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005645 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005646 .name = "<CLI>", /* used for logging */
5647 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005648 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005649};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005650
Willy Tarreaudc13c112013-06-21 23:16:39 +02005651static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005652 { CFG_GLOBAL, "stats", stats_parse_global },
5653 { 0, NULL, NULL },
5654}};
5655
Willy Tarreaud5781202012-09-22 19:32:35 +02005656static struct bind_kw_list bind_kws = { "STAT", { }, {
5657 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5658 { NULL, NULL, 0 },
5659}};
5660
Willy Tarreau10522fd2008-07-09 20:12:41 +02005661__attribute__((constructor))
5662static void __dumpstats_module_init(void)
5663{
5664 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005665 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005666}
5667
Willy Tarreau91861262007-10-17 17:06:05 +02005668/*
5669 * Local variables:
5670 * c-indent-level: 8
5671 * c-basic-offset: 8
5672 * End:
5673 */