blob: 1e137fbb69fc015fc9be309d3cfe6695c51b4549 [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.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100430 * NOTE: Some tools happen to rely on the field position instead of its name,
431 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100432 */
433static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100434{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100435 chunk_appendf(&trash,
436 "# pxname,svname,"
437 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100438 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100439 "bin,bout,"
440 "dreq,dresp,"
441 "ereq,econ,eresp,"
442 "wretr,wredis,"
443 "status,weight,act,bck,"
444 "chkfail,chkdown,lastchg,downtime,qlimit,"
445 "pid,iid,sid,throttle,lbtot,tracked,type,"
446 "rate,rate_lim,rate_max,"
447 "check_status,check_code,check_duration,"
448 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
449 "req_rate,req_rate_max,req_tot,"
450 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100451 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100452 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100453}
454
Simon Hormand9366582011-06-15 15:18:45 +0900455/* print a string of text buffer to <out>. The format is :
456 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
457 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
458 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
459 */
460static int dump_text(struct chunk *out, const char *buf, int bsize)
461{
462 unsigned char c;
463 int ptr = 0;
464
465 while (buf[ptr] && ptr < bsize) {
466 c = buf[ptr];
467 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
468 if (out->len > out->size - 1)
469 break;
470 out->str[out->len++] = c;
471 }
472 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
473 if (out->len > out->size - 2)
474 break;
475 out->str[out->len++] = '\\';
476 switch (c) {
477 case ' ': c = ' '; break;
478 case '\t': c = 't'; break;
479 case '\n': c = 'n'; break;
480 case '\r': c = 'r'; break;
481 case '\e': c = 'e'; break;
482 case '\\': c = '\\'; break;
483 }
484 out->str[out->len++] = c;
485 }
486 else {
487 if (out->len > out->size - 4)
488 break;
489 out->str[out->len++] = '\\';
490 out->str[out->len++] = 'x';
491 out->str[out->len++] = hextab[(c >> 4) & 0xF];
492 out->str[out->len++] = hextab[c & 0xF];
493 }
494 ptr++;
495 }
496
497 return ptr;
498}
499
500/* print a buffer in hexa.
501 * Print stopped if <bsize> is reached, or if no more place in the chunk.
502 */
503static int dump_binary(struct chunk *out, const char *buf, int bsize)
504{
505 unsigned char c;
506 int ptr = 0;
507
508 while (ptr < bsize) {
509 c = buf[ptr];
510
511 if (out->len > out->size - 2)
512 break;
513 out->str[out->len++] = hextab[(c >> 4) & 0xF];
514 out->str[out->len++] = hextab[c & 0xF];
515
516 ptr++;
517 }
518 return ptr;
519}
520
521/* Dump the status of a table to a stream interface's
522 * read buffer. It returns 0 if the output buffer is full
523 * and needs to be called again, otherwise non-zero.
524 */
525static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
526 struct proxy *proxy, struct proxy *target)
527{
Willy Tarreau306f8302013-07-08 15:53:06 +0200528 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900529
Willy Tarreau77804732012-10-29 16:14:26 +0100530 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900531 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
532
533 /* any other information should be dumped here */
534
Willy Tarreau290e63a2012-09-20 18:07:14 +0200535 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100536 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900537
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200538 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900539 return 0;
540
541 return 1;
542}
543
544/* Dump the a table entry to a stream interface's
545 * read buffer. It returns 0 if the output buffer is full
546 * and needs to be called again, otherwise non-zero.
547 */
548static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
549 struct proxy *proxy, struct stksess *entry)
550{
551 int dt;
552
Willy Tarreau77804732012-10-29 16:14:26 +0100553 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900554
555 if (proxy->table.type == STKTABLE_TYPE_IP) {
556 char addr[INET_ADDRSTRLEN];
557 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900559 }
560 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
561 char addr[INET6_ADDRSTRLEN];
562 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900564 }
565 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900567 }
568 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100569 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900570 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
571 }
572 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100573 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900574 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
575 }
576
Willy Tarreau77804732012-10-29 16:14:26 +0100577 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900578
579 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
580 void *ptr;
581
582 if (proxy->table.data_ofs[dt] == 0)
583 continue;
584 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100585 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900586 else
Willy Tarreau77804732012-10-29 16:14:26 +0100587 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900588
589 ptr = stktable_data_ptr(&proxy->table, entry, dt);
590 switch (stktable_data_types[dt].std_type) {
591 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100592 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900593 break;
594 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100595 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900596 break;
597 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100598 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900599 break;
600 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100601 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900602 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
603 proxy->table.data_arg[dt].u));
604 break;
605 }
606 }
Willy Tarreau77804732012-10-29 16:14:26 +0100607 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900608
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200609 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900610 return 0;
611
612 return 1;
613}
614
Willy Tarreaudec98142012-06-06 23:37:08 +0200615static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900616{
Willy Tarreau306f8302013-07-08 15:53:06 +0200617 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100618 struct appctx *appctx = __objt_appctx(si->end);
619 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900620 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900621 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900622 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200623 long long value;
624 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200625 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200626 void *ptr;
627 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900628
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100629 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900630
631 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100632 appctx->ctx.cli.msg = "Key value expected\n";
633 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900634 return;
635 }
636
Simon Hormanc5b89f62011-06-15 15:18:50 +0900637 switch (px->table.type) {
638 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900639 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100640 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900641 break;
642 case STKTABLE_TYPE_IPV6:
643 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100644 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900645 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900646 case STKTABLE_TYPE_INTEGER:
647 {
648 char *endptr;
649 unsigned long val;
650 errno = 0;
651 val = strtoul(args[4], &endptr, 10);
652 if ((errno == ERANGE && val == ULONG_MAX) ||
653 (errno != 0 && val == 0) || endptr == args[4] ||
654 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100655 appctx->ctx.cli.msg = "Invalid key\n";
656 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900657 return;
658 }
659 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100660 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900661 break;
662 }
663 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900664 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100665 static_table_key->key = args[4];
666 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900667 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900668 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200669 switch (action) {
670 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100671 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 +0200672 break;
673 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100674 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 +0200675 break;
676 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100677 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200678 break;
679 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100680 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900681 return;
682 }
683
684 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200685 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100686 appctx->ctx.cli.msg = stats_permission_denied_msg;
687 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900688 return;
689 }
690
Willy Tarreau07115412012-10-29 21:56:59 +0100691 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900692
Willy Tarreaudec98142012-06-06 23:37:08 +0200693 switch (action) {
694 case STAT_CLI_O_TAB:
695 if (!ts)
696 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100697 chunk_reset(&trash);
698 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900699 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100700 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900701 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200702
703 case STAT_CLI_O_CLR:
704 if (!ts)
705 return;
706 if (ts->ref_cnt) {
707 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100708 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
709 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200710 return;
711 }
712 stksess_kill(&px->table, ts);
713 break;
Simon Horman17bce342011-06-15 15:18:47 +0900714
Willy Tarreau654694e2012-06-07 01:03:16 +0200715 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200716 if (ts)
717 stktable_touch(&px->table, ts, 1);
718 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100719 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200720 if (!ts) {
721 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100722 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
723 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200724 return;
725 }
726 stktable_store(&px->table, ts, 1);
727 }
728
Willy Tarreau47060b62013-08-01 21:11:42 +0200729 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
730 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100731 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
732 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200733 return;
734 }
735
736 data_type = stktable_get_data_type(args[cur_arg] + 5);
737 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100738 appctx->ctx.cli.msg = "Unknown data type\n";
739 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200740 return;
741 }
742
743 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100744 appctx->ctx.cli.msg = "Data type not stored in this table\n";
745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200746 return;
747 }
748
749 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100750 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
751 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200752 return;
753 }
754
755 ptr = stktable_data_ptr(&px->table, ts, data_type);
756
757 switch (stktable_data_types[data_type].std_type) {
758 case STD_T_SINT:
759 stktable_data_cast(ptr, std_t_sint) = value;
760 break;
761 case STD_T_UINT:
762 stktable_data_cast(ptr, std_t_uint) = value;
763 break;
764 case STD_T_ULL:
765 stktable_data_cast(ptr, std_t_ull) = value;
766 break;
767 case STD_T_FRQP:
768 /* We set both the current and previous values. That way
769 * the reported frequency is stable during all the period
770 * then slowly fades out. This allows external tools to
771 * push measures without having to update them too often.
772 */
773 frqp = &stktable_data_cast(ptr, std_t_frqp);
774 frqp->curr_tick = now_ms;
775 frqp->prev_ctr = 0;
776 frqp->curr_ctr = value;
777 break;
778 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200779 }
780 break;
781
Willy Tarreaudec98142012-06-06 23:37:08 +0200782 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100783 appctx->ctx.cli.msg = "Unknown action\n";
784 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200785 break;
Simon Horman121f3052011-06-15 15:18:46 +0900786 }
Simon Horman121f3052011-06-15 15:18:46 +0900787}
788
Willy Tarreau654694e2012-06-07 01:03:16 +0200789static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900790{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100791 struct appctx *appctx = __objt_appctx(si->end);
792
Willy Tarreau04b3a192013-04-13 09:41:37 +0200793 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100794 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
795 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200796 return;
797 }
798
Simon Hormand5b9fd92011-06-15 15:18:48 +0900799 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100800 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
801 if (appctx->ctx.table.data_type < 0) {
802 appctx->ctx.cli.msg = "Unknown data type\n";
803 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900804 return;
805 }
806
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100807 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
808 appctx->ctx.cli.msg = "Data type not stored in this table\n";
809 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900810 return;
811 }
812
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 appctx->ctx.table.data_op = get_std_op(args[4]);
814 if (appctx->ctx.table.data_op < 0) {
815 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
816 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900817 return;
818 }
819
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100820 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
821 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
822 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900823 return;
824 }
825}
826
Willy Tarreaudec98142012-06-06 23:37:08 +0200827static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900828{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100829 struct appctx *appctx = __objt_appctx(si->end);
830
831 appctx->ctx.table.data_type = -1;
832 appctx->st2 = STAT_ST_INIT;
833 appctx->ctx.table.target = NULL;
834 appctx->ctx.table.proxy = NULL;
835 appctx->ctx.table.entry = NULL;
836 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900837
838 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100839 appctx->ctx.table.target = find_stktable(args[2]);
840 if (!appctx->ctx.table.target) {
841 appctx->ctx.cli.msg = "No such table\n";
842 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900843 return;
844 }
845 }
846 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200847 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900848 goto err_args;
849 return;
850 }
851
852 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200853 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900854 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200855 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900856 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900857 goto err_args;
858
859 return;
860
861err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200862 switch (action) {
863 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 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 +0200865 break;
866 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100867 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 +0200868 break;
869 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100870 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200871 break;
872 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100873 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900874}
875
Willy Tarreau532a4502011-09-07 22:37:44 +0200876/* Expects to find a frontend named <arg> and returns it, otherwise displays various
877 * adequate error messages and returns NULL. This function also expects the session
878 * level to be admin.
879 */
880static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
881{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100882 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200883 struct proxy *px;
884
Willy Tarreau290e63a2012-09-20 18:07:14 +0200885 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100886 appctx->ctx.cli.msg = stats_permission_denied_msg;
887 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200888 return NULL;
889 }
890
891 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100892 appctx->ctx.cli.msg = "A frontend name is expected.\n";
893 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200894 return NULL;
895 }
896
897 px = findproxy(arg, PR_CAP_FE);
898 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100899 appctx->ctx.cli.msg = "No such frontend.\n";
900 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200901 return NULL;
902 }
903 return px;
904}
905
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200906/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
907 * and returns the pointer to the server. Otherwise, display adequate error messages
908 * and returns NULL. This function also expects the session level to be admin. Note:
909 * the <arg> is modified to remove the '/'.
910 */
911static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
912{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100913 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200914 struct proxy *px;
915 struct server *sv;
916 char *line;
917
Willy Tarreau290e63a2012-09-20 18:07:14 +0200918 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100919 appctx->ctx.cli.msg = stats_permission_denied_msg;
920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200921 return NULL;
922 }
923
924 /* split "backend/server" and make <line> point to server */
925 for (line = arg; *line; line++)
926 if (*line == '/') {
927 *line++ = '\0';
928 break;
929 }
930
931 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100932 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
933 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200934 return NULL;
935 }
936
937 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100938 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
939 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200940 return NULL;
941 }
942
943 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100944 appctx->ctx.cli.msg = "Proxy is disabled.\n";
945 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200946 return NULL;
947 }
948
949 return sv;
950}
951
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100952/* This function is used with map management. It permits to browse each
953 * really allocated descriptors of one map reference. The variable
954 * <appctx->ctx.map.ref> must contain the map reference to browse.
955 * The variable <appctx->ctx.map.desc> contain the descriptor of the
956 * current allocated map descriptor. This variable must be initialized
957 * to NULL.
958 */
959static inline void stats_map_lookup_next(struct stream_interface *si)
960{
961 struct appctx *appctx = __objt_appctx(si->end);
962
963 /* search the next allocated map */
964 while (1) {
965 /* get next descriptor */
966 if (!appctx->ctx.map.desc)
967 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.ref->maps,
968 struct map_descriptor *, list);
969 else
970 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.desc->list,
971 struct map_descriptor *, list);
972
973 /* detect end of list */
974 if (&appctx->ctx.map.desc->list == &appctx->ctx.map.ref->maps) {
975 appctx->ctx.map.desc = NULL;
976 return;
977 }
978
979 /* do not lookup this entry */
980 if (!appctx->ctx.map.desc->do_free)
981 continue;
982
983 /* avalaible descriptor */
984 return;
985 }
986}
987
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200988/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200989 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100990 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200991 * designating the function which will have to process the request, which can
992 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200993 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900994static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200995{
Willy Tarreau306f8302013-07-08 15:53:06 +0200996 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100997 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200998 char *args[MAX_STATS_ARGS + 1];
999 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001000 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001001
1002 while (isspace((unsigned char)*line))
1003 line++;
1004
1005 arg = 0;
1006 args[arg] = line;
1007
1008 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001009 if (*line == '\\') {
1010 line++;
1011 if (*line == '\0')
1012 break;
1013 }
1014 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001015 *line++ = '\0';
1016
1017 while (isspace((unsigned char)*line))
1018 line++;
1019
1020 args[++arg] = line;
1021 continue;
1022 }
1023
1024 line++;
1025 }
1026
1027 while (++arg <= MAX_STATS_ARGS)
1028 args[arg] = line;
1029
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001030 /* remove \ */
1031 arg = 0;
1032 while (*args[arg] != '\0') {
1033 j = 0;
1034 for (i=0; args[arg][i] != '\0'; i++) {
1035 if (args[arg][i] == '\\')
1036 continue;
1037 args[arg][j] = args[arg][i];
1038 j++;
1039 }
1040 args[arg][j] = '\0';
1041 arg++;
1042 }
1043
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001044 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001045 if (strcmp(args[0], "show") == 0) {
1046 if (strcmp(args[1], "stat") == 0) {
1047 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001048 appctx->ctx.stats.flags |= STAT_BOUND;
1049 appctx->ctx.stats.iid = atoi(args[2]);
1050 appctx->ctx.stats.type = atoi(args[3]);
1051 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001052 }
1053
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001054 appctx->st2 = STAT_ST_INIT;
1055 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001056 }
1057 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001058 appctx->st2 = STAT_ST_INIT;
1059 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001060 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001061 else if (strcmp(args[1], "pools") == 0) {
1062 appctx->st2 = STAT_ST_INIT;
1063 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1064 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001065 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001066 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001067 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001068 appctx->ctx.cli.msg = stats_permission_denied_msg;
1069 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001070 return 1;
1071 }
Willy Tarreau76153662012-11-26 01:16:39 +01001072 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001073 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001074 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001075 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001076 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001077 appctx->ctx.sess.target = NULL;
1078 appctx->ctx.sess.section = 0; /* start with session status */
1079 appctx->ctx.sess.pos = 0;
1080 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001081 }
1082 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001083 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001084 appctx->ctx.cli.msg = stats_permission_denied_msg;
1085 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001086 return 1;
1087 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001088 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001090 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001091 appctx->ctx.errors.iid = -1;
1092 appctx->ctx.errors.px = NULL;
1093 appctx->st2 = STAT_ST_INIT;
1094 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001095 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001096 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001097 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001098 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001099 else if (strcmp(args[1], "map") == 0) {
1100
1101 /* no parameter: display all map avalaible */
1102 if (!*args[2]) {
1103 appctx->st2 = STAT_ST_INIT;
1104 appctx->st0 = STAT_CLI_O_MAPS;
1105 return 1;
1106 }
1107
1108 /* lookup into the maps */
1109 appctx->ctx.map.ref = map_get_reference(args[2]);
1110 if (!appctx->ctx.map.ref) {
1111 appctx->ctx.cli.msg = "Unknown map reference.\n";
1112 appctx->st0 = STAT_CLI_PRINT;
1113 return 1;
1114 }
1115 appctx->st2 = STAT_ST_INIT;
1116 appctx->st0 = STAT_CLI_O_MAP;
1117 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001118 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001119 return 0;
1120 }
1121 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001122 else if (strcmp(args[0], "clear") == 0) {
1123 if (strcmp(args[1], "counters") == 0) {
1124 struct proxy *px;
1125 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001126 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001127 int clrall = 0;
1128
1129 if (strcmp(args[2], "all") == 0)
1130 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001131
Willy Tarreau6162db22009-10-10 17:13:00 +02001132 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001133 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1134 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001135 appctx->ctx.cli.msg = stats_permission_denied_msg;
1136 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001137 return 1;
1138 }
1139
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001140 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001141 if (clrall) {
1142 memset(&px->be_counters, 0, sizeof(px->be_counters));
1143 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1144 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001145 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001146 px->be_counters.conn_max = 0;
1147 px->be_counters.p.http.rps_max = 0;
1148 px->be_counters.sps_max = 0;
1149 px->be_counters.cps_max = 0;
1150 px->be_counters.nbpend_max = 0;
1151
1152 px->fe_counters.conn_max = 0;
1153 px->fe_counters.p.http.rps_max = 0;
1154 px->fe_counters.sps_max = 0;
1155 px->fe_counters.cps_max = 0;
1156 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001157 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001158
1159 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001160 if (clrall)
1161 memset(&sv->counters, 0, sizeof(sv->counters));
1162 else {
1163 sv->counters.cur_sess_max = 0;
1164 sv->counters.nbpend_max = 0;
1165 sv->counters.sps_max = 0;
1166 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001167
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001168 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001169 if (li->counters) {
1170 if (clrall)
1171 memset(li->counters, 0, sizeof(*li->counters));
1172 else
1173 li->counters->conn_max = 0;
1174 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001175 }
1176
Willy Tarreau81c25d02011-09-07 15:17:21 +02001177 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001178 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001179 return 1;
1180 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001181 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001182 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001183 /* end of processing */
1184 return 1;
1185 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001186 else if (strcmp(args[1], "map") == 0) {
1187 struct map_reference *mref;
1188 struct map_descriptor *mdesc;
1189 struct map_entry *ent, *nent;
1190
1191 /* no parameter */
1192 if (!*args[2]) {
1193 appctx->ctx.cli.msg = "Expect map reference.\n";
1194 appctx->st0 = STAT_CLI_PRINT;
1195 return 1;
1196 }
1197
1198 /* lookup into the maps */
1199 mref = map_get_reference(args[2]);
1200 if (!mref) {
1201 appctx->ctx.cli.msg = "Unknown map reference.\n";
1202 appctx->st0 = STAT_CLI_PRINT;
1203 return 1;
1204 }
1205
1206 /* clear all maps */
1207 list_for_each_entry(mdesc, &mref->maps, list)
1208 if (mdesc->do_free)
1209 pattern_prune_expr(mdesc->pat);
1210
1211 /* clear map reference */
1212 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1213 LIST_DEL(&ent->list);
1214 free(ent->key);
1215 free(ent->value);
1216 free(ent);
1217 }
1218
1219 /* return response */
1220 appctx->ctx.cli.msg = "Done.\n";
1221 appctx->st0 = STAT_CLI_PRINT;
1222 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001223 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001224 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001225 return 0;
1226 }
1227 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001228 else if (strcmp(args[0], "get") == 0) {
1229 if (strcmp(args[1], "weight") == 0) {
1230 struct proxy *px;
1231 struct server *sv;
1232
1233 /* split "backend/server" and make <line> point to server */
1234 for (line = args[2]; *line; line++)
1235 if (*line == '/') {
1236 *line++ = '\0';
1237 break;
1238 }
1239
1240 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001241 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1242 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001243 return 1;
1244 }
1245
1246 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001247 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1248 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001249 return 1;
1250 }
1251
1252 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001253 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1254 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001255 return 1;
1256 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001257 else if (strcmp(args[1], "map") == 0) {
1258
1259 /* no parameter */
1260 if (!*args[2] || !*args[3]) {
1261 appctx->ctx.cli.msg = "Expect map reference and required key.\n";
1262 appctx->st0 = STAT_CLI_PRINT;
1263 return 1;
1264 }
1265
1266 /* lookup into the maps */
1267 appctx->ctx.map.ref = map_get_reference(args[2]);
1268 if (!appctx->ctx.map.ref) {
1269 appctx->ctx.cli.msg = "Unknown map reference.\n";
1270 appctx->st0 = STAT_CLI_PRINT;
1271 return 1;
1272 }
1273
1274 /* copy input string */
1275 appctx->ctx.map.chunk.len = strlen(args[3]);
1276 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1277 appctx->ctx.map.chunk.str = strdup(args[3]);
1278 if (!appctx->ctx.map.chunk.str) {
1279 appctx->ctx.cli.msg = "Out of memory error.\n";
1280 appctx->st0 = STAT_CLI_PRINT;
1281 return 1;
1282 }
1283
1284 /* prepare response */
1285 appctx->st2 = STAT_ST_INIT;
1286 appctx->st0 = STAT_CLI_O_MLOOK;
1287 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001288 else { /* not "get weight" */
1289 return 0;
1290 }
1291 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001292 else if (strcmp(args[0], "set") == 0) {
1293 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001294 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001295 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001296
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001297 sv = expect_server_admin(s, si, args[2]);
1298 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001299 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001300
Simon Horman7d09b9a2013-02-12 10:45:51 +09001301 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001302 /*
1303 * The user-weight may now be zero and thus
1304 * the server considered to be draining.
1305 * Update the server's drain state as necessary.
1306 */
1307 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001308 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001309 appctx->ctx.cli.msg = warning;
1310 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001311 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001312 return 1;
1313 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001314 else if (strcmp(args[1], "timeout") == 0) {
1315 if (strcmp(args[2], "cli") == 0) {
1316 unsigned timeout;
1317 const char *res;
1318
1319 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001320 appctx->ctx.cli.msg = "Expects an integer value.\n";
1321 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001322 return 1;
1323 }
1324
1325 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1326 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001327 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1328 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001329 return 1;
1330 }
1331
1332 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1333 return 1;
1334 }
1335 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001336 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1337 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001338 return 1;
1339 }
1340 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001341 else if (strcmp(args[1], "maxconn") == 0) {
1342 if (strcmp(args[2], "frontend") == 0) {
1343 struct proxy *px;
1344 struct listener *l;
1345 int v;
1346
Willy Tarreau532a4502011-09-07 22:37:44 +02001347 px = expect_frontend_admin(s, si, args[3]);
1348 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001349 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001350
1351 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001352 appctx->ctx.cli.msg = "Integer value expected.\n";
1353 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001354 return 1;
1355 }
1356
1357 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001358 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001359 appctx->ctx.cli.msg = "Value out of range.\n";
1360 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001361 return 1;
1362 }
1363
1364 /* OK, the value is fine, so we assign it to the proxy and to all of
1365 * its listeners. The blocked ones will be dequeued.
1366 */
1367 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001368 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001369 l->maxconn = v;
1370 if (l->state == LI_FULL)
1371 resume_listener(l);
1372 }
1373
1374 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1375 dequeue_all_listeners(&s->fe->listener_queue);
1376
1377 return 1;
1378 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001379 else if (strcmp(args[2], "global") == 0) {
1380 int v;
1381
Willy Tarreau290e63a2012-09-20 18:07:14 +02001382 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001383 appctx->ctx.cli.msg = stats_permission_denied_msg;
1384 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001385 return 1;
1386 }
1387
1388 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001389 appctx->ctx.cli.msg = "Expects an integer value.\n";
1390 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001391 return 1;
1392 }
1393
1394 v = atoi(args[3]);
1395 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001396 appctx->ctx.cli.msg = "Value out of range.\n";
1397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001398 return 1;
1399 }
1400
1401 /* check for unlimited values */
1402 if (v <= 0)
1403 v = global.hardmaxconn;
1404
1405 global.maxconn = v;
1406
1407 /* Dequeues all of the listeners waiting for a resource */
1408 if (!LIST_ISEMPTY(&global_listener_queue))
1409 dequeue_all_listeners(&global_listener_queue);
1410
1411 return 1;
1412 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001413 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001414 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1415 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001416 return 1;
1417 }
1418 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001419 else if (strcmp(args[1], "rate-limit") == 0) {
1420 if (strcmp(args[2], "connections") == 0) {
1421 if (strcmp(args[3], "global") == 0) {
1422 int v;
1423
Willy Tarreau290e63a2012-09-20 18:07:14 +02001424 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.cli.msg = stats_permission_denied_msg;
1426 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001427 return 1;
1428 }
1429
1430 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001431 appctx->ctx.cli.msg = "Expects an integer value.\n";
1432 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001433 return 1;
1434 }
1435
1436 v = atoi(args[4]);
1437 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001438 appctx->ctx.cli.msg = "Value out of range.\n";
1439 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001440 return 1;
1441 }
1442
1443 global.cps_lim = v;
1444
1445 /* Dequeues all of the listeners waiting for a resource */
1446 if (!LIST_ISEMPTY(&global_listener_queue))
1447 dequeue_all_listeners(&global_listener_queue);
1448
1449 return 1;
1450 }
1451 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001452 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1453 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001454 return 1;
1455 }
1456 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001457 else if (strcmp(args[2], "sessions") == 0) {
1458 if (strcmp(args[3], "global") == 0) {
1459 int v;
1460
1461 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1462 appctx->ctx.cli.msg = stats_permission_denied_msg;
1463 appctx->st0 = STAT_CLI_PRINT;
1464 return 1;
1465 }
1466
1467 if (!*args[4]) {
1468 appctx->ctx.cli.msg = "Expects an integer value.\n";
1469 appctx->st0 = STAT_CLI_PRINT;
1470 return 1;
1471 }
1472
1473 v = atoi(args[4]);
1474 if (v < 0) {
1475 appctx->ctx.cli.msg = "Value out of range.\n";
1476 appctx->st0 = STAT_CLI_PRINT;
1477 return 1;
1478 }
1479
1480 global.sps_lim = v;
1481
1482 /* Dequeues all of the listeners waiting for a resource */
1483 if (!LIST_ISEMPTY(&global_listener_queue))
1484 dequeue_all_listeners(&global_listener_queue);
1485
1486 return 1;
1487 }
1488 else {
1489 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1490 appctx->st0 = STAT_CLI_PRINT;
1491 return 1;
1492 }
1493 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001494#ifdef USE_OPENSSL
1495 else if (strcmp(args[2], "ssl-sessions") == 0) {
1496 if (strcmp(args[3], "global") == 0) {
1497 int v;
1498
1499 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1500 appctx->ctx.cli.msg = stats_permission_denied_msg;
1501 appctx->st0 = STAT_CLI_PRINT;
1502 return 1;
1503 }
1504
1505 if (!*args[4]) {
1506 appctx->ctx.cli.msg = "Expects an integer value.\n";
1507 appctx->st0 = STAT_CLI_PRINT;
1508 return 1;
1509 }
1510
1511 v = atoi(args[4]);
1512 if (v < 0) {
1513 appctx->ctx.cli.msg = "Value out of range.\n";
1514 appctx->st0 = STAT_CLI_PRINT;
1515 return 1;
1516 }
1517
1518 global.ssl_lim = v;
1519
1520 /* Dequeues all of the listeners waiting for a resource */
1521 if (!LIST_ISEMPTY(&global_listener_queue))
1522 dequeue_all_listeners(&global_listener_queue);
1523
1524 return 1;
1525 }
1526 else {
1527 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1528 appctx->st0 = STAT_CLI_PRINT;
1529 return 1;
1530 }
1531 }
1532#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001533 else if (strcmp(args[2], "http-compression") == 0) {
1534 if (strcmp(args[3], "global") == 0) {
1535 int v;
1536
Willy Tarreau85d47f92012-11-21 00:29:50 +01001537 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001538 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1539 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001540 return 1;
1541 }
1542
William Lallemandd85f9172012-11-09 17:05:39 +01001543 v = atoi(args[4]);
1544 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1545 }
1546 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001547 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1548 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001549 return 1;
1550 }
1551 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001552 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001553 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001554 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001555 return 1;
1556 }
1557 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001558 else if (strcmp(args[1], "table") == 0) {
1559 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1560 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001561 else if (strcmp(args[1], "map") == 0) {
1562 struct pattern *pat_elt;
1563 struct pat_idx_elt *idx_elt;
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001564 char *value = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001565
1566 /* Expect three parameters: map name, key and new value. */
1567 if (!*args[2] || !*args[3] || !*args[4]) {
1568 appctx->ctx.cli.msg = "'set map' expect three parameters: map name, key and value.\n";
1569 appctx->st0 = STAT_CLI_PRINT;
1570 return 1;
1571 }
1572
1573 /* Lookup the reference in the maps. */
1574 appctx->ctx.map.ref = map_get_reference(args[2]);
1575 if (!appctx->ctx.map.ref) {
1576 appctx->ctx.cli.msg = "Unknown map reference.\n";
1577 appctx->st0 = STAT_CLI_PRINT;
1578 return 1;
1579 }
1580
1581 /* Lookup the entry in the reference values. */
1582 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1583 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1584 break;
1585
1586 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1587 appctx->ctx.cli.msg = "Entry not found.\n";
1588 appctx->st0 = STAT_CLI_PRINT;
1589 return 1;
1590 }
1591
1592 /* Update each reference entries. */
1593 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1594 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1595 value = strdup(args[4]);
1596 if (!value) {
1597 appctx->ctx.cli.msg = "Out of memory error.\n";
1598 appctx->st0 = STAT_CLI_PRINT;
1599 return 1;
1600 }
1601 free(appctx->ctx.map.ent->value);
1602 appctx->ctx.map.ent->value = value;
1603 }
1604 }
1605
1606 /* Change the sample. The lookup juste return the first entry, other
1607 * entries are not changed, but are never matched.
1608 */
1609 appctx->ctx.map.desc = NULL;
1610 for (stats_map_lookup_next(si);
1611 appctx->ctx.map.desc;
1612 stats_map_lookup_next(si)) {
1613 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1614 if (pat_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001615 appctx->ctx.map.desc->parse(value, pat_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001616 if (idx_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001617 appctx->ctx.map.desc->parse(value, idx_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001618 }
1619
1620 /* The set is done, send message. */
1621 appctx->ctx.cli.msg = "Done.\n";
1622 appctx->st0 = STAT_CLI_PRINT;
1623 return 1;
1624 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001625 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001626 return 0;
1627 }
1628 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001629 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001630 if (strcmp(args[1], "agent") == 0) {
1631 struct server *sv;
1632
1633 sv = expect_server_admin(s, si, args[2]);
1634 if (!sv)
1635 return 1;
1636
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001637 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1638 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1639 appctx->st0 = STAT_CLI_PRINT;
1640 return 1;
1641 }
1642
1643 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001644 return 1;
1645 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001646 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001647 struct server *sv;
1648
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001649 sv = expect_server_admin(s, si, args[2]);
1650 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001651 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001652
Cyril Bontécd19e512010-01-31 22:34:03 +01001653 if (sv->state & SRV_MAINTAIN) {
1654 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001655 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001656 /* If this server tracks the status of another one,
1657 * we must restore the good status.
1658 */
Willy Tarreau44267702011-10-28 15:35:33 +02001659 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001660 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001661 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001662 } else {
1663 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001664 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001665 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001666 }
1667 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001668 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001669 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001670 }
1671 }
1672
Willy Tarreau532a4502011-09-07 22:37:44 +02001673 return 1;
1674 }
1675 else if (strcmp(args[1], "frontend") == 0) {
1676 struct proxy *px;
1677
1678 px = expect_frontend_admin(s, si, args[2]);
1679 if (!px)
1680 return 1;
1681
1682 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001683 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1684 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001685 return 1;
1686 }
1687
1688 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001689 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1690 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001691 return 1;
1692 }
1693
1694 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001695 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1696 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001697 return 1;
1698 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001699 return 1;
1700 }
1701 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001702 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1703 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001704 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001705 }
1706 }
1707 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001708 if (strcmp(args[1], "agent") == 0) {
1709 struct server *sv;
1710
1711 sv = expect_server_admin(s, si, args[2]);
1712 if (!sv)
1713 return 1;
1714
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001715 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001716 return 1;
1717 }
1718 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001719 struct server *sv;
1720
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001721 sv = expect_server_admin(s, si, args[2]);
1722 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001723 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001724
Cyril Bontécd19e512010-01-31 22:34:03 +01001725 if (! (sv->state & SRV_MAINTAIN)) {
1726 /* Not already in maintenance, we can change the server state */
1727 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001728 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001729 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001730 }
1731
Willy Tarreau532a4502011-09-07 22:37:44 +02001732 return 1;
1733 }
1734 else if (strcmp(args[1], "frontend") == 0) {
1735 struct proxy *px;
1736
1737 px = expect_frontend_admin(s, si, args[2]);
1738 if (!px)
1739 return 1;
1740
1741 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001742 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1743 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001744 return 1;
1745 }
1746
1747 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001748 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1749 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001750 return 1;
1751 }
1752
1753 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001754 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1755 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001756 return 1;
1757 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001758 return 1;
1759 }
1760 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001761 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1762 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001763 return 1;
1764 }
1765 }
1766 else if (strcmp(args[0], "shutdown") == 0) {
1767 if (strcmp(args[1], "frontend") == 0) {
1768 struct proxy *px;
1769
1770 px = expect_frontend_admin(s, si, args[2]);
1771 if (!px)
1772 return 1;
1773
1774 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001775 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1776 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001777 return 1;
1778 }
1779
1780 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 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1783 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1784 stop_proxy(px);
1785 return 1;
1786 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001787 else if (strcmp(args[1], "session") == 0) {
1788 struct session *sess, *ptr;
1789
Willy Tarreau290e63a2012-09-20 18:07:14 +02001790 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001791 appctx->ctx.cli.msg = stats_permission_denied_msg;
1792 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001793 return 1;
1794 }
1795
1796 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001797 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1798 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001799 return 1;
1800 }
1801
1802 ptr = (void *)strtoul(args[2], NULL, 0);
1803
1804 /* first, look for the requested session in the session table */
1805 list_for_each_entry(sess, &sessions, list) {
1806 if (sess == ptr)
1807 break;
1808 }
1809
1810 /* do we have the session ? */
1811 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001812 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001814 return 1;
1815 }
1816
1817 session_shutdown(sess, SN_ERR_KILLED);
1818 return 1;
1819 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001820 else if (strcmp(args[1], "sessions") == 0) {
1821 if (strcmp(args[2], "server") == 0) {
1822 struct server *sv;
1823 struct session *sess, *sess_bck;
1824
1825 sv = expect_server_admin(s, si, args[3]);
1826 if (!sv)
1827 return 1;
1828
1829 /* kill all the session that are on this server */
1830 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1831 if (sess->srv_conn == sv)
1832 session_shutdown(sess, SN_ERR_KILLED);
1833
1834 return 1;
1835 }
1836 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001837 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1838 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001839 return 1;
1840 }
1841 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001842 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001843 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1844 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001845 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001846 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001847 }
1848 else if (strcmp(args[0], "del") == 0) {
1849 if (strcmp(args[1], "map") == 0) {
1850 struct pattern *pat_elt;
1851 struct pat_idx_elt *idx_elt;
1852 struct map_entry *ent;
1853
1854 /* Expect two parameters: map name and key. */
1855 if (!*args[2] || !*args[3]) {
1856 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1857 appctx->st0 = STAT_CLI_PRINT;
1858 return 1;
1859 }
1860
1861 /* Lookup the reference in the maps. */
1862 appctx->ctx.map.ref = map_get_reference(args[2]);
1863 if (!appctx->ctx.map.ref) {
1864 appctx->ctx.cli.msg = "Unknown map reference.\n";
1865 appctx->st0 = STAT_CLI_PRINT;
1866 return 1;
1867 }
1868
1869 /* Lookup the entry in the reference values.
1870 * If the entry is not found in the reference, return error message.
1871 */
1872 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1873 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1874 break;
1875
1876 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1877 appctx->ctx.cli.msg = "Entry not found.\n";
1878 appctx->st0 = STAT_CLI_PRINT;
1879 return 1;
1880 }
1881
1882 /* Delete each enties from reference. */
1883 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1884 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1885 LIST_DEL(&appctx->ctx.map.ent->list);
1886 free(appctx->ctx.map.ent->key);
1887 free(appctx->ctx.map.ent->value);
1888 free(appctx->ctx.map.ent);
1889 }
1890 }
1891
1892 /* Delete all matching entries for each map descritor. */
1893 appctx->ctx.map.desc = NULL;
1894 stats_map_lookup_next(si);
1895 while (appctx->ctx.map.desc) {
1896 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1897 if (pat_elt != NULL) {
1898 LIST_DEL(&pat_elt->list);
1899 pattern_free(pat_elt);
1900 }
1901 if (idx_elt != NULL) {
1902 ebmb_delete(&idx_elt->node);
1903 free(idx_elt);
1904 }
1905 }
1906 stats_map_lookup_next(si);
1907 }
1908
1909 /* The deletion is done, send message. */
1910 appctx->ctx.cli.msg = "Done.\n";
1911 appctx->st0 = STAT_CLI_PRINT;
1912 return 1;
1913 }
1914 else { /* unknown "del" parameter */
1915 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1916 appctx->st0 = STAT_CLI_PRINT;
1917 return 1;
1918 }
1919 }
1920 else if (strcmp(args[0], "add") == 0) {
1921 if (strcmp(args[1], "map") == 0) {
1922 const char *params[2];
1923 struct pattern *pat;
1924 struct map_entry *ent;
1925 struct sample_storage *smp;
1926
1927 /* Expect three parameters: map name, key and new value. */
1928 if (!*args[2] || !*args[3] || !*args[4]) {
1929 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1930 appctx->st0 = STAT_CLI_PRINT;
1931 return 1;
1932 }
1933
1934 params[0] = args[3];
1935 params[1] = "";
1936
1937 /* Lookup the reference in the maps. */
1938 appctx->ctx.map.ref = map_get_reference(args[2]);
1939 if (!appctx->ctx.map.ref) {
1940 appctx->ctx.cli.msg = "Unknown map reference.\n";
1941 appctx->st0 = STAT_CLI_PRINT;
1942 return 1;
1943 }
1944
1945 /* Prepare and link the new map_entry element. If out of memory
1946 * error the action is cancelled and the descriptor are left
1947 * coherents.
1948 */
1949 ent = malloc(sizeof(*ent));
1950 if (!ent) {
1951 appctx->ctx.cli.msg = "Out of memory error.\n";
1952 appctx->st0 = STAT_CLI_PRINT;
1953 return 1;
1954 }
1955 ent->key = strdup(args[3]);
1956 if (!ent->key) {
1957 free(ent);
1958 appctx->ctx.cli.msg = "Out of memory error.\n";
1959 appctx->st0 = STAT_CLI_PRINT;
1960 return 1;
1961 }
1962 ent->value = strdup(args[4]);
1963 if (!ent->value) {
1964 free(ent->key);
1965 free(ent);
1966 appctx->ctx.cli.msg = "Out of memory error.\n";
1967 appctx->st0 = STAT_CLI_PRINT;
1968 return 1;
1969 }
1970 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1971
1972 /* Browse each map descritor and try to insert this new value. */
1973 appctx->ctx.map.desc = NULL;
1974 for (stats_map_lookup_next(si);
1975 appctx->ctx.map.desc;
1976 stats_map_lookup_next(si)) {
1977
1978 /* Create new sample. Return out of memory error
1979 * if the memory cannot be allocated. The 'add' process
1980 * is aborted, but the already inserted entries are not
1981 * deleted.
1982 */
1983 smp = calloc(1, sizeof(*smp));
1984 if (!smp) {
1985 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1986 appctx->st0 = STAT_CLI_PRINT;
1987 return 1;
1988 }
1989
1990 /* Create sample. If this function fails, the insertion
1991 * is canceled for this 'descriptor', but continue, for
1992 * the other descriptors.
1993 */
1994 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1995 free(smp);
1996 continue;
1997 }
1998
1999 /* If the value can be indexed, get the first pattern. If
2000 * the return entry is not the indexed entry, new 'pattern' is
2001 * created by the function pattern_register(). If the 'pattern'
2002 * is NULL, new entry is created. This is ugly because the
2003 * following code interfers with the own code of the function
2004 * pattern_register().
2005 */
2006 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
2007 appctx->ctx.map.desc->pat->match == pat_match_ip) {
2008 pat = LIST_NEXT(&appctx->ctx.map.desc->pat->patterns, struct pattern *, list);
2009 if (&pat->list == &appctx->ctx.map.desc->pat->patterns)
2010 pat = NULL;
2011 }
2012 else
2013 pat = NULL;
2014
2015 if (!pattern_register(appctx->ctx.map.desc->pat, params, smp, &pat, 0, NULL)) {
2016 free(smp);
2017 continue;
2018 }
2019 }
2020
2021 /* The add is done, send message. */
2022 appctx->ctx.cli.msg = "Done.\n";
2023 appctx->st0 = STAT_CLI_PRINT;
2024 return 1;
2025 }
2026 else { /* unknown "del" parameter */
2027 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2028 appctx->st0 = STAT_CLI_PRINT;
2029 return 1;
2030 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002031 }
2032 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002033 return 0;
2034 }
2035 return 1;
2036}
2037
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002038/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002039 * used to processes I/O from/to the stats unix socket. The system relies on a
2040 * state machine handling requests and various responses. We read a request,
2041 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002042 * Then we can read again. The state is stored in appctx->st0 and is one of the
2043 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002044 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002045 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002046static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002047{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002048 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002049 struct channel *req = si->ob;
2050 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002051 int reql;
2052 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002053
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002054 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2055 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002056
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002057 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002058 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002059 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002060 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2061 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002062 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002063 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002064 /* Let's close for real now. We just close the request
2065 * side, the conditions below will complete if needed.
2066 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002067 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002068 break;
2069 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002070 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002071 /* ensure we have some output room left in the event we
2072 * would want to return some info right after parsing.
2073 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002074 if (buffer_almost_full(si->ib->buf)) {
2075 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002076 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002077 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002078
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002079 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002080 if (reql <= 0) { /* closed or EOL not found */
2081 if (reql == 0)
2082 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002083 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 continue;
2085 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002086
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002087 /* seek for a possible semi-colon. If we find one, we
2088 * replace it with an LF and skip only this part.
2089 */
2090 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002091 if (trash.str[len] == ';') {
2092 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002093 reql = len + 1;
2094 break;
2095 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002096
Willy Tarreau816fc222009-10-04 07:36:58 +02002097 /* now it is time to check that we have a full line,
2098 * remove the trailing \n and possibly \r, then cut the
2099 * line.
2100 */
2101 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002102 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002103 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002104 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002105 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002106
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002107 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002108 len--;
2109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002110 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002112 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002113 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002114 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002115 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002116 continue;
2117 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002118 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002119 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002120 else if (strcmp(trash.str, "help") == 0 ||
2121 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002122 appctx->ctx.cli.msg = stats_sock_usage_msg;
2123 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002124 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002125 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002126 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002127 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002128 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002129 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002130 /* if prompt is disabled, print help on empty lines,
2131 * so that the user at least knows how to enable
2132 * prompt and find help.
2133 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002134 appctx->ctx.cli.msg = stats_sock_usage_msg;
2135 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002136 }
2137
2138 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002139 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002140 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002141 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002142 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002143 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002144 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002145 continue;
2146 }
2147
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002148 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002149 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002150 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2151 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002152 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002153 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002154 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002155 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002156 break;
2157 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002158 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002159 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002160 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002161 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002162 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002163 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002164 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002165 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002166 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002167 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002168 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002169 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002170 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002171 if (stats_table_request(si, appctx->st0))
2172 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002173 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002174 case STAT_CLI_O_MAPS:
2175 if (stats_maps_list(si))
2176 appctx->st0 = STAT_CLI_PROMPT;
2177 break;
2178 case STAT_CLI_O_MAP:
2179 if (stats_map_list(si))
2180 appctx->st0 = STAT_CLI_PROMPT;
2181 break;
2182 case STAT_CLI_O_MLOOK:
2183 if (stats_map_lookup(si))
2184 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002185 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002186 case STAT_CLI_O_POOLS:
2187 if (stats_dump_pools_to_buffer(si))
2188 appctx->st0 = STAT_CLI_PROMPT;
2189 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002190 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002191 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002192 break;
2193 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002194
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002195 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002196 if (appctx->st0 == STAT_CLI_PROMPT) {
2197 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2198 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002199 }
2200
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002201 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002202 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002203 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002204
2205 /* Now we close the output if one of the writers did so,
2206 * or if we're not in interactive mode and the request
2207 * buffer is empty. This still allows pipelined requests
2208 * to be sent in non-interactive mode.
2209 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002210 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2211 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002212 continue;
2213 }
2214
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002215 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002216 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002217 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002218 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002219
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002220 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002221 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2222 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002223 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002224 * and nothing more to consume. This is comparable to a broken pipe, so
2225 * we forward the close to the request side so that it flows upstream to
2226 * the client.
2227 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002228 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002229 }
2230
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002231 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002232 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2233 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2234 /* We have no more processing to do, and nothing more to send, and
2235 * the client side has closed. So we'll forward this state downstream
2236 * on the response buffer.
2237 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002238 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002239 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002240 }
2241
2242 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002243 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002244
2245 /* we don't want to expire timeouts while we're processing requests */
2246 si->ib->rex = TICK_ETERNITY;
2247 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002248
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002249 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002250 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 +02002251 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002252 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 +02002253
2254 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2255 /* check that we have released everything then unregister */
2256 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002257 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002258}
2259
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002260/* This function dumps information onto the stream interface's read buffer.
2261 * It returns 0 as long as it does not complete, non-zero upon completion.
2262 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002263 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002264static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002265{
2266 unsigned int up = (now.tv_sec - start_date.tv_sec);
2267
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002268 chunk_printf(&trash,
2269 "Name: " PRODUCT_NAME "\n"
2270 "Version: " HAPROXY_VERSION "\n"
2271 "Release_date: " HAPROXY_DATE "\n"
2272 "Nbproc: %d\n"
2273 "Process_num: %d\n"
2274 "Pid: %d\n"
2275 "Uptime: %dd %dh%02dm%02ds\n"
2276 "Uptime_sec: %d\n"
2277 "Memmax_MB: %d\n"
2278 "Ulimit-n: %d\n"
2279 "Maxsock: %d\n"
2280 "Maxconn: %d\n"
2281 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002282 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002283 "CumConns: %d\n"
2284 "CumReq: %d\n"
2285#ifdef USE_OPENSSL
2286 "MaxSslConns: %d\n"
2287 "CurrSslConns: %d\n"
2288 "CumSslConns: %d\n"
2289#endif
2290 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002291 "PipesUsed: %d\n"
2292 "PipesFree: %d\n"
2293 "ConnRate: %d\n"
2294 "ConnRateLimit: %d\n"
2295 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002296 "SessRate: %d\n"
2297 "SessRateLimit: %d\n"
2298 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002299#ifdef USE_OPENSSL
2300 "SslRate: %d\n"
2301 "SslRateLimit: %d\n"
2302 "MaxSslRate: %d\n"
2303#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002304 "CompressBpsIn: %u\n"
2305 "CompressBpsOut: %u\n"
2306 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002307#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002308 "ZlibMemUsage: %ld\n"
2309 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002310#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002311 "Tasks: %d\n"
2312 "Run_queue: %d\n"
2313 "Idle_pct: %d\n"
2314 "node: %s\n"
2315 "description: %s\n"
2316 "",
2317 global.nbproc,
2318 relative_pid,
2319 pid,
2320 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2321 up,
2322 global.rlimit_memmax,
2323 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002324 global.maxsock, global.maxconn, global.hardmaxconn,
2325 actconn, totalconn, global.req_count,
2326#ifdef USE_OPENSSL
2327 global.maxsslconn, sslconns, totalsslconns,
2328#endif
2329 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002330 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002331 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002332#ifdef USE_OPENSSL
2333 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2334#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002335 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2336 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002337#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002338 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002339#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002340 nb_tasks_cur, run_queue_cur, idle_pct,
2341 global.node, global.desc ? global.desc : ""
2342 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002343
2344 if (bi_putchk(si->ib, &trash) == -1)
2345 return 0;
2346
2347 return 1;
2348}
2349
Willy Tarreau12833bb2014-01-28 16:49:56 +01002350/* This function dumps memory usage information onto the stream interface's
2351 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2352 * completion. No state is used.
2353 */
2354static int stats_dump_pools_to_buffer(struct stream_interface *si)
2355{
2356 dump_pools_to_trash();
2357 if (bi_putchk(si->ib, &trash) == -1)
2358 return 0;
2359 return 1;
2360}
2361
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002362/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2363 * the state from stream interface <si>. The caller is responsible for clearing
2364 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002365 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002366static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002367{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002368 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002369 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002370
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002371 if (!(px->cap & PR_CAP_FE))
2372 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375 return 0;
2376
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002377 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002378 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002379 /* name, queue */
2380 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002382 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002383 /* Column sub-heading for Enable or Disable server */
2384 chunk_appendf(&trash, "<td></td>");
2385 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002386
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002387 chunk_appendf(&trash,
2388 "<td class=ac>"
2389 "<a name=\"%s/Frontend\"></a>"
2390 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2391 "<td colspan=3></td>"
2392 "",
2393 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002394
Willy Tarreau466c9b52012-12-23 02:25:03 +01002395 chunk_appendf(&trash,
2396 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002397 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002398 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2399 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2400 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002401 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2402 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2403 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002405 if (px->mode == PR_MODE_HTTP)
2406 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002407 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002408 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002409
2410 chunk_appendf(&trash,
2411 "</table></div></u></td>"
2412 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002413 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002414 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2415 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2416 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002417 U2H(px->fe_counters.sps_max),
2418 U2H(px->fe_counters.cps_max),
2419 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002420
2421 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002422 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002423 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002424 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002425
2426 chunk_appendf(&trash,
2427 "</table></div></u></td>"
2428 /* sessions rate : limit */
2429 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002430 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002431
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002432 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002433 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002434 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002435 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002436 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2437 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002438 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002439 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2440 U2H(px->fe_counters.cum_sess),
2441 U2H(px->fe_counters.cum_conn),
2442 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002443
Willy Tarreau466c9b52012-12-23 02:25:03 +01002444 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002445 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002446 chunk_appendf(&trash,
2447 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2448 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2449 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2450 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2451 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2452 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2453 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2454 "<tr><th>- other responses:</th><td>%s</td></tr>"
2455 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2456 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002457 U2H(px->fe_counters.p.http.cum_req),
2458 U2H(px->fe_counters.p.http.rsp[1]),
2459 U2H(px->fe_counters.p.http.rsp[2]),
2460 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002461 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002462 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002463 U2H(px->fe_counters.p.http.rsp[3]),
2464 U2H(px->fe_counters.p.http.rsp[4]),
2465 U2H(px->fe_counters.p.http.rsp[5]),
2466 U2H(px->fe_counters.p.http.rsp[0]),
2467 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002468 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002469
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002470 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002471 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002472 /* sessions: lbtot, lastsess */
2473 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002474 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002475 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002476 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002477 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002478
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002479 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002480 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002481 "<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 +01002482 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002483 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002484 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2485 px->fe_counters.comp_in ?
2486 (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 +01002487 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002488
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002489 chunk_appendf(&trash,
2490 /* denied: req, resp */
2491 "<td>%s</td><td>%s</td>"
2492 /* errors : request, connect, response */
2493 "<td>%s</td><td></td><td></td>"
2494 /* warnings: retries, redispatches */
2495 "<td></td><td></td>"
2496 /* server status : reflect frontend status */
2497 "<td class=ac>%s</td>"
2498 /* rest of server: nothing */
2499 "<td class=ac colspan=8></td></tr>"
2500 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002501 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2502 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002503 px->state == PR_STREADY ? "OPEN" :
2504 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002505 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002506 else { /* CSV mode */
2507 chunk_appendf(&trash,
2508 /* pxid, name, queue cur, queue max, */
2509 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002510 /* sessions : current, max, limit, total */
2511 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002512 /* bytes : in, out */
2513 "%lld,%lld,"
2514 /* denied: req, resp */
2515 "%lld,%lld,"
2516 /* errors : request, connect, response */
2517 "%lld,,,"
2518 /* warnings: retries, redispatches */
2519 ",,"
2520 /* server status : reflect frontend status */
2521 "%s,"
2522 /* rest of server: nothing */
2523 ",,,,,,,,"
2524 /* pid, iid, sid, throttle, lbtot, tracked, type */
2525 "%d,%d,0,,,,%d,"
2526 /* rate, rate_lim, rate_max */
2527 "%u,%u,%u,"
2528 /* check_status, check_code, check_duration */
2529 ",,,",
2530 px->id,
2531 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2532 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2533 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2534 px->fe_counters.failed_req,
2535 px->state == PR_STREADY ? "OPEN" :
2536 px->state == PR_STFULL ? "FULL" : "STOP",
2537 relative_pid, px->uuid, STATS_TYPE_FE,
2538 read_freq_ctr(&px->fe_sess_per_sec),
2539 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002540
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002541 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2542 if (px->mode == PR_MODE_HTTP) {
2543 for (i=1; i<6; i++)
2544 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2545 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2546 }
2547 else
2548 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002549
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002550 /* failed health analyses */
2551 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002552
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002553 /* requests : req_rate, req_rate_max, req_tot, */
2554 chunk_appendf(&trash, "%u,%u,%lld,",
2555 read_freq_ctr(&px->fe_req_per_sec),
2556 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2557
2558 /* errors: cli_aborts, srv_aborts */
2559 chunk_appendf(&trash, ",,");
2560
2561 /* compression: in, out, bypassed */
2562 chunk_appendf(&trash, "%lld,%lld,%lld,",
2563 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2564
2565 /* compression: comp_rsp */
2566 chunk_appendf(&trash, "%lld,",
2567 px->fe_counters.p.http.comp_rsp);
2568
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002569 /* lastsess */
2570 chunk_appendf(&trash, ",");
2571
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 /* finish with EOL */
2573 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002574 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002576}
2577
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2579 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2580 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2581 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002582 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002584{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002585 struct appctx *appctx = __objt_appctx(si->end);
2586
2587 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002588 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002589 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002590 /* Column sub-heading for Enable or Disable server */
2591 chunk_appendf(&trash, "<td></td>");
2592 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002593 chunk_appendf(&trash,
2594 /* frontend name, listener name */
2595 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2596 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2597 "",
2598 px->id, l->name,
2599 (flags & ST_SHLGNDS)?"<u>":"",
2600 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002602 if (flags & ST_SHLGNDS) {
2603 char str[INET6_ADDRSTRLEN];
2604 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002605
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002606 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002607
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002608 port = get_host_port(&l->addr);
2609 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2610 case AF_INET:
2611 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2612 break;
2613 case AF_INET6:
2614 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2615 break;
2616 case AF_UNIX:
2617 chunk_appendf(&trash, "unix, ");
2618 break;
2619 case -1:
2620 chunk_appendf(&trash, "(%s), ", strerror(errno));
2621 break;
2622 }
Willy Tarreau91861262007-10-17 17:06:05 +02002623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002625 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 }
Willy Tarreau91861262007-10-17 17:06:05 +02002627
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002628 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002629 /* queue */
2630 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002631 /* sessions rate: current, max, limit */
2632 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002633 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002634 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002635 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002636 /* bytes: in, out */
2637 "<td>%s</td><td>%s</td>"
2638 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002639 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002640 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2641 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002642
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002643 chunk_appendf(&trash,
2644 /* denied: req, resp */
2645 "<td>%s</td><td>%s</td>"
2646 /* errors: request, connect, response */
2647 "<td>%s</td><td></td><td></td>"
2648 /* warnings: retries, redispatches */
2649 "<td></td><td></td>"
2650 /* server status: reflect listener status */
2651 "<td class=ac>%s</td>"
2652 /* rest of server: nothing */
2653 "<td class=ac colspan=8></td></tr>"
2654 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002655 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2656 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002657 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2658 }
2659 else { /* CSV mode */
2660 chunk_appendf(&trash,
2661 /* pxid, name, queue cur, queue max, */
2662 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002663 /* sessions: current, max, limit, total */
2664 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002665 /* bytes: in, out */
2666 "%lld,%lld,"
2667 /* denied: req, resp */
2668 "%lld,%lld,"
2669 /* errors: request, connect, response */
2670 "%lld,,,"
2671 /* warnings: retries, redispatches */
2672 ",,"
2673 /* server status: reflect listener status */
2674 "%s,"
2675 /* rest of server: nothing */
2676 ",,,,,,,,"
2677 /* pid, iid, sid, throttle, lbtot, tracked, type */
2678 "%d,%d,%d,,,,%d,"
2679 /* rate, rate_lim, rate_max */
2680 ",,,"
2681 /* check_status, check_code, check_duration */
2682 ",,,"
2683 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2684 ",,,,,,"
2685 /* failed health analyses */
2686 ","
2687 /* requests : req_rate, req_rate_max, req_tot, */
2688 ",,,"
2689 /* errors: cli_aborts, srv_aborts */
2690 ",,"
2691 /* compression: in, out, bypassed, comp_rsp */
2692 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002693 /* lastsess */
2694 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002695 "\n",
2696 px->id, l->name,
2697 l->nbconn, l->counters->conn_max,
2698 l->maxconn, l->counters->cum_conn,
2699 l->counters->bytes_in, l->counters->bytes_out,
2700 l->counters->denied_req, l->counters->denied_resp,
2701 l->counters->failed_req,
2702 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2703 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2704 }
2705 return 1;
2706}
Willy Tarreau91861262007-10-17 17:06:05 +02002707
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002708/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2709 * from stream interface <si>, stats flags <flags>, and server state <state>.
2710 * The caller is responsible for clearing the trash if needed. Returns non-zero
2711 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002712 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2713 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002714 */
2715static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2716{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002717 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 struct server *ref = sv->track ? sv->track : sv;
2719 char str[INET6_ADDRSTRLEN];
2720 struct chunk src;
2721 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002722
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002723 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002724 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 "DOWN",
2726 "DN %d/%d &uarr;",
2727 "UP %d/%d &darr;",
2728 "UP",
2729 "NOLB %d/%d &darr;",
2730 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002731 "DRAIN %d/%d &darr;",
2732 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002733 "<i>no check</i>"
2734 };
Willy Tarreau91861262007-10-17 17:06:05 +02002735
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2737 chunk_appendf(&trash, "<tr class=\"maintain\">");
2738 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002739 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002740 "<tr class=\"%s%d\">",
2741 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002742
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002743 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002744 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002745 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2746 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002747
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002748 chunk_appendf(&trash,
2749 "<td class=ac><a name=\"%s/%s\"></a>%s"
2750 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2751 "",
2752 px->id, sv->id,
2753 (flags & ST_SHLGNDS) ? "<u>" : "",
2754 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002756 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002757 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002758
2759 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2760 case AF_INET:
2761 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2762 break;
2763 case AF_INET6:
2764 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2765 break;
2766 case AF_UNIX:
2767 chunk_appendf(&trash, "unix, ");
2768 break;
2769 case -1:
2770 chunk_appendf(&trash, "(%s), ", strerror(errno));
2771 break;
2772 default: /* address family not supported */
2773 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002774 }
Willy Tarreau91861262007-10-17 17:06:05 +02002775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 /* id */
2777 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002778
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002779 /* cookie */
2780 if (sv->cookie) {
2781 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002782
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002783 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2784 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002785
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002786 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002787 }
2788
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002789 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002790 }
Willy Tarreau91861262007-10-17 17:06:05 +02002791
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002793 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002794 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002795 /* sessions rate : current, max, limit */
2796 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002799 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2800 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002801
Willy Tarreau466c9b52012-12-23 02:25:03 +01002802
2803 chunk_appendf(&trash,
2804 /* sessions: current, max, limit, total */
2805 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002806 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002807 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2808 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002809 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2810 U2H(sv->counters.cum_sess),
2811 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002812
Willy Tarreau466c9b52012-12-23 02:25:03 +01002813 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2814 if (px->mode == PR_MODE_HTTP) {
2815 unsigned long long tot;
2816 for (tot = i = 0; i < 6; i++)
2817 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818
Willy Tarreau466c9b52012-12-23 02:25:03 +01002819 chunk_appendf(&trash,
2820 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2821 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2822 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2823 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2824 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2825 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2826 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2827 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002828 U2H(tot),
2829 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2830 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2831 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2832 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2833 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2834 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 +02002835 }
Willy Tarreau91861262007-10-17 17:06:05 +02002836
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002837 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002838 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002839 /* sessions: lbtot, last */
2840 "<td>%s</td><td>%s</td>",
2841 U2H(sv->counters.cum_lbconn),
2842 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002843
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002844 chunk_appendf(&trash,
2845 /* bytes : in, out */
2846 "<td>%s</td><td>%s</td>"
2847 /* denied: req, resp */
2848 "<td></td><td>%s</td>"
2849 /* errors : request, connect */
2850 "<td></td><td>%s</td>"
2851 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002852 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002853 /* warnings: retries, redispatches */
2854 "<td>%lld</td><td>%lld</td>"
2855 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002856 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2857 U2H(sv->counters.failed_secu),
2858 U2H(sv->counters.failed_conns),
2859 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002860 sv->counters.cli_aborts,
2861 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862 sv->counters.retries, sv->counters.redispatches);
2863
2864 /* status, lest check */
2865 chunk_appendf(&trash, "<td class=ac>");
2866
2867 if (sv->state & SRV_MAINTAIN) {
2868 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2869 chunk_appendf(&trash, "MAINT");
2870 }
2871 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2872 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2873 chunk_appendf(&trash, "MAINT(via)");
2874 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002875 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002876 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2877 chunk_appendf(&trash,
2878 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002879 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2880 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002881 }
Willy Tarreau91861262007-10-17 17:06:05 +02002882
Willy Tarreauff5ae352013-12-11 20:36:34 +01002883 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002884 chunk_appendf(&trash,
2885 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002886 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002887 get_check_status_info(sv->check.status));
2888
2889 if (sv->check.status >= HCHK_STATUS_L57DATA)
2890 chunk_appendf(&trash, "/%d", sv->check.code);
2891
2892 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002893 chunk_appendf(&trash, " in %lums", sv->check.duration);
2894
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002895 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002896 get_check_status_description(sv->check.status));
2897 if (*sv->check.desc) {
2898 chunk_appendf(&trash, ": ");
2899 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2900 chunk_htmlencode(&trash, &src);
2901 }
2902 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002903 }
2904 else
2905 chunk_appendf(&trash, "</td><td>");
2906
2907 chunk_appendf(&trash,
2908 /* weight */
2909 "</td><td class=ac>%d</td>"
2910 /* act, bck */
2911 "<td class=ac>%s</td><td class=ac>%s</td>"
2912 "",
2913 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2914 (sv->state & SRV_BACKUP) ? "-" : "Y",
2915 (sv->state & SRV_BACKUP) ? "Y" : "-");
2916
2917 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002918 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002919 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002920
2921 if (ref->observe)
2922 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2923
2924 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002925 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002926 "<td>%lld</td><td>%s</td>"
2927 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002928 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002929 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2930 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002931 else if (sv != ref) {
2932 if (sv->state & SRV_MAINTAIN)
2933 chunk_appendf(&trash,
2934 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2935 ref->proxy->id, ref->id);
2936 else
2937 chunk_appendf(&trash,
2938 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2939 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2940 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002941 else
2942 chunk_appendf(&trash, "<td colspan=3></td>");
2943
2944 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002945 if (sv->state & SRV_WARMINGUP)
2946 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002947 else
2948 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2949 }
2950 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002951 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002952 "DOWN,",
2953 "DOWN %d/%d,",
2954 "UP %d/%d,",
2955 "UP,",
2956 "NOLB %d/%d,",
2957 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002958 "DRAIN %d/%d,",
2959 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002960 "no check,"
2961 };
2962
2963 chunk_appendf(&trash,
2964 /* pxid, name */
2965 "%s,%s,"
2966 /* queue : current, max */
2967 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002968 /* sessions : current, max, limit, total */
2969 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002970 /* bytes : in, out */
2971 "%lld,%lld,"
2972 /* denied: req, resp */
2973 ",%lld,"
2974 /* errors : request, connect, response */
2975 ",%lld,%lld,"
2976 /* warnings: retries, redispatches */
2977 "%lld,%lld,"
2978 "",
2979 px->id, sv->id,
2980 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002981 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002982 sv->counters.bytes_in, sv->counters.bytes_out,
2983 sv->counters.failed_secu,
2984 sv->counters.failed_conns, sv->counters.failed_resp,
2985 sv->counters.retries, sv->counters.redispatches);
2986
2987 /* status */
2988 if (sv->state & SRV_MAINTAIN)
2989 chunk_appendf(&trash, "MAINT,");
2990 else if (ref != sv && ref->state & SRV_MAINTAIN)
2991 chunk_appendf(&trash, "MAINT(via),");
2992 else
2993 chunk_appendf(&trash,
2994 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002995 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2996 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002997
2998 chunk_appendf(&trash,
2999 /* weight, active, backup */
3000 "%d,%d,%d,"
3001 "",
3002 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3003 (sv->state & SRV_BACKUP) ? 0 : 1,
3004 (sv->state & SRV_BACKUP) ? 1 : 0);
3005
3006 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003007 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003008 chunk_appendf(&trash,
3009 "%lld,%lld,%d,%d,",
3010 sv->counters.failed_checks, sv->counters.down_trans,
3011 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3012 else
3013 chunk_appendf(&trash, ",,,,");
3014
3015 /* queue limit, pid, iid, sid, */
3016 chunk_appendf(&trash,
3017 "%s,"
3018 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003019 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003020 relative_pid, px->uuid, sv->puid);
3021
3022 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01003023 if (sv->state & SRV_WARMINGUP)
3024 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025
3026 /* sessions: lbtot */
3027 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3028
3029 /* tracked */
3030 if (sv->track)
3031 chunk_appendf(&trash, "%s/%s,",
3032 sv->track->proxy->id, sv->track->id);
3033 else
3034 chunk_appendf(&trash, ",");
3035
3036 /* type */
3037 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3038
3039 /* rate */
3040 chunk_appendf(&trash, "%u,,%u,",
3041 read_freq_ctr(&sv->sess_per_sec),
3042 sv->counters.sps_max);
3043
Willy Tarreauff5ae352013-12-11 20:36:34 +01003044 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 /* check_status */
3046 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3047
3048 /* check_code */
3049 if (sv->check.status >= HCHK_STATUS_L57DATA)
3050 chunk_appendf(&trash, "%u,", sv->check.code);
3051 else
3052 chunk_appendf(&trash, ",");
3053
3054 /* check_duration */
3055 if (sv->check.status >= HCHK_STATUS_CHECKED)
3056 chunk_appendf(&trash, "%lu,", sv->check.duration);
3057 else
3058 chunk_appendf(&trash, ",");
3059
3060 }
3061 else
3062 chunk_appendf(&trash, ",,,");
3063
3064 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3065 if (px->mode == PR_MODE_HTTP) {
3066 for (i=1; i<6; i++)
3067 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3068
3069 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3070 }
3071 else
3072 chunk_appendf(&trash, ",,,,,,");
3073
3074 /* failed health analyses */
3075 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3076
3077 /* requests : req_rate, req_rate_max, req_tot, */
3078 chunk_appendf(&trash, ",,,");
3079
3080 /* errors: cli_aborts, srv_aborts */
3081 chunk_appendf(&trash, "%lld,%lld,",
3082 sv->counters.cli_aborts, sv->counters.srv_aborts);
3083
3084 /* compression: in, out, bypassed, comp_rsp */
3085 chunk_appendf(&trash, ",,,,");
3086
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003087 /* lastsess */
3088 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3089
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003090 /* finish with EOL */
3091 chunk_appendf(&trash, "\n");
3092 }
3093 return 1;
3094}
3095
3096/* Dumps a line for backend <px> to the trash for and uses the state from stream
3097 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3098 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3099 */
3100static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3101{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003103 struct chunk src;
3104 int i;
3105
3106 if (!(px->cap & PR_CAP_BE))
3107 return 0;
3108
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003109 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 return 0;
3111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003112 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003113 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003114 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003115 /* Column sub-heading for Enable or Disable server */
3116 chunk_appendf(&trash, "<td></td>");
3117 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003118 chunk_appendf(&trash,
3119 "<td class=ac>"
3120 /* name */
3121 "%s<a name=\"%s/Backend\"></a>"
3122 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3123 "",
3124 (flags & ST_SHLGNDS)?"<u>":"",
3125 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003126
3127 if (flags & ST_SHLGNDS) {
3128 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003129 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3131
3132 /* cookie */
3133 if (px->cookie_name) {
3134 chunk_appendf(&trash, ", cookie: '");
3135 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3136 chunk_htmlencode(&trash, &src);
3137 chunk_appendf(&trash, "'");
3138 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003139 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140 }
3141
3142 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003143 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003144 /* queue : current, max */
3145 "<td>%s</td><td>%s</td><td></td>"
3146 /* sessions rate : current, max, limit */
3147 "<td>%s</td><td>%s</td><td></td>"
3148 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003149 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003150 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3151 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152
3153 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003154 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003155 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003156 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003157 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003158 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003159 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3160 U2H(px->be_counters.cum_conn),
3161 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003162
Willy Tarreau466c9b52012-12-23 02:25:03 +01003163 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003164 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003165 chunk_appendf(&trash,
3166 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3167 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3168 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3169 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3170 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3171 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3172 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3173 "<tr><th>- other responses:</th><td>%s</td></tr>"
3174 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3175 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003176 U2H(px->be_counters.p.http.cum_req),
3177 U2H(px->be_counters.p.http.rsp[1]),
3178 U2H(px->be_counters.p.http.rsp[2]),
3179 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003181 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003182 U2H(px->be_counters.p.http.rsp[3]),
3183 U2H(px->be_counters.p.http.rsp[4]),
3184 U2H(px->be_counters.p.http.rsp[5]),
3185 U2H(px->be_counters.p.http.rsp[0]),
3186 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003187 }
3188
3189 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003190 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003191 /* sessions: lbtot, last */
3192 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003193 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003194 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003195 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003196 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003197 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003198 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003199
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003200 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003201 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003202 "<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 +01003203 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003204 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003205 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3206 px->be_counters.comp_in ?
3207 (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 +01003208 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3209
3210 chunk_appendf(&trash,
3211 /* denied: req, resp */
3212 "<td>%s</td><td>%s</td>"
3213 /* errors : request, connect */
3214 "<td></td><td>%s</td>"
3215 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003216 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 /* warnings: retries, redispatches */
3218 "<td>%lld</td><td>%lld</td>"
3219 /* backend status: reflect backend status (up/down): we display UP
3220 * if the backend has known working servers or if it has no server at
3221 * all (eg: for stats). Then we display the total weight, number of
3222 * active and backups. */
3223 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3224 "<td class=ac>%d</td><td class=ac>%d</td>"
3225 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003226 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3227 U2H(px->be_counters.failed_conns),
3228 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 px->be_counters.cli_aborts,
3230 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231 px->be_counters.retries, px->be_counters.redispatches,
3232 human_time(now.tv_sec - px->last_change, 1),
3233 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3234 "<font color=\"red\"><b>DOWN</b></font>",
3235 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3236 px->srv_act, px->srv_bck);
3237
3238 chunk_appendf(&trash,
3239 /* rest of backend: nothing, down transitions, total downtime, throttle */
3240 "<td class=ac>&nbsp;</td><td>%d</td>"
3241 "<td>%s</td>"
3242 "<td></td>"
3243 "</tr>",
3244 px->down_trans,
3245 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3246 }
3247 else { /* CSV mode */
3248 chunk_appendf(&trash,
3249 /* pxid, name */
3250 "%s,BACKEND,"
3251 /* queue : current, max */
3252 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003253 /* sessions : current, max, limit, total */
3254 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003255 /* bytes : in, out */
3256 "%lld,%lld,"
3257 /* denied: req, resp */
3258 "%lld,%lld,"
3259 /* errors : request, connect, response */
3260 ",%lld,%lld,"
3261 /* warnings: retries, redispatches */
3262 "%lld,%lld,"
3263 /* backend status: reflect backend status (up/down): we display UP
3264 * if the backend has known working servers or if it has no server at
3265 * all (eg: for stats). Then we display the total weight, number of
3266 * active and backups. */
3267 "%s,"
3268 "%d,%d,%d,"
3269 /* rest of backend: nothing, down transitions, last change, total downtime */
3270 ",%d,%d,%d,,"
3271 /* pid, iid, sid, throttle, lbtot, tracked, type */
3272 "%d,%d,0,,%lld,,%d,"
3273 /* rate, rate_lim, rate_max, */
3274 "%u,,%u,"
3275 /* check_status, check_code, check_duration */
3276 ",,,",
3277 px->id,
3278 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3279 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3280 px->be_counters.bytes_in, px->be_counters.bytes_out,
3281 px->be_counters.denied_req, px->be_counters.denied_resp,
3282 px->be_counters.failed_conns, px->be_counters.failed_resp,
3283 px->be_counters.retries, px->be_counters.redispatches,
3284 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3285 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3286 px->srv_act, px->srv_bck,
3287 px->down_trans, (int)(now.tv_sec - px->last_change),
3288 px->srv?be_downtime(px):0,
3289 relative_pid, px->uuid,
3290 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3291 read_freq_ctr(&px->be_sess_per_sec),
3292 px->be_counters.sps_max);
3293
3294 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3295 if (px->mode == PR_MODE_HTTP) {
3296 for (i=1; i<6; i++)
3297 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3298 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3299 }
3300 else
3301 chunk_appendf(&trash, ",,,,,,");
3302
3303 /* failed health analyses */
3304 chunk_appendf(&trash, ",");
3305
3306 /* requests : req_rate, req_rate_max, req_tot, */
3307 chunk_appendf(&trash, ",,,");
3308
3309 /* errors: cli_aborts, srv_aborts */
3310 chunk_appendf(&trash, "%lld,%lld,",
3311 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3312
3313 /* compression: in, out, bypassed */
3314 chunk_appendf(&trash, "%lld,%lld,%lld,",
3315 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3316
3317 /* compression: comp_rsp */
3318 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3319
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003320 /* lastsess */
3321 chunk_appendf(&trash, "%d,", be_lastsession(px));
3322
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 /* finish with EOL */
3324 chunk_appendf(&trash, "\n");
3325 }
3326 return 1;
3327}
3328
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003329/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003330 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003331 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003333static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003334{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003335 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003336 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3337
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003338 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003341 /* 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 +02003342 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003343 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003344 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3346 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003347 }
3348
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003349 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003350 "<form action=\"%s%s%s%s\" method=\"post\">",
3351 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003352 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3353 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003354 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 }
3356
3357 /* print a new table */
3358 chunk_appendf(&trash,
3359 "<table class=\"tbl\" width=\"100%%\">\n"
3360 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003361 "<th class=\"pxname\" width=\"10%%\">");
3362
3363 chunk_appendf(&trash,
3364 "<a name=\"%s\"></a>%s"
3365 "<a class=px href=\"#%s\">%s</a>",
3366 px->id,
3367 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3368 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369
3370 if (uri->flags & ST_SHLGNDS) {
3371 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003372 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3374 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003375 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 }
3377
3378 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003379 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003380 "<th class=\"%s\" width=\"90%%\">%s</th>"
3381 "</tr>\n"
3382 "</table>\n"
3383 "<table class=\"tbl\" width=\"100%%\">\n"
3384 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3386 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3387
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003388 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 /* Column heading for Enable or Disable server */
3390 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003391 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392
3393 chunk_appendf(&trash,
3394 "<th rowspan=2></th>"
3395 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003396 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3398 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3399 "<th colspan=9>Server</th>"
3400 "</tr>\n"
3401 "<tr class=\"titre\">"
3402 "<th>Cur</th><th>Max</th><th>Limit</th>"
3403 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003404 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3406 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3407 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3408 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3409 "<th>Thrtle</th>\n"
3410 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003411}
3412
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003413/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003416static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003418 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 chunk_appendf(&trash, "</table>");
3420
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003421 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 /* close the form used to enable/disable this proxy servers */
3423 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003424 "Choose the action to perform on the checked servers : "
3425 "<select name=action>"
3426 "<option value=\"\"></option>"
3427 "<option value=\"disable\">Disable</option>"
3428 "<option value=\"enable\">Enable</option>"
3429 "<option value=\"stop\">Soft Stop</option>"
3430 "<option value=\"start\">Soft Start</option>"
3431 "<option value=\"shutdown\">Kill Sessions</option>"
3432 "</select>"
3433 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3434 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3435 "</form>",
3436 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 }
3438
3439 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440}
Willy Tarreau91861262007-10-17 17:06:05 +02003441
3442/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003443 * Dumps statistics for a proxy. The output is sent to the stream interface's
3444 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3445 * buffer space, or non-zero if everything completed. This function is used
3446 * both by the CLI and the HTTP entry points, and is able to dump the output
3447 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003448 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003449static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003450{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003451 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003452 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003453 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003454 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003455 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003456
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003457 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003458
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003459 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003460 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003461 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003462 if (uri && uri->scope) {
3463 /* we have a limited scope, we have to check the proxy name */
3464 struct stat_scope *scope;
3465 int len;
3466
3467 len = strlen(px->id);
3468 scope = uri->scope;
3469
3470 while (scope) {
3471 /* match exact proxy name */
3472 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3473 break;
3474
3475 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003476 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003477 break;
3478 scope = scope->next;
3479 }
3480
3481 /* proxy name not found : don't dump anything */
3482 if (scope == NULL)
3483 return 1;
3484 }
3485
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003486 /* if the user has requested a limited output and the proxy
3487 * name does not match, skip it.
3488 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003489 if (appctx->ctx.stats.scope_len &&
3490 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 +02003491 return 1;
3492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003493 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3494 (appctx->ctx.stats.iid != -1) &&
3495 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003496 return 1;
3497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003498 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003499 /* fall through */
3500
Willy Tarreau295a8372011-03-10 11:25:07 +01003501 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003502 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003503 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003504 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003505 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003506 }
Willy Tarreau91861262007-10-17 17:06:05 +02003507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003508 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003509 /* fall through */
3510
Willy Tarreau295a8372011-03-10 11:25:07 +01003511 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003512 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003513 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003514 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003515 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003516
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003517 appctx->ctx.stats.l = px->conf.listeners.n;
3518 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003519 /* fall through */
3520
Willy Tarreau295a8372011-03-10 11:25:07 +01003521 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003522 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003523 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003524 if (buffer_almost_full(rep->buf)) {
3525 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003526 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003527 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003528
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003529 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003530 if (!l->counters)
3531 continue;
3532
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003533 if (appctx->ctx.stats.flags & STAT_BOUND) {
3534 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003535 break;
3536
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003537 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003538 continue;
3539 }
3540
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003541 /* print the frontend */
3542 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3543 if (bi_putchk(rep, &trash) == -1)
3544 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003545 }
3546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3548 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003549 /* fall through */
3550
Willy Tarreau295a8372011-03-10 11:25:07 +01003551 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003552 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003553 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003554 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 +02003555
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003556 if (buffer_almost_full(rep->buf)) {
3557 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003558 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003559 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003560
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003561 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003562
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003563 if (appctx->ctx.stats.flags & STAT_BOUND) {
3564 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003565 break;
3566
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003567 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003568 continue;
3569 }
3570
Willy Tarreau44267702011-10-28 15:35:33 +02003571 if (sv->track)
3572 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003573 else
3574 svs = sv;
3575
Willy Tarreau91861262007-10-17 17:06:05 +02003576 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003577 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003578 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003579 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003580 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003581 sv_state = 3; /* UP */
3582 else
3583 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003584
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003585 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003586 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003587 else if (svs->state & SRV_GOINGDOWN)
3588 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003589 }
Willy Tarreau91861262007-10-17 17:06:05 +02003590 else
Simon Horman125d0992013-02-24 17:23:38 +09003591 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003592 sv_state = 1; /* going up */
3593 else
3594 sv_state = 0; /* DOWN */
3595
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003596 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003597 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003598 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003599 continue;
3600 }
3601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003602 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3603 if (bi_putchk(rep, &trash) == -1)
3604 return 0;
3605 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003606
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003607 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003610 case STAT_PX_ST_BE:
3611 /* print the backend */
3612 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3613 if (bi_putchk(rep, &trash) == -1)
3614 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003615
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003616 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003617 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003618
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003619 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003620 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003621 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003622 if (bi_putchk(rep, &trash) == -1)
3623 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003624 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003625
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003626 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003627 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003628
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003629 case STAT_PX_ST_FIN:
3630 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003631
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003632 default:
3633 /* unknown state, we should put an abort() here ! */
3634 return 1;
3635 }
3636}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003637
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003638/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3639 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003640 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003641static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003642{
3643 /* WARNING! This must fit in the first buffer !!! */
3644 chunk_appendf(&trash,
3645 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3646 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3647 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3648 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3649 "<style type=\"text/css\"><!--\n"
3650 "body {"
3651 " font-family: arial, helvetica, sans-serif;"
3652 " font-size: 12px;"
3653 " font-weight: normal;"
3654 " color: black;"
3655 " background: white;"
3656 "}\n"
3657 "th,td {"
3658 " font-size: 10px;"
3659 "}\n"
3660 "h1 {"
3661 " font-size: x-large;"
3662 " margin-bottom: 0.5em;"
3663 "}\n"
3664 "h2 {"
3665 " font-family: helvetica, arial;"
3666 " font-size: x-large;"
3667 " font-weight: bold;"
3668 " font-style: italic;"
3669 " color: #6020a0;"
3670 " margin-top: 0em;"
3671 " margin-bottom: 0em;"
3672 "}\n"
3673 "h3 {"
3674 " font-family: helvetica, arial;"
3675 " font-size: 16px;"
3676 " font-weight: bold;"
3677 " color: #b00040;"
3678 " background: #e8e8d0;"
3679 " margin-top: 0em;"
3680 " margin-bottom: 0em;"
3681 "}\n"
3682 "li {"
3683 " margin-top: 0.25em;"
3684 " margin-right: 2em;"
3685 "}\n"
3686 ".hr {margin-top: 0.25em;"
3687 " border-color: black;"
3688 " border-bottom-style: solid;"
3689 "}\n"
3690 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3691 ".total {background: #20D0D0;color: #ffff80;}\n"
3692 ".frontend {background: #e8e8d0;}\n"
3693 ".socket {background: #d0d0d0;}\n"
3694 ".backend {background: #e8e8d0;}\n"
3695 ".active0 {background: #ff9090;}\n"
3696 ".active1 {background: #ffd020;}\n"
3697 ".active2 {background: #ffffa0;}\n"
3698 ".active3 {background: #c0ffc0;}\n"
3699 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003700 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3701 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3702 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003703 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003704 ".backup0 {background: #ff9090;}\n"
3705 ".backup1 {background: #ff80ff;}\n"
3706 ".backup2 {background: #c060ff;}\n"
3707 ".backup3 {background: #b0d0ff;}\n"
3708 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3709 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003710 ".backup6 {background: #c060ff;}\n"
3711 ".backup7 {background: #cc9900;}\n"
3712 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003713 ".maintain {background: #c07820;}\n"
3714 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3715 "\n"
3716 "a.px:link {color: #ffff40; text-decoration: none;}"
3717 "a.px:visited {color: #ffff40; text-decoration: none;}"
3718 "a.px:hover {color: #ffffff; text-decoration: none;}"
3719 "a.lfsb:link {color: #000000; text-decoration: none;}"
3720 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3721 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3722 "\n"
3723 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3724 "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"
3725 "table.tbl td.ac { text-align: center;}\n"
3726 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3727 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3728 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3729 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3730 "\n"
3731 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3732 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3733 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003734 "table.det { border-collapse: collapse; border-style: none; }\n"
3735 "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 +01003736 "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 +01003737 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003738 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003739 " display:block;\n"
3740 " visibility:hidden;\n"
3741 " z-index:2147483647;\n"
3742 " position:absolute;\n"
3743 " padding:2px 4px 3px;\n"
3744 " background:#f0f060; color:#000000;\n"
3745 " border:1px solid #7040c0;\n"
3746 " white-space:nowrap;\n"
3747 " font-style:normal;font-size:11px;font-weight:normal;\n"
3748 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3749 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3750 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003751 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003752 "-->\n"
3753 "</style></head>\n",
3754 (uri->flags & ST_SHNODE) ? " on " : "",
3755 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3756 );
3757}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003758
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003759/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003760 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003761 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003762 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003763static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003764{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003765 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003766 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003767 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003768
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003769 /* WARNING! this has to fit the first packet too.
3770 * We are around 3.5 kB, add adding entries will
3771 * become tricky if we want to support 4kB buffers !
3772 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003773 chunk_appendf(&trash,
3774 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3775 PRODUCT_NAME "%s</a></h1>\n"
3776 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3777 "<hr width=\"100%%\" class=\"hr\">\n"
3778 "<h3>&gt; General process information</h3>\n"
3779 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3780 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3781 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3782 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3783 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3784 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3785 "Running tasks: %d/%d; idle = %d %%<br>\n"
3786 "</td><td align=\"center\" nowrap>\n"
3787 "<table class=\"lgd\"><tr>\n"
3788 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3789 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3790 "</tr><tr>\n"
3791 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3792 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3793 "</tr><tr>\n"
3794 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3795 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3796 "</tr><tr>\n"
3797 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003798 "</tr><tr>\n"
3799 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003800 "</tr><tr>\n"
3801 "<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 -07003802 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003803 "<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 +01003804 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003805 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003806 "</td>"
3807 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3808 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3809 "",
3810 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3811 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3812 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3813 (uri->flags & ST_SHDESC) ? ": " : "",
3814 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3815 pid, relative_pid, global.nbproc,
3816 up / 86400, (up % 86400) / 3600,
3817 (up % 3600) / 60, (up % 60),
3818 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3819 global.rlimit_memmax ? " MB" : "",
3820 global.rlimit_nofile,
3821 global.maxsock, global.maxconn, global.maxpipes,
3822 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3823 run_queue_cur, nb_tasks_cur, idle_pct
3824 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003825
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003826 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3827 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3828 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003829
3830 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003831 "<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 +02003832 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003833 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3834 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3835 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003836 STAT_SCOPE_TXT_MAXLEN);
3837
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003838 /* 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 +02003839 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003840 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003841 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003842 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3843 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003844 }
3845
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003846 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
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\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849 uri->uri_prefix,
3850 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003851 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003852 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\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003856 uri->uri_prefix,
3857 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003858 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003859 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003863 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003864 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003865 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003866 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003867 "",
3868 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869 else
3870 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003871 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003872 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003873 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003874 ";norefresh",
3875 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003876 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003877
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003878 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003879 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003880 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003881 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3882 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003883 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003884
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003885 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003886 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003887 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003888 (uri->refresh > 0) ? ";norefresh" : "",
3889 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003890
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003891 chunk_appendf(&trash,
3892 "</ul></td>"
3893 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3894 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3895 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3896 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3897 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3898 "</ul>"
3899 "</td>"
3900 "</tr></table>\n"
3901 ""
3902 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003903
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003904 if (appctx->ctx.stats.st_code) {
3905 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003906 case STAT_STATUS_DONE:
3907 chunk_appendf(&trash,
3908 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003909 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003911 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003912 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3913 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003914 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003915 break;
3916 case STAT_STATUS_NONE:
3917 chunk_appendf(&trash,
3918 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003919 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003920 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003921 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003922 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3923 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003924 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003925 break;
3926 case STAT_STATUS_PART:
3927 chunk_appendf(&trash,
3928 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003929 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930 "Action partially processed.<br>"
3931 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003932 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003933 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3934 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003935 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003936 break;
3937 case STAT_STATUS_ERRP:
3938 chunk_appendf(&trash,
3939 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003940 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003941 "Action not processed because of invalid parameters."
3942 "<ul>"
3943 "<li>The action is maybe unknown.</li>"
3944 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3945 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3946 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003947 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003948 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3949 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003950 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003951 break;
3952 case STAT_STATUS_EXCD:
3953 chunk_appendf(&trash,
3954 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003955 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003956 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3957 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003958 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003959 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3960 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003961 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962 break;
3963 case STAT_STATUS_DENY:
3964 chunk_appendf(&trash,
3965 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003966 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003967 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003968 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003969 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3970 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003971 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972 break;
3973 default:
3974 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003975 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003976 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003977 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003978 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003979 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3980 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003981 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003982 }
3983 chunk_appendf(&trash, "<p>\n");
3984 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003986
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003987/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3988 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003989 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003990static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003991{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003994
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003995/* This function dumps statistics onto the stream interface's read buffer in
3996 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003997 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3998 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3999 * and the session must be closed, or -1 in case of any error. This function is
4000 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004001 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004002static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004004 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 struct channel *rep = si->ib;
4006 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004008 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004009
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004010 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004011 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004012 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004013 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004015 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004016 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004017 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004018 else
4019 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004020
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021 if (bi_putchk(rep, &trash) == -1)
4022 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004023
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004024 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004025 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004026
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004028 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004029 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004030 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004031 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032 }
Willy Tarreau91861262007-10-17 17:06:05 +02004033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004034 appctx->ctx.stats.px = proxy;
4035 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4036 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004037 /* fall through */
4038
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 case STAT_ST_LIST:
4040 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004041 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004042 if (buffer_almost_full(rep->buf)) {
4043 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004044 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004045 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004046
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004047 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004048 /* skip the disabled proxies, global frontend and non-networked ones */
4049 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004050 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004051 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004052
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004053 appctx->ctx.stats.px = px->next;
4054 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004055 }
4056 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004057
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004058 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004059 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004060
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004061 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004062 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004063 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004064 if (bi_putchk(rep, &trash) == -1)
4065 return 0;
4066 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004067
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004068 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004069 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004070
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004071 case STAT_ST_FIN:
4072 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004073
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 default:
4075 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004076 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004077 return -1;
4078 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079}
Willy Tarreauae526782010-03-04 20:34:23 +01004080
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004081/* We reached the stats page through a POST request. The appctx is
4082 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004083 * Parse the posted data and enable/disable servers if necessary.
4084 * Returns 1 if request was parsed or zero if it needs more data.
4085 */
4086static int stats_process_http_post(struct stream_interface *si)
4087{
4088 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004089 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004090
4091 struct proxy *px = NULL;
4092 struct server *sv = NULL;
4093
4094 char key[LINESIZE];
4095 int action = ST_ADM_ACTION_NONE;
4096 int reprocess = 0;
4097
4098 int total_servers = 0;
4099 int altered_servers = 0;
4100
4101 char *first_param, *cur_param, *next_param, *end_params;
4102 char *st_cur_param = NULL;
4103 char *st_next_param = NULL;
4104
4105 struct chunk *temp;
4106 int reql;
4107
4108 temp = get_trash_chunk();
4109 if (temp->size < s->txn.req.body_len) {
4110 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004111 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004112 goto out;
4113 }
4114
4115 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4116 if (reql <= 0) {
4117 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004118 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004119 return 0;
4120 }
4121
4122 first_param = temp->str;
4123 end_params = temp->str + reql;
4124 cur_param = next_param = end_params;
4125 *end_params = '\0';
4126
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004127 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004128
4129 /*
4130 * Parse the parameters in reverse order to only store the last value.
4131 * From the html form, the backend and the action are at the end.
4132 */
4133 while (cur_param > first_param) {
4134 char *value;
4135 int poffset, plen;
4136
4137 cur_param--;
4138
4139 if ((*cur_param == '&') || (cur_param == first_param)) {
4140 reprocess_servers:
4141 /* Parse the key */
4142 poffset = (cur_param != first_param ? 1 : 0);
4143 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4144 if ((plen > 0) && (plen <= sizeof(key))) {
4145 strncpy(key, cur_param + poffset, plen);
4146 key[plen - 1] = '\0';
4147 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004148 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004149 goto out;
4150 }
4151
4152 /* Parse the value */
4153 value = key;
4154 while (*value != '\0' && *value != '=') {
4155 value++;
4156 }
4157 if (*value == '=') {
4158 /* Ok, a value is found, we can mark the end of the key */
4159 *value++ = '\0';
4160 }
4161 if (url_decode(key) < 0 || url_decode(value) < 0)
4162 break;
4163
4164 /* Now we can check the key to see what to do */
4165 if (!px && (strcmp(key, "b") == 0)) {
4166 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4167 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004168 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004169 goto out;
4170 }
4171 }
4172 else if (!action && (strcmp(key, "action") == 0)) {
4173 if (strcmp(value, "disable") == 0) {
4174 action = ST_ADM_ACTION_DISABLE;
4175 }
4176 else if (strcmp(value, "enable") == 0) {
4177 action = ST_ADM_ACTION_ENABLE;
4178 }
4179 else if (strcmp(value, "stop") == 0) {
4180 action = ST_ADM_ACTION_STOP;
4181 }
4182 else if (strcmp(value, "start") == 0) {
4183 action = ST_ADM_ACTION_START;
4184 }
4185 else if (strcmp(value, "shutdown") == 0) {
4186 action = ST_ADM_ACTION_SHUTDOWN;
4187 }
4188 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004189 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004190 goto out;
4191 }
4192 }
4193 else if (strcmp(key, "s") == 0) {
4194 if (!(px && action)) {
4195 /*
4196 * Indicates that we'll need to reprocess the parameters
4197 * as soon as backend and action are known
4198 */
4199 if (!reprocess) {
4200 st_cur_param = cur_param;
4201 st_next_param = next_param;
4202 }
4203 reprocess = 1;
4204 }
4205 else if ((sv = findserver(px, value)) != NULL) {
4206 switch (action) {
4207 case ST_ADM_ACTION_DISABLE:
4208 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4209 /* Not already in maintenance, we can change the server state */
4210 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004211 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004212 set_server_down(&sv->check);
4213 altered_servers++;
4214 total_servers++;
4215 }
4216 break;
4217 case ST_ADM_ACTION_ENABLE:
4218 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004219 /* Already in maintenance, we can change the server state.
4220 * If this server tracks the status of another one,
4221 * we must restore the good status.
4222 */
4223 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4224 set_server_up(&sv->check);
4225 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4226 }
4227 else {
4228 sv->state &= ~SRV_MAINTAIN;
4229 sv->check.state &= ~CHK_ST_PAUSED;
4230 set_server_down(&sv->check);
4231 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004232 altered_servers++;
4233 total_servers++;
4234 }
4235 break;
4236 case ST_ADM_ACTION_STOP:
4237 case ST_ADM_ACTION_START:
4238 if (action == ST_ADM_ACTION_START)
4239 sv->uweight = sv->iweight;
4240 else
4241 sv->uweight = 0;
4242
4243 server_recalc_eweight(sv);
4244 set_server_drain_state(sv);
4245
4246 altered_servers++;
4247 total_servers++;
4248 break;
4249 case ST_ADM_ACTION_SHUTDOWN:
4250 if (px->state != PR_STSTOPPED) {
4251 struct session *sess, *sess_bck;
4252
4253 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4254 if (sess->srv_conn == sv)
4255 session_shutdown(sess, SN_ERR_KILLED);
4256
4257 altered_servers++;
4258 total_servers++;
4259 }
4260 break;
4261 }
4262 } else {
4263 /* the server name is unknown or ambiguous (duplicate names) */
4264 total_servers++;
4265 }
4266 }
4267 if (reprocess && px && action) {
4268 /* Now, we know the backend and the action chosen by the user.
4269 * We can safely restart from the first server parameter
4270 * to reprocess them
4271 */
4272 cur_param = st_cur_param;
4273 next_param = st_next_param;
4274 reprocess = 0;
4275 goto reprocess_servers;
4276 }
4277
4278 next_param = cur_param;
4279 }
4280 }
4281
4282 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004283 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004284 }
4285 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004286 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004287 }
4288 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004289 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004290 }
4291 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004292 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004293 }
4294 out:
4295 return 1;
4296}
4297
4298
4299static int stats_send_http_headers(struct stream_interface *si)
4300{
4301 struct session *s = session_from_task(si->owner);
4302 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004303 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004304
4305 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004306 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004307 "Cache-Control: no-cache\r\n"
4308 "Connection: close\r\n"
4309 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004310 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004311
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004313 chunk_appendf(&trash, "Refresh: %d\r\n",
4314 uri->refresh);
4315
Willy Tarreau51437d22013-12-29 00:43:40 +01004316 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004317
4318 s->txn.status = 200;
4319 s->logs.tv_request = now;
4320
4321 if (bi_putchk(si->ib, &trash) == -1)
4322 return 0;
4323
4324 return 1;
4325}
4326
4327static int stats_send_http_redirect(struct stream_interface *si)
4328{
4329 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4330 struct session *s = session_from_task(si->owner);
4331 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004332 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004333
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004334 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004335 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004336 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004337 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004338 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4339 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004340 }
4341
4342 /* We don't want to land on the posted stats page because a refresh will
4343 * repost the data. We don't want this to happen on accident so we redirect
4344 * the browse to the stats page with a GET.
4345 */
4346 chunk_printf(&trash,
4347 "HTTP/1.1 303 See Other\r\n"
4348 "Cache-Control: no-cache\r\n"
4349 "Content-Type: text/plain\r\n"
4350 "Connection: close\r\n"
4351 "Location: %s;st=%s%s%s%s\r\n"
4352 "\r\n",
4353 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004354 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4355 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4356 stat_status_codes[appctx->ctx.stats.st_code]) ?
4357 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004358 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004359 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4360 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004361 scope_txt);
4362
4363 s->txn.status = 303;
4364 s->logs.tv_request = now;
4365
4366 if (bi_putchk(si->ib, &trash) == -1)
4367 return 0;
4368
4369 return 1;
4370}
4371
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004372/* This I/O handler runs as an applet embedded in a stream interface. It is
4373 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004374 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 * automatically unregisters itself once transfer is complete.
4376 */
4377static void http_stats_io_handler(struct stream_interface *si)
4378{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004379 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004380 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004381 struct channel *req = si->ob;
4382 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004383
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004384 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4385 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004386
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004387 /* check that the output is not closed */
4388 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004389 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004390
Willy Tarreau347a35d2013-11-22 17:51:09 +01004391 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004393 if (stats_send_http_headers(si)) {
4394 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004395 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004396 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004397 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004398 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004399 }
4400
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004401 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004402 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004403 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004404 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004405
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004406 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004407 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004408 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004409 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004410 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004411 }
4412
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004413 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004414 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004415 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004416 }
4417
Willy Tarreau51437d22013-12-29 00:43:40 +01004418 if (appctx->st0 == STAT_HTTP_DONE)
4419 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004420
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004421 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4422 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004423
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004424 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004425 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4426 si_shutr(si);
4427 res->flags |= CF_READ_NULL;
4428 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004429 }
Willy Tarreau91861262007-10-17 17:06:05 +02004430
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004431 /* update all other flags and resync with the other side */
4432 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004433
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004434 /* we don't want to expire timeouts while we're processing requests */
4435 si->ib->rex = TICK_ETERNITY;
4436 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004438 out:
4439 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4440 /* check that we have released everything then unregister */
4441 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004442 }
4443}
4444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445
Willy Tarreau909d5172012-11-26 03:04:41 +01004446static inline const char *get_conn_ctrl_name(const struct connection *conn)
4447{
Willy Tarreau3c728722014-01-23 13:50:42 +01004448 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004449 return "NONE";
4450 return conn->ctrl->name;
4451}
4452
4453static inline const char *get_conn_xprt_name(const struct connection *conn)
4454{
4455 static char ptr[17];
4456
Willy Tarreauaad69382014-01-23 14:21:42 +01004457 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004458 return "NONE";
4459
4460 if (conn->xprt == &raw_sock)
4461 return "RAW";
4462
4463#ifdef USE_OPENSSL
4464 if (conn->xprt == &ssl_sock)
4465 return "SSL";
4466#endif
4467 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4468 return ptr;
4469}
4470
4471static inline const char *get_conn_data_name(const struct connection *conn)
4472{
4473 static char ptr[17];
4474
4475 if (!conn->data)
4476 return "NONE";
4477
4478 if (conn->data == &sess_conn_cb)
4479 return "SESS";
4480
4481 if (conn->data == &si_conn_cb)
4482 return "STRM";
4483
4484 if (conn->data == &check_conn_cb)
4485 return "CHCK";
4486
4487 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4488 return ptr;
4489}
4490
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004491/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004492 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004493 * 0 if the output buffer is full and it needs to be called again, otherwise
4494 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004495 */
Willy Tarreau76153662012-11-26 01:16:39 +01004496static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004497{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004499 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004500 extern const char *monthname[12];
4501 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004502 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004503 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004504
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004505 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004507 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004508 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004509 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4510 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004511 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004512 appctx->ctx.sess.uid = 0;
4513 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004514 return 1;
4515 }
4516
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004517 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004518 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004519 appctx->ctx.sess.uid = sess->uniq_id;
4520 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004521 /* fall through */
4522
4523 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004524 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004525 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004526 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004527 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004528 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4529 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004530 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004531 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004532
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004533 conn = objt_conn(sess->si[0].end);
4534 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004535 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004536 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004537 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004538 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004539 break;
4540 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004541 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004542 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004543 default:
4544 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004545 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004546 break;
4547 }
4548
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004549 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004550 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004551 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004552
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004553 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004554 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004555 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4556 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4557 sess->listener ? sess->listener->luid : 0);
4558
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004559 if (conn)
4560 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004561
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004562 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004563 case AF_INET:
4564 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004565 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004566 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004567 break;
4568 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004569 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004570 break;
4571 default:
4572 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004573 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004574 break;
4575 }
4576
Willy Tarreau50943332011-09-02 17:33:05 +02004577 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004578 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004579 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004580 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004581 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4582 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004583 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004584
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004585 conn = objt_conn(sess->si[1].end);
4586 if (conn)
4587 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004588
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004589 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004590 case AF_INET:
4591 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004592 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004593 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004594 break;
4595 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004596 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004597 break;
4598 default:
4599 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004600 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004601 break;
4602 }
4603
4604 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004605 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004606 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004607 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4608 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004609 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004610 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004611
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004612 if (conn)
4613 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004614
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004615 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004616 case AF_INET:
4617 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004618 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004619 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004620 break;
4621 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004622 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004623 break;
4624 default:
4625 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004626 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004627 break;
4628 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004629
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004630 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004631 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004632 sess->task,
4633 sess->task->state,
4634 sess->task->nice, sess->task->calls,
4635 sess->task->expire ?
4636 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4637 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4638 TICKS_TO_MS(1000)) : "<NEVER>",
4639 task_in_rq(sess->task) ? ", running" : "");
4640
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004641 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004642 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004643 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004645 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004646 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4647 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4648 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4649
4650 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004651 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004652 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004653 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004654 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004655 obj_type_name(sess->si[0].end),
4656 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004657 sess->si[0].exp ?
4658 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4659 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4660 TICKS_TO_MS(1000)) : "<NEVER>",
4661 sess->si[0].err_type);
4662
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004663 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004664 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004665 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004666 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004667 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004668 obj_type_name(sess->si[1].end),
4669 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004670 sess->si[1].exp ?
4671 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4672 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4673 TICKS_TO_MS(1000)) : "<NEVER>",
4674 sess->si[1].err_type);
4675
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004676 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004677 chunk_appendf(&trash,
4678 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004679 conn,
4680 get_conn_ctrl_name(conn),
4681 get_conn_xprt_name(conn),
4682 get_conn_data_name(conn),
4683 obj_type_name(conn->target),
4684 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004685
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004686 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004687 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 conn->flags,
4689 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004690 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004691 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004692 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004693 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004694 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4695 chunk_appendf(&trash,
4696 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4697 tmpctx,
4698 tmpctx->st0,
4699 tmpctx->st1,
4700 tmpctx->st2,
4701 tmpctx->applet->name);
4702 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004703
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004704 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004705 chunk_appendf(&trash,
4706 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004707 conn,
4708 get_conn_ctrl_name(conn),
4709 get_conn_xprt_name(conn),
4710 get_conn_data_name(conn),
4711 obj_type_name(conn->target),
4712 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004713
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004714 chunk_appendf(&trash,
4715 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004716 conn->flags,
4717 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004718 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004719 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004720 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004721 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004722 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4723 chunk_appendf(&trash,
4724 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4725 tmpctx,
4726 tmpctx->st0,
4727 tmpctx->st1,
4728 tmpctx->st2,
4729 tmpctx->applet->name);
4730 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004731
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004732 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004733 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004734 " an_exp=%s",
4735 sess->req,
4736 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004737 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004738 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004739 sess->req->analyse_exp ?
4740 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4741 TICKS_TO_MS(1000)) : "<NEVER>");
4742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004743 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004744 " rex=%s",
4745 sess->req->rex ?
4746 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4747 TICKS_TO_MS(1000)) : "<NEVER>");
4748
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004749 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004750 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004751 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004752 sess->req->wex ?
4753 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4754 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004755 sess->req->buf,
4756 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004757 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004758 sess->txn.req.next, sess->req->buf->i,
4759 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004760
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004762 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004763 " an_exp=%s",
4764 sess->rep,
4765 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004766 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004767 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004768 sess->rep->analyse_exp ?
4769 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4770 TICKS_TO_MS(1000)) : "<NEVER>");
4771
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004772 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004773 " rex=%s",
4774 sess->rep->rex ?
4775 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4776 TICKS_TO_MS(1000)) : "<NEVER>");
4777
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004778 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004779 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004780 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004781 sess->rep->wex ?
4782 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4783 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004784 sess->rep->buf,
4785 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004786 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004787 sess->txn.rsp.next, sess->rep->buf->i,
4788 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004790 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004791 return 0;
4792
4793 /* use other states to dump the contents */
4794 }
4795 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004796 appctx->ctx.sess.uid = 0;
4797 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004798 return 1;
4799}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004800
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004801static int stats_maps_list(struct stream_interface *si)
4802{
4803 struct appctx *appctx = __objt_appctx(si->end);
4804
4805 switch (appctx->st2) {
4806 case STAT_ST_INIT:
4807 /* Init to the first entry. The list cannot be change */
4808 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4809 appctx->st2 = STAT_ST_LIST;
4810 /* fall through */
4811
4812 case STAT_ST_LIST:
4813 while (appctx->ctx.map.ref) {
4814
4815 chunk_reset(&trash);
4816
4817 /* build messages */
4818 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4819
4820 if (bi_putchk(si->ib, &trash) == -1) {
4821 /* let's try again later from this session. We add ourselves into
4822 * this session's users so that it can remove us upon termination.
4823 */
4824 return 0;
4825 }
4826
4827 /* get next list entry and check the end of the list */
4828 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4829 struct map_reference *, list);
4830 if (&appctx->ctx.map.ref->list == &maps)
4831 break;
4832 }
4833
4834 appctx->st2 = STAT_ST_FIN;
4835 /* fall through */
4836
4837 default:
4838 appctx->st2 = STAT_ST_FIN;
4839 return 1;
4840 }
4841}
4842
4843static const char *smp_to_type[SMP_TYPES] = {
4844 [SMP_T_BOOL] = "bool",
4845 [SMP_T_UINT] = "uint",
4846 [SMP_T_SINT] = "sint",
4847 [SMP_T_ADDR] = "addr",
4848 [SMP_T_IPV4] = "ipv4",
4849 [SMP_T_IPV6] = "ipv6",
4850 [SMP_T_STR] = "str",
4851 [SMP_T_BIN] = "bin",
4852 [SMP_T_CSTR] = "cstr",
4853 [SMP_T_CBIN] = "cbin",
4854};
4855
4856static int stats_map_lookup(struct stream_interface *si)
4857{
4858 struct appctx *appctx = __objt_appctx(si->end);
4859 struct sample_storage *smp;
4860 struct sample sample;
4861 struct pattern *pat;
4862 struct pat_idx_elt *elt;
4863 enum pat_match_res res;
4864 struct sockaddr_in addr;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004865 char addr_str[INET_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004866
4867 switch (appctx->st2) {
4868 case STAT_ST_INIT:
4869 appctx->ctx.map.desc = NULL;
4870 stats_map_lookup_next(si);
4871 appctx->st2 = STAT_ST_LIST;
4872 /* fall through */
4873
4874 case STAT_ST_LIST:
4875 /* for each lookup type */
4876 while (appctx->ctx.map.desc) {
4877 /* initialise chunk to build new message */
4878 chunk_reset(&trash);
4879
4880 /* execute pattern matching */
4881 sample.type = SMP_T_CSTR;
4882 sample.data.str.len = appctx->ctx.map.chunk.len;
4883 sample.data.str.str = appctx->ctx.map.chunk.str;
4884 pat = NULL;
4885 elt = NULL;
4886 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4887
4888 /* build return message: set type of match */
4889 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4890 chunk_appendf(&trash, "found, ");
4891 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4892 chunk_appendf(&trash, "bool, ");
4893 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4894 chunk_appendf(&trash, "int, ");
4895 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4896 chunk_appendf(&trash, "ip, ");
4897 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4898 chunk_appendf(&trash, "bin, ");
4899 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4900 chunk_appendf(&trash, "len, ");
4901 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4902 chunk_appendf(&trash, "str, ");
4903 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4904 chunk_appendf(&trash, "beg, ");
4905 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4906 chunk_appendf(&trash, "sub, ");
4907 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4908 chunk_appendf(&trash, "dir, ");
4909 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4910 chunk_appendf(&trash, "dom, ");
4911 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4912 chunk_appendf(&trash, "end, ");
4913 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4914 chunk_appendf(&trash, "reg, ");
4915 else /* The never appens case */
4916 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4917
4918 /* Display no match, and set default value */
4919 if (res == PAT_NOMATCH) {
4920 chunk_appendf(&trash, "no-match, ");
4921 smp = appctx->ctx.map.desc->def;
4922 }
4923
4924 /* Display match and match info */
4925 else {
4926 /* display match */
4927 chunk_appendf(&trash, "match, ");
4928
4929 /* display search mode */
4930 if (elt)
4931 chunk_appendf(&trash, "tree, ");
4932 else
4933 chunk_appendf(&trash, "list, ");
4934
4935 /* display search options */
4936 if (pat) {
4937 /* case sensitive */
4938 if (pat->flags & PAT_F_IGNORE_CASE)
4939 chunk_appendf(&trash, "case-insensitive, ");
4940 else
4941 chunk_appendf(&trash, "case-sensitive, ");
4942
4943 /* display source */
4944 if (pat->flags & PAT_F_FROM_FILE)
4945 chunk_appendf(&trash, "from-file, ");
4946 }
4947
4948 /* display match expresion */
4949 if (elt) {
4950 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4951 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4952 }
4953 /* only IPv4 */
4954 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4955 /* convert ip */
4956 memcpy(&addr.sin_addr, elt->node.key, 4);
4957 addr.sin_family = AF_INET;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004958 if (addr_to_str((struct sockaddr_storage *)&addr, addr_str, INET_ADDRSTRLEN))
4959 chunk_appendf(&trash, "match=\"%s/%d\", ", addr_str, elt->node.node.pfx);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004960 }
4961 }
4962 }
4963
4964 /* display return value */
4965 if (!smp) {
4966 chunk_appendf(&trash, "return=nothing\n");
4967 }
4968 else {
4969 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4970 sample.type = smp->type;
4971 if (sample_casts[sample.type][SMP_T_CSTR] &&
4972 sample_casts[sample.type][SMP_T_CSTR](&sample))
4973 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4974 sample.data.str.str, smp_to_type[smp->type]);
4975 else
4976 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4977 smp_to_type[smp->type]);
4978 }
4979
4980 /* display response */
4981 if (bi_putchk(si->ib, &trash) == -1) {
4982 /* let's try again later from this session. We add ourselves into
4983 * this session's users so that it can remove us upon termination.
4984 */
4985 return 0;
4986 }
4987
4988 /* get next entry */
4989 stats_map_lookup_next(si);
4990 }
4991
4992 appctx->st2 = STAT_ST_FIN;
4993 /* fall through */
4994
4995 default:
4996 appctx->st2 = STAT_ST_FIN;
4997 free(appctx->ctx.map.chunk.str);
4998 return 1;
4999 }
5000}
5001
5002static int stats_map_list(struct stream_interface *si)
5003{
5004 struct appctx *appctx = __objt_appctx(si->end);
5005
5006 switch (appctx->st2) {
5007
5008 case STAT_ST_INIT:
5009 /* Init to the first entry. The list cannot be change */
5010 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
5011 struct map_entry *, list);
5012 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
5013 appctx->ctx.map.ent = NULL;
5014 appctx->st2 = STAT_ST_LIST;
5015 /* fall through */
5016
5017 case STAT_ST_LIST:
5018 while (appctx->ctx.map.ent) {
5019 chunk_reset(&trash);
5020
5021 /* build messages */
5022 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
5023
5024 if (bi_putchk(si->ib, &trash) == -1) {
5025 /* let's try again later from this session. We add ourselves into
5026 * this session's users so that it can remove us upon termination.
5027 */
5028 return 0;
5029 }
5030
5031 /* get next list entry and check the end of the list */
5032 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
5033 struct map_entry *, list);
5034 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
5035 break;
5036 }
5037
5038 appctx->st2 = STAT_ST_FIN;
5039 /* fall through */
5040
5041 default:
5042 appctx->st2 = STAT_ST_FIN;
5043 return 1;
5044 }
5045}
5046
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005047/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005048 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005049 * to be called again, otherwise non-zero. It is designed to be called
5050 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005051 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005052static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005053{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005054 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005055 struct connection *conn;
5056
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005057 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005058 /* If we're forced to shut down, we might have to remove our
5059 * reference to the last session being dumped.
5060 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 if (appctx->st2 == STAT_ST_LIST) {
5062 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5063 LIST_DEL(&appctx->ctx.sess.bref.users);
5064 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005065 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005066 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005067 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005068 }
5069
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005070 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005071
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005072 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005073 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005074 /* the function had not been called yet, let's prepare the
5075 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005076 * pointer to the first in the global list. When a target
5077 * session is being destroyed, it is responsible for updating
5078 * this pointer. We know we have reached the end when this
5079 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005080 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005081 LIST_INIT(&appctx->ctx.sess.bref.users);
5082 appctx->ctx.sess.bref.ref = sessions.n;
5083 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005084 /* fall through */
5085
Willy Tarreau295a8372011-03-10 11:25:07 +01005086 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005087 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005088 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5089 LIST_DEL(&appctx->ctx.sess.bref.users);
5090 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005091 }
5092
5093 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005095 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005096 struct session *curr_sess;
5097
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005098 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005099
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005100 if (appctx->ctx.sess.target) {
5101 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005102 goto next_sess;
5103
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005104 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005105 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005106 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005107 return 0;
5108
5109 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005110 LIST_DEL(&appctx->ctx.sess.bref.users);
5111 LIST_INIT(&appctx->ctx.sess.bref.users);
5112 if (appctx->ctx.sess.target != (void *)-1) {
5113 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005114 break;
5115 }
5116 else
5117 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005118 }
5119
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005120 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005121 "%p: proto=%s",
5122 curr_sess,
5123 curr_sess->listener->proto->name);
5124
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005125
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005126 conn = objt_conn(curr_sess->si[0].end);
5127 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005128 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005129 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005130 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005131 " src=%s:%d fe=%s be=%s srv=%s",
5132 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005133 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005134 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005135 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005136 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005137 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005138 break;
5139 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005140 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005141 " src=unix:%d fe=%s be=%s srv=%s",
5142 curr_sess->listener->luid,
5143 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005144 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005145 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005146 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005147 break;
5148 }
5149
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005150 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005151 " ts=%02x age=%s calls=%d",
5152 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005153 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5154 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005155
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005156 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005157 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005158 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005159 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005160 curr_sess->req->analysers,
5161 curr_sess->req->rex ?
5162 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5163 TICKS_TO_MS(1000)) : "");
5164
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005165 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005166 ",wx=%s",
5167 curr_sess->req->wex ?
5168 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5169 TICKS_TO_MS(1000)) : "");
5170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005171 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005172 ",ax=%s]",
5173 curr_sess->req->analyse_exp ?
5174 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5175 TICKS_TO_MS(1000)) : "");
5176
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005177 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005178 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005179 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005180 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005181 curr_sess->rep->analysers,
5182 curr_sess->rep->rex ?
5183 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5184 TICKS_TO_MS(1000)) : "");
5185
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005186 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005187 ",wx=%s",
5188 curr_sess->rep->wex ?
5189 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5190 TICKS_TO_MS(1000)) : "");
5191
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005192 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005193 ",ax=%s]",
5194 curr_sess->rep->analyse_exp ?
5195 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5196 TICKS_TO_MS(1000)) : "");
5197
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005198 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005199 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005200 " s0=[%d,%1xh,fd=%d,ex=%s]",
5201 curr_sess->si[0].state,
5202 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005203 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005204 curr_sess->si[0].exp ?
5205 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5206 TICKS_TO_MS(1000)) : "");
5207
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005208 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005209 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005210 " s1=[%d,%1xh,fd=%d,ex=%s]",
5211 curr_sess->si[1].state,
5212 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005213 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005214 curr_sess->si[1].exp ?
5215 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5216 TICKS_TO_MS(1000)) : "");
5217
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005218 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005219 " exp=%s",
5220 curr_sess->task->expire ?
5221 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5222 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005223 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005224 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005225
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005226 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005227
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005228 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005229 /* let's try again later from this session. We add ourselves into
5230 * this session's users so that it can remove us upon termination.
5231 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005232 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005233 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005234 }
5235
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005236 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005237 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005238 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005239
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005240 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005241 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005242 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005243 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005244 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005245 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005246
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005247 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005248 return 0;
5249
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005250 appctx->ctx.sess.target = NULL;
5251 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005252 return 1;
5253 }
5254
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005255 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005256 /* fall through */
5257
5258 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005259 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005260 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005261 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005262}
5263
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005264/* This is called when the stream interface is closed. For instance, upon an
5265 * external abort, we won't call the i/o handler anymore so we may need to
5266 * remove back references to the session currently being dumped.
5267 */
Simon Horman74d88312013-02-12 10:45:50 +09005268static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005269{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005270 struct appctx *appctx = __objt_appctx(si->end);
5271
5272 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5273 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5274 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005275 }
5276}
5277
Willy Tarreau20e99322013-04-13 09:22:25 +02005278/* This function is used to either dump tables states (when action is set
5279 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005280 * It returns 0 if the output buffer is full and it needs to be called
5281 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005282 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005283static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005284{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005285 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005286 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005287 struct ebmb_node *eb;
5288 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005289 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005290 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005291
5292 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005293 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005294 * - STAT_ST_INIT : the first call
5295 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005296 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005297 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005298 * and the entry pointer points to the next entry to be dumped,
5299 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005300 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005301 * data though.
5302 */
5303
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005304 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005305 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005306 if (appctx->st2 == STAT_ST_LIST) {
5307 appctx->ctx.table.entry->ref_cnt--;
5308 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005309 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005310 return 1;
5311 }
5312
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005313 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005314
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005315 while (appctx->st2 != STAT_ST_FIN) {
5316 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005317 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 appctx->ctx.table.proxy = appctx->ctx.table.target;
5319 if (!appctx->ctx.table.proxy)
5320 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005322 appctx->ctx.table.entry = NULL;
5323 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005324 break;
5325
Willy Tarreau295a8372011-03-10 11:25:07 +01005326 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005327 if (!appctx->ctx.table.proxy ||
5328 (appctx->ctx.table.target &&
5329 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5330 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005331 break;
5332 }
5333
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005334 if (appctx->ctx.table.proxy->table.size) {
5335 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5336 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005337 return 0;
5338
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005339 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005340 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005341 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005342 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005343 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005344 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5345 appctx->ctx.table.entry->ref_cnt++;
5346 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005347 break;
5348 }
5349 }
5350 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005351 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005352 break;
5353
Willy Tarreau295a8372011-03-10 11:25:07 +01005354 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005355 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005356
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005357 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005358 /* we're filtering on some data contents */
5359 void *ptr;
5360 long long data;
5361
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005362 dt = appctx->ctx.table.data_type;
5363 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5364 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005365 dt);
5366
5367 data = 0;
5368 switch (stktable_data_types[dt].std_type) {
5369 case STD_T_SINT:
5370 data = stktable_data_cast(ptr, std_t_sint);
5371 break;
5372 case STD_T_UINT:
5373 data = stktable_data_cast(ptr, std_t_uint);
5374 break;
5375 case STD_T_ULL:
5376 data = stktable_data_cast(ptr, std_t_ull);
5377 break;
5378 case STD_T_FRQP:
5379 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005380 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005381 break;
5382 }
5383
5384 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005385 if ((data < appctx->ctx.table.value &&
5386 (appctx->ctx.table.data_op == STD_OP_EQ ||
5387 appctx->ctx.table.data_op == STD_OP_GT ||
5388 appctx->ctx.table.data_op == STD_OP_GE)) ||
5389 (data == appctx->ctx.table.value &&
5390 (appctx->ctx.table.data_op == STD_OP_NE ||
5391 appctx->ctx.table.data_op == STD_OP_GT ||
5392 appctx->ctx.table.data_op == STD_OP_LT)) ||
5393 (data > appctx->ctx.table.value &&
5394 (appctx->ctx.table.data_op == STD_OP_EQ ||
5395 appctx->ctx.table.data_op == STD_OP_LT ||
5396 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005397 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005398 }
5399
Simon Hormanc88b8872011-06-15 15:18:49 +09005400 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005401 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5402 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005403 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005404
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005405 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005406
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005407 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005408 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005409 struct stksess *old = appctx->ctx.table.entry;
5410 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005411 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005412 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5413 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5414 stksess_kill(&appctx->ctx.table.proxy->table, old);
5415 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005416 break;
5417 }
5418
Simon Hormanc88b8872011-06-15 15:18:49 +09005419
5420 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005421 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5422 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5423 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005424
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005425 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5426 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005427 break;
5428
Willy Tarreau295a8372011-03-10 11:25:07 +01005429 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005430 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005431 break;
5432 }
5433 }
5434 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005435}
5436
Willy Tarreaud426a182010-03-05 14:58:26 +01005437/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005438 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5439 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5440 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5441 * lines are respected within the limit of 70 output chars. Lines that are
5442 * continuation of a previous truncated line begin with "+" instead of " "
5443 * after the offset. The new pointer is returned.
5444 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005445static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5446 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005447{
5448 int end;
5449 unsigned char c;
5450
5451 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005452 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005453 return ptr;
5454
Willy Tarreau77804732012-10-29 16:14:26 +01005455 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005456
Willy Tarreaud426a182010-03-05 14:58:26 +01005457 while (ptr < len && ptr < bsize) {
5458 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005459 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005460 if (out->len > end - 2)
5461 break;
5462 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005463 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005464 if (out->len > end - 3)
5465 break;
5466 out->str[out->len++] = '\\';
5467 switch (c) {
5468 case '\t': c = 't'; break;
5469 case '\n': c = 'n'; break;
5470 case '\r': c = 'r'; break;
5471 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005472 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005473 }
5474 out->str[out->len++] = c;
5475 } else {
5476 if (out->len > end - 5)
5477 break;
5478 out->str[out->len++] = '\\';
5479 out->str[out->len++] = 'x';
5480 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5481 out->str[out->len++] = hextab[c & 0xF];
5482 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005483 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005484 /* we had a line break, let's return now */
5485 out->str[out->len++] = '\n';
5486 *line = ptr;
5487 return ptr;
5488 }
5489 }
5490 /* we have an incomplete line, we return it as-is */
5491 out->str[out->len++] = '\n';
5492 return ptr;
5493}
5494
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005495/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005496 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005497 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005498 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005499static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005500{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005501 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005502 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005503
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005504 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005505 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005506
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005507 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005508
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005509 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005510 /* the function had not been called yet, let's prepare the
5511 * buffer for a response.
5512 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005513 struct tm tm;
5514
5515 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005516 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005517 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5518 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5519 error_snapshot_id);
5520
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005521 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005522 /* Socket buffer full. Let's try again later from the same point */
5523 return 0;
5524 }
5525
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005526 appctx->ctx.errors.px = proxy;
5527 appctx->ctx.errors.buf = 0;
5528 appctx->ctx.errors.bol = 0;
5529 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005530 }
5531
5532 /* we have two inner loops here, one for the proxy, the other one for
5533 * the buffer.
5534 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005535 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005536 struct error_snapshot *es;
5537
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005538 if (appctx->ctx.errors.buf == 0)
5539 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005540 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005541 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005542
5543 if (!es->when.tv_sec)
5544 goto next;
5545
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005546 if (appctx->ctx.errors.iid >= 0 &&
5547 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5548 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005549 goto next;
5550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005551 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005552 /* just print headers now */
5553
5554 char pn[INET6_ADDRSTRLEN];
5555 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005556 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005557
5558 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005559 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005560 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005561 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005562
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005563 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5564 case AF_INET:
5565 case AF_INET6:
5566 port = get_host_port(&es->src);
5567 break;
5568 default:
5569 port = 0;
5570 }
5571
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005572 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005573 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005574 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005575 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005576 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005577 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005578 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5579 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005580 break;
5581 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005582 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005583 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005584 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005585 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005586 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005587 break;
5588 }
5589
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005590 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005591 ", server %s (#%d), event #%u\n"
5592 " src %s:%d, session #%d, session flags 0x%08x\n"
5593 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5594 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5595 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5596 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5597 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5598 es->ev_id,
5599 pn, port, es->sid, es->s_flags,
5600 es->state, es->m_flags, es->t_flags,
5601 es->m_clen, es->m_blen,
5602 es->b_flags, es->b_out, es->b_tot,
5603 es->len, es->b_wrap, es->pos);
5604
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005605 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005606 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005607 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005608 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005609 appctx->ctx.errors.ptr = 0;
5610 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005611 }
5612
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005613 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005614 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005615 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005616 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005617 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005618 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005619 goto next;
5620 }
5621
5622 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005623 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005624 int newptr;
5625 int newline;
5626
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005627 newline = appctx->ctx.errors.bol;
5628 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5629 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005630 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005632 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005633 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005634 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005635 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005636 appctx->ctx.errors.ptr = newptr;
5637 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005638 };
5639 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005640 appctx->ctx.errors.bol = 0;
5641 appctx->ctx.errors.ptr = -1;
5642 appctx->ctx.errors.buf++;
5643 if (appctx->ctx.errors.buf > 1) {
5644 appctx->ctx.errors.buf = 0;
5645 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005646 }
5647 }
5648
5649 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005650 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005651}
5652
Willy Tarreaud5781202012-09-22 19:32:35 +02005653/* parse the "level" argument on the bind lines */
5654static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5655{
5656 if (!*args[cur_arg + 1]) {
5657 memprintf(err, "'%s' : missing level", args[cur_arg]);
5658 return ERR_ALERT | ERR_FATAL;
5659 }
5660
5661 if (!strcmp(args[cur_arg+1], "user"))
5662 conf->level = ACCESS_LVL_USER;
5663 else if (!strcmp(args[cur_arg+1], "operator"))
5664 conf->level = ACCESS_LVL_OPER;
5665 else if (!strcmp(args[cur_arg+1], "admin"))
5666 conf->level = ACCESS_LVL_ADMIN;
5667 else {
5668 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5669 args[cur_arg], args[cur_arg+1]);
5670 return ERR_ALERT | ERR_FATAL;
5671 }
5672
5673 return 0;
5674}
5675
Willy Tarreaub24281b2011-02-13 13:16:36 +01005676struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005677 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005678 .name = "<STATS>", /* used for logging */
5679 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005680 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005681};
5682
Simon Horman9bd2c732011-06-15 15:18:44 +09005683static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005684 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005685 .name = "<CLI>", /* used for logging */
5686 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005687 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005688};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005689
Willy Tarreaudc13c112013-06-21 23:16:39 +02005690static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005691 { CFG_GLOBAL, "stats", stats_parse_global },
5692 { 0, NULL, NULL },
5693}};
5694
Willy Tarreaud5781202012-09-22 19:32:35 +02005695static struct bind_kw_list bind_kws = { "STAT", { }, {
5696 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5697 { NULL, NULL, 0 },
5698}};
5699
Willy Tarreau10522fd2008-07-09 20:12:41 +02005700__attribute__((constructor))
5701static void __dumpstats_module_init(void)
5702{
5703 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005704 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005705}
5706
Willy Tarreau91861262007-10-17 17:06:05 +02005707/*
5708 * Local variables:
5709 * c-indent-level: 8
5710 * c-basic-offset: 8
5711 * End:
5712 */