blob: 3adfdf3cf4cff926daee363d6cfafe6447c43d3a [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 */
Willy Tarreau12833bb2014-01-28 16:49:56 +010084 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010085};
86
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010087static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +010088static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010089static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090090static int stats_dump_sess_to_buffer(struct stream_interface *si);
91static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010092static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010093static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
94static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010095static int stats_maps_list(struct stream_interface *si);
96static int stats_map_list(struct stream_interface *si);
97static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +090098
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010099/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100100 * cli_io_handler()
101 * -> stats_dump_sess_to_buffer() // "show sess"
102 * -> stats_dump_errors_to_buffer() // "show errors"
103 * -> stats_dump_info_to_buffer() // "show info"
104 * -> stats_dump_stat_to_buffer() // "show stat"
105 * -> stats_dump_csv_header()
106 * -> stats_dump_proxy_to_buffer()
107 * -> stats_dump_fe_stats()
108 * -> stats_dump_li_stats()
109 * -> stats_dump_sv_stats()
110 * -> stats_dump_be_stats()
111 *
112 * http_stats_io_handler()
113 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
114 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
115 * -> stats_dump_html_head() // emits the HTML headers
116 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
117 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
118 * -> stats_dump_html_px_hdr()
119 * -> stats_dump_fe_stats()
120 * -> stats_dump_li_stats()
121 * -> stats_dump_sv_stats()
122 * -> stats_dump_be_stats()
123 * -> stats_dump_html_px_end()
124 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100125 */
126
Simon Horman9bd2c732011-06-15 15:18:44 +0900127static struct si_applet cli_applet;
128
129static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200130 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200131 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200132 " clear table : remove an entry from a table\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100133 " clear map [id] : clear the content of this map\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200134 " help : this message\n"
135 " prompt : toggle interactive mode with prompt\n"
136 " quit : disconnect\n"
137 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100138 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200139 " show stat : report counters for each proxy and server\n"
140 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100141 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200142 " show table [id]: report table usage stats or dump this table's contents\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100143 " show map [id] : report avalaible maps or dump this map's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200144 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200145 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200146 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100147 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200148 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200149 " set rate-limit : change a rate limiting value\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100150 " set map [id] [key] [value] : modify map entry\n"
151 " add map [id] [key] [value] : add map entry\n"
152 " del map [id] [key] : delete map entry\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200153 " disable : put a server or frontend in maintenance mode\n"
154 " enable : re-enable a server or frontend which is in maintenance mode\n"
155 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200156 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200157
Simon Horman9bd2c732011-06-15 15:18:44 +0900158static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200159 "Permission denied\n"
160 "";
161
Willy Tarreau295a8372011-03-10 11:25:07 +0100162/* data transmission states for the stats responses */
163enum {
164 STAT_ST_INIT = 0,
165 STAT_ST_HEAD,
166 STAT_ST_INFO,
167 STAT_ST_LIST,
168 STAT_ST_END,
169 STAT_ST_FIN,
170};
171
172/* data transmission states for the stats responses inside a proxy */
173enum {
174 STAT_PX_ST_INIT = 0,
175 STAT_PX_ST_TH,
176 STAT_PX_ST_FE,
177 STAT_PX_ST_LI,
178 STAT_PX_ST_SV,
179 STAT_PX_ST_BE,
180 STAT_PX_ST_END,
181 STAT_PX_ST_FIN,
182};
183
Cyril Bonté19979e12012-04-04 12:57:21 +0200184extern const char *stat_status_codes[];
185
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200186/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200187 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200188 * needs to be closed and ignored, or a negative value upon critical failure.
189 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900190static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200191{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100192 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100193 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200194
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200195 tv_zero(&s->logs.tv_request);
196 s->logs.t_queue = 0;
197 s->logs.t_connect = 0;
198 s->logs.t_data = 0;
199 s->logs.t_close = 0;
200 s->logs.bytes_in = s->logs.bytes_out = 0;
201 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
202 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200203
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200204 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200205
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200206 if (s->listener->timeout) {
207 s->req->rto = *s->listener->timeout;
208 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200209 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200210 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200211}
212
Willy Tarreau07e9e642010-08-17 21:48:17 +0200213/* allocate a new stats frontend named <name>, and return it
214 * (or NULL in case of lack of memory).
215 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200216static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200217{
218 struct proxy *fe;
219
220 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
221 if (!fe)
222 return NULL;
223
Willy Tarreau237250c2011-07-29 01:49:03 +0200224 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200225 fe->next = proxy;
226 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200227 fe->last_change = now.tv_sec;
228 fe->id = strdup("GLOBAL");
229 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200230 fe->maxconn = 10; /* default to 10 concurrent connections */
231 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200232 fe->conf.file = strdup(file);
233 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200234 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200235
236 /* the stats frontend is the only one able to assign ID #0 */
237 fe->conf.id.key = fe->uuid = 0;
238 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200239 return fe;
240}
241
Willy Tarreaufbee7132007-10-18 13:53:22 +0200242/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200243 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
244 * error message into the <err> buffer which will be preallocated. The trailing
245 * '\n' must not be written. The function must be called with <args> pointing to
246 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200247 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200248static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200249 struct proxy *defpx, const char *file, int line,
250 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200251{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200252 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200253 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200254
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200255 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200256 int cur_arg;
257
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200258 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200259 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 +0200260 return -1;
261 }
262
Willy Tarreau89a63132009-08-16 17:41:45 +0200263 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200264 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200265 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200266 return -1;
267 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200268 }
269
Willy Tarreau4348fad2012-09-20 16:48:07 +0200270 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200271 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200272
Willy Tarreauc53d4222012-09-20 20:19:28 +0200273 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
274 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
275 file, line, args[0], args[1], err && *err ? *err : "error");
276 return -1;
277 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200279 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200281 static int bind_dumped;
282 struct bind_kw *kw;
283
284 kw = bind_find_kw(args[cur_arg]);
285 if (kw) {
286 if (!kw->parse) {
287 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
288 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200289 return -1;
290 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200291
292 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
293 if (err && *err)
294 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
295 else
296 memprintf(err, "'%s %s' : error encountered while processing '%s'",
297 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200298 return -1;
299 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200300
301 cur_arg += 1 + kw->skip;
302 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200303 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200304
305 if (!bind_dumped) {
306 bind_dump_kws(err);
307 indent_msg(err, 4);
308 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200310
311 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
312 args[0], args[1], args[cur_arg],
313 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
314 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200315 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100316
Willy Tarreauc53d4222012-09-20 20:19:28 +0200317 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
318 l->maxconn = global.stats_fe->maxconn;
319 l->backlog = global.stats_fe->backlog;
320 l->timeout = &global.stats_fe->timeout.client;
321 l->accept = session_accept;
322 l->handler = process_session;
323 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
324 l->nice = -64; /* we want to boost priority for local stats */
325 global.maxsock += l->maxconn;
326 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200327 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200328 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100329 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200330 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100331
332 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200333 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100334 return -1;
335 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200336
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100337 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200338 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200339 return -1;
340 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200341 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200342 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200343 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200344 return -1;
345 }
346 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200347 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200348 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200349 else if (!strcmp(args[1], "maxconn")) {
350 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200351
352 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200353 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200354 return -1;
355 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200356
357 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200358 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200359 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200360 return -1;
361 }
362 }
363 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200364 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200365 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
366 int cur_arg = 2;
367 unsigned int set = 0;
368
Willy Tarreau91319572013-04-20 09:48:50 +0200369 if (!global.stats_fe) {
370 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
371 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
372 return -1;
373 }
374 }
375
Willy Tarreau35b7b162012-10-22 23:17:18 +0200376 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100377 unsigned int low, high;
378
Willy Tarreau35b7b162012-10-22 23:17:18 +0200379 if (strcmp(args[cur_arg], "all") == 0) {
380 set = 0;
381 break;
382 }
383 else if (strcmp(args[cur_arg], "odd") == 0) {
384 set |= 0x55555555;
385 }
386 else if (strcmp(args[cur_arg], "even") == 0) {
387 set |= 0xAAAAAAAA;
388 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100389 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100390 char *dash = strchr(args[cur_arg], '-');
391
392 low = high = str2uic(args[cur_arg]);
393 if (dash)
394 high = str2uic(dash + 1);
395
396 if (high < low) {
397 unsigned int swap = low;
398 low = high;
399 high = swap;
400 }
401
402 if (low < 1 || high > 32) {
403 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
404 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200405 return -1;
406 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100407
408 while (low <= high)
409 set |= 1 << (low++ - 1);
410 }
411 else {
412 memprintf(err,
413 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
414 args[0], args[1]);
415 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200416 }
417 cur_arg++;
418 }
419 global.stats_fe->bind_proc = set;
420 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200421 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200422 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200423 return -1;
424 }
425 return 0;
426}
427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100428/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
429 * for clearing it if needed.
430 */
431static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100432{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100433 chunk_appendf(&trash,
434 "# pxname,svname,"
435 "qcur,qmax,"
436 "scur,smax,slim,stot,"
437 "bin,bout,"
438 "dreq,dresp,"
439 "ereq,econ,eresp,"
440 "wretr,wredis,"
441 "status,weight,act,bck,"
442 "chkfail,chkdown,lastchg,downtime,qlimit,"
443 "pid,iid,sid,throttle,lbtot,tracked,type,"
444 "rate,rate_lim,rate_max,"
445 "check_status,check_code,check_duration,"
446 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
447 "req_rate,req_rate_max,req_tot,"
448 "cli_abrt,srv_abrt,"
449 "comp_in,comp_out,comp_byp,comp_rsp,"
450 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100451}
452
Simon Hormand9366582011-06-15 15:18:45 +0900453/* print a string of text buffer to <out>. The format is :
454 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
455 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
456 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
457 */
458static int dump_text(struct chunk *out, const char *buf, int bsize)
459{
460 unsigned char c;
461 int ptr = 0;
462
463 while (buf[ptr] && ptr < bsize) {
464 c = buf[ptr];
465 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
466 if (out->len > out->size - 1)
467 break;
468 out->str[out->len++] = c;
469 }
470 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
471 if (out->len > out->size - 2)
472 break;
473 out->str[out->len++] = '\\';
474 switch (c) {
475 case ' ': c = ' '; break;
476 case '\t': c = 't'; break;
477 case '\n': c = 'n'; break;
478 case '\r': c = 'r'; break;
479 case '\e': c = 'e'; break;
480 case '\\': c = '\\'; break;
481 }
482 out->str[out->len++] = c;
483 }
484 else {
485 if (out->len > out->size - 4)
486 break;
487 out->str[out->len++] = '\\';
488 out->str[out->len++] = 'x';
489 out->str[out->len++] = hextab[(c >> 4) & 0xF];
490 out->str[out->len++] = hextab[c & 0xF];
491 }
492 ptr++;
493 }
494
495 return ptr;
496}
497
498/* print a buffer in hexa.
499 * Print stopped if <bsize> is reached, or if no more place in the chunk.
500 */
501static int dump_binary(struct chunk *out, const char *buf, int bsize)
502{
503 unsigned char c;
504 int ptr = 0;
505
506 while (ptr < bsize) {
507 c = buf[ptr];
508
509 if (out->len > out->size - 2)
510 break;
511 out->str[out->len++] = hextab[(c >> 4) & 0xF];
512 out->str[out->len++] = hextab[c & 0xF];
513
514 ptr++;
515 }
516 return ptr;
517}
518
519/* Dump the status of a table to a stream interface's
520 * read buffer. It returns 0 if the output buffer is full
521 * and needs to be called again, otherwise non-zero.
522 */
523static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
524 struct proxy *proxy, struct proxy *target)
525{
Willy Tarreau306f8302013-07-08 15:53:06 +0200526 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900527
Willy Tarreau77804732012-10-29 16:14:26 +0100528 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900529 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
530
531 /* any other information should be dumped here */
532
Willy Tarreau290e63a2012-09-20 18:07:14 +0200533 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900535
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200536 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900537 return 0;
538
539 return 1;
540}
541
542/* Dump the a table entry to a stream interface's
543 * read buffer. It returns 0 if the output buffer is full
544 * and needs to be called again, otherwise non-zero.
545 */
546static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
547 struct proxy *proxy, struct stksess *entry)
548{
549 int dt;
550
Willy Tarreau77804732012-10-29 16:14:26 +0100551 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900552
553 if (proxy->table.type == STKTABLE_TYPE_IP) {
554 char addr[INET_ADDRSTRLEN];
555 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900557 }
558 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
559 char addr[INET6_ADDRSTRLEN];
560 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100561 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900562 }
563 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100564 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900565 }
566 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100567 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900568 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
569 }
570 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100571 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900572 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
573 }
574
Willy Tarreau77804732012-10-29 16:14:26 +0100575 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900576
577 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
578 void *ptr;
579
580 if (proxy->table.data_ofs[dt] == 0)
581 continue;
582 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100583 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900584 else
Willy Tarreau77804732012-10-29 16:14:26 +0100585 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900586
587 ptr = stktable_data_ptr(&proxy->table, entry, dt);
588 switch (stktable_data_types[dt].std_type) {
589 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100590 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900591 break;
592 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100593 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900594 break;
595 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100596 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900597 break;
598 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100599 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900600 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
601 proxy->table.data_arg[dt].u));
602 break;
603 }
604 }
Willy Tarreau77804732012-10-29 16:14:26 +0100605 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900606
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200607 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900608 return 0;
609
610 return 1;
611}
612
Willy Tarreaudec98142012-06-06 23:37:08 +0200613static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900614{
Willy Tarreau306f8302013-07-08 15:53:06 +0200615 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100616 struct appctx *appctx = __objt_appctx(si->end);
617 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900618 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900619 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900620 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200621 long long value;
622 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200623 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200624 void *ptr;
625 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900626
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100627 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900628
629 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100630 appctx->ctx.cli.msg = "Key value expected\n";
631 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900632 return;
633 }
634
Simon Hormanc5b89f62011-06-15 15:18:50 +0900635 switch (px->table.type) {
636 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900637 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100638 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900639 break;
640 case STKTABLE_TYPE_IPV6:
641 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100642 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900643 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900644 case STKTABLE_TYPE_INTEGER:
645 {
646 char *endptr;
647 unsigned long val;
648 errno = 0;
649 val = strtoul(args[4], &endptr, 10);
650 if ((errno == ERANGE && val == ULONG_MAX) ||
651 (errno != 0 && val == 0) || endptr == args[4] ||
652 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100653 appctx->ctx.cli.msg = "Invalid key\n";
654 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900655 return;
656 }
657 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100658 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900659 break;
660 }
661 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900662 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100663 static_table_key->key = args[4];
664 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900665 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900666 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200667 switch (action) {
668 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100669 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 +0200670 break;
671 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100672 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 +0200673 break;
674 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100675 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200676 break;
677 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100678 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900679 return;
680 }
681
682 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200683 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100684 appctx->ctx.cli.msg = stats_permission_denied_msg;
685 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900686 return;
687 }
688
Willy Tarreau07115412012-10-29 21:56:59 +0100689 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900690
Willy Tarreaudec98142012-06-06 23:37:08 +0200691 switch (action) {
692 case STAT_CLI_O_TAB:
693 if (!ts)
694 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100695 chunk_reset(&trash);
696 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900697 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100698 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900699 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200700
701 case STAT_CLI_O_CLR:
702 if (!ts)
703 return;
704 if (ts->ref_cnt) {
705 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100706 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
707 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200708 return;
709 }
710 stksess_kill(&px->table, ts);
711 break;
Simon Horman17bce342011-06-15 15:18:47 +0900712
Willy Tarreau654694e2012-06-07 01:03:16 +0200713 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200714 if (ts)
715 stktable_touch(&px->table, ts, 1);
716 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100717 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200718 if (!ts) {
719 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100720 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
721 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200722 return;
723 }
724 stktable_store(&px->table, ts, 1);
725 }
726
Willy Tarreau47060b62013-08-01 21:11:42 +0200727 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
728 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100729 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
730 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200731 return;
732 }
733
734 data_type = stktable_get_data_type(args[cur_arg] + 5);
735 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100736 appctx->ctx.cli.msg = "Unknown data type\n";
737 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200738 return;
739 }
740
741 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100742 appctx->ctx.cli.msg = "Data type not stored in this table\n";
743 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200744 return;
745 }
746
747 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100748 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
749 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200750 return;
751 }
752
753 ptr = stktable_data_ptr(&px->table, ts, data_type);
754
755 switch (stktable_data_types[data_type].std_type) {
756 case STD_T_SINT:
757 stktable_data_cast(ptr, std_t_sint) = value;
758 break;
759 case STD_T_UINT:
760 stktable_data_cast(ptr, std_t_uint) = value;
761 break;
762 case STD_T_ULL:
763 stktable_data_cast(ptr, std_t_ull) = value;
764 break;
765 case STD_T_FRQP:
766 /* We set both the current and previous values. That way
767 * the reported frequency is stable during all the period
768 * then slowly fades out. This allows external tools to
769 * push measures without having to update them too often.
770 */
771 frqp = &stktable_data_cast(ptr, std_t_frqp);
772 frqp->curr_tick = now_ms;
773 frqp->prev_ctr = 0;
774 frqp->curr_ctr = value;
775 break;
776 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200777 }
778 break;
779
Willy Tarreaudec98142012-06-06 23:37:08 +0200780 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100781 appctx->ctx.cli.msg = "Unknown action\n";
782 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200783 break;
Simon Horman121f3052011-06-15 15:18:46 +0900784 }
Simon Horman121f3052011-06-15 15:18:46 +0900785}
786
Willy Tarreau654694e2012-06-07 01:03:16 +0200787static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900788{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100789 struct appctx *appctx = __objt_appctx(si->end);
790
Willy Tarreau04b3a192013-04-13 09:41:37 +0200791 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100792 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
793 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200794 return;
795 }
796
Simon Hormand5b9fd92011-06-15 15:18:48 +0900797 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100798 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
799 if (appctx->ctx.table.data_type < 0) {
800 appctx->ctx.cli.msg = "Unknown data type\n";
801 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900802 return;
803 }
804
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100805 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
806 appctx->ctx.cli.msg = "Data type not stored in this table\n";
807 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900808 return;
809 }
810
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100811 appctx->ctx.table.data_op = get_std_op(args[4]);
812 if (appctx->ctx.table.data_op < 0) {
813 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
814 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900815 return;
816 }
817
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100818 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
819 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
820 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900821 return;
822 }
823}
824
Willy Tarreaudec98142012-06-06 23:37:08 +0200825static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900826{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100827 struct appctx *appctx = __objt_appctx(si->end);
828
829 appctx->ctx.table.data_type = -1;
830 appctx->st2 = STAT_ST_INIT;
831 appctx->ctx.table.target = NULL;
832 appctx->ctx.table.proxy = NULL;
833 appctx->ctx.table.entry = NULL;
834 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900835
836 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100837 appctx->ctx.table.target = find_stktable(args[2]);
838 if (!appctx->ctx.table.target) {
839 appctx->ctx.cli.msg = "No such table\n";
840 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900841 return;
842 }
843 }
844 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200845 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900846 goto err_args;
847 return;
848 }
849
850 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200851 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900852 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200853 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900854 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900855 goto err_args;
856
857 return;
858
859err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200860 switch (action) {
861 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100862 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 +0200863 break;
864 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100865 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 +0200866 break;
867 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100868 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200869 break;
870 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100871 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900872}
873
Willy Tarreau532a4502011-09-07 22:37:44 +0200874/* Expects to find a frontend named <arg> and returns it, otherwise displays various
875 * adequate error messages and returns NULL. This function also expects the session
876 * level to be admin.
877 */
878static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
879{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100880 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200881 struct proxy *px;
882
Willy Tarreau290e63a2012-09-20 18:07:14 +0200883 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100884 appctx->ctx.cli.msg = stats_permission_denied_msg;
885 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200886 return NULL;
887 }
888
889 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100890 appctx->ctx.cli.msg = "A frontend name is expected.\n";
891 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200892 return NULL;
893 }
894
895 px = findproxy(arg, PR_CAP_FE);
896 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->ctx.cli.msg = "No such frontend.\n";
898 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200899 return NULL;
900 }
901 return px;
902}
903
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200904/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
905 * and returns the pointer to the server. Otherwise, display adequate error messages
906 * and returns NULL. This function also expects the session level to be admin. Note:
907 * the <arg> is modified to remove the '/'.
908 */
909static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
910{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200912 struct proxy *px;
913 struct server *sv;
914 char *line;
915
Willy Tarreau290e63a2012-09-20 18:07:14 +0200916 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->ctx.cli.msg = stats_permission_denied_msg;
918 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200919 return NULL;
920 }
921
922 /* split "backend/server" and make <line> point to server */
923 for (line = arg; *line; line++)
924 if (*line == '/') {
925 *line++ = '\0';
926 break;
927 }
928
929 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100930 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
931 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200932 return NULL;
933 }
934
935 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100936 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
937 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200938 return NULL;
939 }
940
941 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100942 appctx->ctx.cli.msg = "Proxy is disabled.\n";
943 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200944 return NULL;
945 }
946
947 return sv;
948}
949
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100950/* This function is used with map management. It permits to browse each
951 * really allocated descriptors of one map reference. The variable
952 * <appctx->ctx.map.ref> must contain the map reference to browse.
953 * The variable <appctx->ctx.map.desc> contain the descriptor of the
954 * current allocated map descriptor. This variable must be initialized
955 * to NULL.
956 */
957static inline void stats_map_lookup_next(struct stream_interface *si)
958{
959 struct appctx *appctx = __objt_appctx(si->end);
960
961 /* search the next allocated map */
962 while (1) {
963 /* get next descriptor */
964 if (!appctx->ctx.map.desc)
965 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.ref->maps,
966 struct map_descriptor *, list);
967 else
968 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.desc->list,
969 struct map_descriptor *, list);
970
971 /* detect end of list */
972 if (&appctx->ctx.map.desc->list == &appctx->ctx.map.ref->maps) {
973 appctx->ctx.map.desc = NULL;
974 return;
975 }
976
977 /* do not lookup this entry */
978 if (!appctx->ctx.map.desc->do_free)
979 continue;
980
981 /* avalaible descriptor */
982 return;
983 }
984}
985
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200986/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200987 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100988 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200989 * designating the function which will have to process the request, which can
990 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200991 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900992static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200993{
Willy Tarreau306f8302013-07-08 15:53:06 +0200994 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100995 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200996 char *args[MAX_STATS_ARGS + 1];
997 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100998 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200999
1000 while (isspace((unsigned char)*line))
1001 line++;
1002
1003 arg = 0;
1004 args[arg] = line;
1005
1006 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001007 if (*line == '\\') {
1008 line++;
1009 if (*line == '\0')
1010 break;
1011 }
1012 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001013 *line++ = '\0';
1014
1015 while (isspace((unsigned char)*line))
1016 line++;
1017
1018 args[++arg] = line;
1019 continue;
1020 }
1021
1022 line++;
1023 }
1024
1025 while (++arg <= MAX_STATS_ARGS)
1026 args[arg] = line;
1027
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001028 /* remove \ */
1029 arg = 0;
1030 while (*args[arg] != '\0') {
1031 j = 0;
1032 for (i=0; args[arg][i] != '\0'; i++) {
1033 if (args[arg][i] == '\\')
1034 continue;
1035 args[arg][j] = args[arg][i];
1036 j++;
1037 }
1038 args[arg][j] = '\0';
1039 arg++;
1040 }
1041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001042 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001043 if (strcmp(args[0], "show") == 0) {
1044 if (strcmp(args[1], "stat") == 0) {
1045 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001046 appctx->ctx.stats.flags |= STAT_BOUND;
1047 appctx->ctx.stats.iid = atoi(args[2]);
1048 appctx->ctx.stats.type = atoi(args[3]);
1049 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001050 }
1051
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001052 appctx->st2 = STAT_ST_INIT;
1053 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001054 }
1055 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001056 appctx->st2 = STAT_ST_INIT;
1057 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001058 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001059 else if (strcmp(args[1], "pools") == 0) {
1060 appctx->st2 = STAT_ST_INIT;
1061 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1062 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001063 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001064 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001065 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001066 appctx->ctx.cli.msg = stats_permission_denied_msg;
1067 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001068 return 1;
1069 }
Willy Tarreau76153662012-11-26 01:16:39 +01001070 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001071 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001072 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001073 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001074 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001075 appctx->ctx.sess.target = NULL;
1076 appctx->ctx.sess.section = 0; /* start with session status */
1077 appctx->ctx.sess.pos = 0;
1078 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001079 }
1080 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001081 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001082 appctx->ctx.cli.msg = stats_permission_denied_msg;
1083 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001084 return 1;
1085 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001086 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001087 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001088 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 appctx->ctx.errors.iid = -1;
1090 appctx->ctx.errors.px = NULL;
1091 appctx->st2 = STAT_ST_INIT;
1092 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001093 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001094 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001095 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001096 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001097 else if (strcmp(args[1], "map") == 0) {
1098
1099 /* no parameter: display all map avalaible */
1100 if (!*args[2]) {
1101 appctx->st2 = STAT_ST_INIT;
1102 appctx->st0 = STAT_CLI_O_MAPS;
1103 return 1;
1104 }
1105
1106 /* lookup into the maps */
1107 appctx->ctx.map.ref = map_get_reference(args[2]);
1108 if (!appctx->ctx.map.ref) {
1109 appctx->ctx.cli.msg = "Unknown map reference.\n";
1110 appctx->st0 = STAT_CLI_PRINT;
1111 return 1;
1112 }
1113 appctx->st2 = STAT_ST_INIT;
1114 appctx->st0 = STAT_CLI_O_MAP;
1115 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001116 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001117 return 0;
1118 }
1119 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001120 else if (strcmp(args[0], "clear") == 0) {
1121 if (strcmp(args[1], "counters") == 0) {
1122 struct proxy *px;
1123 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001124 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001125 int clrall = 0;
1126
1127 if (strcmp(args[2], "all") == 0)
1128 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001129
Willy Tarreau6162db22009-10-10 17:13:00 +02001130 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001131 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1132 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = stats_permission_denied_msg;
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001135 return 1;
1136 }
1137
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001138 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001139 if (clrall) {
1140 memset(&px->be_counters, 0, sizeof(px->be_counters));
1141 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1142 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001143 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001144 px->be_counters.conn_max = 0;
1145 px->be_counters.p.http.rps_max = 0;
1146 px->be_counters.sps_max = 0;
1147 px->be_counters.cps_max = 0;
1148 px->be_counters.nbpend_max = 0;
1149
1150 px->fe_counters.conn_max = 0;
1151 px->fe_counters.p.http.rps_max = 0;
1152 px->fe_counters.sps_max = 0;
1153 px->fe_counters.cps_max = 0;
1154 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001155 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001156
1157 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001158 if (clrall)
1159 memset(&sv->counters, 0, sizeof(sv->counters));
1160 else {
1161 sv->counters.cur_sess_max = 0;
1162 sv->counters.nbpend_max = 0;
1163 sv->counters.sps_max = 0;
1164 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001165
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001166 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001167 if (li->counters) {
1168 if (clrall)
1169 memset(li->counters, 0, sizeof(*li->counters));
1170 else
1171 li->counters->conn_max = 0;
1172 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001173 }
1174
Willy Tarreau81c25d02011-09-07 15:17:21 +02001175 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001176 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001177 return 1;
1178 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001179 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001180 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001181 /* end of processing */
1182 return 1;
1183 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001184 else if (strcmp(args[1], "map") == 0) {
1185 struct map_reference *mref;
1186 struct map_descriptor *mdesc;
1187 struct map_entry *ent, *nent;
1188
1189 /* no parameter */
1190 if (!*args[2]) {
1191 appctx->ctx.cli.msg = "Expect map reference.\n";
1192 appctx->st0 = STAT_CLI_PRINT;
1193 return 1;
1194 }
1195
1196 /* lookup into the maps */
1197 mref = map_get_reference(args[2]);
1198 if (!mref) {
1199 appctx->ctx.cli.msg = "Unknown map reference.\n";
1200 appctx->st0 = STAT_CLI_PRINT;
1201 return 1;
1202 }
1203
1204 /* clear all maps */
1205 list_for_each_entry(mdesc, &mref->maps, list)
1206 if (mdesc->do_free)
1207 pattern_prune_expr(mdesc->pat);
1208
1209 /* clear map reference */
1210 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1211 LIST_DEL(&ent->list);
1212 free(ent->key);
1213 free(ent->value);
1214 free(ent);
1215 }
1216
1217 /* return response */
1218 appctx->ctx.cli.msg = "Done.\n";
1219 appctx->st0 = STAT_CLI_PRINT;
1220 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001221 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001222 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001223 return 0;
1224 }
1225 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001226 else if (strcmp(args[0], "get") == 0) {
1227 if (strcmp(args[1], "weight") == 0) {
1228 struct proxy *px;
1229 struct server *sv;
1230
1231 /* split "backend/server" and make <line> point to server */
1232 for (line = args[2]; *line; line++)
1233 if (*line == '/') {
1234 *line++ = '\0';
1235 break;
1236 }
1237
1238 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001239 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1240 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001241 return 1;
1242 }
1243
1244 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001245 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1246 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001247 return 1;
1248 }
1249
1250 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001251 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1252 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001253 return 1;
1254 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001255 else if (strcmp(args[1], "map") == 0) {
1256
1257 /* no parameter */
1258 if (!*args[2] || !*args[3]) {
1259 appctx->ctx.cli.msg = "Expect map reference and required key.\n";
1260 appctx->st0 = STAT_CLI_PRINT;
1261 return 1;
1262 }
1263
1264 /* lookup into the maps */
1265 appctx->ctx.map.ref = map_get_reference(args[2]);
1266 if (!appctx->ctx.map.ref) {
1267 appctx->ctx.cli.msg = "Unknown map reference.\n";
1268 appctx->st0 = STAT_CLI_PRINT;
1269 return 1;
1270 }
1271
1272 /* copy input string */
1273 appctx->ctx.map.chunk.len = strlen(args[3]);
1274 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1275 appctx->ctx.map.chunk.str = strdup(args[3]);
1276 if (!appctx->ctx.map.chunk.str) {
1277 appctx->ctx.cli.msg = "Out of memory error.\n";
1278 appctx->st0 = STAT_CLI_PRINT;
1279 return 1;
1280 }
1281
1282 /* prepare response */
1283 appctx->st2 = STAT_ST_INIT;
1284 appctx->st0 = STAT_CLI_O_MLOOK;
1285 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001286 else { /* not "get weight" */
1287 return 0;
1288 }
1289 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001290 else if (strcmp(args[0], "set") == 0) {
1291 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001292 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001293 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001294
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001295 sv = expect_server_admin(s, si, args[2]);
1296 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001297 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001298
Simon Horman7d09b9a2013-02-12 10:45:51 +09001299 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001300 /*
1301 * The user-weight may now be zero and thus
1302 * the server considered to be draining.
1303 * Update the server's drain state as necessary.
1304 */
1305 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001306 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001307 appctx->ctx.cli.msg = warning;
1308 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001309 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001310 return 1;
1311 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001312 else if (strcmp(args[1], "timeout") == 0) {
1313 if (strcmp(args[2], "cli") == 0) {
1314 unsigned timeout;
1315 const char *res;
1316
1317 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001318 appctx->ctx.cli.msg = "Expects an integer value.\n";
1319 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001320 return 1;
1321 }
1322
1323 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1324 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001325 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1326 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001327 return 1;
1328 }
1329
1330 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1331 return 1;
1332 }
1333 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001334 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1335 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001336 return 1;
1337 }
1338 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001339 else if (strcmp(args[1], "maxconn") == 0) {
1340 if (strcmp(args[2], "frontend") == 0) {
1341 struct proxy *px;
1342 struct listener *l;
1343 int v;
1344
Willy Tarreau532a4502011-09-07 22:37:44 +02001345 px = expect_frontend_admin(s, si, args[3]);
1346 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001347 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001348
1349 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001350 appctx->ctx.cli.msg = "Integer value expected.\n";
1351 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001352 return 1;
1353 }
1354
1355 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001356 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001357 appctx->ctx.cli.msg = "Value out of range.\n";
1358 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001359 return 1;
1360 }
1361
1362 /* OK, the value is fine, so we assign it to the proxy and to all of
1363 * its listeners. The blocked ones will be dequeued.
1364 */
1365 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001366 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001367 l->maxconn = v;
1368 if (l->state == LI_FULL)
1369 resume_listener(l);
1370 }
1371
1372 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1373 dequeue_all_listeners(&s->fe->listener_queue);
1374
1375 return 1;
1376 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001377 else if (strcmp(args[2], "global") == 0) {
1378 int v;
1379
Willy Tarreau290e63a2012-09-20 18:07:14 +02001380 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001381 appctx->ctx.cli.msg = stats_permission_denied_msg;
1382 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001383 return 1;
1384 }
1385
1386 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001387 appctx->ctx.cli.msg = "Expects an integer value.\n";
1388 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001389 return 1;
1390 }
1391
1392 v = atoi(args[3]);
1393 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001394 appctx->ctx.cli.msg = "Value out of range.\n";
1395 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001396 return 1;
1397 }
1398
1399 /* check for unlimited values */
1400 if (v <= 0)
1401 v = global.hardmaxconn;
1402
1403 global.maxconn = v;
1404
1405 /* Dequeues all of the listeners waiting for a resource */
1406 if (!LIST_ISEMPTY(&global_listener_queue))
1407 dequeue_all_listeners(&global_listener_queue);
1408
1409 return 1;
1410 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001411 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001412 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1413 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001414 return 1;
1415 }
1416 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001417 else if (strcmp(args[1], "rate-limit") == 0) {
1418 if (strcmp(args[2], "connections") == 0) {
1419 if (strcmp(args[3], "global") == 0) {
1420 int v;
1421
Willy Tarreau290e63a2012-09-20 18:07:14 +02001422 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.cli.msg = stats_permission_denied_msg;
1424 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001425 return 1;
1426 }
1427
1428 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001429 appctx->ctx.cli.msg = "Expects an integer value.\n";
1430 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001431 return 1;
1432 }
1433
1434 v = atoi(args[4]);
1435 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001436 appctx->ctx.cli.msg = "Value out of range.\n";
1437 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001438 return 1;
1439 }
1440
1441 global.cps_lim = v;
1442
1443 /* Dequeues all of the listeners waiting for a resource */
1444 if (!LIST_ISEMPTY(&global_listener_queue))
1445 dequeue_all_listeners(&global_listener_queue);
1446
1447 return 1;
1448 }
1449 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001450 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1451 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001452 return 1;
1453 }
1454 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001455 else if (strcmp(args[2], "sessions") == 0) {
1456 if (strcmp(args[3], "global") == 0) {
1457 int v;
1458
1459 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1460 appctx->ctx.cli.msg = stats_permission_denied_msg;
1461 appctx->st0 = STAT_CLI_PRINT;
1462 return 1;
1463 }
1464
1465 if (!*args[4]) {
1466 appctx->ctx.cli.msg = "Expects an integer value.\n";
1467 appctx->st0 = STAT_CLI_PRINT;
1468 return 1;
1469 }
1470
1471 v = atoi(args[4]);
1472 if (v < 0) {
1473 appctx->ctx.cli.msg = "Value out of range.\n";
1474 appctx->st0 = STAT_CLI_PRINT;
1475 return 1;
1476 }
1477
1478 global.sps_lim = v;
1479
1480 /* Dequeues all of the listeners waiting for a resource */
1481 if (!LIST_ISEMPTY(&global_listener_queue))
1482 dequeue_all_listeners(&global_listener_queue);
1483
1484 return 1;
1485 }
1486 else {
1487 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1488 appctx->st0 = STAT_CLI_PRINT;
1489 return 1;
1490 }
1491 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001492#ifdef USE_OPENSSL
1493 else if (strcmp(args[2], "ssl-sessions") == 0) {
1494 if (strcmp(args[3], "global") == 0) {
1495 int v;
1496
1497 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1498 appctx->ctx.cli.msg = stats_permission_denied_msg;
1499 appctx->st0 = STAT_CLI_PRINT;
1500 return 1;
1501 }
1502
1503 if (!*args[4]) {
1504 appctx->ctx.cli.msg = "Expects an integer value.\n";
1505 appctx->st0 = STAT_CLI_PRINT;
1506 return 1;
1507 }
1508
1509 v = atoi(args[4]);
1510 if (v < 0) {
1511 appctx->ctx.cli.msg = "Value out of range.\n";
1512 appctx->st0 = STAT_CLI_PRINT;
1513 return 1;
1514 }
1515
1516 global.ssl_lim = v;
1517
1518 /* Dequeues all of the listeners waiting for a resource */
1519 if (!LIST_ISEMPTY(&global_listener_queue))
1520 dequeue_all_listeners(&global_listener_queue);
1521
1522 return 1;
1523 }
1524 else {
1525 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1526 appctx->st0 = STAT_CLI_PRINT;
1527 return 1;
1528 }
1529 }
1530#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001531 else if (strcmp(args[2], "http-compression") == 0) {
1532 if (strcmp(args[3], "global") == 0) {
1533 int v;
1534
Willy Tarreau85d47f92012-11-21 00:29:50 +01001535 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001536 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1537 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001538 return 1;
1539 }
1540
William Lallemandd85f9172012-11-09 17:05:39 +01001541 v = atoi(args[4]);
1542 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1543 }
1544 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1546 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001547 return 1;
1548 }
1549 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001550 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001551 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001552 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001553 return 1;
1554 }
1555 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001556 else if (strcmp(args[1], "table") == 0) {
1557 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1558 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001559 else if (strcmp(args[1], "map") == 0) {
1560 struct pattern *pat_elt;
1561 struct pat_idx_elt *idx_elt;
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001562 char *value = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001563
1564 /* Expect three parameters: map name, key and new value. */
1565 if (!*args[2] || !*args[3] || !*args[4]) {
1566 appctx->ctx.cli.msg = "'set map' expect three parameters: map name, key and value.\n";
1567 appctx->st0 = STAT_CLI_PRINT;
1568 return 1;
1569 }
1570
1571 /* Lookup the reference in the maps. */
1572 appctx->ctx.map.ref = map_get_reference(args[2]);
1573 if (!appctx->ctx.map.ref) {
1574 appctx->ctx.cli.msg = "Unknown map reference.\n";
1575 appctx->st0 = STAT_CLI_PRINT;
1576 return 1;
1577 }
1578
1579 /* Lookup the entry in the reference values. */
1580 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1581 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1582 break;
1583
1584 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1585 appctx->ctx.cli.msg = "Entry not found.\n";
1586 appctx->st0 = STAT_CLI_PRINT;
1587 return 1;
1588 }
1589
1590 /* Update each reference entries. */
1591 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1592 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1593 value = strdup(args[4]);
1594 if (!value) {
1595 appctx->ctx.cli.msg = "Out of memory error.\n";
1596 appctx->st0 = STAT_CLI_PRINT;
1597 return 1;
1598 }
1599 free(appctx->ctx.map.ent->value);
1600 appctx->ctx.map.ent->value = value;
1601 }
1602 }
1603
1604 /* Change the sample. The lookup juste return the first entry, other
1605 * entries are not changed, but are never matched.
1606 */
1607 appctx->ctx.map.desc = NULL;
1608 for (stats_map_lookup_next(si);
1609 appctx->ctx.map.desc;
1610 stats_map_lookup_next(si)) {
1611 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1612 if (pat_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001613 appctx->ctx.map.desc->parse(value, pat_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001614 if (idx_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001615 appctx->ctx.map.desc->parse(value, idx_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001616 }
1617
1618 /* The set is done, send message. */
1619 appctx->ctx.cli.msg = "Done.\n";
1620 appctx->st0 = STAT_CLI_PRINT;
1621 return 1;
1622 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001623 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001624 return 0;
1625 }
1626 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001627 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001628 if (strcmp(args[1], "agent") == 0) {
1629 struct server *sv;
1630
1631 sv = expect_server_admin(s, si, args[2]);
1632 if (!sv)
1633 return 1;
1634
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001635 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1636 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1637 appctx->st0 = STAT_CLI_PRINT;
1638 return 1;
1639 }
1640
1641 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001642 return 1;
1643 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001644 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001645 struct server *sv;
1646
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001647 sv = expect_server_admin(s, si, args[2]);
1648 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001649 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001650
Cyril Bontécd19e512010-01-31 22:34:03 +01001651 if (sv->state & SRV_MAINTAIN) {
1652 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001653 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001654 /* If this server tracks the status of another one,
1655 * we must restore the good status.
1656 */
Willy Tarreau44267702011-10-28 15:35:33 +02001657 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001658 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001659 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001660 } else {
1661 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001662 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001663 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001664 }
1665 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001666 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001667 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001668 }
1669 }
1670
Willy Tarreau532a4502011-09-07 22:37:44 +02001671 return 1;
1672 }
1673 else if (strcmp(args[1], "frontend") == 0) {
1674 struct proxy *px;
1675
1676 px = expect_frontend_admin(s, si, args[2]);
1677 if (!px)
1678 return 1;
1679
1680 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001681 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1682 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001683 return 1;
1684 }
1685
1686 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001687 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1688 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001689 return 1;
1690 }
1691
1692 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001693 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1694 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001695 return 1;
1696 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001697 return 1;
1698 }
1699 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001700 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1701 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001702 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001703 }
1704 }
1705 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001706 if (strcmp(args[1], "agent") == 0) {
1707 struct server *sv;
1708
1709 sv = expect_server_admin(s, si, args[2]);
1710 if (!sv)
1711 return 1;
1712
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001713 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001714 return 1;
1715 }
1716 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001717 struct server *sv;
1718
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001719 sv = expect_server_admin(s, si, args[2]);
1720 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001721 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001722
Cyril Bontécd19e512010-01-31 22:34:03 +01001723 if (! (sv->state & SRV_MAINTAIN)) {
1724 /* Not already in maintenance, we can change the server state */
1725 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001726 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001727 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001728 }
1729
Willy Tarreau532a4502011-09-07 22:37:44 +02001730 return 1;
1731 }
1732 else if (strcmp(args[1], "frontend") == 0) {
1733 struct proxy *px;
1734
1735 px = expect_frontend_admin(s, si, args[2]);
1736 if (!px)
1737 return 1;
1738
1739 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001740 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1741 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001742 return 1;
1743 }
1744
1745 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001746 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1747 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001748 return 1;
1749 }
1750
1751 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001752 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001754 return 1;
1755 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001756 return 1;
1757 }
1758 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001759 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1760 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001761 return 1;
1762 }
1763 }
1764 else if (strcmp(args[0], "shutdown") == 0) {
1765 if (strcmp(args[1], "frontend") == 0) {
1766 struct proxy *px;
1767
1768 px = expect_frontend_admin(s, si, args[2]);
1769 if (!px)
1770 return 1;
1771
1772 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001773 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1774 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001775 return 1;
1776 }
1777
1778 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1779 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1780 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1781 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1782 stop_proxy(px);
1783 return 1;
1784 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001785 else if (strcmp(args[1], "session") == 0) {
1786 struct session *sess, *ptr;
1787
Willy Tarreau290e63a2012-09-20 18:07:14 +02001788 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001789 appctx->ctx.cli.msg = stats_permission_denied_msg;
1790 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001791 return 1;
1792 }
1793
1794 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001795 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1796 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001797 return 1;
1798 }
1799
1800 ptr = (void *)strtoul(args[2], NULL, 0);
1801
1802 /* first, look for the requested session in the session table */
1803 list_for_each_entry(sess, &sessions, list) {
1804 if (sess == ptr)
1805 break;
1806 }
1807
1808 /* do we have the session ? */
1809 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001810 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1811 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001812 return 1;
1813 }
1814
1815 session_shutdown(sess, SN_ERR_KILLED);
1816 return 1;
1817 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001818 else if (strcmp(args[1], "sessions") == 0) {
1819 if (strcmp(args[2], "server") == 0) {
1820 struct server *sv;
1821 struct session *sess, *sess_bck;
1822
1823 sv = expect_server_admin(s, si, args[3]);
1824 if (!sv)
1825 return 1;
1826
1827 /* kill all the session that are on this server */
1828 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1829 if (sess->srv_conn == sv)
1830 session_shutdown(sess, SN_ERR_KILLED);
1831
1832 return 1;
1833 }
1834 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001835 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1836 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001837 return 1;
1838 }
1839 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001840 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001841 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1842 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001843 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001844 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001845 }
1846 else if (strcmp(args[0], "del") == 0) {
1847 if (strcmp(args[1], "map") == 0) {
1848 struct pattern *pat_elt;
1849 struct pat_idx_elt *idx_elt;
1850 struct map_entry *ent;
1851
1852 /* Expect two parameters: map name and key. */
1853 if (!*args[2] || !*args[3]) {
1854 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1855 appctx->st0 = STAT_CLI_PRINT;
1856 return 1;
1857 }
1858
1859 /* Lookup the reference in the maps. */
1860 appctx->ctx.map.ref = map_get_reference(args[2]);
1861 if (!appctx->ctx.map.ref) {
1862 appctx->ctx.cli.msg = "Unknown map reference.\n";
1863 appctx->st0 = STAT_CLI_PRINT;
1864 return 1;
1865 }
1866
1867 /* Lookup the entry in the reference values.
1868 * If the entry is not found in the reference, return error message.
1869 */
1870 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1871 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1872 break;
1873
1874 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1875 appctx->ctx.cli.msg = "Entry not found.\n";
1876 appctx->st0 = STAT_CLI_PRINT;
1877 return 1;
1878 }
1879
1880 /* Delete each enties from reference. */
1881 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1882 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1883 LIST_DEL(&appctx->ctx.map.ent->list);
1884 free(appctx->ctx.map.ent->key);
1885 free(appctx->ctx.map.ent->value);
1886 free(appctx->ctx.map.ent);
1887 }
1888 }
1889
1890 /* Delete all matching entries for each map descritor. */
1891 appctx->ctx.map.desc = NULL;
1892 stats_map_lookup_next(si);
1893 while (appctx->ctx.map.desc) {
1894 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1895 if (pat_elt != NULL) {
1896 LIST_DEL(&pat_elt->list);
1897 pattern_free(pat_elt);
1898 }
1899 if (idx_elt != NULL) {
1900 ebmb_delete(&idx_elt->node);
1901 free(idx_elt);
1902 }
1903 }
1904 stats_map_lookup_next(si);
1905 }
1906
1907 /* The deletion is done, send message. */
1908 appctx->ctx.cli.msg = "Done.\n";
1909 appctx->st0 = STAT_CLI_PRINT;
1910 return 1;
1911 }
1912 else { /* unknown "del" parameter */
1913 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1914 appctx->st0 = STAT_CLI_PRINT;
1915 return 1;
1916 }
1917 }
1918 else if (strcmp(args[0], "add") == 0) {
1919 if (strcmp(args[1], "map") == 0) {
1920 const char *params[2];
1921 struct pattern *pat;
1922 struct map_entry *ent;
1923 struct sample_storage *smp;
1924
1925 /* Expect three parameters: map name, key and new value. */
1926 if (!*args[2] || !*args[3] || !*args[4]) {
1927 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1928 appctx->st0 = STAT_CLI_PRINT;
1929 return 1;
1930 }
1931
1932 params[0] = args[3];
1933 params[1] = "";
1934
1935 /* Lookup the reference in the maps. */
1936 appctx->ctx.map.ref = map_get_reference(args[2]);
1937 if (!appctx->ctx.map.ref) {
1938 appctx->ctx.cli.msg = "Unknown map reference.\n";
1939 appctx->st0 = STAT_CLI_PRINT;
1940 return 1;
1941 }
1942
1943 /* Prepare and link the new map_entry element. If out of memory
1944 * error the action is cancelled and the descriptor are left
1945 * coherents.
1946 */
1947 ent = malloc(sizeof(*ent));
1948 if (!ent) {
1949 appctx->ctx.cli.msg = "Out of memory error.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
1951 return 1;
1952 }
1953 ent->key = strdup(args[3]);
1954 if (!ent->key) {
1955 free(ent);
1956 appctx->ctx.cli.msg = "Out of memory error.\n";
1957 appctx->st0 = STAT_CLI_PRINT;
1958 return 1;
1959 }
1960 ent->value = strdup(args[4]);
1961 if (!ent->value) {
1962 free(ent->key);
1963 free(ent);
1964 appctx->ctx.cli.msg = "Out of memory error.\n";
1965 appctx->st0 = STAT_CLI_PRINT;
1966 return 1;
1967 }
1968 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1969
1970 /* Browse each map descritor and try to insert this new value. */
1971 appctx->ctx.map.desc = NULL;
1972 for (stats_map_lookup_next(si);
1973 appctx->ctx.map.desc;
1974 stats_map_lookup_next(si)) {
1975
1976 /* Create new sample. Return out of memory error
1977 * if the memory cannot be allocated. The 'add' process
1978 * is aborted, but the already inserted entries are not
1979 * deleted.
1980 */
1981 smp = calloc(1, sizeof(*smp));
1982 if (!smp) {
1983 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1984 appctx->st0 = STAT_CLI_PRINT;
1985 return 1;
1986 }
1987
1988 /* Create sample. If this function fails, the insertion
1989 * is canceled for this 'descriptor', but continue, for
1990 * the other descriptors.
1991 */
1992 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1993 free(smp);
1994 continue;
1995 }
1996
1997 /* If the value can be indexed, get the first pattern. If
1998 * the return entry is not the indexed entry, new 'pattern' is
1999 * created by the function pattern_register(). If the 'pattern'
2000 * is NULL, new entry is created. This is ugly because the
2001 * following code interfers with the own code of the function
2002 * pattern_register().
2003 */
2004 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
2005 appctx->ctx.map.desc->pat->match == pat_match_ip) {
2006 pat = LIST_NEXT(&appctx->ctx.map.desc->pat->patterns, struct pattern *, list);
2007 if (&pat->list == &appctx->ctx.map.desc->pat->patterns)
2008 pat = NULL;
2009 }
2010 else
2011 pat = NULL;
2012
2013 if (!pattern_register(appctx->ctx.map.desc->pat, params, smp, &pat, 0, NULL)) {
2014 free(smp);
2015 continue;
2016 }
2017 }
2018
2019 /* The add is done, send message. */
2020 appctx->ctx.cli.msg = "Done.\n";
2021 appctx->st0 = STAT_CLI_PRINT;
2022 return 1;
2023 }
2024 else { /* unknown "del" parameter */
2025 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2026 appctx->st0 = STAT_CLI_PRINT;
2027 return 1;
2028 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002029 }
2030 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002031 return 0;
2032 }
2033 return 1;
2034}
2035
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002036/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002037 * used to processes I/O from/to the stats unix socket. The system relies on a
2038 * state machine handling requests and various responses. We read a request,
2039 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 * Then we can read again. The state is stored in appctx->st0 and is one of the
2041 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002042 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002043 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002044static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002045{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002046 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002047 struct channel *req = si->ob;
2048 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002049 int reql;
2050 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002051
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002052 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2053 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002054
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002055 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002056 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002057 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002058 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2059 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002060 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002061 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002062 /* Let's close for real now. We just close the request
2063 * side, the conditions below will complete if needed.
2064 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002065 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002066 break;
2067 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002068 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002069 /* ensure we have some output room left in the event we
2070 * would want to return some info right after parsing.
2071 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002072 if (buffer_almost_full(si->ib->buf)) {
2073 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002074 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002075 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002076
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002077 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002078 if (reql <= 0) { /* closed or EOL not found */
2079 if (reql == 0)
2080 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002081 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002082 continue;
2083 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002084
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002085 /* seek for a possible semi-colon. If we find one, we
2086 * replace it with an LF and skip only this part.
2087 */
2088 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002089 if (trash.str[len] == ';') {
2090 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002091 reql = len + 1;
2092 break;
2093 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002094
Willy Tarreau816fc222009-10-04 07:36:58 +02002095 /* now it is time to check that we have a full line,
2096 * remove the trailing \n and possibly \r, then cut the
2097 * line.
2098 */
2099 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002100 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002101 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002102 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002103 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002104
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002105 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002106 len--;
2107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002108 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002109
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002110 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002111 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002112 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002113 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002114 continue;
2115 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002116 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002117 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002118 else if (strcmp(trash.str, "help") == 0 ||
2119 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002120 appctx->ctx.cli.msg = stats_sock_usage_msg;
2121 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002122 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002123 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002124 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002125 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002126 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002127 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002128 /* if prompt is disabled, print help on empty lines,
2129 * so that the user at least knows how to enable
2130 * prompt and find help.
2131 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002132 appctx->ctx.cli.msg = stats_sock_usage_msg;
2133 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002134 }
2135
2136 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002137 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002138 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002139 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002140 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002141 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002142 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002143 continue;
2144 }
2145
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002147 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002148 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2149 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002150 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002151 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002152 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002153 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002154 break;
2155 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002156 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002157 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002158 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002159 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002160 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002161 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002162 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002163 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002164 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002165 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002166 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002167 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002168 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002169 if (stats_table_request(si, appctx->st0))
2170 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002171 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002172 case STAT_CLI_O_MAPS:
2173 if (stats_maps_list(si))
2174 appctx->st0 = STAT_CLI_PROMPT;
2175 break;
2176 case STAT_CLI_O_MAP:
2177 if (stats_map_list(si))
2178 appctx->st0 = STAT_CLI_PROMPT;
2179 break;
2180 case STAT_CLI_O_MLOOK:
2181 if (stats_map_lookup(si))
2182 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002183 case STAT_CLI_O_POOLS:
2184 if (stats_dump_pools_to_buffer(si))
2185 appctx->st0 = STAT_CLI_PROMPT;
2186 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002187 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002188 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002189 break;
2190 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002191
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002192 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002193 if (appctx->st0 == STAT_CLI_PROMPT) {
2194 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2195 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002196 }
2197
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002198 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002199 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002200 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002201
2202 /* Now we close the output if one of the writers did so,
2203 * or if we're not in interactive mode and the request
2204 * buffer is empty. This still allows pipelined requests
2205 * to be sent in non-interactive mode.
2206 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002207 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2208 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002209 continue;
2210 }
2211
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002212 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002213 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002214 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002215 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002217 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002218 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2219 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002220 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002221 * and nothing more to consume. This is comparable to a broken pipe, so
2222 * we forward the close to the request side so that it flows upstream to
2223 * the client.
2224 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002225 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002226 }
2227
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002228 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002229 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2230 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2231 /* We have no more processing to do, and nothing more to send, and
2232 * the client side has closed. So we'll forward this state downstream
2233 * on the response buffer.
2234 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002235 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002236 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002237 }
2238
2239 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002240 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002241
2242 /* we don't want to expire timeouts while we're processing requests */
2243 si->ib->rex = TICK_ETERNITY;
2244 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002245
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002246 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002247 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 +02002248 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002249 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 +02002250
2251 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2252 /* check that we have released everything then unregister */
2253 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002254 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002255}
2256
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002257/* This function dumps information onto the stream interface's read buffer.
2258 * It returns 0 as long as it does not complete, non-zero upon completion.
2259 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002260 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002261static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002262{
2263 unsigned int up = (now.tv_sec - start_date.tv_sec);
2264
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002265 chunk_printf(&trash,
2266 "Name: " PRODUCT_NAME "\n"
2267 "Version: " HAPROXY_VERSION "\n"
2268 "Release_date: " HAPROXY_DATE "\n"
2269 "Nbproc: %d\n"
2270 "Process_num: %d\n"
2271 "Pid: %d\n"
2272 "Uptime: %dd %dh%02dm%02ds\n"
2273 "Uptime_sec: %d\n"
2274 "Memmax_MB: %d\n"
2275 "Ulimit-n: %d\n"
2276 "Maxsock: %d\n"
2277 "Maxconn: %d\n"
2278 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002279 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002280 "CumConns: %d\n"
2281 "CumReq: %d\n"
2282#ifdef USE_OPENSSL
2283 "MaxSslConns: %d\n"
2284 "CurrSslConns: %d\n"
2285 "CumSslConns: %d\n"
2286#endif
2287 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002288 "PipesUsed: %d\n"
2289 "PipesFree: %d\n"
2290 "ConnRate: %d\n"
2291 "ConnRateLimit: %d\n"
2292 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002293 "SessRate: %d\n"
2294 "SessRateLimit: %d\n"
2295 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002296#ifdef USE_OPENSSL
2297 "SslRate: %d\n"
2298 "SslRateLimit: %d\n"
2299 "MaxSslRate: %d\n"
2300#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002301 "CompressBpsIn: %u\n"
2302 "CompressBpsOut: %u\n"
2303 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002304#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002305 "ZlibMemUsage: %ld\n"
2306 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002307#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002308 "Tasks: %d\n"
2309 "Run_queue: %d\n"
2310 "Idle_pct: %d\n"
2311 "node: %s\n"
2312 "description: %s\n"
2313 "",
2314 global.nbproc,
2315 relative_pid,
2316 pid,
2317 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2318 up,
2319 global.rlimit_memmax,
2320 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002321 global.maxsock, global.maxconn, global.hardmaxconn,
2322 actconn, totalconn, global.req_count,
2323#ifdef USE_OPENSSL
2324 global.maxsslconn, sslconns, totalsslconns,
2325#endif
2326 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002327 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002328 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002329#ifdef USE_OPENSSL
2330 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2331#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002332 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2333 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002334#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002335 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002336#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002337 nb_tasks_cur, run_queue_cur, idle_pct,
2338 global.node, global.desc ? global.desc : ""
2339 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002340
2341 if (bi_putchk(si->ib, &trash) == -1)
2342 return 0;
2343
2344 return 1;
2345}
2346
Willy Tarreau12833bb2014-01-28 16:49:56 +01002347/* This function dumps memory usage information onto the stream interface's
2348 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2349 * completion. No state is used.
2350 */
2351static int stats_dump_pools_to_buffer(struct stream_interface *si)
2352{
2353 dump_pools_to_trash();
2354 if (bi_putchk(si->ib, &trash) == -1)
2355 return 0;
2356 return 1;
2357}
2358
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2360 * the state from stream interface <si>. The caller is responsible for clearing
2361 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002362 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002363static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002364{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002366 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002368 if (!(px->cap & PR_CAP_FE))
2369 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002370
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002371 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002372 return 0;
2373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002375 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002376 /* name, queue */
2377 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002379 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002380 /* Column sub-heading for Enable or Disable server */
2381 chunk_appendf(&trash, "<td></td>");
2382 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002383
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002384 chunk_appendf(&trash,
2385 "<td class=ac>"
2386 "<a name=\"%s/Frontend\"></a>"
2387 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2388 "<td colspan=3></td>"
2389 "",
2390 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002391
Willy Tarreau466c9b52012-12-23 02:25:03 +01002392 chunk_appendf(&trash,
2393 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002394 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002395 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2396 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2397 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002398 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2399 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2400 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002401
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002402 if (px->mode == PR_MODE_HTTP)
2403 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002404 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002405 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002406
2407 chunk_appendf(&trash,
2408 "</table></div></u></td>"
2409 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002410 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002411 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2412 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2413 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002414 U2H(px->fe_counters.sps_max),
2415 U2H(px->fe_counters.cps_max),
2416 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002417
2418 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002419 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002420 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002421 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002422
2423 chunk_appendf(&trash,
2424 "</table></div></u></td>"
2425 /* sessions rate : limit */
2426 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002427 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002428
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002429 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002430 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002431 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002432 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002433 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2434 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002435 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002436 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2437 U2H(px->fe_counters.cum_sess),
2438 U2H(px->fe_counters.cum_conn),
2439 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002440
Willy Tarreau466c9b52012-12-23 02:25:03 +01002441 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002442 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002443 chunk_appendf(&trash,
2444 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2445 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2446 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2447 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2448 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2449 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2450 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2451 "<tr><th>- other responses:</th><td>%s</td></tr>"
2452 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2453 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002454 U2H(px->fe_counters.p.http.cum_req),
2455 U2H(px->fe_counters.p.http.rsp[1]),
2456 U2H(px->fe_counters.p.http.rsp[2]),
2457 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002458 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002459 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002460 U2H(px->fe_counters.p.http.rsp[3]),
2461 U2H(px->fe_counters.p.http.rsp[4]),
2462 U2H(px->fe_counters.p.http.rsp[5]),
2463 U2H(px->fe_counters.p.http.rsp[0]),
2464 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002465 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002466
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002467 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002468 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002469 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002470 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002471 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002472 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002473 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002474 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002476 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002477 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002478 "<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 +01002479 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002480 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002481 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2482 px->fe_counters.comp_in ?
2483 (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 +01002484 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002485
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002486 chunk_appendf(&trash,
2487 /* denied: req, resp */
2488 "<td>%s</td><td>%s</td>"
2489 /* errors : request, connect, response */
2490 "<td>%s</td><td></td><td></td>"
2491 /* warnings: retries, redispatches */
2492 "<td></td><td></td>"
2493 /* server status : reflect frontend status */
2494 "<td class=ac>%s</td>"
2495 /* rest of server: nothing */
2496 "<td class=ac colspan=8></td></tr>"
2497 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002498 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2499 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002500 px->state == PR_STREADY ? "OPEN" :
2501 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002502 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002503 else { /* CSV mode */
2504 chunk_appendf(&trash,
2505 /* pxid, name, queue cur, queue max, */
2506 "%s,FRONTEND,,,"
2507 /* sessions : current, max, limit, total */
2508 "%d,%d,%d,%lld,"
2509 /* bytes : in, out */
2510 "%lld,%lld,"
2511 /* denied: req, resp */
2512 "%lld,%lld,"
2513 /* errors : request, connect, response */
2514 "%lld,,,"
2515 /* warnings: retries, redispatches */
2516 ",,"
2517 /* server status : reflect frontend status */
2518 "%s,"
2519 /* rest of server: nothing */
2520 ",,,,,,,,"
2521 /* pid, iid, sid, throttle, lbtot, tracked, type */
2522 "%d,%d,0,,,,%d,"
2523 /* rate, rate_lim, rate_max */
2524 "%u,%u,%u,"
2525 /* check_status, check_code, check_duration */
2526 ",,,",
2527 px->id,
2528 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2529 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2530 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2531 px->fe_counters.failed_req,
2532 px->state == PR_STREADY ? "OPEN" :
2533 px->state == PR_STFULL ? "FULL" : "STOP",
2534 relative_pid, px->uuid, STATS_TYPE_FE,
2535 read_freq_ctr(&px->fe_sess_per_sec),
2536 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002537
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002538 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2539 if (px->mode == PR_MODE_HTTP) {
2540 for (i=1; i<6; i++)
2541 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2542 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2543 }
2544 else
2545 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002546
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547 /* failed health analyses */
2548 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002549
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002550 /* requests : req_rate, req_rate_max, req_tot, */
2551 chunk_appendf(&trash, "%u,%u,%lld,",
2552 read_freq_ctr(&px->fe_req_per_sec),
2553 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2554
2555 /* errors: cli_aborts, srv_aborts */
2556 chunk_appendf(&trash, ",,");
2557
2558 /* compression: in, out, bypassed */
2559 chunk_appendf(&trash, "%lld,%lld,%lld,",
2560 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2561
2562 /* compression: comp_rsp */
2563 chunk_appendf(&trash, "%lld,",
2564 px->fe_counters.p.http.comp_rsp);
2565
2566 /* finish with EOL */
2567 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002568 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002570}
2571
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2573 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2574 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2575 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002576 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002578{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002579 struct appctx *appctx = __objt_appctx(si->end);
2580
2581 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002583 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002584 /* Column sub-heading for Enable or Disable server */
2585 chunk_appendf(&trash, "<td></td>");
2586 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002587 chunk_appendf(&trash,
2588 /* frontend name, listener name */
2589 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2590 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2591 "",
2592 px->id, l->name,
2593 (flags & ST_SHLGNDS)?"<u>":"",
2594 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002595
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002596 if (flags & ST_SHLGNDS) {
2597 char str[INET6_ADDRSTRLEN];
2598 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002599
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002600 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002602 port = get_host_port(&l->addr);
2603 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2604 case AF_INET:
2605 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2606 break;
2607 case AF_INET6:
2608 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2609 break;
2610 case AF_UNIX:
2611 chunk_appendf(&trash, "unix, ");
2612 break;
2613 case -1:
2614 chunk_appendf(&trash, "(%s), ", strerror(errno));
2615 break;
2616 }
Willy Tarreau91861262007-10-17 17:06:05 +02002617
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002618 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002619 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002620 }
Willy Tarreau91861262007-10-17 17:06:05 +02002621
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002622 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002623 /* queue */
2624 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002625 /* sessions rate: current, max, limit */
2626 "<td colspan=3>&nbsp;</td>"
2627 /* sessions: current, max, limit, total, lbtot */
2628 "<td>%s</td><td>%s</td><td>%s</td>"
2629 "<td>%s</td><td>&nbsp;</td>"
2630 /* bytes: in, out */
2631 "<td>%s</td><td>%s</td>"
2632 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002633 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002634 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2635 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002636
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637 chunk_appendf(&trash,
2638 /* denied: req, resp */
2639 "<td>%s</td><td>%s</td>"
2640 /* errors: request, connect, response */
2641 "<td>%s</td><td></td><td></td>"
2642 /* warnings: retries, redispatches */
2643 "<td></td><td></td>"
2644 /* server status: reflect listener status */
2645 "<td class=ac>%s</td>"
2646 /* rest of server: nothing */
2647 "<td class=ac colspan=8></td></tr>"
2648 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002649 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2650 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2652 }
2653 else { /* CSV mode */
2654 chunk_appendf(&trash,
2655 /* pxid, name, queue cur, queue max, */
2656 "%s,%s,,,"
2657 /* sessions: current, max, limit, total */
2658 "%d,%d,%d,%lld,"
2659 /* bytes: in, out */
2660 "%lld,%lld,"
2661 /* denied: req, resp */
2662 "%lld,%lld,"
2663 /* errors: request, connect, response */
2664 "%lld,,,"
2665 /* warnings: retries, redispatches */
2666 ",,"
2667 /* server status: reflect listener status */
2668 "%s,"
2669 /* rest of server: nothing */
2670 ",,,,,,,,"
2671 /* pid, iid, sid, throttle, lbtot, tracked, type */
2672 "%d,%d,%d,,,,%d,"
2673 /* rate, rate_lim, rate_max */
2674 ",,,"
2675 /* check_status, check_code, check_duration */
2676 ",,,"
2677 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2678 ",,,,,,"
2679 /* failed health analyses */
2680 ","
2681 /* requests : req_rate, req_rate_max, req_tot, */
2682 ",,,"
2683 /* errors: cli_aborts, srv_aborts */
2684 ",,"
2685 /* compression: in, out, bypassed, comp_rsp */
2686 ",,,,"
2687 "\n",
2688 px->id, l->name,
2689 l->nbconn, l->counters->conn_max,
2690 l->maxconn, l->counters->cum_conn,
2691 l->counters->bytes_in, l->counters->bytes_out,
2692 l->counters->denied_req, l->counters->denied_resp,
2693 l->counters->failed_req,
2694 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2695 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2696 }
2697 return 1;
2698}
Willy Tarreau91861262007-10-17 17:06:05 +02002699
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002700/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2701 * from stream interface <si>, stats flags <flags>, and server state <state>.
2702 * The caller is responsible for clearing the trash if needed. Returns non-zero
2703 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002704 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2705 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002706 */
2707static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2708{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002709 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002710 struct server *ref = sv->track ? sv->track : sv;
2711 char str[INET6_ADDRSTRLEN];
2712 struct chunk src;
2713 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002714
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002715 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002716 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002717 "DOWN",
2718 "DN %d/%d &uarr;",
2719 "UP %d/%d &darr;",
2720 "UP",
2721 "NOLB %d/%d &darr;",
2722 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002723 "DRAIN %d/%d &darr;",
2724 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 "<i>no check</i>"
2726 };
Willy Tarreau91861262007-10-17 17:06:05 +02002727
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002728 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2729 chunk_appendf(&trash, "<tr class=\"maintain\">");
2730 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002731 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002732 "<tr class=\"%s%d\">",
2733 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002734
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002735 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002736 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002737 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2738 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002739
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002740 chunk_appendf(&trash,
2741 "<td class=ac><a name=\"%s/%s\"></a>%s"
2742 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2743 "",
2744 px->id, sv->id,
2745 (flags & ST_SHLGNDS) ? "<u>" : "",
2746 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002747
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002748 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002749 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002750
2751 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2752 case AF_INET:
2753 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2754 break;
2755 case AF_INET6:
2756 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2757 break;
2758 case AF_UNIX:
2759 chunk_appendf(&trash, "unix, ");
2760 break;
2761 case -1:
2762 chunk_appendf(&trash, "(%s), ", strerror(errno));
2763 break;
2764 default: /* address family not supported */
2765 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002766 }
Willy Tarreau91861262007-10-17 17:06:05 +02002767
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002768 /* id */
2769 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002770
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002771 /* cookie */
2772 if (sv->cookie) {
2773 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002774
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2776 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002777
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002779 }
2780
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002781 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002782 }
Willy Tarreau91861262007-10-17 17:06:05 +02002783
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002784 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002786 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 /* sessions rate : current, max, limit */
2788 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002791 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2792 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002793
Willy Tarreau466c9b52012-12-23 02:25:03 +01002794
2795 chunk_appendf(&trash,
2796 /* sessions: current, max, limit, total */
2797 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002798 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002799 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2800 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002801 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2802 U2H(sv->counters.cum_sess),
2803 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002804
Willy Tarreau466c9b52012-12-23 02:25:03 +01002805 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2806 if (px->mode == PR_MODE_HTTP) {
2807 unsigned long long tot;
2808 for (tot = i = 0; i < 6; i++)
2809 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810
Willy Tarreau466c9b52012-12-23 02:25:03 +01002811 chunk_appendf(&trash,
2812 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2813 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2814 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2815 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2816 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2817 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2818 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2819 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002820 U2H(tot),
2821 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2822 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2823 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2824 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2825 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2826 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 +02002827 }
Willy Tarreau91861262007-10-17 17:06:05 +02002828
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002830 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002831 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002832 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002833 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002834
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002835 chunk_appendf(&trash,
2836 /* bytes : in, out */
2837 "<td>%s</td><td>%s</td>"
2838 /* denied: req, resp */
2839 "<td></td><td>%s</td>"
2840 /* errors : request, connect */
2841 "<td></td><td>%s</td>"
2842 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002843 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002844 /* warnings: retries, redispatches */
2845 "<td>%lld</td><td>%lld</td>"
2846 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002847 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2848 U2H(sv->counters.failed_secu),
2849 U2H(sv->counters.failed_conns),
2850 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 sv->counters.cli_aborts,
2852 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002853 sv->counters.retries, sv->counters.redispatches);
2854
2855 /* status, lest check */
2856 chunk_appendf(&trash, "<td class=ac>");
2857
2858 if (sv->state & SRV_MAINTAIN) {
2859 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2860 chunk_appendf(&trash, "MAINT");
2861 }
2862 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2863 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2864 chunk_appendf(&trash, "MAINT(via)");
2865 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002866 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002867 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2868 chunk_appendf(&trash,
2869 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002870 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2871 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002872 }
Willy Tarreau91861262007-10-17 17:06:05 +02002873
Willy Tarreauff5ae352013-12-11 20:36:34 +01002874 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002875 chunk_appendf(&trash,
2876 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002877 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002878 get_check_status_info(sv->check.status));
2879
2880 if (sv->check.status >= HCHK_STATUS_L57DATA)
2881 chunk_appendf(&trash, "/%d", sv->check.code);
2882
2883 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002884 chunk_appendf(&trash, " in %lums", sv->check.duration);
2885
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002886 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002887 get_check_status_description(sv->check.status));
2888 if (*sv->check.desc) {
2889 chunk_appendf(&trash, ": ");
2890 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2891 chunk_htmlencode(&trash, &src);
2892 }
2893 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002894 }
2895 else
2896 chunk_appendf(&trash, "</td><td>");
2897
2898 chunk_appendf(&trash,
2899 /* weight */
2900 "</td><td class=ac>%d</td>"
2901 /* act, bck */
2902 "<td class=ac>%s</td><td class=ac>%s</td>"
2903 "",
2904 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2905 (sv->state & SRV_BACKUP) ? "-" : "Y",
2906 (sv->state & SRV_BACKUP) ? "Y" : "-");
2907
2908 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002909 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002910 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002911
2912 if (ref->observe)
2913 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2914
2915 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002916 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002917 "<td>%lld</td><td>%s</td>"
2918 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002919 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002920 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2921 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002922 else if (sv != ref) {
2923 if (sv->state & SRV_MAINTAIN)
2924 chunk_appendf(&trash,
2925 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2926 ref->proxy->id, ref->id);
2927 else
2928 chunk_appendf(&trash,
2929 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2930 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2931 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932 else
2933 chunk_appendf(&trash, "<td colspan=3></td>");
2934
2935 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002936 if (sv->state & SRV_WARMINGUP)
2937 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002938 else
2939 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2940 }
2941 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002942 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002943 "DOWN,",
2944 "DOWN %d/%d,",
2945 "UP %d/%d,",
2946 "UP,",
2947 "NOLB %d/%d,",
2948 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002949 "DRAIN %d/%d,",
2950 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 "no check,"
2952 };
2953
2954 chunk_appendf(&trash,
2955 /* pxid, name */
2956 "%s,%s,"
2957 /* queue : current, max */
2958 "%d,%d,"
2959 /* sessions : current, max, limit, total */
2960 "%d,%d,%s,%lld,"
2961 /* bytes : in, out */
2962 "%lld,%lld,"
2963 /* denied: req, resp */
2964 ",%lld,"
2965 /* errors : request, connect, response */
2966 ",%lld,%lld,"
2967 /* warnings: retries, redispatches */
2968 "%lld,%lld,"
2969 "",
2970 px->id, sv->id,
2971 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002972 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 sv->counters.bytes_in, sv->counters.bytes_out,
2974 sv->counters.failed_secu,
2975 sv->counters.failed_conns, sv->counters.failed_resp,
2976 sv->counters.retries, sv->counters.redispatches);
2977
2978 /* status */
2979 if (sv->state & SRV_MAINTAIN)
2980 chunk_appendf(&trash, "MAINT,");
2981 else if (ref != sv && ref->state & SRV_MAINTAIN)
2982 chunk_appendf(&trash, "MAINT(via),");
2983 else
2984 chunk_appendf(&trash,
2985 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002986 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2987 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988
2989 chunk_appendf(&trash,
2990 /* weight, active, backup */
2991 "%d,%d,%d,"
2992 "",
2993 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2994 (sv->state & SRV_BACKUP) ? 0 : 1,
2995 (sv->state & SRV_BACKUP) ? 1 : 0);
2996
2997 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002998 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002999 chunk_appendf(&trash,
3000 "%lld,%lld,%d,%d,",
3001 sv->counters.failed_checks, sv->counters.down_trans,
3002 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3003 else
3004 chunk_appendf(&trash, ",,,,");
3005
3006 /* queue limit, pid, iid, sid, */
3007 chunk_appendf(&trash,
3008 "%s,"
3009 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003010 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 relative_pid, px->uuid, sv->puid);
3012
3013 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01003014 if (sv->state & SRV_WARMINGUP)
3015 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003016
3017 /* sessions: lbtot */
3018 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3019
3020 /* tracked */
3021 if (sv->track)
3022 chunk_appendf(&trash, "%s/%s,",
3023 sv->track->proxy->id, sv->track->id);
3024 else
3025 chunk_appendf(&trash, ",");
3026
3027 /* type */
3028 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3029
3030 /* rate */
3031 chunk_appendf(&trash, "%u,,%u,",
3032 read_freq_ctr(&sv->sess_per_sec),
3033 sv->counters.sps_max);
3034
Willy Tarreauff5ae352013-12-11 20:36:34 +01003035 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003036 /* check_status */
3037 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3038
3039 /* check_code */
3040 if (sv->check.status >= HCHK_STATUS_L57DATA)
3041 chunk_appendf(&trash, "%u,", sv->check.code);
3042 else
3043 chunk_appendf(&trash, ",");
3044
3045 /* check_duration */
3046 if (sv->check.status >= HCHK_STATUS_CHECKED)
3047 chunk_appendf(&trash, "%lu,", sv->check.duration);
3048 else
3049 chunk_appendf(&trash, ",");
3050
3051 }
3052 else
3053 chunk_appendf(&trash, ",,,");
3054
3055 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3056 if (px->mode == PR_MODE_HTTP) {
3057 for (i=1; i<6; i++)
3058 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3059
3060 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3061 }
3062 else
3063 chunk_appendf(&trash, ",,,,,,");
3064
3065 /* failed health analyses */
3066 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3067
3068 /* requests : req_rate, req_rate_max, req_tot, */
3069 chunk_appendf(&trash, ",,,");
3070
3071 /* errors: cli_aborts, srv_aborts */
3072 chunk_appendf(&trash, "%lld,%lld,",
3073 sv->counters.cli_aborts, sv->counters.srv_aborts);
3074
3075 /* compression: in, out, bypassed, comp_rsp */
3076 chunk_appendf(&trash, ",,,,");
3077
3078 /* finish with EOL */
3079 chunk_appendf(&trash, "\n");
3080 }
3081 return 1;
3082}
3083
3084/* Dumps a line for backend <px> to the trash for and uses the state from stream
3085 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3086 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3087 */
3088static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3089{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003090 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003091 struct chunk src;
3092 int i;
3093
3094 if (!(px->cap & PR_CAP_BE))
3095 return 0;
3096
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003097 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003098 return 0;
3099
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003100 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003101 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003103 /* Column sub-heading for Enable or Disable server */
3104 chunk_appendf(&trash, "<td></td>");
3105 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003106 chunk_appendf(&trash,
3107 "<td class=ac>"
3108 /* name */
3109 "%s<a name=\"%s/Backend\"></a>"
3110 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3111 "",
3112 (flags & ST_SHLGNDS)?"<u>":"",
3113 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003114
3115 if (flags & ST_SHLGNDS) {
3116 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003117 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003118 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3119
3120 /* cookie */
3121 if (px->cookie_name) {
3122 chunk_appendf(&trash, ", cookie: '");
3123 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3124 chunk_htmlencode(&trash, &src);
3125 chunk_appendf(&trash, "'");
3126 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003127 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003128 }
3129
3130 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003131 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132 /* queue : current, max */
3133 "<td>%s</td><td>%s</td><td></td>"
3134 /* sessions rate : current, max, limit */
3135 "<td>%s</td><td>%s</td><td></td>"
3136 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003138 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3139 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140
3141 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003142 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003144 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003145 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003146 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003147 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3148 U2H(px->be_counters.cum_conn),
3149 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003150
Willy Tarreau466c9b52012-12-23 02:25:03 +01003151 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003153 chunk_appendf(&trash,
3154 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3155 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3156 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3157 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3158 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3159 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3160 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3161 "<tr><th>- other responses:</th><td>%s</td></tr>"
3162 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3163 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003164 U2H(px->be_counters.p.http.cum_req),
3165 U2H(px->be_counters.p.http.rsp[1]),
3166 U2H(px->be_counters.p.http.rsp[2]),
3167 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003169 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003170 U2H(px->be_counters.p.http.rsp[3]),
3171 U2H(px->be_counters.p.http.rsp[4]),
3172 U2H(px->be_counters.p.http.rsp[5]),
3173 U2H(px->be_counters.p.http.rsp[0]),
3174 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 }
3176
3177 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003178 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003179 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01003180 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003181 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003182 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003183 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003184 U2H(px->be_counters.cum_lbconn),
3185 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003186
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003187 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003188 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003189 "<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 +01003190 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003191 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003192 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3193 px->be_counters.comp_in ?
3194 (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 +01003195 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3196
3197 chunk_appendf(&trash,
3198 /* denied: req, resp */
3199 "<td>%s</td><td>%s</td>"
3200 /* errors : request, connect */
3201 "<td></td><td>%s</td>"
3202 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003203 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003204 /* warnings: retries, redispatches */
3205 "<td>%lld</td><td>%lld</td>"
3206 /* backend status: reflect backend status (up/down): we display UP
3207 * if the backend has known working servers or if it has no server at
3208 * all (eg: for stats). Then we display the total weight, number of
3209 * active and backups. */
3210 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3211 "<td class=ac>%d</td><td class=ac>%d</td>"
3212 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003213 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3214 U2H(px->be_counters.failed_conns),
3215 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003216 px->be_counters.cli_aborts,
3217 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218 px->be_counters.retries, px->be_counters.redispatches,
3219 human_time(now.tv_sec - px->last_change, 1),
3220 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3221 "<font color=\"red\"><b>DOWN</b></font>",
3222 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3223 px->srv_act, px->srv_bck);
3224
3225 chunk_appendf(&trash,
3226 /* rest of backend: nothing, down transitions, total downtime, throttle */
3227 "<td class=ac>&nbsp;</td><td>%d</td>"
3228 "<td>%s</td>"
3229 "<td></td>"
3230 "</tr>",
3231 px->down_trans,
3232 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3233 }
3234 else { /* CSV mode */
3235 chunk_appendf(&trash,
3236 /* pxid, name */
3237 "%s,BACKEND,"
3238 /* queue : current, max */
3239 "%d,%d,"
3240 /* sessions : current, max, limit, total */
3241 "%d,%d,%d,%lld,"
3242 /* bytes : in, out */
3243 "%lld,%lld,"
3244 /* denied: req, resp */
3245 "%lld,%lld,"
3246 /* errors : request, connect, response */
3247 ",%lld,%lld,"
3248 /* warnings: retries, redispatches */
3249 "%lld,%lld,"
3250 /* backend status: reflect backend status (up/down): we display UP
3251 * if the backend has known working servers or if it has no server at
3252 * all (eg: for stats). Then we display the total weight, number of
3253 * active and backups. */
3254 "%s,"
3255 "%d,%d,%d,"
3256 /* rest of backend: nothing, down transitions, last change, total downtime */
3257 ",%d,%d,%d,,"
3258 /* pid, iid, sid, throttle, lbtot, tracked, type */
3259 "%d,%d,0,,%lld,,%d,"
3260 /* rate, rate_lim, rate_max, */
3261 "%u,,%u,"
3262 /* check_status, check_code, check_duration */
3263 ",,,",
3264 px->id,
3265 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3266 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3267 px->be_counters.bytes_in, px->be_counters.bytes_out,
3268 px->be_counters.denied_req, px->be_counters.denied_resp,
3269 px->be_counters.failed_conns, px->be_counters.failed_resp,
3270 px->be_counters.retries, px->be_counters.redispatches,
3271 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3272 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3273 px->srv_act, px->srv_bck,
3274 px->down_trans, (int)(now.tv_sec - px->last_change),
3275 px->srv?be_downtime(px):0,
3276 relative_pid, px->uuid,
3277 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3278 read_freq_ctr(&px->be_sess_per_sec),
3279 px->be_counters.sps_max);
3280
3281 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3282 if (px->mode == PR_MODE_HTTP) {
3283 for (i=1; i<6; i++)
3284 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3285 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3286 }
3287 else
3288 chunk_appendf(&trash, ",,,,,,");
3289
3290 /* failed health analyses */
3291 chunk_appendf(&trash, ",");
3292
3293 /* requests : req_rate, req_rate_max, req_tot, */
3294 chunk_appendf(&trash, ",,,");
3295
3296 /* errors: cli_aborts, srv_aborts */
3297 chunk_appendf(&trash, "%lld,%lld,",
3298 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3299
3300 /* compression: in, out, bypassed */
3301 chunk_appendf(&trash, "%lld,%lld,%lld,",
3302 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3303
3304 /* compression: comp_rsp */
3305 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3306
3307 /* finish with EOL */
3308 chunk_appendf(&trash, "\n");
3309 }
3310 return 1;
3311}
3312
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003313/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003315 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003317static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003318{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003319 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003320 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003322 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003324
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003325 /* 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 +02003326 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003327 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003328 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003329 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3330 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003331 }
3332
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003334 "<form action=\"%s%s%s%s\" method=\"post\">",
3335 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003336 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3337 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003338 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 }
3340
3341 /* print a new table */
3342 chunk_appendf(&trash,
3343 "<table class=\"tbl\" width=\"100%%\">\n"
3344 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003345 "<th class=\"pxname\" width=\"10%%\">");
3346
3347 chunk_appendf(&trash,
3348 "<a name=\"%s\"></a>%s"
3349 "<a class=px href=\"#%s\">%s</a>",
3350 px->id,
3351 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3352 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353
3354 if (uri->flags & ST_SHLGNDS) {
3355 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003356 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3358 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003359 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 }
3361
3362 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003363 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 "<th class=\"%s\" width=\"90%%\">%s</th>"
3365 "</tr>\n"
3366 "</table>\n"
3367 "<table class=\"tbl\" width=\"100%%\">\n"
3368 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3370 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003372 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 /* Column heading for Enable or Disable server */
3374 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003375 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376
3377 chunk_appendf(&trash,
3378 "<th rowspan=2></th>"
3379 "<th colspan=3>Queue</th>"
3380 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
3381 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3382 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3383 "<th colspan=9>Server</th>"
3384 "</tr>\n"
3385 "<tr class=\"titre\">"
3386 "<th>Cur</th><th>Max</th><th>Limit</th>"
3387 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3388 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
3389 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3390 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3391 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3392 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3393 "<th>Thrtle</th>\n"
3394 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003395}
3396
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003397/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003400static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003401{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003402 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 chunk_appendf(&trash, "</table>");
3404
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003405 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003406 /* close the form used to enable/disable this proxy servers */
3407 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003408 "Choose the action to perform on the checked servers : "
3409 "<select name=action>"
3410 "<option value=\"\"></option>"
3411 "<option value=\"disable\">Disable</option>"
3412 "<option value=\"enable\">Enable</option>"
3413 "<option value=\"stop\">Soft Stop</option>"
3414 "<option value=\"start\">Soft Start</option>"
3415 "<option value=\"shutdown\">Kill Sessions</option>"
3416 "</select>"
3417 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3418 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3419 "</form>",
3420 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421 }
3422
3423 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424}
Willy Tarreau91861262007-10-17 17:06:05 +02003425
3426/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003427 * Dumps statistics for a proxy. The output is sent to the stream interface's
3428 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3429 * buffer space, or non-zero if everything completed. This function is used
3430 * both by the CLI and the HTTP entry points, and is able to dump the output
3431 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003432 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003433static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003434{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003435 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003436 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003437 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003438 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003439 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003440
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003441 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003442
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003443 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003444 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003445 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003446 if (uri && uri->scope) {
3447 /* we have a limited scope, we have to check the proxy name */
3448 struct stat_scope *scope;
3449 int len;
3450
3451 len = strlen(px->id);
3452 scope = uri->scope;
3453
3454 while (scope) {
3455 /* match exact proxy name */
3456 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3457 break;
3458
3459 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003460 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003461 break;
3462 scope = scope->next;
3463 }
3464
3465 /* proxy name not found : don't dump anything */
3466 if (scope == NULL)
3467 return 1;
3468 }
3469
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003470 /* if the user has requested a limited output and the proxy
3471 * name does not match, skip it.
3472 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003473 if (appctx->ctx.stats.scope_len &&
3474 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 +02003475 return 1;
3476
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003477 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3478 (appctx->ctx.stats.iid != -1) &&
3479 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003480 return 1;
3481
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003482 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003483 /* fall through */
3484
Willy Tarreau295a8372011-03-10 11:25:07 +01003485 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003486 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003487 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003488 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003489 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003490 }
Willy Tarreau91861262007-10-17 17:06:05 +02003491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003492 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003493 /* fall through */
3494
Willy Tarreau295a8372011-03-10 11:25:07 +01003495 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003496 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003498 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003499 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003500
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003501 appctx->ctx.stats.l = px->conf.listeners.n;
3502 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003503 /* fall through */
3504
Willy Tarreau295a8372011-03-10 11:25:07 +01003505 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003506 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003507 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003508 if (buffer_almost_full(rep->buf)) {
3509 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003510 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003511 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003512
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003513 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003514 if (!l->counters)
3515 continue;
3516
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003517 if (appctx->ctx.stats.flags & STAT_BOUND) {
3518 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003519 break;
3520
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003521 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003522 continue;
3523 }
3524
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003525 /* print the frontend */
3526 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3527 if (bi_putchk(rep, &trash) == -1)
3528 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003529 }
3530
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003531 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3532 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003533 /* fall through */
3534
Willy Tarreau295a8372011-03-10 11:25:07 +01003535 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003536 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003537 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003538 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 +02003539
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003540 if (buffer_almost_full(rep->buf)) {
3541 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003542 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003543 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003544
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003545 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547 if (appctx->ctx.stats.flags & STAT_BOUND) {
3548 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003549 break;
3550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003551 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003552 continue;
3553 }
3554
Willy Tarreau44267702011-10-28 15:35:33 +02003555 if (sv->track)
3556 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003557 else
3558 svs = sv;
3559
Willy Tarreau91861262007-10-17 17:06:05 +02003560 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003561 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003562 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003563 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003564 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003565 sv_state = 3; /* UP */
3566 else
3567 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003568
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003569 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003570 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003571 else if (svs->state & SRV_GOINGDOWN)
3572 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003573 }
Willy Tarreau91861262007-10-17 17:06:05 +02003574 else
Simon Horman125d0992013-02-24 17:23:38 +09003575 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003576 sv_state = 1; /* going up */
3577 else
3578 sv_state = 0; /* DOWN */
3579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003580 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003581 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003582 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003583 continue;
3584 }
3585
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003586 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3587 if (bi_putchk(rep, &trash) == -1)
3588 return 0;
3589 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003590
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003591 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003592 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003593
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003594 case STAT_PX_ST_BE:
3595 /* print the backend */
3596 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3597 if (bi_putchk(rep, &trash) == -1)
3598 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003599
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003600 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003602
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003603 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003604 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003605 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003606 if (bi_putchk(rep, &trash) == -1)
3607 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003608 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003609
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003610 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003611 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003613 case STAT_PX_ST_FIN:
3614 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003615
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003616 default:
3617 /* unknown state, we should put an abort() here ! */
3618 return 1;
3619 }
3620}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003621
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003622/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3623 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003624 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003625static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003626{
3627 /* WARNING! This must fit in the first buffer !!! */
3628 chunk_appendf(&trash,
3629 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3630 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3631 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3632 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3633 "<style type=\"text/css\"><!--\n"
3634 "body {"
3635 " font-family: arial, helvetica, sans-serif;"
3636 " font-size: 12px;"
3637 " font-weight: normal;"
3638 " color: black;"
3639 " background: white;"
3640 "}\n"
3641 "th,td {"
3642 " font-size: 10px;"
3643 "}\n"
3644 "h1 {"
3645 " font-size: x-large;"
3646 " margin-bottom: 0.5em;"
3647 "}\n"
3648 "h2 {"
3649 " font-family: helvetica, arial;"
3650 " font-size: x-large;"
3651 " font-weight: bold;"
3652 " font-style: italic;"
3653 " color: #6020a0;"
3654 " margin-top: 0em;"
3655 " margin-bottom: 0em;"
3656 "}\n"
3657 "h3 {"
3658 " font-family: helvetica, arial;"
3659 " font-size: 16px;"
3660 " font-weight: bold;"
3661 " color: #b00040;"
3662 " background: #e8e8d0;"
3663 " margin-top: 0em;"
3664 " margin-bottom: 0em;"
3665 "}\n"
3666 "li {"
3667 " margin-top: 0.25em;"
3668 " margin-right: 2em;"
3669 "}\n"
3670 ".hr {margin-top: 0.25em;"
3671 " border-color: black;"
3672 " border-bottom-style: solid;"
3673 "}\n"
3674 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3675 ".total {background: #20D0D0;color: #ffff80;}\n"
3676 ".frontend {background: #e8e8d0;}\n"
3677 ".socket {background: #d0d0d0;}\n"
3678 ".backend {background: #e8e8d0;}\n"
3679 ".active0 {background: #ff9090;}\n"
3680 ".active1 {background: #ffd020;}\n"
3681 ".active2 {background: #ffffa0;}\n"
3682 ".active3 {background: #c0ffc0;}\n"
3683 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003684 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3685 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3686 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003687 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003688 ".backup0 {background: #ff9090;}\n"
3689 ".backup1 {background: #ff80ff;}\n"
3690 ".backup2 {background: #c060ff;}\n"
3691 ".backup3 {background: #b0d0ff;}\n"
3692 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3693 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003694 ".backup6 {background: #c060ff;}\n"
3695 ".backup7 {background: #cc9900;}\n"
3696 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003697 ".maintain {background: #c07820;}\n"
3698 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3699 "\n"
3700 "a.px:link {color: #ffff40; text-decoration: none;}"
3701 "a.px:visited {color: #ffff40; text-decoration: none;}"
3702 "a.px:hover {color: #ffffff; text-decoration: none;}"
3703 "a.lfsb:link {color: #000000; text-decoration: none;}"
3704 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3705 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3706 "\n"
3707 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3708 "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"
3709 "table.tbl td.ac { text-align: center;}\n"
3710 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3711 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3712 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3713 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3714 "\n"
3715 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3716 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3717 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003718 "table.det { border-collapse: collapse; border-style: none; }\n"
3719 "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 +01003720 "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 +01003721 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003722 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003723 " display:block;\n"
3724 " visibility:hidden;\n"
3725 " z-index:2147483647;\n"
3726 " position:absolute;\n"
3727 " padding:2px 4px 3px;\n"
3728 " background:#f0f060; color:#000000;\n"
3729 " border:1px solid #7040c0;\n"
3730 " white-space:nowrap;\n"
3731 " font-style:normal;font-size:11px;font-weight:normal;\n"
3732 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3733 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3734 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003735 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003736 "-->\n"
3737 "</style></head>\n",
3738 (uri->flags & ST_SHNODE) ? " on " : "",
3739 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3740 );
3741}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003742
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003743/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003744 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003745 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003746 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003747static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003748{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003749 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003750 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003751 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003753 /* WARNING! this has to fit the first packet too.
3754 * We are around 3.5 kB, add adding entries will
3755 * become tricky if we want to support 4kB buffers !
3756 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003757 chunk_appendf(&trash,
3758 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3759 PRODUCT_NAME "%s</a></h1>\n"
3760 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3761 "<hr width=\"100%%\" class=\"hr\">\n"
3762 "<h3>&gt; General process information</h3>\n"
3763 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3764 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3765 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3766 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3767 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3768 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3769 "Running tasks: %d/%d; idle = %d %%<br>\n"
3770 "</td><td align=\"center\" nowrap>\n"
3771 "<table class=\"lgd\"><tr>\n"
3772 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3773 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3774 "</tr><tr>\n"
3775 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3776 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3777 "</tr><tr>\n"
3778 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3779 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3780 "</tr><tr>\n"
3781 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003782 "</tr><tr>\n"
3783 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003784 "</tr><tr>\n"
3785 "<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 -07003786 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003787 "<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 +01003788 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003789 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003790 "</td>"
3791 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3792 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3793 "",
3794 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3795 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3796 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3797 (uri->flags & ST_SHDESC) ? ": " : "",
3798 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3799 pid, relative_pid, global.nbproc,
3800 up / 86400, (up % 86400) / 3600,
3801 (up % 3600) / 60, (up % 60),
3802 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3803 global.rlimit_memmax ? " MB" : "",
3804 global.rlimit_nofile,
3805 global.maxsock, global.maxconn, global.maxpipes,
3806 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3807 run_queue_cur, nb_tasks_cur, idle_pct
3808 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003809
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003810 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3811 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3812 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003813
3814 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003815 "<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 +02003816 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003817 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3818 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3819 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003820 STAT_SCOPE_TXT_MAXLEN);
3821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003822 /* 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 +02003823 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003824 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003825 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003826 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3827 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003828 }
3829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003830 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003831 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003832 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833 uri->uri_prefix,
3834 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003835 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003836 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003837 else
3838 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003839 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003840 uri->uri_prefix,
3841 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003842 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003843 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003844
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003845 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003846 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003847 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003848 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003850 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003851 "",
3852 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003853 else
3854 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003855 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003856 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003857 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003858 ";norefresh",
3859 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003861
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003862 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003863 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003864 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003865 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3866 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003867 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003868
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003870 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003871 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003872 (uri->refresh > 0) ? ";norefresh" : "",
3873 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003874
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003875 chunk_appendf(&trash,
3876 "</ul></td>"
3877 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3878 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3879 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3880 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3881 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3882 "</ul>"
3883 "</td>"
3884 "</tr></table>\n"
3885 ""
3886 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003887
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003888 if (appctx->ctx.stats.st_code) {
3889 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003890 case STAT_STATUS_DONE:
3891 chunk_appendf(&trash,
3892 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003893 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003894 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003895 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003896 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3897 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003898 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003899 break;
3900 case STAT_STATUS_NONE:
3901 chunk_appendf(&trash,
3902 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003903 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003904 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003905 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003906 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3907 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003908 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003909 break;
3910 case STAT_STATUS_PART:
3911 chunk_appendf(&trash,
3912 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003913 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003914 "Action partially processed.<br>"
3915 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003916 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003917 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3918 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003919 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003920 break;
3921 case STAT_STATUS_ERRP:
3922 chunk_appendf(&trash,
3923 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003924 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003925 "Action not processed because of invalid parameters."
3926 "<ul>"
3927 "<li>The action is maybe unknown.</li>"
3928 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3929 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3930 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003931 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003932 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3933 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003934 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003935 break;
3936 case STAT_STATUS_EXCD:
3937 chunk_appendf(&trash,
3938 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003939 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003940 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3941 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003942 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003943 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3944 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003945 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003946 break;
3947 case STAT_STATUS_DENY:
3948 chunk_appendf(&trash,
3949 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003950 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003951 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003952 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003953 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3954 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003955 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003956 break;
3957 default:
3958 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003959 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003960 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003961 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003962 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003963 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3964 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003965 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 }
3967 chunk_appendf(&trash, "<p>\n");
3968 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003969}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003970
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003971/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3972 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003973 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003974static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003975{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003976 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003977}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003978
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003979/* This function dumps statistics onto the stream interface's read buffer in
3980 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003981 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3982 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3983 * and the session must be closed, or -1 in case of any error. This function is
3984 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003986static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003987{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003988 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003989 struct channel *rep = si->ib;
3990 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003991
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003994 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003997 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003998
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003999 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004000 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004001 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004002 else
4003 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004004
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 if (bi_putchk(rep, &trash) == -1)
4006 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004008 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004009 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004010
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004011 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004012 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004013 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004014 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004015 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016 }
Willy Tarreau91861262007-10-17 17:06:05 +02004017
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004018 appctx->ctx.stats.px = proxy;
4019 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4020 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004021 /* fall through */
4022
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004023 case STAT_ST_LIST:
4024 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004025 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004026 if (buffer_almost_full(rep->buf)) {
4027 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004028 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004029 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004030
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004031 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032 /* skip the disabled proxies, global frontend and non-networked ones */
4033 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004034 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004035 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004037 appctx->ctx.stats.px = px->next;
4038 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 }
4040 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004042 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004043 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004044
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004045 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004046 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004047 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004048 if (bi_putchk(rep, &trash) == -1)
4049 return 0;
4050 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004051
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004052 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004053 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004054
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004055 case STAT_ST_FIN:
4056 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004057
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004058 default:
4059 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004060 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004061 return -1;
4062 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063}
Willy Tarreauae526782010-03-04 20:34:23 +01004064
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004065/* We reached the stats page through a POST request. The appctx is
4066 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004067 * Parse the posted data and enable/disable servers if necessary.
4068 * Returns 1 if request was parsed or zero if it needs more data.
4069 */
4070static int stats_process_http_post(struct stream_interface *si)
4071{
4072 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004073 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004074
4075 struct proxy *px = NULL;
4076 struct server *sv = NULL;
4077
4078 char key[LINESIZE];
4079 int action = ST_ADM_ACTION_NONE;
4080 int reprocess = 0;
4081
4082 int total_servers = 0;
4083 int altered_servers = 0;
4084
4085 char *first_param, *cur_param, *next_param, *end_params;
4086 char *st_cur_param = NULL;
4087 char *st_next_param = NULL;
4088
4089 struct chunk *temp;
4090 int reql;
4091
4092 temp = get_trash_chunk();
4093 if (temp->size < s->txn.req.body_len) {
4094 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004095 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004096 goto out;
4097 }
4098
4099 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4100 if (reql <= 0) {
4101 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004102 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004103 return 0;
4104 }
4105
4106 first_param = temp->str;
4107 end_params = temp->str + reql;
4108 cur_param = next_param = end_params;
4109 *end_params = '\0';
4110
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004111 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004112
4113 /*
4114 * Parse the parameters in reverse order to only store the last value.
4115 * From the html form, the backend and the action are at the end.
4116 */
4117 while (cur_param > first_param) {
4118 char *value;
4119 int poffset, plen;
4120
4121 cur_param--;
4122
4123 if ((*cur_param == '&') || (cur_param == first_param)) {
4124 reprocess_servers:
4125 /* Parse the key */
4126 poffset = (cur_param != first_param ? 1 : 0);
4127 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4128 if ((plen > 0) && (plen <= sizeof(key))) {
4129 strncpy(key, cur_param + poffset, plen);
4130 key[plen - 1] = '\0';
4131 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004132 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004133 goto out;
4134 }
4135
4136 /* Parse the value */
4137 value = key;
4138 while (*value != '\0' && *value != '=') {
4139 value++;
4140 }
4141 if (*value == '=') {
4142 /* Ok, a value is found, we can mark the end of the key */
4143 *value++ = '\0';
4144 }
4145 if (url_decode(key) < 0 || url_decode(value) < 0)
4146 break;
4147
4148 /* Now we can check the key to see what to do */
4149 if (!px && (strcmp(key, "b") == 0)) {
4150 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4151 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004152 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004153 goto out;
4154 }
4155 }
4156 else if (!action && (strcmp(key, "action") == 0)) {
4157 if (strcmp(value, "disable") == 0) {
4158 action = ST_ADM_ACTION_DISABLE;
4159 }
4160 else if (strcmp(value, "enable") == 0) {
4161 action = ST_ADM_ACTION_ENABLE;
4162 }
4163 else if (strcmp(value, "stop") == 0) {
4164 action = ST_ADM_ACTION_STOP;
4165 }
4166 else if (strcmp(value, "start") == 0) {
4167 action = ST_ADM_ACTION_START;
4168 }
4169 else if (strcmp(value, "shutdown") == 0) {
4170 action = ST_ADM_ACTION_SHUTDOWN;
4171 }
4172 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004173 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004174 goto out;
4175 }
4176 }
4177 else if (strcmp(key, "s") == 0) {
4178 if (!(px && action)) {
4179 /*
4180 * Indicates that we'll need to reprocess the parameters
4181 * as soon as backend and action are known
4182 */
4183 if (!reprocess) {
4184 st_cur_param = cur_param;
4185 st_next_param = next_param;
4186 }
4187 reprocess = 1;
4188 }
4189 else if ((sv = findserver(px, value)) != NULL) {
4190 switch (action) {
4191 case ST_ADM_ACTION_DISABLE:
4192 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4193 /* Not already in maintenance, we can change the server state */
4194 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004195 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004196 set_server_down(&sv->check);
4197 altered_servers++;
4198 total_servers++;
4199 }
4200 break;
4201 case ST_ADM_ACTION_ENABLE:
4202 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004203 /* Already in maintenance, we can change the server state.
4204 * If this server tracks the status of another one,
4205 * we must restore the good status.
4206 */
4207 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4208 set_server_up(&sv->check);
4209 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4210 }
4211 else {
4212 sv->state &= ~SRV_MAINTAIN;
4213 sv->check.state &= ~CHK_ST_PAUSED;
4214 set_server_down(&sv->check);
4215 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004216 altered_servers++;
4217 total_servers++;
4218 }
4219 break;
4220 case ST_ADM_ACTION_STOP:
4221 case ST_ADM_ACTION_START:
4222 if (action == ST_ADM_ACTION_START)
4223 sv->uweight = sv->iweight;
4224 else
4225 sv->uweight = 0;
4226
4227 server_recalc_eweight(sv);
4228 set_server_drain_state(sv);
4229
4230 altered_servers++;
4231 total_servers++;
4232 break;
4233 case ST_ADM_ACTION_SHUTDOWN:
4234 if (px->state != PR_STSTOPPED) {
4235 struct session *sess, *sess_bck;
4236
4237 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4238 if (sess->srv_conn == sv)
4239 session_shutdown(sess, SN_ERR_KILLED);
4240
4241 altered_servers++;
4242 total_servers++;
4243 }
4244 break;
4245 }
4246 } else {
4247 /* the server name is unknown or ambiguous (duplicate names) */
4248 total_servers++;
4249 }
4250 }
4251 if (reprocess && px && action) {
4252 /* Now, we know the backend and the action chosen by the user.
4253 * We can safely restart from the first server parameter
4254 * to reprocess them
4255 */
4256 cur_param = st_cur_param;
4257 next_param = st_next_param;
4258 reprocess = 0;
4259 goto reprocess_servers;
4260 }
4261
4262 next_param = cur_param;
4263 }
4264 }
4265
4266 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004267 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004268 }
4269 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004270 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004271 }
4272 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004273 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004274 }
4275 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004276 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004277 }
4278 out:
4279 return 1;
4280}
4281
4282
4283static int stats_send_http_headers(struct stream_interface *si)
4284{
4285 struct session *s = session_from_task(si->owner);
4286 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004287 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004288
4289 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004290 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004291 "Cache-Control: no-cache\r\n"
4292 "Connection: close\r\n"
4293 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004294 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004295
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004296 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004297 chunk_appendf(&trash, "Refresh: %d\r\n",
4298 uri->refresh);
4299
Willy Tarreau51437d22013-12-29 00:43:40 +01004300 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004301
4302 s->txn.status = 200;
4303 s->logs.tv_request = now;
4304
4305 if (bi_putchk(si->ib, &trash) == -1)
4306 return 0;
4307
4308 return 1;
4309}
4310
4311static int stats_send_http_redirect(struct stream_interface *si)
4312{
4313 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4314 struct session *s = session_from_task(si->owner);
4315 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004316 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004318 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004319 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004320 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004321 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004322 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4323 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004324 }
4325
4326 /* We don't want to land on the posted stats page because a refresh will
4327 * repost the data. We don't want this to happen on accident so we redirect
4328 * the browse to the stats page with a GET.
4329 */
4330 chunk_printf(&trash,
4331 "HTTP/1.1 303 See Other\r\n"
4332 "Cache-Control: no-cache\r\n"
4333 "Content-Type: text/plain\r\n"
4334 "Connection: close\r\n"
4335 "Location: %s;st=%s%s%s%s\r\n"
4336 "\r\n",
4337 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004338 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4339 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4340 stat_status_codes[appctx->ctx.stats.st_code]) ?
4341 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004342 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004343 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4344 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004345 scope_txt);
4346
4347 s->txn.status = 303;
4348 s->logs.tv_request = now;
4349
4350 if (bi_putchk(si->ib, &trash) == -1)
4351 return 0;
4352
4353 return 1;
4354}
4355
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356/* This I/O handler runs as an applet embedded in a stream interface. It is
4357 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004358 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004359 * automatically unregisters itself once transfer is complete.
4360 */
4361static void http_stats_io_handler(struct stream_interface *si)
4362{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004364 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365 struct channel *req = si->ob;
4366 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4369 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004370
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 /* check that the output is not closed */
4372 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004373 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004374
Willy Tarreau347a35d2013-11-22 17:51:09 +01004375 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004377 if (stats_send_http_headers(si)) {
4378 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004379 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004380 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004381 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004383 }
4384
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004385 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004386 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004388 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004389
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004390 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004391 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004393 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004394 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004395 }
4396
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004397 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004398 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004399 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004400 }
4401
Willy Tarreau51437d22013-12-29 00:43:40 +01004402 if (appctx->st0 == STAT_HTTP_DONE)
4403 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004405 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4406 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004407
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004408 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004409 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4410 si_shutr(si);
4411 res->flags |= CF_READ_NULL;
4412 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004413 }
Willy Tarreau91861262007-10-17 17:06:05 +02004414
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004415 /* update all other flags and resync with the other side */
4416 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004417
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004418 /* we don't want to expire timeouts while we're processing requests */
4419 si->ib->rex = TICK_ETERNITY;
4420 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004421
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004422 out:
4423 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4424 /* check that we have released everything then unregister */
4425 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004426 }
4427}
4428
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004429
Willy Tarreau909d5172012-11-26 03:04:41 +01004430static inline const char *get_conn_ctrl_name(const struct connection *conn)
4431{
Willy Tarreau3c728722014-01-23 13:50:42 +01004432 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004433 return "NONE";
4434 return conn->ctrl->name;
4435}
4436
4437static inline const char *get_conn_xprt_name(const struct connection *conn)
4438{
4439 static char ptr[17];
4440
Willy Tarreauaad69382014-01-23 14:21:42 +01004441 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004442 return "NONE";
4443
4444 if (conn->xprt == &raw_sock)
4445 return "RAW";
4446
4447#ifdef USE_OPENSSL
4448 if (conn->xprt == &ssl_sock)
4449 return "SSL";
4450#endif
4451 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4452 return ptr;
4453}
4454
4455static inline const char *get_conn_data_name(const struct connection *conn)
4456{
4457 static char ptr[17];
4458
4459 if (!conn->data)
4460 return "NONE";
4461
4462 if (conn->data == &sess_conn_cb)
4463 return "SESS";
4464
4465 if (conn->data == &si_conn_cb)
4466 return "STRM";
4467
4468 if (conn->data == &check_conn_cb)
4469 return "CHCK";
4470
4471 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4472 return ptr;
4473}
4474
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004475/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004476 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004477 * 0 if the output buffer is full and it needs to be called again, otherwise
4478 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004479 */
Willy Tarreau76153662012-11-26 01:16:39 +01004480static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004481{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004482 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004483 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004484 extern const char *monthname[12];
4485 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004486 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004487 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004488
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004489 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004490
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004491 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004492 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004493 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4494 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004495 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004496 appctx->ctx.sess.uid = 0;
4497 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004498 return 1;
4499 }
4500
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004501 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004502 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004503 appctx->ctx.sess.uid = sess->uniq_id;
4504 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004505 /* fall through */
4506
4507 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004508 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004509 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004510 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004511 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004512 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4513 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004514 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004515 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004516
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004517 conn = objt_conn(sess->si[0].end);
4518 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004519 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004520 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004521 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004522 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004523 break;
4524 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004525 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004526 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004527 default:
4528 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004529 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004530 break;
4531 }
4532
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004533 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004534 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004535 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004536
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004537 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004538 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004539 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4540 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4541 sess->listener ? sess->listener->luid : 0);
4542
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004543 if (conn)
4544 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004545
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004546 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004547 case AF_INET:
4548 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004549 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004550 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004551 break;
4552 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004553 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004554 break;
4555 default:
4556 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004557 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004558 break;
4559 }
4560
Willy Tarreau50943332011-09-02 17:33:05 +02004561 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004562 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004563 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004564 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004565 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4566 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004567 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004568
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004569 conn = objt_conn(sess->si[1].end);
4570 if (conn)
4571 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004572
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004573 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004574 case AF_INET:
4575 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004576 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004577 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004578 break;
4579 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004580 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004581 break;
4582 default:
4583 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004584 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004585 break;
4586 }
4587
4588 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004589 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004590 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004591 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4592 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004593 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004594 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004595
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004596 if (conn)
4597 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004598
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004599 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004600 case AF_INET:
4601 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004602 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004603 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004604 break;
4605 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004606 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004607 break;
4608 default:
4609 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004610 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004611 break;
4612 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004613
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004614 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004615 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004616 sess->task,
4617 sess->task->state,
4618 sess->task->nice, sess->task->calls,
4619 sess->task->expire ?
4620 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4621 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4622 TICKS_TO_MS(1000)) : "<NEVER>",
4623 task_in_rq(sess->task) ? ", running" : "");
4624
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004625 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004626 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004627 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4628
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004630 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4631 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4632 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4633
4634 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004635 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004636 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004637 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004638 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004639 obj_type_name(sess->si[0].end),
4640 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004641 sess->si[0].exp ?
4642 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4643 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4644 TICKS_TO_MS(1000)) : "<NEVER>",
4645 sess->si[0].err_type);
4646
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004647 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004648 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004649 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004650 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004651 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004652 obj_type_name(sess->si[1].end),
4653 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004654 sess->si[1].exp ?
4655 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4656 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4657 TICKS_TO_MS(1000)) : "<NEVER>",
4658 sess->si[1].err_type);
4659
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004660 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004661 chunk_appendf(&trash,
4662 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004663 conn,
4664 get_conn_ctrl_name(conn),
4665 get_conn_xprt_name(conn),
4666 get_conn_data_name(conn),
4667 obj_type_name(conn->target),
4668 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004669
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004670 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004671 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004672 conn->flags,
4673 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004674 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004675 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004676 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004677 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004678 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4679 chunk_appendf(&trash,
4680 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4681 tmpctx,
4682 tmpctx->st0,
4683 tmpctx->st1,
4684 tmpctx->st2,
4685 tmpctx->applet->name);
4686 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004687
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004688 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004689 chunk_appendf(&trash,
4690 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004691 conn,
4692 get_conn_ctrl_name(conn),
4693 get_conn_xprt_name(conn),
4694 get_conn_data_name(conn),
4695 obj_type_name(conn->target),
4696 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004697
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004698 chunk_appendf(&trash,
4699 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004700 conn->flags,
4701 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004702 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004703 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004704 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004705 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004706 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4707 chunk_appendf(&trash,
4708 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4709 tmpctx,
4710 tmpctx->st0,
4711 tmpctx->st1,
4712 tmpctx->st2,
4713 tmpctx->applet->name);
4714 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004715
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004716 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004717 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004718 " an_exp=%s",
4719 sess->req,
4720 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004721 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004722 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004723 sess->req->analyse_exp ?
4724 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4725 TICKS_TO_MS(1000)) : "<NEVER>");
4726
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004727 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004728 " rex=%s",
4729 sess->req->rex ?
4730 human_time(TICKS_TO_MS(sess->req->rex - 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 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004735 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004736 sess->req->wex ?
4737 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4738 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004739 sess->req->buf,
4740 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004741 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004742 sess->txn.req.next, sess->req->buf->i,
4743 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004744
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004745 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004746 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004747 " an_exp=%s",
4748 sess->rep,
4749 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004750 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004751 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004752 sess->rep->analyse_exp ?
4753 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4754 TICKS_TO_MS(1000)) : "<NEVER>");
4755
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004756 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004757 " rex=%s",
4758 sess->rep->rex ?
4759 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4760 TICKS_TO_MS(1000)) : "<NEVER>");
4761
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004762 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004763 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004764 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004765 sess->rep->wex ?
4766 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4767 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004768 sess->rep->buf,
4769 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004770 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004771 sess->txn.rsp.next, sess->rep->buf->i,
4772 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004773
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004774 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004775 return 0;
4776
4777 /* use other states to dump the contents */
4778 }
4779 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004780 appctx->ctx.sess.uid = 0;
4781 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004782 return 1;
4783}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004784
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004785static int stats_maps_list(struct stream_interface *si)
4786{
4787 struct appctx *appctx = __objt_appctx(si->end);
4788
4789 switch (appctx->st2) {
4790 case STAT_ST_INIT:
4791 /* Init to the first entry. The list cannot be change */
4792 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4793 appctx->st2 = STAT_ST_LIST;
4794 /* fall through */
4795
4796 case STAT_ST_LIST:
4797 while (appctx->ctx.map.ref) {
4798
4799 chunk_reset(&trash);
4800
4801 /* build messages */
4802 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4803
4804 if (bi_putchk(si->ib, &trash) == -1) {
4805 /* let's try again later from this session. We add ourselves into
4806 * this session's users so that it can remove us upon termination.
4807 */
4808 return 0;
4809 }
4810
4811 /* get next list entry and check the end of the list */
4812 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4813 struct map_reference *, list);
4814 if (&appctx->ctx.map.ref->list == &maps)
4815 break;
4816 }
4817
4818 appctx->st2 = STAT_ST_FIN;
4819 /* fall through */
4820
4821 default:
4822 appctx->st2 = STAT_ST_FIN;
4823 return 1;
4824 }
4825}
4826
4827static const char *smp_to_type[SMP_TYPES] = {
4828 [SMP_T_BOOL] = "bool",
4829 [SMP_T_UINT] = "uint",
4830 [SMP_T_SINT] = "sint",
4831 [SMP_T_ADDR] = "addr",
4832 [SMP_T_IPV4] = "ipv4",
4833 [SMP_T_IPV6] = "ipv6",
4834 [SMP_T_STR] = "str",
4835 [SMP_T_BIN] = "bin",
4836 [SMP_T_CSTR] = "cstr",
4837 [SMP_T_CBIN] = "cbin",
4838};
4839
4840static int stats_map_lookup(struct stream_interface *si)
4841{
4842 struct appctx *appctx = __objt_appctx(si->end);
4843 struct sample_storage *smp;
4844 struct sample sample;
4845 struct pattern *pat;
4846 struct pat_idx_elt *elt;
4847 enum pat_match_res res;
4848 struct sockaddr_in addr;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004849 char addr_str[INET_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004850
4851 switch (appctx->st2) {
4852 case STAT_ST_INIT:
4853 appctx->ctx.map.desc = NULL;
4854 stats_map_lookup_next(si);
4855 appctx->st2 = STAT_ST_LIST;
4856 /* fall through */
4857
4858 case STAT_ST_LIST:
4859 /* for each lookup type */
4860 while (appctx->ctx.map.desc) {
4861 /* initialise chunk to build new message */
4862 chunk_reset(&trash);
4863
4864 /* execute pattern matching */
4865 sample.type = SMP_T_CSTR;
4866 sample.data.str.len = appctx->ctx.map.chunk.len;
4867 sample.data.str.str = appctx->ctx.map.chunk.str;
4868 pat = NULL;
4869 elt = NULL;
4870 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4871
4872 /* build return message: set type of match */
4873 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4874 chunk_appendf(&trash, "found, ");
4875 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4876 chunk_appendf(&trash, "bool, ");
4877 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4878 chunk_appendf(&trash, "int, ");
4879 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4880 chunk_appendf(&trash, "ip, ");
4881 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4882 chunk_appendf(&trash, "bin, ");
4883 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4884 chunk_appendf(&trash, "len, ");
4885 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4886 chunk_appendf(&trash, "str, ");
4887 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4888 chunk_appendf(&trash, "beg, ");
4889 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4890 chunk_appendf(&trash, "sub, ");
4891 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4892 chunk_appendf(&trash, "dir, ");
4893 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4894 chunk_appendf(&trash, "dom, ");
4895 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4896 chunk_appendf(&trash, "end, ");
4897 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4898 chunk_appendf(&trash, "reg, ");
4899 else /* The never appens case */
4900 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4901
4902 /* Display no match, and set default value */
4903 if (res == PAT_NOMATCH) {
4904 chunk_appendf(&trash, "no-match, ");
4905 smp = appctx->ctx.map.desc->def;
4906 }
4907
4908 /* Display match and match info */
4909 else {
4910 /* display match */
4911 chunk_appendf(&trash, "match, ");
4912
4913 /* display search mode */
4914 if (elt)
4915 chunk_appendf(&trash, "tree, ");
4916 else
4917 chunk_appendf(&trash, "list, ");
4918
4919 /* display search options */
4920 if (pat) {
4921 /* case sensitive */
4922 if (pat->flags & PAT_F_IGNORE_CASE)
4923 chunk_appendf(&trash, "case-insensitive, ");
4924 else
4925 chunk_appendf(&trash, "case-sensitive, ");
4926
4927 /* display source */
4928 if (pat->flags & PAT_F_FROM_FILE)
4929 chunk_appendf(&trash, "from-file, ");
4930 }
4931
4932 /* display match expresion */
4933 if (elt) {
4934 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4935 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4936 }
4937 /* only IPv4 */
4938 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4939 /* convert ip */
4940 memcpy(&addr.sin_addr, elt->node.key, 4);
4941 addr.sin_family = AF_INET;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004942 if (addr_to_str((struct sockaddr_storage *)&addr, addr_str, INET_ADDRSTRLEN))
4943 chunk_appendf(&trash, "match=\"%s/%d\", ", addr_str, elt->node.node.pfx);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004944 }
4945 }
4946 }
4947
4948 /* display return value */
4949 if (!smp) {
4950 chunk_appendf(&trash, "return=nothing\n");
4951 }
4952 else {
4953 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4954 sample.type = smp->type;
4955 if (sample_casts[sample.type][SMP_T_CSTR] &&
4956 sample_casts[sample.type][SMP_T_CSTR](&sample))
4957 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4958 sample.data.str.str, smp_to_type[smp->type]);
4959 else
4960 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4961 smp_to_type[smp->type]);
4962 }
4963
4964 /* display response */
4965 if (bi_putchk(si->ib, &trash) == -1) {
4966 /* let's try again later from this session. We add ourselves into
4967 * this session's users so that it can remove us upon termination.
4968 */
4969 return 0;
4970 }
4971
4972 /* get next entry */
4973 stats_map_lookup_next(si);
4974 }
4975
4976 appctx->st2 = STAT_ST_FIN;
4977 /* fall through */
4978
4979 default:
4980 appctx->st2 = STAT_ST_FIN;
4981 free(appctx->ctx.map.chunk.str);
4982 return 1;
4983 }
4984}
4985
4986static int stats_map_list(struct stream_interface *si)
4987{
4988 struct appctx *appctx = __objt_appctx(si->end);
4989
4990 switch (appctx->st2) {
4991
4992 case STAT_ST_INIT:
4993 /* Init to the first entry. The list cannot be change */
4994 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
4995 struct map_entry *, list);
4996 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4997 appctx->ctx.map.ent = NULL;
4998 appctx->st2 = STAT_ST_LIST;
4999 /* fall through */
5000
5001 case STAT_ST_LIST:
5002 while (appctx->ctx.map.ent) {
5003 chunk_reset(&trash);
5004
5005 /* build messages */
5006 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
5007
5008 if (bi_putchk(si->ib, &trash) == -1) {
5009 /* let's try again later from this session. We add ourselves into
5010 * this session's users so that it can remove us upon termination.
5011 */
5012 return 0;
5013 }
5014
5015 /* get next list entry and check the end of the list */
5016 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
5017 struct map_entry *, list);
5018 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
5019 break;
5020 }
5021
5022 appctx->st2 = STAT_ST_FIN;
5023 /* fall through */
5024
5025 default:
5026 appctx->st2 = STAT_ST_FIN;
5027 return 1;
5028 }
5029}
5030
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005031/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005032 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005033 * to be called again, otherwise non-zero. It is designed to be called
5034 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005035 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005036static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005037{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005038 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005039 struct connection *conn;
5040
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005041 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005042 /* If we're forced to shut down, we might have to remove our
5043 * reference to the last session being dumped.
5044 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005045 if (appctx->st2 == STAT_ST_LIST) {
5046 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5047 LIST_DEL(&appctx->ctx.sess.bref.users);
5048 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005049 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005050 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005051 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005052 }
5053
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005054 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005056 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005057 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005058 /* the function had not been called yet, let's prepare the
5059 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005060 * pointer to the first in the global list. When a target
5061 * session is being destroyed, it is responsible for updating
5062 * this pointer. We know we have reached the end when this
5063 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005064 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005065 LIST_INIT(&appctx->ctx.sess.bref.users);
5066 appctx->ctx.sess.bref.ref = sessions.n;
5067 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005068 /* fall through */
5069
Willy Tarreau295a8372011-03-10 11:25:07 +01005070 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005071 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005072 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5073 LIST_DEL(&appctx->ctx.sess.bref.users);
5074 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005075 }
5076
5077 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005078 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005079 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005080 struct session *curr_sess;
5081
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005082 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005083
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005084 if (appctx->ctx.sess.target) {
5085 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005086 goto next_sess;
5087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005088 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005089 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005090 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005091 return 0;
5092
5093 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 LIST_DEL(&appctx->ctx.sess.bref.users);
5095 LIST_INIT(&appctx->ctx.sess.bref.users);
5096 if (appctx->ctx.sess.target != (void *)-1) {
5097 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005098 break;
5099 }
5100 else
5101 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005102 }
5103
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005104 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005105 "%p: proto=%s",
5106 curr_sess,
5107 curr_sess->listener->proto->name);
5108
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005109
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005110 conn = objt_conn(curr_sess->si[0].end);
5111 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005112 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005113 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005114 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005115 " src=%s:%d fe=%s be=%s srv=%s",
5116 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005117 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005118 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005119 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005120 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005121 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005122 break;
5123 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005124 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005125 " src=unix:%d fe=%s be=%s srv=%s",
5126 curr_sess->listener->luid,
5127 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005128 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005129 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005130 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005131 break;
5132 }
5133
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005134 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005135 " ts=%02x age=%s calls=%d",
5136 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005137 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5138 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005140 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005141 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005142 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005143 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005144 curr_sess->req->analysers,
5145 curr_sess->req->rex ?
5146 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5147 TICKS_TO_MS(1000)) : "");
5148
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005149 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005150 ",wx=%s",
5151 curr_sess->req->wex ?
5152 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5153 TICKS_TO_MS(1000)) : "");
5154
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005155 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005156 ",ax=%s]",
5157 curr_sess->req->analyse_exp ?
5158 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5159 TICKS_TO_MS(1000)) : "");
5160
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005161 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005162 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005163 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005164 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005165 curr_sess->rep->analysers,
5166 curr_sess->rep->rex ?
5167 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5168 TICKS_TO_MS(1000)) : "");
5169
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005170 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005171 ",wx=%s",
5172 curr_sess->rep->wex ?
5173 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5174 TICKS_TO_MS(1000)) : "");
5175
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005176 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005177 ",ax=%s]",
5178 curr_sess->rep->analyse_exp ?
5179 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5180 TICKS_TO_MS(1000)) : "");
5181
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005182 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005183 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005184 " s0=[%d,%1xh,fd=%d,ex=%s]",
5185 curr_sess->si[0].state,
5186 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005187 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005188 curr_sess->si[0].exp ?
5189 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5190 TICKS_TO_MS(1000)) : "");
5191
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005192 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005193 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005194 " s1=[%d,%1xh,fd=%d,ex=%s]",
5195 curr_sess->si[1].state,
5196 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005197 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005198 curr_sess->si[1].exp ?
5199 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5200 TICKS_TO_MS(1000)) : "");
5201
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005202 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005203 " exp=%s",
5204 curr_sess->task->expire ?
5205 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5206 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005207 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005208 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005209
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005210 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005211
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005212 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005213 /* let's try again later from this session. We add ourselves into
5214 * this session's users so that it can remove us upon termination.
5215 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005216 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005217 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005218 }
5219
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005220 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005221 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005222 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005223
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005224 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005225 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005226 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005227 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005228 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005229 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005230
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005231 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005232 return 0;
5233
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005234 appctx->ctx.sess.target = NULL;
5235 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005236 return 1;
5237 }
5238
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005239 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005240 /* fall through */
5241
5242 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005243 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005244 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005245 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005246}
5247
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005248/* This is called when the stream interface is closed. For instance, upon an
5249 * external abort, we won't call the i/o handler anymore so we may need to
5250 * remove back references to the session currently being dumped.
5251 */
Simon Horman74d88312013-02-12 10:45:50 +09005252static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005253{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 struct appctx *appctx = __objt_appctx(si->end);
5255
5256 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5257 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5258 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005259 }
5260}
5261
Willy Tarreau20e99322013-04-13 09:22:25 +02005262/* This function is used to either dump tables states (when action is set
5263 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005264 * It returns 0 if the output buffer is full and it needs to be called
5265 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005266 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005267static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005268{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005269 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005270 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005271 struct ebmb_node *eb;
5272 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005273 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005274 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005275
5276 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005277 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005278 * - STAT_ST_INIT : the first call
5279 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005280 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005281 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005282 * and the entry pointer points to the next entry to be dumped,
5283 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005284 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005285 * data though.
5286 */
5287
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005288 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005289 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005290 if (appctx->st2 == STAT_ST_LIST) {
5291 appctx->ctx.table.entry->ref_cnt--;
5292 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005293 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005294 return 1;
5295 }
5296
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005297 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005299 while (appctx->st2 != STAT_ST_FIN) {
5300 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005301 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005302 appctx->ctx.table.proxy = appctx->ctx.table.target;
5303 if (!appctx->ctx.table.proxy)
5304 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005305
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005306 appctx->ctx.table.entry = NULL;
5307 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005308 break;
5309
Willy Tarreau295a8372011-03-10 11:25:07 +01005310 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005311 if (!appctx->ctx.table.proxy ||
5312 (appctx->ctx.table.target &&
5313 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5314 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005315 break;
5316 }
5317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 if (appctx->ctx.table.proxy->table.size) {
5319 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5320 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005321 return 0;
5322
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005323 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005324 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005325 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005326 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005327 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005328 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5329 appctx->ctx.table.entry->ref_cnt++;
5330 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005331 break;
5332 }
5333 }
5334 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005335 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005336 break;
5337
Willy Tarreau295a8372011-03-10 11:25:07 +01005338 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005339 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005341 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005342 /* we're filtering on some data contents */
5343 void *ptr;
5344 long long data;
5345
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005346 dt = appctx->ctx.table.data_type;
5347 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5348 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005349 dt);
5350
5351 data = 0;
5352 switch (stktable_data_types[dt].std_type) {
5353 case STD_T_SINT:
5354 data = stktable_data_cast(ptr, std_t_sint);
5355 break;
5356 case STD_T_UINT:
5357 data = stktable_data_cast(ptr, std_t_uint);
5358 break;
5359 case STD_T_ULL:
5360 data = stktable_data_cast(ptr, std_t_ull);
5361 break;
5362 case STD_T_FRQP:
5363 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005364 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005365 break;
5366 }
5367
5368 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005369 if ((data < appctx->ctx.table.value &&
5370 (appctx->ctx.table.data_op == STD_OP_EQ ||
5371 appctx->ctx.table.data_op == STD_OP_GT ||
5372 appctx->ctx.table.data_op == STD_OP_GE)) ||
5373 (data == appctx->ctx.table.value &&
5374 (appctx->ctx.table.data_op == STD_OP_NE ||
5375 appctx->ctx.table.data_op == STD_OP_GT ||
5376 appctx->ctx.table.data_op == STD_OP_LT)) ||
5377 (data > appctx->ctx.table.value &&
5378 (appctx->ctx.table.data_op == STD_OP_EQ ||
5379 appctx->ctx.table.data_op == STD_OP_LT ||
5380 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005381 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005382 }
5383
Simon Hormanc88b8872011-06-15 15:18:49 +09005384 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005385 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5386 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005387 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005388
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005389 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005390
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005391 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005392 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005393 struct stksess *old = appctx->ctx.table.entry;
5394 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005395 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005396 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5397 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5398 stksess_kill(&appctx->ctx.table.proxy->table, old);
5399 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005400 break;
5401 }
5402
Simon Hormanc88b8872011-06-15 15:18:49 +09005403
5404 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005405 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5406 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5407 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005408
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005409 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5410 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005411 break;
5412
Willy Tarreau295a8372011-03-10 11:25:07 +01005413 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005414 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005415 break;
5416 }
5417 }
5418 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005419}
5420
Willy Tarreaud426a182010-03-05 14:58:26 +01005421/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005422 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5423 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5424 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5425 * lines are respected within the limit of 70 output chars. Lines that are
5426 * continuation of a previous truncated line begin with "+" instead of " "
5427 * after the offset. The new pointer is returned.
5428 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005429static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5430 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005431{
5432 int end;
5433 unsigned char c;
5434
5435 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005436 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005437 return ptr;
5438
Willy Tarreau77804732012-10-29 16:14:26 +01005439 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005440
Willy Tarreaud426a182010-03-05 14:58:26 +01005441 while (ptr < len && ptr < bsize) {
5442 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005443 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005444 if (out->len > end - 2)
5445 break;
5446 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005447 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005448 if (out->len > end - 3)
5449 break;
5450 out->str[out->len++] = '\\';
5451 switch (c) {
5452 case '\t': c = 't'; break;
5453 case '\n': c = 'n'; break;
5454 case '\r': c = 'r'; break;
5455 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005456 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005457 }
5458 out->str[out->len++] = c;
5459 } else {
5460 if (out->len > end - 5)
5461 break;
5462 out->str[out->len++] = '\\';
5463 out->str[out->len++] = 'x';
5464 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5465 out->str[out->len++] = hextab[c & 0xF];
5466 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005467 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005468 /* we had a line break, let's return now */
5469 out->str[out->len++] = '\n';
5470 *line = ptr;
5471 return ptr;
5472 }
5473 }
5474 /* we have an incomplete line, we return it as-is */
5475 out->str[out->len++] = '\n';
5476 return ptr;
5477}
5478
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005479/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005480 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005481 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005482 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005483static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005484{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005485 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005486 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005487
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005488 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005489 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005490
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005491 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005493 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005494 /* the function had not been called yet, let's prepare the
5495 * buffer for a response.
5496 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005497 struct tm tm;
5498
5499 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005500 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005501 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5502 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5503 error_snapshot_id);
5504
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005505 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005506 /* Socket buffer full. Let's try again later from the same point */
5507 return 0;
5508 }
5509
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005510 appctx->ctx.errors.px = proxy;
5511 appctx->ctx.errors.buf = 0;
5512 appctx->ctx.errors.bol = 0;
5513 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005514 }
5515
5516 /* we have two inner loops here, one for the proxy, the other one for
5517 * the buffer.
5518 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005519 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005520 struct error_snapshot *es;
5521
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005522 if (appctx->ctx.errors.buf == 0)
5523 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005524 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005525 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005526
5527 if (!es->when.tv_sec)
5528 goto next;
5529
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005530 if (appctx->ctx.errors.iid >= 0 &&
5531 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5532 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005533 goto next;
5534
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005535 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005536 /* just print headers now */
5537
5538 char pn[INET6_ADDRSTRLEN];
5539 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005540 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005541
5542 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005543 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005544 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005545 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005546
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005547 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5548 case AF_INET:
5549 case AF_INET6:
5550 port = get_host_port(&es->src);
5551 break;
5552 default:
5553 port = 0;
5554 }
5555
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005556 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005557 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005558 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005559 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005560 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005561 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005562 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5563 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005564 break;
5565 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005566 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005567 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005568 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005570 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005571 break;
5572 }
5573
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005574 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005575 ", server %s (#%d), event #%u\n"
5576 " src %s:%d, session #%d, session flags 0x%08x\n"
5577 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5578 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5579 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5580 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5581 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5582 es->ev_id,
5583 pn, port, es->sid, es->s_flags,
5584 es->state, es->m_flags, es->t_flags,
5585 es->m_clen, es->m_blen,
5586 es->b_flags, es->b_out, es->b_tot,
5587 es->len, es->b_wrap, es->pos);
5588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005589 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005590 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005591 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005592 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005593 appctx->ctx.errors.ptr = 0;
5594 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005595 }
5596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005597 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005598 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005600 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005601 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005602 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005603 goto next;
5604 }
5605
5606 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005607 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005608 int newptr;
5609 int newline;
5610
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005611 newline = appctx->ctx.errors.bol;
5612 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5613 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005614 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005615
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005616 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005617 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005618 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005619 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005620 appctx->ctx.errors.ptr = newptr;
5621 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005622 };
5623 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005624 appctx->ctx.errors.bol = 0;
5625 appctx->ctx.errors.ptr = -1;
5626 appctx->ctx.errors.buf++;
5627 if (appctx->ctx.errors.buf > 1) {
5628 appctx->ctx.errors.buf = 0;
5629 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005630 }
5631 }
5632
5633 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005634 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005635}
5636
Willy Tarreaud5781202012-09-22 19:32:35 +02005637/* parse the "level" argument on the bind lines */
5638static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5639{
5640 if (!*args[cur_arg + 1]) {
5641 memprintf(err, "'%s' : missing level", args[cur_arg]);
5642 return ERR_ALERT | ERR_FATAL;
5643 }
5644
5645 if (!strcmp(args[cur_arg+1], "user"))
5646 conf->level = ACCESS_LVL_USER;
5647 else if (!strcmp(args[cur_arg+1], "operator"))
5648 conf->level = ACCESS_LVL_OPER;
5649 else if (!strcmp(args[cur_arg+1], "admin"))
5650 conf->level = ACCESS_LVL_ADMIN;
5651 else {
5652 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5653 args[cur_arg], args[cur_arg+1]);
5654 return ERR_ALERT | ERR_FATAL;
5655 }
5656
5657 return 0;
5658}
5659
Willy Tarreaub24281b2011-02-13 13:16:36 +01005660struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005661 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005662 .name = "<STATS>", /* used for logging */
5663 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005664 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005665};
5666
Simon Horman9bd2c732011-06-15 15:18:44 +09005667static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005668 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005669 .name = "<CLI>", /* used for logging */
5670 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005671 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005672};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005673
Willy Tarreaudc13c112013-06-21 23:16:39 +02005674static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005675 { CFG_GLOBAL, "stats", stats_parse_global },
5676 { 0, NULL, NULL },
5677}};
5678
Willy Tarreaud5781202012-09-22 19:32:35 +02005679static struct bind_kw_list bind_kws = { "STAT", { }, {
5680 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5681 { NULL, NULL, 0 },
5682}};
5683
Willy Tarreau10522fd2008-07-09 20:12:41 +02005684__attribute__((constructor))
5685static void __dumpstats_module_init(void)
5686{
5687 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005688 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005689}
5690
Willy Tarreau91861262007-10-17 17:06:05 +02005691/*
5692 * Local variables:
5693 * c-indent-level: 8
5694 * c-basic-offset: 8
5695 * End:
5696 */