blob: 0df28829ed0b96330e3da4e757d35d22b74632bc [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>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010051#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010064static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010065static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090066static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010068static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010069static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Simon Horman9bd2c732011-06-15 15:18:44 +090071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010072/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010073 * cli_io_handler()
74 * -> stats_dump_sess_to_buffer() // "show sess"
75 * -> stats_dump_errors_to_buffer() // "show errors"
76 * -> stats_dump_info_to_buffer() // "show info"
77 * -> stats_dump_stat_to_buffer() // "show stat"
78 * -> stats_dump_csv_header()
79 * -> stats_dump_proxy_to_buffer()
80 * -> stats_dump_fe_stats()
81 * -> stats_dump_li_stats()
82 * -> stats_dump_sv_stats()
83 * -> stats_dump_be_stats()
84 *
85 * http_stats_io_handler()
86 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
87 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
88 * -> stats_dump_html_head() // emits the HTML headers
89 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
90 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
91 * -> stats_dump_html_px_hdr()
92 * -> stats_dump_fe_stats()
93 * -> stats_dump_li_stats()
94 * -> stats_dump_sv_stats()
95 * -> stats_dump_be_stats()
96 * -> stats_dump_html_px_end()
97 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010098 */
99
Simon Horman9bd2c732011-06-15 15:18:44 +0900100static struct si_applet cli_applet;
101
102static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200103 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200104 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200105 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200106 " help : this message\n"
107 " prompt : toggle interactive mode with prompt\n"
108 " quit : disconnect\n"
109 " show info : report information about the running process\n"
110 " show stat : report counters for each proxy and server\n"
111 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100112 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200113 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200114 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200115 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200116 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100117 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200118 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200119 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200120 " disable : put a server or frontend in maintenance mode\n"
121 " enable : re-enable a server or frontend which is in maintenance mode\n"
122 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200123 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200126 "Permission denied\n"
127 "";
128
Willy Tarreau295a8372011-03-10 11:25:07 +0100129/* data transmission states for the stats responses */
130enum {
131 STAT_ST_INIT = 0,
132 STAT_ST_HEAD,
133 STAT_ST_INFO,
134 STAT_ST_LIST,
135 STAT_ST_END,
136 STAT_ST_FIN,
137};
138
139/* data transmission states for the stats responses inside a proxy */
140enum {
141 STAT_PX_ST_INIT = 0,
142 STAT_PX_ST_TH,
143 STAT_PX_ST_FE,
144 STAT_PX_ST_LI,
145 STAT_PX_ST_SV,
146 STAT_PX_ST_BE,
147 STAT_PX_ST_END,
148 STAT_PX_ST_FIN,
149};
150
Cyril Bonté19979e12012-04-04 12:57:21 +0200151extern const char *stat_status_codes[];
152
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200153/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200155 * needs to be closed and ignored, or a negative value upon critical failure.
156 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900157static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200158{
Willy Tarreauce9dbcd2013-10-01 17:12:05 +0200159 /* we have a dedicated I/O handler for the CLI/stats, so we can safely
160 * release the pre-allocated connection that we will never use.
161 */
162 pool_free2(pool2_connection, s->si[1].conn);
163 s->si[1].conn = NULL;
164
Willy Tarreaub24281b2011-02-13 13:16:36 +0100165 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreaucf644ed2013-09-29 17:19:56 +0200166 s->target = &cli_applet.obj_type; // for logging only
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100167 s->si[1].appctx.st1 = 0;
168 s->si[1].appctx.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200169
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200170 tv_zero(&s->logs.tv_request);
171 s->logs.t_queue = 0;
172 s->logs.t_connect = 0;
173 s->logs.t_data = 0;
174 s->logs.t_close = 0;
175 s->logs.bytes_in = s->logs.bytes_out = 0;
176 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
177 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200178
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200179 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200180
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200181 if (s->listener->timeout) {
182 s->req->rto = *s->listener->timeout;
183 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200184 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200185 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200186}
187
Willy Tarreau07e9e642010-08-17 21:48:17 +0200188/* allocate a new stats frontend named <name>, and return it
189 * (or NULL in case of lack of memory).
190 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200191static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200192{
193 struct proxy *fe;
194
195 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
196 if (!fe)
197 return NULL;
198
Willy Tarreau237250c2011-07-29 01:49:03 +0200199 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200200 fe->next = proxy;
201 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200202 fe->last_change = now.tv_sec;
203 fe->id = strdup("GLOBAL");
204 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200205 fe->maxconn = 10; /* default to 10 concurrent connections */
206 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200207 fe->conf.file = strdup(file);
208 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200209 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200210
211 /* the stats frontend is the only one able to assign ID #0 */
212 fe->conf.id.key = fe->uuid = 0;
213 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200214 return fe;
215}
216
Willy Tarreaufbee7132007-10-18 13:53:22 +0200217/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200218 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
219 * error message into the <err> buffer which will be preallocated. The trailing
220 * '\n' must not be written. The function must be called with <args> pointing to
221 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200222 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200223static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200224 struct proxy *defpx, const char *file, int line,
225 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200227 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200228 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200229
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200230 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200231 int cur_arg;
232
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200233 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200234 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 +0200235 return -1;
236 }
237
Willy Tarreau89a63132009-08-16 17:41:45 +0200238 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200239 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200240 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200241 return -1;
242 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200243 }
244
Willy Tarreau4348fad2012-09-20 16:48:07 +0200245 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200246 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200247
Willy Tarreauc53d4222012-09-20 20:19:28 +0200248 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
249 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
250 file, line, args[0], args[1], err && *err ? *err : "error");
251 return -1;
252 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200254 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200255 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200256 static int bind_dumped;
257 struct bind_kw *kw;
258
259 kw = bind_find_kw(args[cur_arg]);
260 if (kw) {
261 if (!kw->parse) {
262 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
263 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200264 return -1;
265 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200266
267 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
268 if (err && *err)
269 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
270 else
271 memprintf(err, "'%s %s' : error encountered while processing '%s'",
272 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200273 return -1;
274 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200275
276 cur_arg += 1 + kw->skip;
277 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200278 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200279
280 if (!bind_dumped) {
281 bind_dump_kws(err);
282 indent_msg(err, 4);
283 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200285
286 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
287 args[0], args[1], args[cur_arg],
288 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
289 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100291
Willy Tarreauc53d4222012-09-20 20:19:28 +0200292 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
293 l->maxconn = global.stats_fe->maxconn;
294 l->backlog = global.stats_fe->backlog;
295 l->timeout = &global.stats_fe->timeout.client;
296 l->accept = session_accept;
297 l->handler = process_session;
298 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
299 l->nice = -64; /* we want to boost priority for local stats */
300 global.maxsock += l->maxconn;
301 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200305 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100306
307 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200308 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100309 return -1;
310 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100312 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200313 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200314 return -1;
315 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200316 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200317 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200318 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200319 return -1;
320 }
321 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200322 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200323 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200324 else if (!strcmp(args[1], "maxconn")) {
325 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200326
327 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200328 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200329 return -1;
330 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200331
332 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200333 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200334 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200335 return -1;
336 }
337 }
338 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200339 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200340 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
341 int cur_arg = 2;
342 unsigned int set = 0;
343
Willy Tarreau91319572013-04-20 09:48:50 +0200344 if (!global.stats_fe) {
345 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
346 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
347 return -1;
348 }
349 }
350
Willy Tarreau35b7b162012-10-22 23:17:18 +0200351 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100352 unsigned int low, high;
353
Willy Tarreau35b7b162012-10-22 23:17:18 +0200354 if (strcmp(args[cur_arg], "all") == 0) {
355 set = 0;
356 break;
357 }
358 else if (strcmp(args[cur_arg], "odd") == 0) {
359 set |= 0x55555555;
360 }
361 else if (strcmp(args[cur_arg], "even") == 0) {
362 set |= 0xAAAAAAAA;
363 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100364 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100365 char *dash = strchr(args[cur_arg], '-');
366
367 low = high = str2uic(args[cur_arg]);
368 if (dash)
369 high = str2uic(dash + 1);
370
371 if (high < low) {
372 unsigned int swap = low;
373 low = high;
374 high = swap;
375 }
376
377 if (low < 1 || high > 32) {
378 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
379 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200380 return -1;
381 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100382
383 while (low <= high)
384 set |= 1 << (low++ - 1);
385 }
386 else {
387 memprintf(err,
388 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
389 args[0], args[1]);
390 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200391 }
392 cur_arg++;
393 }
394 global.stats_fe->bind_proc = set;
395 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200396 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200397 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200398 return -1;
399 }
400 return 0;
401}
402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100403/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
404 * for clearing it if needed.
405 */
406static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100407{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100408 chunk_appendf(&trash,
409 "# pxname,svname,"
410 "qcur,qmax,"
411 "scur,smax,slim,stot,"
412 "bin,bout,"
413 "dreq,dresp,"
414 "ereq,econ,eresp,"
415 "wretr,wredis,"
416 "status,weight,act,bck,"
417 "chkfail,chkdown,lastchg,downtime,qlimit,"
418 "pid,iid,sid,throttle,lbtot,tracked,type,"
419 "rate,rate_lim,rate_max,"
420 "check_status,check_code,check_duration,"
421 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
422 "req_rate,req_rate_max,req_tot,"
423 "cli_abrt,srv_abrt,"
424 "comp_in,comp_out,comp_byp,comp_rsp,"
425 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100426}
427
Simon Hormand9366582011-06-15 15:18:45 +0900428/* print a string of text buffer to <out>. The format is :
429 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
430 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
431 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
432 */
433static int dump_text(struct chunk *out, const char *buf, int bsize)
434{
435 unsigned char c;
436 int ptr = 0;
437
438 while (buf[ptr] && ptr < bsize) {
439 c = buf[ptr];
440 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
441 if (out->len > out->size - 1)
442 break;
443 out->str[out->len++] = c;
444 }
445 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
446 if (out->len > out->size - 2)
447 break;
448 out->str[out->len++] = '\\';
449 switch (c) {
450 case ' ': c = ' '; break;
451 case '\t': c = 't'; break;
452 case '\n': c = 'n'; break;
453 case '\r': c = 'r'; break;
454 case '\e': c = 'e'; break;
455 case '\\': c = '\\'; break;
456 }
457 out->str[out->len++] = c;
458 }
459 else {
460 if (out->len > out->size - 4)
461 break;
462 out->str[out->len++] = '\\';
463 out->str[out->len++] = 'x';
464 out->str[out->len++] = hextab[(c >> 4) & 0xF];
465 out->str[out->len++] = hextab[c & 0xF];
466 }
467 ptr++;
468 }
469
470 return ptr;
471}
472
473/* print a buffer in hexa.
474 * Print stopped if <bsize> is reached, or if no more place in the chunk.
475 */
476static int dump_binary(struct chunk *out, const char *buf, int bsize)
477{
478 unsigned char c;
479 int ptr = 0;
480
481 while (ptr < bsize) {
482 c = buf[ptr];
483
484 if (out->len > out->size - 2)
485 break;
486 out->str[out->len++] = hextab[(c >> 4) & 0xF];
487 out->str[out->len++] = hextab[c & 0xF];
488
489 ptr++;
490 }
491 return ptr;
492}
493
494/* Dump the status of a table to a stream interface's
495 * read buffer. It returns 0 if the output buffer is full
496 * and needs to be called again, otherwise non-zero.
497 */
498static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
499 struct proxy *proxy, struct proxy *target)
500{
Willy Tarreau306f8302013-07-08 15:53:06 +0200501 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900502
Willy Tarreau77804732012-10-29 16:14:26 +0100503 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900504 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
505
506 /* any other information should be dumped here */
507
Willy Tarreau290e63a2012-09-20 18:07:14 +0200508 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100509 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900510
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200511 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900512 return 0;
513
514 return 1;
515}
516
517/* Dump the a table entry to a stream interface's
518 * read buffer. It returns 0 if the output buffer is full
519 * and needs to be called again, otherwise non-zero.
520 */
521static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
522 struct proxy *proxy, struct stksess *entry)
523{
524 int dt;
525
Willy Tarreau77804732012-10-29 16:14:26 +0100526 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900527
528 if (proxy->table.type == STKTABLE_TYPE_IP) {
529 char addr[INET_ADDRSTRLEN];
530 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900532 }
533 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
534 char addr[INET6_ADDRSTRLEN];
535 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100536 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900537 }
538 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100539 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900540 }
541 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100542 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900543 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
544 }
545 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100546 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900547 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
548 }
549
Willy Tarreau77804732012-10-29 16:14:26 +0100550 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900551
552 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
553 void *ptr;
554
555 if (proxy->table.data_ofs[dt] == 0)
556 continue;
557 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900559 else
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900561
562 ptr = stktable_data_ptr(&proxy->table, entry, dt);
563 switch (stktable_data_types[dt].std_type) {
564 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100565 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900566 break;
567 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100568 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900569 break;
570 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100571 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900572 break;
573 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100574 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900575 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
576 proxy->table.data_arg[dt].u));
577 break;
578 }
579 }
Willy Tarreau77804732012-10-29 16:14:26 +0100580 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900581
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200582 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900583 return 0;
584
585 return 1;
586}
587
Willy Tarreaudec98142012-06-06 23:37:08 +0200588static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900589{
Willy Tarreau306f8302013-07-08 15:53:06 +0200590 struct session *s = session_from_task(si->owner);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100591 struct proxy *px = si->appctx.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900592 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900593 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900594 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200595 long long value;
596 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200597 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200598 void *ptr;
599 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900600
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100601 si->appctx.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900602
603 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100604 si->appctx.ctx.cli.msg = "Key value expected\n";
605 si->appctx.st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900606 return;
607 }
608
Simon Hormanc5b89f62011-06-15 15:18:50 +0900609 switch (px->table.type) {
610 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900611 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100612 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900613 break;
614 case STKTABLE_TYPE_IPV6:
615 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100616 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900617 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900618 case STKTABLE_TYPE_INTEGER:
619 {
620 char *endptr;
621 unsigned long val;
622 errno = 0;
623 val = strtoul(args[4], &endptr, 10);
624 if ((errno == ERANGE && val == ULONG_MAX) ||
625 (errno != 0 && val == 0) || endptr == args[4] ||
626 val > 0xffffffff) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100627 si->appctx.ctx.cli.msg = "Invalid key\n";
628 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900629 return;
630 }
631 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100632 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900633 break;
634 }
635 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900636 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100637 static_table_key->key = args[4];
638 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900639 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900640 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200641 switch (action) {
642 case STAT_CLI_O_TAB:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100643 si->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 +0200644 break;
645 case STAT_CLI_O_CLR:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100646 si->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 +0200647 break;
648 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100649 si->appctx.ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200650 break;
651 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100652 si->appctx.st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900653 return;
654 }
655
656 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200657 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100658 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
659 si->appctx.st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900660 return;
661 }
662
Willy Tarreau07115412012-10-29 21:56:59 +0100663 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900664
Willy Tarreaudec98142012-06-06 23:37:08 +0200665 switch (action) {
666 case STAT_CLI_O_TAB:
667 if (!ts)
668 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100669 chunk_reset(&trash);
670 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900671 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100672 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900673 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200674
675 case STAT_CLI_O_CLR:
676 if (!ts)
677 return;
678 if (ts->ref_cnt) {
679 /* don't delete an entry which is currently referenced */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100680 si->appctx.ctx.cli.msg = "Entry currently in use, cannot remove\n";
681 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200682 return;
683 }
684 stksess_kill(&px->table, ts);
685 break;
Simon Horman17bce342011-06-15 15:18:47 +0900686
Willy Tarreau654694e2012-06-07 01:03:16 +0200687 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200688 if (ts)
689 stktable_touch(&px->table, ts, 1);
690 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100691 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200692 if (!ts) {
693 /* don't delete an entry which is currently referenced */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100694 si->appctx.ctx.cli.msg = "Unable to allocate a new entry\n";
695 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200696 return;
697 }
698 stktable_store(&px->table, ts, 1);
699 }
700
Willy Tarreau47060b62013-08-01 21:11:42 +0200701 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
702 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100703 si->appctx.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
704 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200705 return;
706 }
707
708 data_type = stktable_get_data_type(args[cur_arg] + 5);
709 if (data_type < 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100710 si->appctx.ctx.cli.msg = "Unknown data type\n";
711 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200712 return;
713 }
714
715 if (!px->table.data_ofs[data_type]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100716 si->appctx.ctx.cli.msg = "Data type not stored in this table\n";
717 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200718 return;
719 }
720
721 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100722 si->appctx.ctx.cli.msg = "Require a valid integer value to store\n";
723 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200724 return;
725 }
726
727 ptr = stktable_data_ptr(&px->table, ts, data_type);
728
729 switch (stktable_data_types[data_type].std_type) {
730 case STD_T_SINT:
731 stktable_data_cast(ptr, std_t_sint) = value;
732 break;
733 case STD_T_UINT:
734 stktable_data_cast(ptr, std_t_uint) = value;
735 break;
736 case STD_T_ULL:
737 stktable_data_cast(ptr, std_t_ull) = value;
738 break;
739 case STD_T_FRQP:
740 /* We set both the current and previous values. That way
741 * the reported frequency is stable during all the period
742 * then slowly fades out. This allows external tools to
743 * push measures without having to update them too often.
744 */
745 frqp = &stktable_data_cast(ptr, std_t_frqp);
746 frqp->curr_tick = now_ms;
747 frqp->prev_ctr = 0;
748 frqp->curr_ctr = value;
749 break;
750 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200751 }
752 break;
753
Willy Tarreaudec98142012-06-06 23:37:08 +0200754 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100755 si->appctx.ctx.cli.msg = "Unknown action\n";
756 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200757 break;
Simon Horman121f3052011-06-15 15:18:46 +0900758 }
Simon Horman121f3052011-06-15 15:18:46 +0900759}
760
Willy Tarreau654694e2012-06-07 01:03:16 +0200761static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900762{
Willy Tarreau04b3a192013-04-13 09:41:37 +0200763 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100764 si->appctx.ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
765 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200766 return;
767 }
768
Simon Hormand5b9fd92011-06-15 15:18:48 +0900769 /* condition on stored data value */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100770 si->appctx.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
771 if (si->appctx.ctx.table.data_type < 0) {
772 si->appctx.ctx.cli.msg = "Unknown data type\n";
773 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900774 return;
775 }
776
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100777 if (!((struct proxy *)si->appctx.ctx.table.target)->table.data_ofs[si->appctx.ctx.table.data_type]) {
778 si->appctx.ctx.cli.msg = "Data type not stored in this table\n";
779 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900780 return;
781 }
782
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100783 si->appctx.ctx.table.data_op = get_std_op(args[4]);
784 if (si->appctx.ctx.table.data_op < 0) {
785 si->appctx.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
786 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900787 return;
788 }
789
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100790 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->appctx.ctx.table.value) != 0) {
791 si->appctx.ctx.cli.msg = "Require a valid integer value to compare against\n";
792 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900793 return;
794 }
795}
796
Willy Tarreaudec98142012-06-06 23:37:08 +0200797static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900798{
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100799 si->appctx.ctx.table.data_type = -1;
800 si->appctx.st2 = STAT_ST_INIT;
801 si->appctx.ctx.table.target = NULL;
802 si->appctx.ctx.table.proxy = NULL;
803 si->appctx.ctx.table.entry = NULL;
804 si->appctx.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900805
806 if (*args[2]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100807 si->appctx.ctx.table.target = find_stktable(args[2]);
808 if (!si->appctx.ctx.table.target) {
809 si->appctx.ctx.cli.msg = "No such table\n";
810 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811 return;
812 }
813 }
814 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200815 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900816 goto err_args;
817 return;
818 }
819
820 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200821 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900822 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200823 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900824 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900825 goto err_args;
826
827 return;
828
829err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200830 switch (action) {
831 case STAT_CLI_O_TAB:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100832 si->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 +0200833 break;
834 case STAT_CLI_O_CLR:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100835 si->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 +0200836 break;
837 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100838 si->appctx.ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200839 break;
840 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100841 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900842}
843
Willy Tarreau532a4502011-09-07 22:37:44 +0200844/* Expects to find a frontend named <arg> and returns it, otherwise displays various
845 * adequate error messages and returns NULL. This function also expects the session
846 * level to be admin.
847 */
848static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
849{
850 struct proxy *px;
851
Willy Tarreau290e63a2012-09-20 18:07:14 +0200852 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100853 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
854 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200855 return NULL;
856 }
857
858 if (!*arg) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100859 si->appctx.ctx.cli.msg = "A frontend name is expected.\n";
860 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200861 return NULL;
862 }
863
864 px = findproxy(arg, PR_CAP_FE);
865 if (!px) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100866 si->appctx.ctx.cli.msg = "No such frontend.\n";
867 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200868 return NULL;
869 }
870 return px;
871}
872
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200873/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
874 * and returns the pointer to the server. Otherwise, display adequate error messages
875 * and returns NULL. This function also expects the session level to be admin. Note:
876 * the <arg> is modified to remove the '/'.
877 */
878static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
879{
880 struct proxy *px;
881 struct server *sv;
882 char *line;
883
Willy Tarreau290e63a2012-09-20 18:07:14 +0200884 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100885 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
886 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200887 return NULL;
888 }
889
890 /* split "backend/server" and make <line> point to server */
891 for (line = arg; *line; line++)
892 if (*line == '/') {
893 *line++ = '\0';
894 break;
895 }
896
897 if (!*line || !*arg) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100898 si->appctx.ctx.cli.msg = "Require 'backend/server'.\n";
899 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200900 return NULL;
901 }
902
903 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100904 si->appctx.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
905 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200906 return NULL;
907 }
908
909 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100910 si->appctx.ctx.cli.msg = "Proxy is disabled.\n";
911 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200912 return NULL;
913 }
914
915 return sv;
916}
917
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200918/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200919 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100920 * if the request was understood, otherwise zero. It sets si->appctx.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200921 * designating the function which will have to process the request, which can
922 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900924static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200925{
Willy Tarreau306f8302013-07-08 15:53:06 +0200926 struct session *s = session_from_task(si->owner);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200927 char *args[MAX_STATS_ARGS + 1];
928 int arg;
929
930 while (isspace((unsigned char)*line))
931 line++;
932
933 arg = 0;
934 args[arg] = line;
935
936 while (*line && arg < MAX_STATS_ARGS) {
937 if (isspace((unsigned char)*line)) {
938 *line++ = '\0';
939
940 while (isspace((unsigned char)*line))
941 line++;
942
943 args[++arg] = line;
944 continue;
945 }
946
947 line++;
948 }
949
950 while (++arg <= MAX_STATS_ARGS)
951 args[arg] = line;
952
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100953 si->appctx.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200954 if (strcmp(args[0], "show") == 0) {
955 if (strcmp(args[1], "stat") == 0) {
956 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100957 si->appctx.ctx.stats.flags |= STAT_BOUND;
958 si->appctx.ctx.stats.iid = atoi(args[2]);
959 si->appctx.ctx.stats.type = atoi(args[3]);
960 si->appctx.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200961 }
962
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100963 si->appctx.st2 = STAT_ST_INIT;
964 si->appctx.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200965 }
966 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100967 si->appctx.st2 = STAT_ST_INIT;
968 si->appctx.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200969 }
970 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100971 si->appctx.st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200972 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100973 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
974 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200975 return 1;
976 }
Willy Tarreau76153662012-11-26 01:16:39 +0100977 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100978 si->appctx.ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +0100979 else if (*args[2])
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100980 si->appctx.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100981 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100982 si->appctx.ctx.sess.target = NULL;
983 si->appctx.ctx.sess.section = 0; /* start with session status */
984 si->appctx.ctx.sess.pos = 0;
985 si->appctx.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200986 }
987 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200988 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100989 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
990 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200991 return 1;
992 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200993 if (*args[2])
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100994 si->appctx.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200995 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100996 si->appctx.ctx.errors.iid = -1;
997 si->appctx.ctx.errors.px = NULL;
998 si->appctx.st2 = STAT_ST_INIT;
999 si->appctx.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001001 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001002 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001003 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001004 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001005 return 0;
1006 }
1007 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001008 else if (strcmp(args[0], "clear") == 0) {
1009 if (strcmp(args[1], "counters") == 0) {
1010 struct proxy *px;
1011 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001012 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001013 int clrall = 0;
1014
1015 if (strcmp(args[2], "all") == 0)
1016 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001017
Willy Tarreau6162db22009-10-10 17:13:00 +02001018 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001019 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1020 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001021 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1022 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001023 return 1;
1024 }
1025
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001026 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001027 if (clrall) {
1028 memset(&px->be_counters, 0, sizeof(px->be_counters));
1029 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1030 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001031 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001032 px->be_counters.conn_max = 0;
1033 px->be_counters.p.http.rps_max = 0;
1034 px->be_counters.sps_max = 0;
1035 px->be_counters.cps_max = 0;
1036 px->be_counters.nbpend_max = 0;
1037
1038 px->fe_counters.conn_max = 0;
1039 px->fe_counters.p.http.rps_max = 0;
1040 px->fe_counters.sps_max = 0;
1041 px->fe_counters.cps_max = 0;
1042 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001043 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001044
1045 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001046 if (clrall)
1047 memset(&sv->counters, 0, sizeof(sv->counters));
1048 else {
1049 sv->counters.cur_sess_max = 0;
1050 sv->counters.nbpend_max = 0;
1051 sv->counters.sps_max = 0;
1052 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001053
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001054 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001055 if (li->counters) {
1056 if (clrall)
1057 memset(li->counters, 0, sizeof(*li->counters));
1058 else
1059 li->counters->conn_max = 0;
1060 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001061 }
1062
Willy Tarreau81c25d02011-09-07 15:17:21 +02001063 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001064 return 1;
1065 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001066 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001067 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001068 /* end of processing */
1069 return 1;
1070 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001071 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001072 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001073 return 0;
1074 }
1075 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001076 else if (strcmp(args[0], "get") == 0) {
1077 if (strcmp(args[1], "weight") == 0) {
1078 struct proxy *px;
1079 struct server *sv;
1080
1081 /* split "backend/server" and make <line> point to server */
1082 for (line = args[2]; *line; line++)
1083 if (*line == '/') {
1084 *line++ = '\0';
1085 break;
1086 }
1087
1088 if (!*line) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001089 si->appctx.ctx.cli.msg = "Require 'backend/server'.\n";
1090 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001091 return 1;
1092 }
1093
1094 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001095 si->appctx.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1096 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001097 return 1;
1098 }
1099
1100 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1102 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001103 return 1;
1104 }
1105 else { /* not "get weight" */
1106 return 0;
1107 }
1108 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001109 else if (strcmp(args[0], "set") == 0) {
1110 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001111 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001112 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001113
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001114 sv = expect_server_admin(s, si, args[2]);
1115 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001116 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001117
Simon Horman7d09b9a2013-02-12 10:45:51 +09001118 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001119 /*
1120 * The user-weight may now be zero and thus
1121 * the server considered to be draining.
1122 * Update the server's drain state as necessary.
1123 */
1124 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001125 if (warning) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001126 si->appctx.ctx.cli.msg = warning;
1127 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001128 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001129 return 1;
1130 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001131 else if (strcmp(args[1], "timeout") == 0) {
1132 if (strcmp(args[2], "cli") == 0) {
1133 unsigned timeout;
1134 const char *res;
1135
1136 if (!*args[3]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001137 si->appctx.ctx.cli.msg = "Expects an integer value.\n";
1138 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001139 return 1;
1140 }
1141
1142 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1143 if (res || timeout < 1) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001144 si->appctx.ctx.cli.msg = "Invalid timeout value.\n";
1145 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001146 return 1;
1147 }
1148
1149 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1150 return 1;
1151 }
1152 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001153 si->appctx.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1154 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001155 return 1;
1156 }
1157 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001158 else if (strcmp(args[1], "maxconn") == 0) {
1159 if (strcmp(args[2], "frontend") == 0) {
1160 struct proxy *px;
1161 struct listener *l;
1162 int v;
1163
Willy Tarreau532a4502011-09-07 22:37:44 +02001164 px = expect_frontend_admin(s, si, args[3]);
1165 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001166 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001167
1168 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001169 si->appctx.ctx.cli.msg = "Integer value expected.\n";
1170 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001171 return 1;
1172 }
1173
1174 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001175 if (v < 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001176 si->appctx.ctx.cli.msg = "Value out of range.\n";
1177 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001178 return 1;
1179 }
1180
1181 /* OK, the value is fine, so we assign it to the proxy and to all of
1182 * its listeners. The blocked ones will be dequeued.
1183 */
1184 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001185 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001186 l->maxconn = v;
1187 if (l->state == LI_FULL)
1188 resume_listener(l);
1189 }
1190
1191 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1192 dequeue_all_listeners(&s->fe->listener_queue);
1193
1194 return 1;
1195 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001196 else if (strcmp(args[2], "global") == 0) {
1197 int v;
1198
Willy Tarreau290e63a2012-09-20 18:07:14 +02001199 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001200 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1201 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001202 return 1;
1203 }
1204
1205 if (!*args[3]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001206 si->appctx.ctx.cli.msg = "Expects an integer value.\n";
1207 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001208 return 1;
1209 }
1210
1211 v = atoi(args[3]);
1212 if (v > global.hardmaxconn) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001213 si->appctx.ctx.cli.msg = "Value out of range.\n";
1214 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001215 return 1;
1216 }
1217
1218 /* check for unlimited values */
1219 if (v <= 0)
1220 v = global.hardmaxconn;
1221
1222 global.maxconn = v;
1223
1224 /* Dequeues all of the listeners waiting for a resource */
1225 if (!LIST_ISEMPTY(&global_listener_queue))
1226 dequeue_all_listeners(&global_listener_queue);
1227
1228 return 1;
1229 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001230 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001231 si->appctx.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1232 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001233 return 1;
1234 }
1235 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001236 else if (strcmp(args[1], "rate-limit") == 0) {
1237 if (strcmp(args[2], "connections") == 0) {
1238 if (strcmp(args[3], "global") == 0) {
1239 int v;
1240
Willy Tarreau290e63a2012-09-20 18:07:14 +02001241 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001242 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1243 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001244 return 1;
1245 }
1246
1247 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001248 si->appctx.ctx.cli.msg = "Expects an integer value.\n";
1249 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001250 return 1;
1251 }
1252
1253 v = atoi(args[4]);
1254 if (v < 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001255 si->appctx.ctx.cli.msg = "Value out of range.\n";
1256 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001257 return 1;
1258 }
1259
1260 global.cps_lim = v;
1261
1262 /* Dequeues all of the listeners waiting for a resource */
1263 if (!LIST_ISEMPTY(&global_listener_queue))
1264 dequeue_all_listeners(&global_listener_queue);
1265
1266 return 1;
1267 }
1268 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001269 si->appctx.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1270 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001271 return 1;
1272 }
1273 }
William Lallemandd85f9172012-11-09 17:05:39 +01001274 else if (strcmp(args[2], "http-compression") == 0) {
1275 if (strcmp(args[3], "global") == 0) {
1276 int v;
1277
Willy Tarreau85d47f92012-11-21 00:29:50 +01001278 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001279 si->appctx.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1280 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001281 return 1;
1282 }
1283
William Lallemandd85f9172012-11-09 17:05:39 +01001284 v = atoi(args[4]);
1285 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1286 }
1287 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001288 si->appctx.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1289 si->appctx.st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001290 return 1;
1291 }
1292 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001293 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001294 si->appctx.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
1295 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001296 return 1;
1297 }
1298 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001299 else if (strcmp(args[1], "table") == 0) {
1300 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1301 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001302 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001303 return 0;
1304 }
1305 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001306 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001307 if (strcmp(args[1], "agent") == 0) {
1308 struct server *sv;
1309
1310 sv = expect_server_admin(s, si, args[2]);
1311 if (!sv)
1312 return 1;
1313
1314 sv->agent.state &= ~CHK_STATE_DISABLED;
1315
1316 return 1;
1317 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001318 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001319 struct server *sv;
1320
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001321 sv = expect_server_admin(s, si, args[2]);
1322 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001323 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001324
Cyril Bontécd19e512010-01-31 22:34:03 +01001325 if (sv->state & SRV_MAINTAIN) {
1326 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001327 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001328 /* If this server tracks the status of another one,
1329 * we must restore the good status.
1330 */
Willy Tarreau44267702011-10-28 15:35:33 +02001331 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001332 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001333 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001334 } else {
1335 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001336 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001337 }
1338 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001339 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001340 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001341 }
1342 }
1343
Willy Tarreau532a4502011-09-07 22:37:44 +02001344 return 1;
1345 }
1346 else if (strcmp(args[1], "frontend") == 0) {
1347 struct proxy *px;
1348
1349 px = expect_frontend_admin(s, si, args[2]);
1350 if (!px)
1351 return 1;
1352
1353 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001354 si->appctx.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1355 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001356 return 1;
1357 }
1358
1359 if (px->state != PR_STPAUSED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001360 si->appctx.ctx.cli.msg = "Frontend is already enabled.\n";
1361 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001362 return 1;
1363 }
1364
1365 if (!resume_proxy(px)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001366 si->appctx.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1367 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001368 return 1;
1369 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001370 return 1;
1371 }
1372 else { /* unknown "enable" parameter */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001373 si->appctx.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1374 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001375 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001376 }
1377 }
1378 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001379 if (strcmp(args[1], "agent") == 0) {
1380 struct server *sv;
1381
1382 sv = expect_server_admin(s, si, args[2]);
1383 if (!sv)
1384 return 1;
1385
1386 sv->agent.state |= CHK_STATE_DISABLED;
1387
1388 return 1;
1389 }
1390 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001391 struct server *sv;
1392
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001393 sv = expect_server_admin(s, si, args[2]);
1394 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001395 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001396
Cyril Bontécd19e512010-01-31 22:34:03 +01001397 if (! (sv->state & SRV_MAINTAIN)) {
1398 /* Not already in maintenance, we can change the server state */
1399 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001400 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001401 }
1402
Willy Tarreau532a4502011-09-07 22:37:44 +02001403 return 1;
1404 }
1405 else if (strcmp(args[1], "frontend") == 0) {
1406 struct proxy *px;
1407
1408 px = expect_frontend_admin(s, si, args[2]);
1409 if (!px)
1410 return 1;
1411
1412 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001413 si->appctx.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1414 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001415 return 1;
1416 }
1417
1418 if (px->state == PR_STPAUSED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001419 si->appctx.ctx.cli.msg = "Frontend is already disabled.\n";
1420 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001421 return 1;
1422 }
1423
1424 if (!pause_proxy(px)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001425 si->appctx.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1426 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001427 return 1;
1428 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001429 return 1;
1430 }
1431 else { /* unknown "disable" parameter */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001432 si->appctx.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1433 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001434 return 1;
1435 }
1436 }
1437 else if (strcmp(args[0], "shutdown") == 0) {
1438 if (strcmp(args[1], "frontend") == 0) {
1439 struct proxy *px;
1440
1441 px = expect_frontend_admin(s, si, args[2]);
1442 if (!px)
1443 return 1;
1444
1445 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001446 si->appctx.ctx.cli.msg = "Frontend was already shut down.\n";
1447 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001448 return 1;
1449 }
1450
1451 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1452 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1453 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1454 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1455 stop_proxy(px);
1456 return 1;
1457 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001458 else if (strcmp(args[1], "session") == 0) {
1459 struct session *sess, *ptr;
1460
Willy Tarreau290e63a2012-09-20 18:07:14 +02001461 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001462 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1463 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001464 return 1;
1465 }
1466
1467 if (!*args[2]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001468 si->appctx.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1469 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001470 return 1;
1471 }
1472
1473 ptr = (void *)strtoul(args[2], NULL, 0);
1474
1475 /* first, look for the requested session in the session table */
1476 list_for_each_entry(sess, &sessions, list) {
1477 if (sess == ptr)
1478 break;
1479 }
1480
1481 /* do we have the session ? */
1482 if (sess != ptr) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001483 si->appctx.ctx.cli.msg = "No such session (use 'show sess').\n";
1484 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001485 return 1;
1486 }
1487
1488 session_shutdown(sess, SN_ERR_KILLED);
1489 return 1;
1490 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001491 else if (strcmp(args[1], "sessions") == 0) {
1492 if (strcmp(args[2], "server") == 0) {
1493 struct server *sv;
1494 struct session *sess, *sess_bck;
1495
1496 sv = expect_server_admin(s, si, args[3]);
1497 if (!sv)
1498 return 1;
1499
1500 /* kill all the session that are on this server */
1501 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1502 if (sess->srv_conn == sv)
1503 session_shutdown(sess, SN_ERR_KILLED);
1504
1505 return 1;
1506 }
1507 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001508 si->appctx.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1509 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001510 return 1;
1511 }
1512 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001513 else { /* unknown "disable" parameter */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001514 si->appctx.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1515 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001516 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001517 }
1518 }
1519 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001520 return 0;
1521 }
1522 return 1;
1523}
1524
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001526 * used to processes I/O from/to the stats unix socket. The system relies on a
1527 * state machine handling requests and various responses. We read a request,
1528 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001529 * Then we can read again. The state is stored in si->appctx.st0 and is one of the
1530 * STAT_CLI_* constants. si->appctx.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001531 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001532 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001533static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001534{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001535 struct channel *req = si->ob;
1536 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001537 int reql;
1538 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001539
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001540 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1541 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001542
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001543 while (1) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001544 if (si->appctx.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 /* Stats output not initialized yet */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001546 memset(&si->appctx.ctx.stats, 0, sizeof(si->appctx.ctx.stats));
1547 si->appctx.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001548 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001549 else if (si->appctx.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001550 /* Let's close for real now. We just close the request
1551 * side, the conditions below will complete if needed.
1552 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001553 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001554 break;
1555 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001556 else if (si->appctx.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001557 /* ensure we have some output room left in the event we
1558 * would want to return some info right after parsing.
1559 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001560 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001561 break;
1562
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001563 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001564 if (reql <= 0) { /* closed or EOL not found */
1565 if (reql == 0)
1566 break;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001567 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001568 continue;
1569 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001570
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001571 /* seek for a possible semi-colon. If we find one, we
1572 * replace it with an LF and skip only this part.
1573 */
1574 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001575 if (trash.str[len] == ';') {
1576 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001577 reql = len + 1;
1578 break;
1579 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001580
Willy Tarreau816fc222009-10-04 07:36:58 +02001581 /* now it is time to check that we have a full line,
1582 * remove the trailing \n and possibly \r, then cut the
1583 * line.
1584 */
1585 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001586 if (trash.str[len] != '\n') {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001587 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001588 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001589 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001590
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001591 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001592 len--;
1593
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001594 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001595
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001596 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001598 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001599 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001600 continue;
1601 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001602 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001603 si->appctx.st1 = !si->appctx.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001604 else if (strcmp(trash.str, "help") == 0 ||
1605 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001606 si->appctx.ctx.cli.msg = stats_sock_usage_msg;
1607 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001608 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001609 /* NB: stats_sock_parse_request() may have put
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001610 * another STAT_CLI_O_* into si->appctx.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001611 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001612 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001613 else if (!si->appctx.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001614 /* if prompt is disabled, print help on empty lines,
1615 * so that the user at least knows how to enable
1616 * prompt and find help.
1617 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001618 si->appctx.ctx.cli.msg = stats_sock_usage_msg;
1619 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001620 }
1621
1622 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001623 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001624 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001625 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001626 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001627 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001628 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001629 continue;
1630 }
1631
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001632 switch (si->appctx.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001633 case STAT_CLI_PRINT:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001634 if (bi_putstr(si->ib, si->appctx.ctx.cli.msg) != -1)
1635 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001636 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001637 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001638 if (stats_dump_info_to_buffer(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001639 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001640 break;
1641 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001642 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001643 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001644 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001645 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001646 if (stats_dump_sess_to_buffer(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001647 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001648 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001649 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001650 if (stats_dump_errors_to_buffer(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001651 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001652 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001653 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001654 case STAT_CLI_O_CLR:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001655 if (stats_table_request(si, si->appctx.st0))
1656 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001657 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001658 default: /* abnormal state */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001659 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001660 break;
1661 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001662
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001663 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001664 if (si->appctx.st0 == STAT_CLI_PROMPT) {
1665 if (bi_putstr(si->ib, si->appctx.st1 ? "\n> " : "\n") != -1)
1666 si->appctx.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667 }
1668
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001669 /* If the output functions are still there, it means they require more room. */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001670 if (si->appctx.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001671 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001672
1673 /* Now we close the output if one of the writers did so,
1674 * or if we're not in interactive mode and the request
1675 * buffer is empty. This still allows pipelined requests
1676 * to be sent in non-interactive mode.
1677 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001678 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->appctx.st1 && !req->buf->o)) {
1679 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001680 continue;
1681 }
1682
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001683 /* switch state back to GETREQ to read next requests */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001684 si->appctx.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001685 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001686 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001687
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001688 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->appctx.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001689 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1690 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001691 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001692 * and nothing more to consume. This is comparable to a broken pipe, so
1693 * we forward the close to the request side so that it flows upstream to
1694 * the client.
1695 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001696 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001697 }
1698
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001699 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->appctx.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001700 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1701 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1702 /* We have no more processing to do, and nothing more to send, and
1703 * the client side has closed. So we'll forward this state downstream
1704 * on the response buffer.
1705 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001706 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001707 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001708 }
1709
1710 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001711 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001712
1713 /* we don't want to expire timeouts while we're processing requests */
1714 si->ib->rex = TICK_ETERNITY;
1715 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001716
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001717 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001718 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 +02001719 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001720 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 +02001721
1722 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1723 /* check that we have released everything then unregister */
1724 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001725 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001726}
1727
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001728/* This function dumps information onto the stream interface's read buffer.
1729 * It returns 0 as long as it does not complete, non-zero upon completion.
1730 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001731 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001732static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001733{
1734 unsigned int up = (now.tv_sec - start_date.tv_sec);
1735
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001736 chunk_printf(&trash,
1737 "Name: " PRODUCT_NAME "\n"
1738 "Version: " HAPROXY_VERSION "\n"
1739 "Release_date: " HAPROXY_DATE "\n"
1740 "Nbproc: %d\n"
1741 "Process_num: %d\n"
1742 "Pid: %d\n"
1743 "Uptime: %dd %dh%02dm%02ds\n"
1744 "Uptime_sec: %d\n"
1745 "Memmax_MB: %d\n"
1746 "Ulimit-n: %d\n"
1747 "Maxsock: %d\n"
1748 "Maxconn: %d\n"
1749 "Hard_maxconn: %d\n"
1750 "Maxpipes: %d\n"
1751 "CurrConns: %d\n"
1752 "PipesUsed: %d\n"
1753 "PipesFree: %d\n"
1754 "ConnRate: %d\n"
1755 "ConnRateLimit: %d\n"
1756 "MaxConnRate: %d\n"
1757 "CompressBpsIn: %u\n"
1758 "CompressBpsOut: %u\n"
1759 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001760#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001761 "ZlibMemUsage: %ld\n"
1762 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001763#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001764 "Tasks: %d\n"
1765 "Run_queue: %d\n"
1766 "Idle_pct: %d\n"
1767 "node: %s\n"
1768 "description: %s\n"
1769 "",
1770 global.nbproc,
1771 relative_pid,
1772 pid,
1773 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1774 up,
1775 global.rlimit_memmax,
1776 global.rlimit_nofile,
1777 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1778 actconn, pipes_used, pipes_free,
1779 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1780 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1781 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001782#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001783 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001784#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001785 nb_tasks_cur, run_queue_cur, idle_pct,
1786 global.node, global.desc ? global.desc : ""
1787 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001788
1789 if (bi_putchk(si->ib, &trash) == -1)
1790 return 0;
1791
1792 return 1;
1793}
1794
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001795/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1796 * the state from stream interface <si>. The caller is responsible for clearing
1797 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001798 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001799static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001800{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001801 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001802
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001803 if (!(px->cap & PR_CAP_FE))
1804 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001805
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001806 if ((si->appctx.ctx.stats.flags & STAT_BOUND) && !(si->appctx.ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001807 return 0;
1808
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001809 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001810 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001811 /* name, queue */
1812 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001813
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001814 if (px->cap & PR_CAP_BE && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001815 /* Column sub-heading for Enable or Disable server */
1816 chunk_appendf(&trash, "<td></td>");
1817 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001819 chunk_appendf(&trash,
1820 "<td class=ac>"
1821 "<a name=\"%s/Frontend\"></a>"
1822 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1823 "<td colspan=3></td>"
1824 "",
1825 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001826
Willy Tarreau466c9b52012-12-23 02:25:03 +01001827 chunk_appendf(&trash,
1828 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001829 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001830 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1831 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1832 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001833 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1834 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1835 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001836
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001837 if (px->mode == PR_MODE_HTTP)
1838 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001839 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001840 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001841
1842 chunk_appendf(&trash,
1843 "</table></div></u></td>"
1844 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001845 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001846 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1847 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1848 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001849 U2H(px->fe_counters.sps_max),
1850 U2H(px->fe_counters.cps_max),
1851 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001852
1853 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001854 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001855 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001856 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001857
1858 chunk_appendf(&trash,
1859 "</table></div></u></td>"
1860 /* sessions rate : limit */
1861 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001862 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001863
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001864 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001865 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001866 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001867 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001868 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1869 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001870 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001871 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1872 U2H(px->fe_counters.cum_sess),
1873 U2H(px->fe_counters.cum_conn),
1874 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001875
Willy Tarreau466c9b52012-12-23 02:25:03 +01001876 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001877 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001878 chunk_appendf(&trash,
1879 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1880 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1881 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1882 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1883 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1884 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1885 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1886 "<tr><th>- other responses:</th><td>%s</td></tr>"
1887 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1888 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001889 U2H(px->fe_counters.p.http.cum_req),
1890 U2H(px->fe_counters.p.http.rsp[1]),
1891 U2H(px->fe_counters.p.http.rsp[2]),
1892 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001893 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001894 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001895 U2H(px->fe_counters.p.http.rsp[3]),
1896 U2H(px->fe_counters.p.http.rsp[4]),
1897 U2H(px->fe_counters.p.http.rsp[5]),
1898 U2H(px->fe_counters.p.http.rsp[0]),
1899 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001900 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001901
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001902 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001903 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001904 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001905 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001906 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001907 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001908 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001909 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001910
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001911 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001912 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001913 "<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 +01001914 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001915 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001916 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1917 px->fe_counters.comp_in ?
1918 (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 +01001919 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001920
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001921 chunk_appendf(&trash,
1922 /* denied: req, resp */
1923 "<td>%s</td><td>%s</td>"
1924 /* errors : request, connect, response */
1925 "<td>%s</td><td></td><td></td>"
1926 /* warnings: retries, redispatches */
1927 "<td></td><td></td>"
1928 /* server status : reflect frontend status */
1929 "<td class=ac>%s</td>"
1930 /* rest of server: nothing */
1931 "<td class=ac colspan=8></td></tr>"
1932 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001933 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1934 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001935 px->state == PR_STREADY ? "OPEN" :
1936 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001937 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001938 else { /* CSV mode */
1939 chunk_appendf(&trash,
1940 /* pxid, name, queue cur, queue max, */
1941 "%s,FRONTEND,,,"
1942 /* sessions : current, max, limit, total */
1943 "%d,%d,%d,%lld,"
1944 /* bytes : in, out */
1945 "%lld,%lld,"
1946 /* denied: req, resp */
1947 "%lld,%lld,"
1948 /* errors : request, connect, response */
1949 "%lld,,,"
1950 /* warnings: retries, redispatches */
1951 ",,"
1952 /* server status : reflect frontend status */
1953 "%s,"
1954 /* rest of server: nothing */
1955 ",,,,,,,,"
1956 /* pid, iid, sid, throttle, lbtot, tracked, type */
1957 "%d,%d,0,,,,%d,"
1958 /* rate, rate_lim, rate_max */
1959 "%u,%u,%u,"
1960 /* check_status, check_code, check_duration */
1961 ",,,",
1962 px->id,
1963 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1964 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1965 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1966 px->fe_counters.failed_req,
1967 px->state == PR_STREADY ? "OPEN" :
1968 px->state == PR_STFULL ? "FULL" : "STOP",
1969 relative_pid, px->uuid, STATS_TYPE_FE,
1970 read_freq_ctr(&px->fe_sess_per_sec),
1971 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001972
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001973 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1974 if (px->mode == PR_MODE_HTTP) {
1975 for (i=1; i<6; i++)
1976 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1977 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1978 }
1979 else
1980 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001981
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001982 /* failed health analyses */
1983 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001984
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001985 /* requests : req_rate, req_rate_max, req_tot, */
1986 chunk_appendf(&trash, "%u,%u,%lld,",
1987 read_freq_ctr(&px->fe_req_per_sec),
1988 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1989
1990 /* errors: cli_aborts, srv_aborts */
1991 chunk_appendf(&trash, ",,");
1992
1993 /* compression: in, out, bypassed */
1994 chunk_appendf(&trash, "%lld,%lld,%lld,",
1995 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1996
1997 /* compression: comp_rsp */
1998 chunk_appendf(&trash, "%lld,",
1999 px->fe_counters.p.http.comp_rsp);
2000
2001 /* finish with EOL */
2002 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002003 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002004 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002005}
2006
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002007/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2008 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2009 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2010 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002011 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002012static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002013{
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002014 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002015 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002016 if (px->cap & PR_CAP_BE && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002017 /* Column sub-heading for Enable or Disable server */
2018 chunk_appendf(&trash, "<td></td>");
2019 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002020 chunk_appendf(&trash,
2021 /* frontend name, listener name */
2022 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2023 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2024 "",
2025 px->id, l->name,
2026 (flags & ST_SHLGNDS)?"<u>":"",
2027 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002028
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002029 if (flags & ST_SHLGNDS) {
2030 char str[INET6_ADDRSTRLEN];
2031 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002032
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002033 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002035 port = get_host_port(&l->addr);
2036 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2037 case AF_INET:
2038 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2039 break;
2040 case AF_INET6:
2041 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2042 break;
2043 case AF_UNIX:
2044 chunk_appendf(&trash, "unix, ");
2045 break;
2046 case -1:
2047 chunk_appendf(&trash, "(%s), ", strerror(errno));
2048 break;
2049 }
Willy Tarreau91861262007-10-17 17:06:05 +02002050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002051 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002052 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002053 }
Willy Tarreau91861262007-10-17 17:06:05 +02002054
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002055 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002056 /* queue */
2057 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002058 /* sessions rate: current, max, limit */
2059 "<td colspan=3>&nbsp;</td>"
2060 /* sessions: current, max, limit, total, lbtot */
2061 "<td>%s</td><td>%s</td><td>%s</td>"
2062 "<td>%s</td><td>&nbsp;</td>"
2063 /* bytes: in, out */
2064 "<td>%s</td><td>%s</td>"
2065 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002066 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002067 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2068 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002069
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002070 chunk_appendf(&trash,
2071 /* denied: req, resp */
2072 "<td>%s</td><td>%s</td>"
2073 /* errors: request, connect, response */
2074 "<td>%s</td><td></td><td></td>"
2075 /* warnings: retries, redispatches */
2076 "<td></td><td></td>"
2077 /* server status: reflect listener status */
2078 "<td class=ac>%s</td>"
2079 /* rest of server: nothing */
2080 "<td class=ac colspan=8></td></tr>"
2081 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002082 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2083 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002084 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2085 }
2086 else { /* CSV mode */
2087 chunk_appendf(&trash,
2088 /* pxid, name, queue cur, queue max, */
2089 "%s,%s,,,"
2090 /* sessions: current, max, limit, total */
2091 "%d,%d,%d,%lld,"
2092 /* bytes: in, out */
2093 "%lld,%lld,"
2094 /* denied: req, resp */
2095 "%lld,%lld,"
2096 /* errors: request, connect, response */
2097 "%lld,,,"
2098 /* warnings: retries, redispatches */
2099 ",,"
2100 /* server status: reflect listener status */
2101 "%s,"
2102 /* rest of server: nothing */
2103 ",,,,,,,,"
2104 /* pid, iid, sid, throttle, lbtot, tracked, type */
2105 "%d,%d,%d,,,,%d,"
2106 /* rate, rate_lim, rate_max */
2107 ",,,"
2108 /* check_status, check_code, check_duration */
2109 ",,,"
2110 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2111 ",,,,,,"
2112 /* failed health analyses */
2113 ","
2114 /* requests : req_rate, req_rate_max, req_tot, */
2115 ",,,"
2116 /* errors: cli_aborts, srv_aborts */
2117 ",,"
2118 /* compression: in, out, bypassed, comp_rsp */
2119 ",,,,"
2120 "\n",
2121 px->id, l->name,
2122 l->nbconn, l->counters->conn_max,
2123 l->maxconn, l->counters->cum_conn,
2124 l->counters->bytes_in, l->counters->bytes_out,
2125 l->counters->denied_req, l->counters->denied_resp,
2126 l->counters->failed_req,
2127 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2128 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2129 }
2130 return 1;
2131}
Willy Tarreau91861262007-10-17 17:06:05 +02002132
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002133/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2134 * from stream interface <si>, stats flags <flags>, and server state <state>.
2135 * The caller is responsible for clearing the trash if needed. Returns non-zero
2136 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002137 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2138 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002139 */
2140static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2141{
2142 struct server *ref = sv->track ? sv->track : sv;
2143 char str[INET6_ADDRSTRLEN];
2144 struct chunk src;
2145 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002146
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002147 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002148 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002149 "DOWN",
2150 "DN %d/%d &uarr;",
2151 "UP %d/%d &darr;",
2152 "UP",
2153 "NOLB %d/%d &darr;",
2154 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002155 "DRAIN %d/%d &darr;",
2156 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002157 "<i>no check</i>"
2158 };
Willy Tarreau91861262007-10-17 17:06:05 +02002159
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002160 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2161 chunk_appendf(&trash, "<tr class=\"maintain\">");
2162 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002163 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002164 "<tr class=\"%s%d\">",
2165 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002166
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002167 if ((px->cap & PR_CAP_BE) && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002168 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002169 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2170 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002171
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002172 chunk_appendf(&trash,
2173 "<td class=ac><a name=\"%s/%s\"></a>%s"
2174 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2175 "",
2176 px->id, sv->id,
2177 (flags & ST_SHLGNDS) ? "<u>" : "",
2178 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002179
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002180 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002181 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002182
2183 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2184 case AF_INET:
2185 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2186 break;
2187 case AF_INET6:
2188 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2189 break;
2190 case AF_UNIX:
2191 chunk_appendf(&trash, "unix, ");
2192 break;
2193 case -1:
2194 chunk_appendf(&trash, "(%s), ", strerror(errno));
2195 break;
2196 default: /* address family not supported */
2197 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002198 }
Willy Tarreau91861262007-10-17 17:06:05 +02002199
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002200 /* id */
2201 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002202
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002203 /* cookie */
2204 if (sv->cookie) {
2205 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002206
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002207 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2208 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002209
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002210 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002211 }
2212
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002213 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002214 }
Willy Tarreau91861262007-10-17 17:06:05 +02002215
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002216 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002217 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002218 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002219 /* sessions rate : current, max, limit */
2220 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002221 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002222 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002223 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2224 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002225
Willy Tarreau466c9b52012-12-23 02:25:03 +01002226
2227 chunk_appendf(&trash,
2228 /* sessions: current, max, limit, total */
2229 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002230 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002231 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2232 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002233 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2234 U2H(sv->counters.cum_sess),
2235 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002236
Willy Tarreau466c9b52012-12-23 02:25:03 +01002237 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2238 if (px->mode == PR_MODE_HTTP) {
2239 unsigned long long tot;
2240 for (tot = i = 0; i < 6; i++)
2241 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002242
Willy Tarreau466c9b52012-12-23 02:25:03 +01002243 chunk_appendf(&trash,
2244 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2245 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2246 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2247 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2248 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2249 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2250 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2251 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002252 U2H(tot),
2253 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2254 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2255 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2256 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2257 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2258 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 +02002259 }
Willy Tarreau91861262007-10-17 17:06:05 +02002260
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002261 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002262 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002263 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002264 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002265 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002266
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002267 chunk_appendf(&trash,
2268 /* bytes : in, out */
2269 "<td>%s</td><td>%s</td>"
2270 /* denied: req, resp */
2271 "<td></td><td>%s</td>"
2272 /* errors : request, connect */
2273 "<td></td><td>%s</td>"
2274 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002275 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002276 /* warnings: retries, redispatches */
2277 "<td>%lld</td><td>%lld</td>"
2278 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002279 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2280 U2H(sv->counters.failed_secu),
2281 U2H(sv->counters.failed_conns),
2282 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002283 sv->counters.cli_aborts,
2284 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002285 sv->counters.retries, sv->counters.redispatches);
2286
2287 /* status, lest check */
2288 chunk_appendf(&trash, "<td class=ac>");
2289
2290 if (sv->state & SRV_MAINTAIN) {
2291 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2292 chunk_appendf(&trash, "MAINT");
2293 }
2294 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2295 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2296 chunk_appendf(&trash, "MAINT(via)");
2297 }
2298 else if (ref->state & SRV_CHECKED) {
2299 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2300 chunk_appendf(&trash,
2301 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002302 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2303 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002304 }
Willy Tarreau91861262007-10-17 17:06:05 +02002305
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002306 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002307 chunk_appendf(&trash,
2308 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002309 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002310 get_check_status_info(sv->check.status));
2311
2312 if (sv->check.status >= HCHK_STATUS_L57DATA)
2313 chunk_appendf(&trash, "/%d", sv->check.code);
2314
2315 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002316 chunk_appendf(&trash, " in %lums", sv->check.duration);
2317
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002318 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002319 get_check_status_description(sv->check.status));
2320 if (*sv->check.desc) {
2321 chunk_appendf(&trash, ": ");
2322 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2323 chunk_htmlencode(&trash, &src);
2324 }
2325 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002326 }
2327 else
2328 chunk_appendf(&trash, "</td><td>");
2329
2330 chunk_appendf(&trash,
2331 /* weight */
2332 "</td><td class=ac>%d</td>"
2333 /* act, bck */
2334 "<td class=ac>%s</td><td class=ac>%s</td>"
2335 "",
2336 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2337 (sv->state & SRV_BACKUP) ? "-" : "Y",
2338 (sv->state & SRV_BACKUP) ? "Y" : "-");
2339
2340 /* check failures: unique, fatal, down time */
2341 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002342 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002343
2344 if (ref->observe)
2345 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2346
2347 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002348 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002349 "<td>%lld</td><td>%s</td>"
2350 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002351 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002352 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2353 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002354 else if (sv != ref) {
2355 if (sv->state & SRV_MAINTAIN)
2356 chunk_appendf(&trash,
2357 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2358 ref->proxy->id, ref->id);
2359 else
2360 chunk_appendf(&trash,
2361 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2362 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2363 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002364 else
2365 chunk_appendf(&trash, "<td colspan=3></td>");
2366
2367 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002368 if (sv->state & SRV_WARMINGUP)
2369 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002370 else
2371 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2372 }
2373 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002374 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375 "DOWN,",
2376 "DOWN %d/%d,",
2377 "UP %d/%d,",
2378 "UP,",
2379 "NOLB %d/%d,",
2380 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002381 "DRAIN %d/%d,",
2382 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002383 "no check,"
2384 };
2385
2386 chunk_appendf(&trash,
2387 /* pxid, name */
2388 "%s,%s,"
2389 /* queue : current, max */
2390 "%d,%d,"
2391 /* sessions : current, max, limit, total */
2392 "%d,%d,%s,%lld,"
2393 /* bytes : in, out */
2394 "%lld,%lld,"
2395 /* denied: req, resp */
2396 ",%lld,"
2397 /* errors : request, connect, response */
2398 ",%lld,%lld,"
2399 /* warnings: retries, redispatches */
2400 "%lld,%lld,"
2401 "",
2402 px->id, sv->id,
2403 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002404 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002405 sv->counters.bytes_in, sv->counters.bytes_out,
2406 sv->counters.failed_secu,
2407 sv->counters.failed_conns, sv->counters.failed_resp,
2408 sv->counters.retries, sv->counters.redispatches);
2409
2410 /* status */
2411 if (sv->state & SRV_MAINTAIN)
2412 chunk_appendf(&trash, "MAINT,");
2413 else if (ref != sv && ref->state & SRV_MAINTAIN)
2414 chunk_appendf(&trash, "MAINT(via),");
2415 else
2416 chunk_appendf(&trash,
2417 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002418 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2419 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002420
2421 chunk_appendf(&trash,
2422 /* weight, active, backup */
2423 "%d,%d,%d,"
2424 "",
2425 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2426 (sv->state & SRV_BACKUP) ? 0 : 1,
2427 (sv->state & SRV_BACKUP) ? 1 : 0);
2428
2429 /* check failures: unique, fatal; last change, total downtime */
2430 if (sv->state & SRV_CHECKED)
2431 chunk_appendf(&trash,
2432 "%lld,%lld,%d,%d,",
2433 sv->counters.failed_checks, sv->counters.down_trans,
2434 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2435 else
2436 chunk_appendf(&trash, ",,,,");
2437
2438 /* queue limit, pid, iid, sid, */
2439 chunk_appendf(&trash,
2440 "%s,"
2441 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002442 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002443 relative_pid, px->uuid, sv->puid);
2444
2445 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002446 if (sv->state & SRV_WARMINGUP)
2447 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002448
2449 /* sessions: lbtot */
2450 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2451
2452 /* tracked */
2453 if (sv->track)
2454 chunk_appendf(&trash, "%s/%s,",
2455 sv->track->proxy->id, sv->track->id);
2456 else
2457 chunk_appendf(&trash, ",");
2458
2459 /* type */
2460 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2461
2462 /* rate */
2463 chunk_appendf(&trash, "%u,,%u,",
2464 read_freq_ctr(&sv->sess_per_sec),
2465 sv->counters.sps_max);
2466
2467 if (sv->state & SRV_CHECKED) {
2468 /* check_status */
2469 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2470
2471 /* check_code */
2472 if (sv->check.status >= HCHK_STATUS_L57DATA)
2473 chunk_appendf(&trash, "%u,", sv->check.code);
2474 else
2475 chunk_appendf(&trash, ",");
2476
2477 /* check_duration */
2478 if (sv->check.status >= HCHK_STATUS_CHECKED)
2479 chunk_appendf(&trash, "%lu,", sv->check.duration);
2480 else
2481 chunk_appendf(&trash, ",");
2482
2483 }
2484 else
2485 chunk_appendf(&trash, ",,,");
2486
2487 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2488 if (px->mode == PR_MODE_HTTP) {
2489 for (i=1; i<6; i++)
2490 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2491
2492 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2493 }
2494 else
2495 chunk_appendf(&trash, ",,,,,,");
2496
2497 /* failed health analyses */
2498 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2499
2500 /* requests : req_rate, req_rate_max, req_tot, */
2501 chunk_appendf(&trash, ",,,");
2502
2503 /* errors: cli_aborts, srv_aborts */
2504 chunk_appendf(&trash, "%lld,%lld,",
2505 sv->counters.cli_aborts, sv->counters.srv_aborts);
2506
2507 /* compression: in, out, bypassed, comp_rsp */
2508 chunk_appendf(&trash, ",,,,");
2509
2510 /* finish with EOL */
2511 chunk_appendf(&trash, "\n");
2512 }
2513 return 1;
2514}
2515
2516/* Dumps a line for backend <px> to the trash for and uses the state from stream
2517 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2518 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2519 */
2520static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2521{
2522 struct chunk src;
2523 int i;
2524
2525 if (!(px->cap & PR_CAP_BE))
2526 return 0;
2527
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002528 if ((si->appctx.ctx.stats.flags & STAT_BOUND) && !(si->appctx.ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002529 return 0;
2530
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002531 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002532 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002533 if (px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 /* Column sub-heading for Enable or Disable server */
2535 chunk_appendf(&trash, "<td></td>");
2536 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002537 chunk_appendf(&trash,
2538 "<td class=ac>"
2539 /* name */
2540 "%s<a name=\"%s/Backend\"></a>"
2541 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2542 "",
2543 (flags & ST_SHLGNDS)?"<u>":"",
2544 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002545
2546 if (flags & ST_SHLGNDS) {
2547 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002548 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002549 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2550
2551 /* cookie */
2552 if (px->cookie_name) {
2553 chunk_appendf(&trash, ", cookie: '");
2554 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2555 chunk_htmlencode(&trash, &src);
2556 chunk_appendf(&trash, "'");
2557 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002558 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002559 }
2560
2561 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002562 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 /* queue : current, max */
2564 "<td>%s</td><td>%s</td><td></td>"
2565 /* sessions rate : current, max, limit */
2566 "<td>%s</td><td>%s</td><td></td>"
2567 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002568 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002569 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2570 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571
2572 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002573 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002574 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002575 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002576 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002578 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2579 U2H(px->be_counters.cum_conn),
2580 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002581
Willy Tarreau466c9b52012-12-23 02:25:03 +01002582 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002584 chunk_appendf(&trash,
2585 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2586 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2587 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2588 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2589 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2590 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2591 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2592 "<tr><th>- other responses:</th><td>%s</td></tr>"
2593 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2594 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002595 U2H(px->be_counters.p.http.cum_req),
2596 U2H(px->be_counters.p.http.rsp[1]),
2597 U2H(px->be_counters.p.http.rsp[2]),
2598 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002599 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002600 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002601 U2H(px->be_counters.p.http.rsp[3]),
2602 U2H(px->be_counters.p.http.rsp[4]),
2603 U2H(px->be_counters.p.http.rsp[5]),
2604 U2H(px->be_counters.p.http.rsp[0]),
2605 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002606 }
2607
2608 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002609 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002610 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002611 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002613 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002615 U2H(px->be_counters.cum_lbconn),
2616 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002617
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002618 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002619 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002620 "<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 +01002621 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002622 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002623 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2624 px->be_counters.comp_in ?
2625 (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 +01002626 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2627
2628 chunk_appendf(&trash,
2629 /* denied: req, resp */
2630 "<td>%s</td><td>%s</td>"
2631 /* errors : request, connect */
2632 "<td></td><td>%s</td>"
2633 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002634 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002635 /* warnings: retries, redispatches */
2636 "<td>%lld</td><td>%lld</td>"
2637 /* backend status: reflect backend status (up/down): we display UP
2638 * if the backend has known working servers or if it has no server at
2639 * all (eg: for stats). Then we display the total weight, number of
2640 * active and backups. */
2641 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2642 "<td class=ac>%d</td><td class=ac>%d</td>"
2643 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002644 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2645 U2H(px->be_counters.failed_conns),
2646 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 px->be_counters.cli_aborts,
2648 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002649 px->be_counters.retries, px->be_counters.redispatches,
2650 human_time(now.tv_sec - px->last_change, 1),
2651 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2652 "<font color=\"red\"><b>DOWN</b></font>",
2653 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2654 px->srv_act, px->srv_bck);
2655
2656 chunk_appendf(&trash,
2657 /* rest of backend: nothing, down transitions, total downtime, throttle */
2658 "<td class=ac>&nbsp;</td><td>%d</td>"
2659 "<td>%s</td>"
2660 "<td></td>"
2661 "</tr>",
2662 px->down_trans,
2663 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2664 }
2665 else { /* CSV mode */
2666 chunk_appendf(&trash,
2667 /* pxid, name */
2668 "%s,BACKEND,"
2669 /* queue : current, max */
2670 "%d,%d,"
2671 /* sessions : current, max, limit, total */
2672 "%d,%d,%d,%lld,"
2673 /* bytes : in, out */
2674 "%lld,%lld,"
2675 /* denied: req, resp */
2676 "%lld,%lld,"
2677 /* errors : request, connect, response */
2678 ",%lld,%lld,"
2679 /* warnings: retries, redispatches */
2680 "%lld,%lld,"
2681 /* backend status: reflect backend status (up/down): we display UP
2682 * if the backend has known working servers or if it has no server at
2683 * all (eg: for stats). Then we display the total weight, number of
2684 * active and backups. */
2685 "%s,"
2686 "%d,%d,%d,"
2687 /* rest of backend: nothing, down transitions, last change, total downtime */
2688 ",%d,%d,%d,,"
2689 /* pid, iid, sid, throttle, lbtot, tracked, type */
2690 "%d,%d,0,,%lld,,%d,"
2691 /* rate, rate_lim, rate_max, */
2692 "%u,,%u,"
2693 /* check_status, check_code, check_duration */
2694 ",,,",
2695 px->id,
2696 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2697 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2698 px->be_counters.bytes_in, px->be_counters.bytes_out,
2699 px->be_counters.denied_req, px->be_counters.denied_resp,
2700 px->be_counters.failed_conns, px->be_counters.failed_resp,
2701 px->be_counters.retries, px->be_counters.redispatches,
2702 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2703 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2704 px->srv_act, px->srv_bck,
2705 px->down_trans, (int)(now.tv_sec - px->last_change),
2706 px->srv?be_downtime(px):0,
2707 relative_pid, px->uuid,
2708 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2709 read_freq_ctr(&px->be_sess_per_sec),
2710 px->be_counters.sps_max);
2711
2712 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2713 if (px->mode == PR_MODE_HTTP) {
2714 for (i=1; i<6; i++)
2715 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2716 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2717 }
2718 else
2719 chunk_appendf(&trash, ",,,,,,");
2720
2721 /* failed health analyses */
2722 chunk_appendf(&trash, ",");
2723
2724 /* requests : req_rate, req_rate_max, req_tot, */
2725 chunk_appendf(&trash, ",,,");
2726
2727 /* errors: cli_aborts, srv_aborts */
2728 chunk_appendf(&trash, "%lld,%lld,",
2729 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2730
2731 /* compression: in, out, bypassed */
2732 chunk_appendf(&trash, "%lld,%lld,%lld,",
2733 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2734
2735 /* compression: comp_rsp */
2736 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2737
2738 /* finish with EOL */
2739 chunk_appendf(&trash, "\n");
2740 }
2741 return 1;
2742}
2743
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002744/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002745 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002746 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002748static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749{
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002750 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2751
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002752 if (px->cap & PR_CAP_BE && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002754
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002755 /* scope_txt = search pattern + search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002756 scope_txt[0] = 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002757 if (si->appctx.ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002758 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002759 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
2760 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->appctx.ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002761 }
2762
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002763 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002764 "<form action=\"%s%s%s%s\" method=\"post\">",
2765 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002766 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2767 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002768 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002769 }
2770
2771 /* print a new table */
2772 chunk_appendf(&trash,
2773 "<table class=\"tbl\" width=\"100%%\">\n"
2774 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002775 "<th class=\"pxname\" width=\"10%%\">");
2776
2777 chunk_appendf(&trash,
2778 "<a name=\"%s\"></a>%s"
2779 "<a class=px href=\"#%s\">%s</a>",
2780 px->id,
2781 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2782 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002783
2784 if (uri->flags & ST_SHLGNDS) {
2785 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002786 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2788 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002789 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 }
2791
2792 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002793 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 "<th class=\"%s\" width=\"90%%\">%s</th>"
2795 "</tr>\n"
2796 "</table>\n"
2797 "<table class=\"tbl\" width=\"100%%\">\n"
2798 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002799 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2800 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2801
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002802 if ((px->cap & PR_CAP_BE) && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803 /* Column heading for Enable or Disable server */
2804 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002805 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002806
2807 chunk_appendf(&trash,
2808 "<th rowspan=2></th>"
2809 "<th colspan=3>Queue</th>"
2810 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2811 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2812 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2813 "<th colspan=9>Server</th>"
2814 "</tr>\n"
2815 "<tr class=\"titre\">"
2816 "<th>Cur</th><th>Max</th><th>Limit</th>"
2817 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2818 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2819 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2820 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2821 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2822 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2823 "<th>Thrtle</th>\n"
2824 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002825}
2826
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002827/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002828 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002830static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002831{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002832 chunk_appendf(&trash, "</table>");
2833
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002834 if ((px->cap & PR_CAP_BE) && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002835 /* close the form used to enable/disable this proxy servers */
2836 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002837 "Choose the action to perform on the checked servers : "
2838 "<select name=action>"
2839 "<option value=\"\"></option>"
2840 "<option value=\"disable\">Disable</option>"
2841 "<option value=\"enable\">Enable</option>"
2842 "<option value=\"stop\">Soft Stop</option>"
2843 "<option value=\"start\">Soft Start</option>"
2844 "<option value=\"shutdown\">Kill Sessions</option>"
2845 "</select>"
2846 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2847 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2848 "</form>",
2849 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002850 }
2851
2852 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002853}
Willy Tarreau91861262007-10-17 17:06:05 +02002854
2855/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002856 * Dumps statistics for a proxy. The output is sent to the stream interface's
2857 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2858 * buffer space, or non-zero if everything completed. This function is used
2859 * both by the CLI and the HTTP entry points, and is able to dump the output
2860 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002861 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002862static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002863{
Willy Tarreau306f8302013-07-08 15:53:06 +02002864 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002865 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002866 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002867 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002869 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002870
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002871 switch (si->appctx.ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002872 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002873 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002874 if (uri && uri->scope) {
2875 /* we have a limited scope, we have to check the proxy name */
2876 struct stat_scope *scope;
2877 int len;
2878
2879 len = strlen(px->id);
2880 scope = uri->scope;
2881
2882 while (scope) {
2883 /* match exact proxy name */
2884 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2885 break;
2886
2887 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002888 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002889 break;
2890 scope = scope->next;
2891 }
2892
2893 /* proxy name not found : don't dump anything */
2894 if (scope == NULL)
2895 return 1;
2896 }
2897
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002898 /* if the user has requested a limited output and the proxy
2899 * name does not match, skip it.
2900 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002901 if (si->appctx.ctx.stats.scope_len &&
2902 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002903 return 1;
2904
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002905 if ((si->appctx.ctx.stats.flags & STAT_BOUND) &&
2906 (si->appctx.ctx.stats.iid != -1) &&
2907 (px->uuid != si->appctx.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002908 return 1;
2909
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002910 si->appctx.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002911 /* fall through */
2912
Willy Tarreau295a8372011-03-10 11:25:07 +01002913 case STAT_PX_ST_TH:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002914 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002915 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002916 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002917 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002918 }
Willy Tarreau91861262007-10-17 17:06:05 +02002919
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002920 si->appctx.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002921 /* fall through */
2922
Willy Tarreau295a8372011-03-10 11:25:07 +01002923 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002924 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002925 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002926 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002927 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002928
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002929 si->appctx.ctx.stats.l = px->conf.listeners.n;
2930 si->appctx.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002931 /* fall through */
2932
Willy Tarreau295a8372011-03-10 11:25:07 +01002933 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002934 /* stats.l has been initialized above */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002935 for (; si->appctx.ctx.stats.l != &px->conf.listeners; si->appctx.ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002936 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002937 return 0;
2938
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002939 l = LIST_ELEM(si->appctx.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002940 if (!l->counters)
2941 continue;
2942
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002943 if (si->appctx.ctx.stats.flags & STAT_BOUND) {
2944 if (!(si->appctx.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002945 break;
2946
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002947 if (si->appctx.ctx.stats.sid != -1 && l->luid != si->appctx.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002948 continue;
2949 }
2950
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 /* print the frontend */
2952 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2953 if (bi_putchk(rep, &trash) == -1)
2954 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002955 }
2956
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002957 si->appctx.ctx.stats.sv = px->srv; /* may be NULL */
2958 si->appctx.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002959 /* fall through */
2960
Willy Tarreau295a8372011-03-10 11:25:07 +01002961 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002962 /* stats.sv has been initialized above */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002963 for (; si->appctx.ctx.stats.sv != NULL; si->appctx.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002964 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 +02002965
Willy Tarreau9b28e032012-10-12 23:49:43 +02002966 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002967 return 0;
2968
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002969 sv = si->appctx.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002970
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002971 if (si->appctx.ctx.stats.flags & STAT_BOUND) {
2972 if (!(si->appctx.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002973 break;
2974
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002975 if (si->appctx.ctx.stats.sid != -1 && sv->puid != si->appctx.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002976 continue;
2977 }
2978
Willy Tarreau44267702011-10-28 15:35:33 +02002979 if (sv->track)
2980 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002981 else
2982 svs = sv;
2983
Willy Tarreau91861262007-10-17 17:06:05 +02002984 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002985 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09002986 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002987 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09002988 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002989 sv_state = 3; /* UP */
2990 else
2991 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002992
Willy Tarreau6b7764a2013-12-04 00:43:21 +01002993 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09002994 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01002995 else if (svs->state & SRV_GOINGDOWN)
2996 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01002997 }
Willy Tarreau91861262007-10-17 17:06:05 +02002998 else
Simon Horman125d0992013-02-24 17:23:38 +09002999 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003000 sv_state = 1; /* going up */
3001 else
3002 sv_state = 0; /* DOWN */
3003
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003004 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003005 /* do not report servers which are DOWN */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003006 si->appctx.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003007 continue;
3008 }
3009
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003010 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3011 if (bi_putchk(rep, &trash) == -1)
3012 return 0;
3013 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003014
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003015 si->appctx.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003016 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003017
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 case STAT_PX_ST_BE:
3019 /* print the backend */
3020 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3021 if (bi_putchk(rep, &trash) == -1)
3022 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003023
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003024 si->appctx.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003026
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027 case STAT_PX_ST_END:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003028 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003029 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 if (bi_putchk(rep, &trash) == -1)
3031 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003032 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003033
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003034 si->appctx.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003036
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003037 case STAT_PX_ST_FIN:
3038 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003039
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003040 default:
3041 /* unknown state, we should put an abort() here ! */
3042 return 1;
3043 }
3044}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003045
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003046/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3047 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003048 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003049static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003050{
3051 /* WARNING! This must fit in the first buffer !!! */
3052 chunk_appendf(&trash,
3053 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3054 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3055 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3056 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3057 "<style type=\"text/css\"><!--\n"
3058 "body {"
3059 " font-family: arial, helvetica, sans-serif;"
3060 " font-size: 12px;"
3061 " font-weight: normal;"
3062 " color: black;"
3063 " background: white;"
3064 "}\n"
3065 "th,td {"
3066 " font-size: 10px;"
3067 "}\n"
3068 "h1 {"
3069 " font-size: x-large;"
3070 " margin-bottom: 0.5em;"
3071 "}\n"
3072 "h2 {"
3073 " font-family: helvetica, arial;"
3074 " font-size: x-large;"
3075 " font-weight: bold;"
3076 " font-style: italic;"
3077 " color: #6020a0;"
3078 " margin-top: 0em;"
3079 " margin-bottom: 0em;"
3080 "}\n"
3081 "h3 {"
3082 " font-family: helvetica, arial;"
3083 " font-size: 16px;"
3084 " font-weight: bold;"
3085 " color: #b00040;"
3086 " background: #e8e8d0;"
3087 " margin-top: 0em;"
3088 " margin-bottom: 0em;"
3089 "}\n"
3090 "li {"
3091 " margin-top: 0.25em;"
3092 " margin-right: 2em;"
3093 "}\n"
3094 ".hr {margin-top: 0.25em;"
3095 " border-color: black;"
3096 " border-bottom-style: solid;"
3097 "}\n"
3098 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3099 ".total {background: #20D0D0;color: #ffff80;}\n"
3100 ".frontend {background: #e8e8d0;}\n"
3101 ".socket {background: #d0d0d0;}\n"
3102 ".backend {background: #e8e8d0;}\n"
3103 ".active0 {background: #ff9090;}\n"
3104 ".active1 {background: #ffd020;}\n"
3105 ".active2 {background: #ffffa0;}\n"
3106 ".active3 {background: #c0ffc0;}\n"
3107 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003108 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3109 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3110 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003111 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112 ".backup0 {background: #ff9090;}\n"
3113 ".backup1 {background: #ff80ff;}\n"
3114 ".backup2 {background: #c060ff;}\n"
3115 ".backup3 {background: #b0d0ff;}\n"
3116 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3117 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003118 ".backup6 {background: #c060ff;}\n"
3119 ".backup7 {background: #cc9900;}\n"
3120 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003121 ".maintain {background: #c07820;}\n"
3122 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3123 "\n"
3124 "a.px:link {color: #ffff40; text-decoration: none;}"
3125 "a.px:visited {color: #ffff40; text-decoration: none;}"
3126 "a.px:hover {color: #ffffff; text-decoration: none;}"
3127 "a.lfsb:link {color: #000000; text-decoration: none;}"
3128 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3129 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3130 "\n"
3131 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3132 "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"
3133 "table.tbl td.ac { text-align: center;}\n"
3134 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3135 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3136 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3137 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3138 "\n"
3139 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3140 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3141 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003142 "table.det { border-collapse: collapse; border-style: none; }\n"
3143 "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"
3144 "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;font-family: monospace;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003146 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003147 " display:block;\n"
3148 " visibility:hidden;\n"
3149 " z-index:2147483647;\n"
3150 " position:absolute;\n"
3151 " padding:2px 4px 3px;\n"
3152 " background:#f0f060; color:#000000;\n"
3153 " border:1px solid #7040c0;\n"
3154 " white-space:nowrap;\n"
3155 " font-style:normal;font-size:11px;font-weight:normal;\n"
3156 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3157 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3158 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003159 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003160 "-->\n"
3161 "</style></head>\n",
3162 (uri->flags & ST_SHNODE) ? " on " : "",
3163 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3164 );
3165}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003166
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003167/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003169 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003170 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003171static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003172{
3173 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003174 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003175
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 /* WARNING! this has to fit the first packet too.
3177 * We are around 3.5 kB, add adding entries will
3178 * become tricky if we want to support 4kB buffers !
3179 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180 chunk_appendf(&trash,
3181 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3182 PRODUCT_NAME "%s</a></h1>\n"
3183 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3184 "<hr width=\"100%%\" class=\"hr\">\n"
3185 "<h3>&gt; General process information</h3>\n"
3186 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3187 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3188 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3189 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3190 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3191 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3192 "Running tasks: %d/%d; idle = %d %%<br>\n"
3193 "</td><td align=\"center\" nowrap>\n"
3194 "<table class=\"lgd\"><tr>\n"
3195 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3196 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3197 "</tr><tr>\n"
3198 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3199 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3200 "</tr><tr>\n"
3201 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3202 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3203 "</tr><tr>\n"
3204 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003205 "</tr><tr>\n"
3206 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003207 "</tr><tr>\n"
3208 "<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 -07003209 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003210 "<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 +01003211 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003212 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003213 "</td>"
3214 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3215 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3216 "",
3217 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3218 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3219 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3220 (uri->flags & ST_SHDESC) ? ": " : "",
3221 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3222 pid, relative_pid, global.nbproc,
3223 up / 86400, (up % 86400) / 3600,
3224 (up % 3600) / 60, (up % 60),
3225 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3226 global.rlimit_memmax ? " MB" : "",
3227 global.rlimit_nofile,
3228 global.maxsock, global.maxconn, global.maxpipes,
3229 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3230 run_queue_cur, nb_tasks_cur, idle_pct
3231 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003232
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003233 /* scope_txt = search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3234 memcpy(scope_txt, bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
3235 scope_txt[si->appctx.ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003236
3237 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003238 "<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 +02003239 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003240 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3241 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3242 (si->appctx.ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003243 STAT_SCOPE_TXT_MAXLEN);
3244
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003245 /* scope_txt = search pattern + search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003246 scope_txt[0] = 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003247 if (si->appctx.ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003248 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003249 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
3250 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->appctx.ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003251 }
3252
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003253 if (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003255 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 uri->uri_prefix,
3257 "",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003258 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003259 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003260 else
3261 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003262 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263 uri->uri_prefix,
3264 ";up",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003265 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003266 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003267
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003268 if (uri->refresh > 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003269 if (si->appctx.ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003271 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003273 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003274 "",
3275 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003276 else
3277 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003278 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003280 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003281 ";norefresh",
3282 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003284
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003285 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003286 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003288 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3289 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003290 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003291
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003293 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003295 (uri->refresh > 0) ? ";norefresh" : "",
3296 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003297
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003298 chunk_appendf(&trash,
3299 "</ul></td>"
3300 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3301 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3302 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3303 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3304 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3305 "</ul>"
3306 "</td>"
3307 "</tr></table>\n"
3308 ""
3309 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003310
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003311 if (si->appctx.ctx.stats.st_code) {
3312 switch (si->appctx.ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003313 case STAT_STATUS_DONE:
3314 chunk_appendf(&trash,
3315 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003316 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003318 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003319 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3320 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003321 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 break;
3323 case STAT_STATUS_NONE:
3324 chunk_appendf(&trash,
3325 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003326 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003328 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003329 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3330 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003331 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 break;
3333 case STAT_STATUS_PART:
3334 chunk_appendf(&trash,
3335 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003336 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 "Action partially processed.<br>"
3338 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003339 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003340 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3341 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003342 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 break;
3344 case STAT_STATUS_ERRP:
3345 chunk_appendf(&trash,
3346 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003347 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003348 "Action not processed because of invalid parameters."
3349 "<ul>"
3350 "<li>The action is maybe unknown.</li>"
3351 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3352 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3353 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003354 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003355 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3356 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003357 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 break;
3359 case STAT_STATUS_EXCD:
3360 chunk_appendf(&trash,
3361 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003362 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3364 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003365 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003366 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3367 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003368 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 break;
3370 case STAT_STATUS_DENY:
3371 chunk_appendf(&trash,
3372 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003373 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003375 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003376 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3377 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003378 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 break;
3380 default:
3381 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003382 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003383 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003385 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003386 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3387 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003388 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 }
3390 chunk_appendf(&trash, "<p>\n");
3391 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003393
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003394/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3395 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003397static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003401
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003402/* This function dumps statistics onto the stream interface's read buffer in
3403 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003404 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3405 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3406 * and the session must be closed, or -1 in case of any error. This function is
3407 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003409static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411 struct channel *rep = si->ib;
3412 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003413
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003415
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003416 switch (si->appctx.st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 case STAT_ST_INIT:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003418 si->appctx.st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003420
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421 case STAT_ST_HEAD:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003422 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003423 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003424 else
3425 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003426
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 if (bi_putchk(rep, &trash) == -1)
3428 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003429
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003430 si->appctx.st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003432
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 case STAT_ST_INFO:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003434 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003435 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003436 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003437 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 }
Willy Tarreau91861262007-10-17 17:06:05 +02003439
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003440 si->appctx.ctx.stats.px = proxy;
3441 si->appctx.ctx.stats.px_st = STAT_PX_ST_INIT;
3442 si->appctx.st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003443 /* fall through */
3444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 case STAT_ST_LIST:
3446 /* dump proxies */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003447 while (si->appctx.ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 if (buffer_almost_full(rep->buf))
3449 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003450
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003451 px = si->appctx.ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 /* skip the disabled proxies, global frontend and non-networked ones */
3453 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003454 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003456
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003457 si->appctx.ctx.stats.px = px->next;
3458 si->appctx.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 }
3460 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003461
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003462 si->appctx.st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003464
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003465 case STAT_ST_END:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003466 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003467 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003468 if (bi_putchk(rep, &trash) == -1)
3469 return 0;
3470 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003471
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003472 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003474
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475 case STAT_ST_FIN:
3476 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003477
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478 default:
3479 /* unknown state ! */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003480 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003481 return -1;
3482 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003483}
Willy Tarreauae526782010-03-04 20:34:23 +01003484
Willy Tarreau347a35d2013-11-22 17:51:09 +01003485/* We reached the stats page through a POST request.
3486 * Parse the posted data and enable/disable servers if necessary.
3487 * Returns 1 if request was parsed or zero if it needs more data.
3488 */
3489static int stats_process_http_post(struct stream_interface *si)
3490{
3491 struct session *s = session_from_task(si->owner);
3492
3493 struct proxy *px = NULL;
3494 struct server *sv = NULL;
3495
3496 char key[LINESIZE];
3497 int action = ST_ADM_ACTION_NONE;
3498 int reprocess = 0;
3499
3500 int total_servers = 0;
3501 int altered_servers = 0;
3502
3503 char *first_param, *cur_param, *next_param, *end_params;
3504 char *st_cur_param = NULL;
3505 char *st_next_param = NULL;
3506
3507 struct chunk *temp;
3508 int reql;
3509
3510 temp = get_trash_chunk();
3511 if (temp->size < s->txn.req.body_len) {
3512 /* too large request */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003513 si->appctx.ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003514 goto out;
3515 }
3516
3517 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3518 if (reql <= 0) {
3519 /* we need more data */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003520 si->appctx.ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003521 return 0;
3522 }
3523
3524 first_param = temp->str;
3525 end_params = temp->str + reql;
3526 cur_param = next_param = end_params;
3527 *end_params = '\0';
3528
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003529 si->appctx.ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003530
3531 /*
3532 * Parse the parameters in reverse order to only store the last value.
3533 * From the html form, the backend and the action are at the end.
3534 */
3535 while (cur_param > first_param) {
3536 char *value;
3537 int poffset, plen;
3538
3539 cur_param--;
3540
3541 if ((*cur_param == '&') || (cur_param == first_param)) {
3542 reprocess_servers:
3543 /* Parse the key */
3544 poffset = (cur_param != first_param ? 1 : 0);
3545 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3546 if ((plen > 0) && (plen <= sizeof(key))) {
3547 strncpy(key, cur_param + poffset, plen);
3548 key[plen - 1] = '\0';
3549 } else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003550 si->appctx.ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003551 goto out;
3552 }
3553
3554 /* Parse the value */
3555 value = key;
3556 while (*value != '\0' && *value != '=') {
3557 value++;
3558 }
3559 if (*value == '=') {
3560 /* Ok, a value is found, we can mark the end of the key */
3561 *value++ = '\0';
3562 }
3563 if (url_decode(key) < 0 || url_decode(value) < 0)
3564 break;
3565
3566 /* Now we can check the key to see what to do */
3567 if (!px && (strcmp(key, "b") == 0)) {
3568 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
3569 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003570 si->appctx.ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003571 goto out;
3572 }
3573 }
3574 else if (!action && (strcmp(key, "action") == 0)) {
3575 if (strcmp(value, "disable") == 0) {
3576 action = ST_ADM_ACTION_DISABLE;
3577 }
3578 else if (strcmp(value, "enable") == 0) {
3579 action = ST_ADM_ACTION_ENABLE;
3580 }
3581 else if (strcmp(value, "stop") == 0) {
3582 action = ST_ADM_ACTION_STOP;
3583 }
3584 else if (strcmp(value, "start") == 0) {
3585 action = ST_ADM_ACTION_START;
3586 }
3587 else if (strcmp(value, "shutdown") == 0) {
3588 action = ST_ADM_ACTION_SHUTDOWN;
3589 }
3590 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003591 si->appctx.ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003592 goto out;
3593 }
3594 }
3595 else if (strcmp(key, "s") == 0) {
3596 if (!(px && action)) {
3597 /*
3598 * Indicates that we'll need to reprocess the parameters
3599 * as soon as backend and action are known
3600 */
3601 if (!reprocess) {
3602 st_cur_param = cur_param;
3603 st_next_param = next_param;
3604 }
3605 reprocess = 1;
3606 }
3607 else if ((sv = findserver(px, value)) != NULL) {
3608 switch (action) {
3609 case ST_ADM_ACTION_DISABLE:
3610 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
3611 /* Not already in maintenance, we can change the server state */
3612 sv->state |= SRV_MAINTAIN;
3613 set_server_down(&sv->check);
3614 altered_servers++;
3615 total_servers++;
3616 }
3617 break;
3618 case ST_ADM_ACTION_ENABLE:
3619 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
3620 /* Already in maintenance, we can change the server state */
3621 set_server_up(&sv->check);
3622 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
3623 altered_servers++;
3624 total_servers++;
3625 }
3626 break;
3627 case ST_ADM_ACTION_STOP:
3628 case ST_ADM_ACTION_START:
3629 if (action == ST_ADM_ACTION_START)
3630 sv->uweight = sv->iweight;
3631 else
3632 sv->uweight = 0;
3633
3634 server_recalc_eweight(sv);
3635 set_server_drain_state(sv);
3636
3637 altered_servers++;
3638 total_servers++;
3639 break;
3640 case ST_ADM_ACTION_SHUTDOWN:
3641 if (px->state != PR_STSTOPPED) {
3642 struct session *sess, *sess_bck;
3643
3644 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3645 if (sess->srv_conn == sv)
3646 session_shutdown(sess, SN_ERR_KILLED);
3647
3648 altered_servers++;
3649 total_servers++;
3650 }
3651 break;
3652 }
3653 } else {
3654 /* the server name is unknown or ambiguous (duplicate names) */
3655 total_servers++;
3656 }
3657 }
3658 if (reprocess && px && action) {
3659 /* Now, we know the backend and the action chosen by the user.
3660 * We can safely restart from the first server parameter
3661 * to reprocess them
3662 */
3663 cur_param = st_cur_param;
3664 next_param = st_next_param;
3665 reprocess = 0;
3666 goto reprocess_servers;
3667 }
3668
3669 next_param = cur_param;
3670 }
3671 }
3672
3673 if (total_servers == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003674 si->appctx.ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003675 }
3676 else if (altered_servers == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003677 si->appctx.ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003678 }
3679 else if (altered_servers == total_servers) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003680 si->appctx.ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003681 }
3682 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003683 si->appctx.ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003684 }
3685 out:
3686 return 1;
3687}
3688
3689
3690static int stats_send_http_headers(struct stream_interface *si)
3691{
3692 struct session *s = session_from_task(si->owner);
3693 struct uri_auth *uri = s->be->uri_auth;
3694
3695 chunk_printf(&trash,
3696 "HTTP/1.0 200 OK\r\n"
3697 "Cache-Control: no-cache\r\n"
3698 "Connection: close\r\n"
3699 "Content-Type: %s\r\n",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003700 (si->appctx.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01003701
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003702 if (uri->refresh > 0 && !(si->appctx.ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01003703 chunk_appendf(&trash, "Refresh: %d\r\n",
3704 uri->refresh);
3705
3706 chunk_appendf(&trash, "\r\n");
3707
3708 s->txn.status = 200;
3709 s->logs.tv_request = now;
3710
3711 if (bi_putchk(si->ib, &trash) == -1)
3712 return 0;
3713
3714 return 1;
3715}
3716
3717static int stats_send_http_redirect(struct stream_interface *si)
3718{
3719 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3720 struct session *s = session_from_task(si->owner);
3721 struct uri_auth *uri = s->be->uri_auth;
3722
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003723 /* scope_txt = search pattern + search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003724 scope_txt[0] = 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003725 if (si->appctx.ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003726 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003727 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
3728 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->appctx.ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003729 }
3730
3731 /* We don't want to land on the posted stats page because a refresh will
3732 * repost the data. We don't want this to happen on accident so we redirect
3733 * the browse to the stats page with a GET.
3734 */
3735 chunk_printf(&trash,
3736 "HTTP/1.1 303 See Other\r\n"
3737 "Cache-Control: no-cache\r\n"
3738 "Content-Type: text/plain\r\n"
3739 "Connection: close\r\n"
3740 "Location: %s;st=%s%s%s%s\r\n"
3741 "\r\n",
3742 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003743 ((si->appctx.ctx.stats.st_code > STAT_STATUS_INIT) &&
3744 (si->appctx.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3745 stat_status_codes[si->appctx.ctx.stats.st_code]) ?
3746 stat_status_codes[si->appctx.ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01003747 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003748 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3749 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01003750 scope_txt);
3751
3752 s->txn.status = 303;
3753 s->logs.tv_request = now;
3754
3755 if (bi_putchk(si->ib, &trash) == -1)
3756 return 0;
3757
3758 return 1;
3759}
3760
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003761/* This I/O handler runs as an applet embedded in a stream interface. It is
3762 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003763 * si->appctx.st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003764 * automatically unregisters itself once transfer is complete.
3765 */
3766static void http_stats_io_handler(struct stream_interface *si)
3767{
Willy Tarreau306f8302013-07-08 15:53:06 +02003768 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003769 struct channel *req = si->ob;
3770 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003771
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003772 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3773 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003774
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003775 /* check that the output is not closed */
3776 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003777 si->appctx.st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003778
Willy Tarreau347a35d2013-11-22 17:51:09 +01003779 /* all states are processed in sequence */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003780 if (si->appctx.st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003781 if (stats_send_http_headers(si)) {
3782 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003783 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003784 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003785 si->appctx.st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003786 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003787 }
3788
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003789 if (si->appctx.st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003790 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003791 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003792 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003793
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003794 if (si->appctx.st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003795 if (stats_process_http_post(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003796 si->appctx.st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003797 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003798 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003799 }
3800
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003801 if (si->appctx.st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003802 if (stats_send_http_redirect(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003803 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003804 }
3805
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003806 if (si->appctx.st0 == STAT_HTTP_DONE)
Willy Tarreau347a35d2013-11-22 17:51:09 +01003807 si_shutw(si);
3808
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003809 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3810 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003811
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003812 if (si->appctx.st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01003813 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3814 si_shutr(si);
3815 res->flags |= CF_READ_NULL;
3816 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 }
Willy Tarreau91861262007-10-17 17:06:05 +02003818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 /* update all other flags and resync with the other side */
3820 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003821
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003822 /* we don't want to expire timeouts while we're processing requests */
3823 si->ib->rex = TICK_ETERNITY;
3824 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003825
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826 out:
3827 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3828 /* check that we have released everything then unregister */
3829 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003830 }
3831}
3832
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833
Willy Tarreau909d5172012-11-26 03:04:41 +01003834static inline const char *get_conn_ctrl_name(const struct connection *conn)
3835{
3836 if (!conn->ctrl)
3837 return "NONE";
3838 return conn->ctrl->name;
3839}
3840
3841static inline const char *get_conn_xprt_name(const struct connection *conn)
3842{
3843 static char ptr[17];
3844
3845 if (!conn->xprt)
3846 return "NONE";
3847
3848 if (conn->xprt == &raw_sock)
3849 return "RAW";
3850
3851#ifdef USE_OPENSSL
3852 if (conn->xprt == &ssl_sock)
3853 return "SSL";
3854#endif
3855 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3856 return ptr;
3857}
3858
3859static inline const char *get_conn_data_name(const struct connection *conn)
3860{
3861 static char ptr[17];
3862
3863 if (!conn->data)
3864 return "NONE";
3865
3866 if (conn->data == &sess_conn_cb)
3867 return "SESS";
3868
3869 if (conn->data == &si_conn_cb)
3870 return "STRM";
3871
3872 if (conn->data == &check_conn_cb)
3873 return "CHCK";
3874
3875 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3876 return ptr;
3877}
3878
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003879/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003880 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003881 * 0 if the output buffer is full and it needs to be called again, otherwise
3882 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003883 */
Willy Tarreau76153662012-11-26 01:16:39 +01003884static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003885{
3886 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003887 extern const char *monthname[12];
3888 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003889 struct connection *conn;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003890
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003891 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003892
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003893 if (si->appctx.ctx.sess.section > 0 && si->appctx.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003894 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003895 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3896 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003897 return 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003898 si->appctx.ctx.sess.uid = 0;
3899 si->appctx.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003900 return 1;
3901 }
3902
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003903 switch (si->appctx.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003904 case 0: /* main status of the session */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003905 si->appctx.ctx.sess.uid = sess->uniq_id;
3906 si->appctx.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003907 /* fall through */
3908
3909 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003910 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003912 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003913 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003914 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3915 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003916 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003917 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003918
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003919 conn = objt_conn(sess->si[0].end);
3920 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003921 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003922 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003923 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003924 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003925 break;
3926 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003927 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003928 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003929 default:
3930 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003931 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003932 break;
3933 }
3934
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003935 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003936 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003937 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003939 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003940 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003941 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3942 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3943 sess->listener ? sess->listener->luid : 0);
3944
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003945 if (conn)
3946 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003947
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003948 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003949 case AF_INET:
3950 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003951 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003952 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003953 break;
3954 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003955 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003956 break;
3957 default:
3958 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003959 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003960 break;
3961 }
3962
Willy Tarreau50943332011-09-02 17:33:05 +02003963 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003964 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003965 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003966 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003967 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3968 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003969 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003970
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003971 conn = objt_conn(sess->si[1].end);
3972 if (conn)
3973 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003974
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003975 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003976 case AF_INET:
3977 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003978 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003979 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003980 break;
3981 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003982 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003983 break;
3984 default:
3985 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003986 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003987 break;
3988 }
3989
3990 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003992 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003993 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3994 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003995 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003996 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003997
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003998 if (conn)
3999 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004000
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004001 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004002 case AF_INET:
4003 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004004 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004005 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004006 break;
4007 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004008 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004009 break;
4010 default:
4011 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004012 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004013 break;
4014 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004015
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004016 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004017 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004018 sess->task,
4019 sess->task->state,
4020 sess->task->nice, sess->task->calls,
4021 sess->task->expire ?
4022 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4023 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4024 TICKS_TO_MS(1000)) : "<NEVER>",
4025 task_in_rq(sess->task) ? ", running" : "");
4026
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004027 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004028 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004029 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004031 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004032 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4033 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4034 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4035
4036 chunk_appendf(&trash,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004037 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004038 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004039 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004040 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004041 obj_type_name(sess->si[0].end),
4042 obj_base_ptr(sess->si[0].end),
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004043 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004044 sess->si[0].exp ?
4045 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4046 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4047 TICKS_TO_MS(1000)) : "<NEVER>",
4048 sess->si[0].err_type);
4049
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004050 chunk_appendf(&trash,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004051 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004052 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004053 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004054 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004055 obj_type_name(sess->si[1].end),
4056 obj_base_ptr(sess->si[1].end),
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004057 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004058 sess->si[1].exp ?
4059 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4060 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4061 TICKS_TO_MS(1000)) : "<NEVER>",
4062 sess->si[1].err_type);
4063
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004064 conn = objt_conn(sess->si[0].end);
4065 if (conn) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004066 chunk_appendf(&trash,
4067 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004068 conn,
4069 get_conn_ctrl_name(conn),
4070 get_conn_xprt_name(conn),
4071 get_conn_data_name(conn),
4072 obj_type_name(conn->target),
4073 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004074
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004075 chunk_appendf(&trash,
4076 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004077 conn->flags,
4078 conn->t.sock.fd,
4079 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4080 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4081 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004082 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004083
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004084 conn = objt_conn(sess->si[1].end);
4085 if (conn) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004086 chunk_appendf(&trash,
4087 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004088 conn,
4089 get_conn_ctrl_name(conn),
4090 get_conn_xprt_name(conn),
4091 get_conn_data_name(conn),
4092 obj_type_name(conn->target),
4093 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004094
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004095 chunk_appendf(&trash,
4096 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004097 conn->flags,
4098 conn->t.sock.fd,
4099 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4100 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4101 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004102 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004103
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004104 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004105 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004106 " an_exp=%s",
4107 sess->req,
4108 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004109 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004110 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004111 sess->req->analyse_exp ?
4112 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4113 TICKS_TO_MS(1000)) : "<NEVER>");
4114
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004115 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004116 " rex=%s",
4117 sess->req->rex ?
4118 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4119 TICKS_TO_MS(1000)) : "<NEVER>");
4120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004121 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004122 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004123 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004124 sess->req->wex ?
4125 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4126 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004127 sess->req->buf,
4128 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004129 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004130 sess->txn.req.next, sess->req->buf->i,
4131 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004132
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004133 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004134 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004135 " an_exp=%s",
4136 sess->rep,
4137 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004138 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004139 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004140 sess->rep->analyse_exp ?
4141 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4142 TICKS_TO_MS(1000)) : "<NEVER>");
4143
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004144 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004145 " rex=%s",
4146 sess->rep->rex ?
4147 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4148 TICKS_TO_MS(1000)) : "<NEVER>");
4149
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004150 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004151 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004152 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004153 sess->rep->wex ?
4154 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4155 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004156 sess->rep->buf,
4157 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004158 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004159 sess->txn.rsp.next, sess->rep->buf->i,
4160 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004161
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004162 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004163 return 0;
4164
4165 /* use other states to dump the contents */
4166 }
4167 /* end of dump */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004168 si->appctx.ctx.sess.uid = 0;
4169 si->appctx.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004170 return 1;
4171}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004172
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004173/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004174 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004175 * to be called again, otherwise non-zero. It is designed to be called
4176 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004177 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004178static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004179{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004180 struct connection *conn;
4181
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004182 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004183 /* If we're forced to shut down, we might have to remove our
4184 * reference to the last session being dumped.
4185 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004186 if (si->appctx.st2 == STAT_ST_LIST) {
4187 if (!LIST_ISEMPTY(&si->appctx.ctx.sess.bref.users)) {
4188 LIST_DEL(&si->appctx.ctx.sess.bref.users);
4189 LIST_INIT(&si->appctx.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004190 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004191 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004192 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004193 }
4194
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004195 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004196
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004197 switch (si->appctx.st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004198 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004199 /* the function had not been called yet, let's prepare the
4200 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004201 * pointer to the first in the global list. When a target
4202 * session is being destroyed, it is responsible for updating
4203 * this pointer. We know we have reached the end when this
4204 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004205 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004206 LIST_INIT(&si->appctx.ctx.sess.bref.users);
4207 si->appctx.ctx.sess.bref.ref = sessions.n;
4208 si->appctx.st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004209 /* fall through */
4210
Willy Tarreau295a8372011-03-10 11:25:07 +01004211 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004212 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004213 if (!LIST_ISEMPTY(&si->appctx.ctx.sess.bref.users)) {
4214 LIST_DEL(&si->appctx.ctx.sess.bref.users);
4215 LIST_INIT(&si->appctx.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004216 }
4217
4218 /* and start from where we stopped */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004219 while (si->appctx.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004220 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004221 struct session *curr_sess;
4222
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004223 curr_sess = LIST_ELEM(si->appctx.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004224
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004225 if (si->appctx.ctx.sess.target) {
4226 if (si->appctx.ctx.sess.target != (void *)-1 && si->appctx.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004227 goto next_sess;
4228
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004229 LIST_ADDQ(&curr_sess->back_refs, &si->appctx.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004230 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01004231 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004232 return 0;
4233
4234 /* session dump complete */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004235 LIST_DEL(&si->appctx.ctx.sess.bref.users);
4236 LIST_INIT(&si->appctx.ctx.sess.bref.users);
4237 if (si->appctx.ctx.sess.target != (void *)-1) {
4238 si->appctx.ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01004239 break;
4240 }
4241 else
4242 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004243 }
4244
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004245 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004246 "%p: proto=%s",
4247 curr_sess,
4248 curr_sess->listener->proto->name);
4249
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004250
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004251 conn = objt_conn(curr_sess->si[0].end);
4252 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02004253 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004254 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004255 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004256 " src=%s:%d fe=%s be=%s srv=%s",
4257 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004258 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004259 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004260 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004261 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004262 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004263 break;
4264 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004265 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02004266 " src=unix:%d fe=%s be=%s srv=%s",
4267 curr_sess->listener->luid,
4268 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004269 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004270 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02004271 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004272 break;
4273 }
4274
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004275 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02004276 " ts=%02x age=%s calls=%d",
4277 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01004278 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
4279 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004280
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004281 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004282 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004283 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004284 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004285 curr_sess->req->analysers,
4286 curr_sess->req->rex ?
4287 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
4288 TICKS_TO_MS(1000)) : "");
4289
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004290 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004291 ",wx=%s",
4292 curr_sess->req->wex ?
4293 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
4294 TICKS_TO_MS(1000)) : "");
4295
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004296 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004297 ",ax=%s]",
4298 curr_sess->req->analyse_exp ?
4299 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
4300 TICKS_TO_MS(1000)) : "");
4301
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004302 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004303 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004304 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004305 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004306 curr_sess->rep->analysers,
4307 curr_sess->rep->rex ?
4308 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
4309 TICKS_TO_MS(1000)) : "");
4310
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004311 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004312 ",wx=%s",
4313 curr_sess->rep->wex ?
4314 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
4315 TICKS_TO_MS(1000)) : "");
4316
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004317 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004318 ",ax=%s]",
4319 curr_sess->rep->analyse_exp ?
4320 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
4321 TICKS_TO_MS(1000)) : "");
4322
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004323 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004324 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004325 " s0=[%d,%1xh,fd=%d,ex=%s]",
4326 curr_sess->si[0].state,
4327 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004328 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004329 curr_sess->si[0].exp ?
4330 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4331 TICKS_TO_MS(1000)) : "");
4332
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004333 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004334 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004335 " s1=[%d,%1xh,fd=%d,ex=%s]",
4336 curr_sess->si[1].state,
4337 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004338 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004339 curr_sess->si[1].exp ?
4340 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4341 TICKS_TO_MS(1000)) : "");
4342
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004343 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004344 " exp=%s",
4345 curr_sess->task->expire ?
4346 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4347 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004348 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004349 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004350
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004351 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004353 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004354 /* let's try again later from this session. We add ourselves into
4355 * this session's users so that it can remove us upon termination.
4356 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004357 LIST_ADDQ(&curr_sess->back_refs, &si->appctx.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004358 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004359 }
4360
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004361 next_sess:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004362 si->appctx.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004363 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004364
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004365 if (si->appctx.ctx.sess.target && si->appctx.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004366 /* specified session not found */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004367 if (si->appctx.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004368 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004369 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004370 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004371
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004372 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004373 return 0;
4374
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004375 si->appctx.ctx.sess.target = NULL;
4376 si->appctx.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004377 return 1;
4378 }
4379
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004380 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004381 /* fall through */
4382
4383 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004384 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004385 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004386 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004387}
4388
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004389/* This is called when the stream interface is closed. For instance, upon an
4390 * external abort, we won't call the i/o handler anymore so we may need to
4391 * remove back references to the session currently being dumped.
4392 */
Simon Horman74d88312013-02-12 10:45:50 +09004393static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004394{
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004395 if (si->appctx.st0 == STAT_CLI_O_SESS && si->appctx.st2 == STAT_ST_LIST) {
4396 if (!LIST_ISEMPTY(&si->appctx.ctx.sess.bref.users))
4397 LIST_DEL(&si->appctx.ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004398 }
4399}
4400
Willy Tarreau20e99322013-04-13 09:22:25 +02004401/* This function is used to either dump tables states (when action is set
4402 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02004403 * It returns 0 if the output buffer is full and it needs to be called
4404 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004405 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004406static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004407{
Willy Tarreau306f8302013-07-08 15:53:06 +02004408 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004409 struct ebmb_node *eb;
4410 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004411 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004412 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004413
4414 /*
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004415 * We have 3 possible states in si->appctx.st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01004416 * - STAT_ST_INIT : the first call
4417 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004418 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004419 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004420 * and the entry pointer points to the next entry to be dumped,
4421 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004422 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004423 * data though.
4424 */
4425
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004426 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004427 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004428 if (si->appctx.st2 == STAT_ST_LIST) {
4429 si->appctx.ctx.table.entry->ref_cnt--;
4430 stksess_kill_if_expired(&si->appctx.ctx.table.proxy->table, si->appctx.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004431 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004432 return 1;
4433 }
4434
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004435 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004436
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004437 while (si->appctx.st2 != STAT_ST_FIN) {
4438 switch (si->appctx.st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004439 case STAT_ST_INIT:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004440 si->appctx.ctx.table.proxy = si->appctx.ctx.table.target;
4441 if (!si->appctx.ctx.table.proxy)
4442 si->appctx.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004443
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004444 si->appctx.ctx.table.entry = NULL;
4445 si->appctx.st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004446 break;
4447
Willy Tarreau295a8372011-03-10 11:25:07 +01004448 case STAT_ST_INFO:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004449 if (!si->appctx.ctx.table.proxy ||
4450 (si->appctx.ctx.table.target &&
4451 si->appctx.ctx.table.proxy != si->appctx.ctx.table.target)) {
4452 si->appctx.st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004453 break;
4454 }
4455
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004456 if (si->appctx.ctx.table.proxy->table.size) {
4457 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->appctx.ctx.table.proxy,
4458 si->appctx.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004459 return 0;
4460
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004461 if (si->appctx.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004462 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004463 /* dump entries only if table explicitly requested */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004464 eb = ebmb_first(&si->appctx.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004465 if (eb) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004466 si->appctx.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4467 si->appctx.ctx.table.entry->ref_cnt++;
4468 si->appctx.st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004469 break;
4470 }
4471 }
4472 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004473 si->appctx.ctx.table.proxy = si->appctx.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004474 break;
4475
Willy Tarreau295a8372011-03-10 11:25:07 +01004476 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004477 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004478
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004479 if (si->appctx.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004480 /* we're filtering on some data contents */
4481 void *ptr;
4482 long long data;
4483
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004484 dt = si->appctx.ctx.table.data_type;
4485 ptr = stktable_data_ptr(&si->appctx.ctx.table.proxy->table,
4486 si->appctx.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004487 dt);
4488
4489 data = 0;
4490 switch (stktable_data_types[dt].std_type) {
4491 case STD_T_SINT:
4492 data = stktable_data_cast(ptr, std_t_sint);
4493 break;
4494 case STD_T_UINT:
4495 data = stktable_data_cast(ptr, std_t_uint);
4496 break;
4497 case STD_T_ULL:
4498 data = stktable_data_cast(ptr, std_t_ull);
4499 break;
4500 case STD_T_FRQP:
4501 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004502 si->appctx.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004503 break;
4504 }
4505
4506 /* skip the entry if the data does not match the test and the value */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004507 if ((data < si->appctx.ctx.table.value &&
4508 (si->appctx.ctx.table.data_op == STD_OP_EQ ||
4509 si->appctx.ctx.table.data_op == STD_OP_GT ||
4510 si->appctx.ctx.table.data_op == STD_OP_GE)) ||
4511 (data == si->appctx.ctx.table.value &&
4512 (si->appctx.ctx.table.data_op == STD_OP_NE ||
4513 si->appctx.ctx.table.data_op == STD_OP_GT ||
4514 si->appctx.ctx.table.data_op == STD_OP_LT)) ||
4515 (data > si->appctx.ctx.table.value &&
4516 (si->appctx.ctx.table.data_op == STD_OP_EQ ||
4517 si->appctx.ctx.table.data_op == STD_OP_LT ||
4518 si->appctx.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004519 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004520 }
4521
Simon Hormanc88b8872011-06-15 15:18:49 +09004522 if (show && !skip_entry &&
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004523 !stats_dump_table_entry_to_buffer(&trash, si, si->appctx.ctx.table.proxy,
4524 si->appctx.ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09004525 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004526
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004527 si->appctx.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004528
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004529 eb = ebmb_next(&si->appctx.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004530 if (eb) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004531 struct stksess *old = si->appctx.ctx.table.entry;
4532 si->appctx.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004533 if (show)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004534 stksess_kill_if_expired(&si->appctx.ctx.table.proxy->table, old);
4535 else if (!skip_entry && !si->appctx.ctx.table.entry->ref_cnt)
4536 stksess_kill(&si->appctx.ctx.table.proxy->table, old);
4537 si->appctx.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004538 break;
4539 }
4540
Simon Hormanc88b8872011-06-15 15:18:49 +09004541
4542 if (show)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004543 stksess_kill_if_expired(&si->appctx.ctx.table.proxy->table, si->appctx.ctx.table.entry);
4544 else if (!skip_entry && !si->appctx.ctx.table.entry->ref_cnt)
4545 stksess_kill(&si->appctx.ctx.table.proxy->table, si->appctx.ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09004546
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004547 si->appctx.ctx.table.proxy = si->appctx.ctx.table.proxy->next;
4548 si->appctx.st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004549 break;
4550
Willy Tarreau295a8372011-03-10 11:25:07 +01004551 case STAT_ST_END:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004552 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004553 break;
4554 }
4555 }
4556 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004557}
4558
Willy Tarreaud426a182010-03-05 14:58:26 +01004559/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004560 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4561 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4562 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4563 * lines are respected within the limit of 70 output chars. Lines that are
4564 * continuation of a previous truncated line begin with "+" instead of " "
4565 * after the offset. The new pointer is returned.
4566 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004567static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4568 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004569{
4570 int end;
4571 unsigned char c;
4572
4573 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004574 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004575 return ptr;
4576
Willy Tarreau77804732012-10-29 16:14:26 +01004577 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004578
Willy Tarreaud426a182010-03-05 14:58:26 +01004579 while (ptr < len && ptr < bsize) {
4580 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004581 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004582 if (out->len > end - 2)
4583 break;
4584 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004585 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004586 if (out->len > end - 3)
4587 break;
4588 out->str[out->len++] = '\\';
4589 switch (c) {
4590 case '\t': c = 't'; break;
4591 case '\n': c = 'n'; break;
4592 case '\r': c = 'r'; break;
4593 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004594 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004595 }
4596 out->str[out->len++] = c;
4597 } else {
4598 if (out->len > end - 5)
4599 break;
4600 out->str[out->len++] = '\\';
4601 out->str[out->len++] = 'x';
4602 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4603 out->str[out->len++] = hextab[c & 0xF];
4604 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004605 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004606 /* we had a line break, let's return now */
4607 out->str[out->len++] = '\n';
4608 *line = ptr;
4609 return ptr;
4610 }
4611 }
4612 /* we have an incomplete line, we return it as-is */
4613 out->str[out->len++] = '\n';
4614 return ptr;
4615}
4616
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004617/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004618 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004619 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004620 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004621static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004622{
4623 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004624
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004625 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004626 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004627
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004628 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004629
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004630 if (!si->appctx.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004631 /* the function had not been called yet, let's prepare the
4632 * buffer for a response.
4633 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004634 struct tm tm;
4635
4636 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004637 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004638 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4639 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4640 error_snapshot_id);
4641
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004642 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004643 /* Socket buffer full. Let's try again later from the same point */
4644 return 0;
4645 }
4646
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004647 si->appctx.ctx.errors.px = proxy;
4648 si->appctx.ctx.errors.buf = 0;
4649 si->appctx.ctx.errors.bol = 0;
4650 si->appctx.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004651 }
4652
4653 /* we have two inner loops here, one for the proxy, the other one for
4654 * the buffer.
4655 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004656 while (si->appctx.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004657 struct error_snapshot *es;
4658
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004659 if (si->appctx.ctx.errors.buf == 0)
4660 es = &si->appctx.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004661 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004662 es = &si->appctx.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004663
4664 if (!es->when.tv_sec)
4665 goto next;
4666
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004667 if (si->appctx.ctx.errors.iid >= 0 &&
4668 si->appctx.ctx.errors.px->uuid != si->appctx.ctx.errors.iid &&
4669 es->oe->uuid != si->appctx.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004670 goto next;
4671
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004672 if (si->appctx.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004673 /* just print headers now */
4674
4675 char pn[INET6_ADDRSTRLEN];
4676 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004677 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004678
4679 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004680 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004681 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004682 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004683
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004684 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4685 case AF_INET:
4686 case AF_INET6:
4687 port = get_host_port(&es->src);
4688 break;
4689 default:
4690 port = 0;
4691 }
4692
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004693 switch (si->appctx.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004694 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004695 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004696 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004697 " backend %s (#%d)",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004698 si->appctx.ctx.errors.px->id, si->appctx.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004699 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4700 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004701 break;
4702 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004704 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004705 " frontend %s (#%d)",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004706 si->appctx.ctx.errors.px->id, si->appctx.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004707 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004708 break;
4709 }
4710
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004711 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004712 ", server %s (#%d), event #%u\n"
4713 " src %s:%d, session #%d, session flags 0x%08x\n"
4714 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4715 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4716 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4717 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4718 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4719 es->ev_id,
4720 pn, port, es->sid, es->s_flags,
4721 es->state, es->m_flags, es->t_flags,
4722 es->m_clen, es->m_blen,
4723 es->b_flags, es->b_out, es->b_tot,
4724 es->len, es->b_wrap, es->pos);
4725
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004726 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004727 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004728 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004729 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004730 si->appctx.ctx.errors.ptr = 0;
4731 si->appctx.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004732 }
4733
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004734 if (si->appctx.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004735 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004736 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004737 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004738 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004739 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004740 goto next;
4741 }
4742
4743 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004744 while (si->appctx.ctx.errors.ptr < es->len && si->appctx.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004745 int newptr;
4746 int newline;
4747
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004748 newline = si->appctx.ctx.errors.bol;
4749 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, si->appctx.ctx.errors.ptr);
4750 if (newptr == si->appctx.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004751 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004752
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004753 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004754 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004755 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004756 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004757 si->appctx.ctx.errors.ptr = newptr;
4758 si->appctx.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004759 };
4760 next:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004761 si->appctx.ctx.errors.bol = 0;
4762 si->appctx.ctx.errors.ptr = -1;
4763 si->appctx.ctx.errors.buf++;
4764 if (si->appctx.ctx.errors.buf > 1) {
4765 si->appctx.ctx.errors.buf = 0;
4766 si->appctx.ctx.errors.px = si->appctx.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004767 }
4768 }
4769
4770 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004771 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004772}
4773
Willy Tarreaud5781202012-09-22 19:32:35 +02004774/* parse the "level" argument on the bind lines */
4775static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4776{
4777 if (!*args[cur_arg + 1]) {
4778 memprintf(err, "'%s' : missing level", args[cur_arg]);
4779 return ERR_ALERT | ERR_FATAL;
4780 }
4781
4782 if (!strcmp(args[cur_arg+1], "user"))
4783 conf->level = ACCESS_LVL_USER;
4784 else if (!strcmp(args[cur_arg+1], "operator"))
4785 conf->level = ACCESS_LVL_OPER;
4786 else if (!strcmp(args[cur_arg+1], "admin"))
4787 conf->level = ACCESS_LVL_ADMIN;
4788 else {
4789 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4790 args[cur_arg], args[cur_arg+1]);
4791 return ERR_ALERT | ERR_FATAL;
4792 }
4793
4794 return 0;
4795}
4796
Willy Tarreaub24281b2011-02-13 13:16:36 +01004797struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004798 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004799 .name = "<STATS>", /* used for logging */
4800 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004801 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004802};
4803
Simon Horman9bd2c732011-06-15 15:18:44 +09004804static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004805 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004806 .name = "<CLI>", /* used for logging */
4807 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004808 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004809};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004810
Willy Tarreaudc13c112013-06-21 23:16:39 +02004811static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004812 { CFG_GLOBAL, "stats", stats_parse_global },
4813 { 0, NULL, NULL },
4814}};
4815
Willy Tarreaud5781202012-09-22 19:32:35 +02004816static struct bind_kw_list bind_kws = { "STAT", { }, {
4817 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4818 { NULL, NULL, 0 },
4819}};
4820
Willy Tarreau10522fd2008-07-09 20:12:41 +02004821__attribute__((constructor))
4822static void __dumpstats_module_init(void)
4823{
4824 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004825 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004826}
4827
Willy Tarreau91861262007-10-17 17:06:05 +02004828/*
4829 * Local variables:
4830 * c-indent-level: 8
4831 * c-basic-offset: 8
4832 * End:
4833 */