blob: 35d75c0887c13a473299ecb0b20f589e3d247e88 [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 Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 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>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
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 Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020055#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Willy Tarreau7a0169a2012-11-19 17:13:16 +010059#ifdef USE_OPENSSL
60#include <proto/ssl_sock.h>
61#endif
62
Simon Horman9bd2c732011-06-15 15:18:44 +090063static int stats_dump_raw_to_buffer(struct stream_interface *si);
64static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
65static int stats_dump_sess_to_buffer(struct stream_interface *si);
66static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090067static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090068static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
69static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
70
71static struct si_applet cli_applet;
72
73static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020074 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020075 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020076 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020077 " help : this message\n"
78 " prompt : toggle interactive mode with prompt\n"
79 " quit : disconnect\n"
80 " show info : report information about the running process\n"
81 " show stat : report counters for each proxy and server\n"
82 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010083 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020084 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020085 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020086 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +020087 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010088 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020089 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020090 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020091 " disable : put a server or frontend in maintenance mode\n"
92 " enable : re-enable a server or frontend which is in maintenance mode\n"
93 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020094 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020095
Simon Horman9bd2c732011-06-15 15:18:44 +090096static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020097 "Permission denied\n"
98 "";
99
Willy Tarreau295a8372011-03-10 11:25:07 +0100100/* data transmission states for the stats responses */
101enum {
102 STAT_ST_INIT = 0,
103 STAT_ST_HEAD,
104 STAT_ST_INFO,
105 STAT_ST_LIST,
106 STAT_ST_END,
107 STAT_ST_FIN,
108};
109
110/* data transmission states for the stats responses inside a proxy */
111enum {
112 STAT_PX_ST_INIT = 0,
113 STAT_PX_ST_TH,
114 STAT_PX_ST_FE,
115 STAT_PX_ST_LI,
116 STAT_PX_ST_SV,
117 STAT_PX_ST_BE,
118 STAT_PX_ST_END,
119 STAT_PX_ST_FIN,
120};
121
Cyril Bonté19979e12012-04-04 12:57:21 +0200122extern const char *stat_status_codes[];
123
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200124/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200125 * a new stats socket. It returns a positive value upon success, 0 if the connection
126 * needs to be closed and ignored, or a negative value upon critical failure.
127 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900128static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200129{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200130 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100131 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100132 s->target = s->si[1].conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200133 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100134 s->si[1].applet.st1 = 0;
135 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200136
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200137 tv_zero(&s->logs.tv_request);
138 s->logs.t_queue = 0;
139 s->logs.t_connect = 0;
140 s->logs.t_data = 0;
141 s->logs.t_close = 0;
142 s->logs.bytes_in = s->logs.bytes_out = 0;
143 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
144 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200145
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200146 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200147
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200148 if (s->listener->timeout) {
149 s->req->rto = *s->listener->timeout;
150 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200151 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200152 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200153}
154
Willy Tarreau07e9e642010-08-17 21:48:17 +0200155/* allocate a new stats frontend named <name>, and return it
156 * (or NULL in case of lack of memory).
157 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200158static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200159{
160 struct proxy *fe;
161
162 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
163 if (!fe)
164 return NULL;
165
Willy Tarreau237250c2011-07-29 01:49:03 +0200166 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200167 fe->next = proxy;
168 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200169 fe->last_change = now.tv_sec;
170 fe->id = strdup("GLOBAL");
171 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200172 fe->maxconn = 10; /* default to 10 concurrent connections */
173 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200174 fe->conf.file = strdup(file);
175 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200176 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200177
178 /* the stats frontend is the only one able to assign ID #0 */
179 fe->conf.id.key = fe->uuid = 0;
180 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200181 return fe;
182}
183
Willy Tarreaufbee7132007-10-18 13:53:22 +0200184/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200185 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
186 * error message into the <err> buffer which will be preallocated. The trailing
187 * '\n' must not be written. The function must be called with <args> pointing to
188 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200189 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200190static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200191 struct proxy *defpx, const char *file, int line,
192 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200193{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200194 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200195 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200196
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200197 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200198 int cur_arg;
199
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200200 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200201 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 +0200202 return -1;
203 }
204
Willy Tarreau89a63132009-08-16 17:41:45 +0200205 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200206 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200207 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200208 return -1;
209 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200210 }
211
Willy Tarreau4348fad2012-09-20 16:48:07 +0200212 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200213 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200214
Willy Tarreauc53d4222012-09-20 20:19:28 +0200215 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
216 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
217 file, line, args[0], args[1], err && *err ? *err : "error");
218 return -1;
219 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200220
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200221 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200222 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200223 static int bind_dumped;
224 struct bind_kw *kw;
225
226 kw = bind_find_kw(args[cur_arg]);
227 if (kw) {
228 if (!kw->parse) {
229 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
230 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200231 return -1;
232 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200233
234 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
235 if (err && *err)
236 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
237 else
238 memprintf(err, "'%s %s' : error encountered while processing '%s'",
239 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200240 return -1;
241 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200242
243 cur_arg += 1 + kw->skip;
244 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200245 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200246
247 if (!bind_dumped) {
248 bind_dump_kws(err);
249 indent_msg(err, 4);
250 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200251 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200252
253 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
254 args[0], args[1], args[cur_arg],
255 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
256 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200257 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100258
Willy Tarreauc53d4222012-09-20 20:19:28 +0200259 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
260 l->maxconn = global.stats_fe->maxconn;
261 l->backlog = global.stats_fe->backlog;
262 l->timeout = &global.stats_fe->timeout.client;
263 l->accept = session_accept;
264 l->handler = process_session;
265 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
266 l->nice = -64; /* we want to boost priority for local stats */
267 global.maxsock += l->maxconn;
268 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200270 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100271 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200272 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100273
274 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200275 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100276 return -1;
277 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100279 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200281 return -1;
282 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200283 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200284 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200286 return -1;
287 }
288 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200289 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200291 else if (!strcmp(args[1], "maxconn")) {
292 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200293
294 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200295 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200296 return -1;
297 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200298
299 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200300 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200302 return -1;
303 }
304 }
305 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200307 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
308 int cur_arg = 2;
309 unsigned int set = 0;
310
311 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100312 unsigned int low, high;
313
Willy Tarreau35b7b162012-10-22 23:17:18 +0200314 if (strcmp(args[cur_arg], "all") == 0) {
315 set = 0;
316 break;
317 }
318 else if (strcmp(args[cur_arg], "odd") == 0) {
319 set |= 0x55555555;
320 }
321 else if (strcmp(args[cur_arg], "even") == 0) {
322 set |= 0xAAAAAAAA;
323 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100324 else if (isdigit(*args[cur_arg])) {
325 char *dash = strchr(args[cur_arg], '-');
326
327 low = high = str2uic(args[cur_arg]);
328 if (dash)
329 high = str2uic(dash + 1);
330
331 if (high < low) {
332 unsigned int swap = low;
333 low = high;
334 high = swap;
335 }
336
337 if (low < 1 || high > 32) {
338 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
339 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200340 return -1;
341 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100342
343 while (low <= high)
344 set |= 1 << (low++ - 1);
345 }
346 else {
347 memprintf(err,
348 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
349 args[0], args[1]);
350 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200351 }
352 cur_arg++;
353 }
354 global.stats_fe->bind_proc = set;
355 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200356 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200357 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200358 return -1;
359 }
360 return 0;
361}
362
Simon Horman9bd2c732011-06-15 15:18:44 +0900363static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100364{
Willy Tarreau77804732012-10-29 16:14:26 +0100365 return chunk_appendf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100366 "# pxname,svname,"
367 "qcur,qmax,"
368 "scur,smax,slim,stot,"
369 "bin,bout,"
370 "dreq,dresp,"
371 "ereq,econ,eresp,"
372 "wretr,wredis,"
373 "status,weight,act,bck,"
374 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200375 "pid,iid,sid,throttle,lbtot,tracked,type,"
376 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200377 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100378 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100379 "req_rate,req_rate_max,req_tot,"
380 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100381 "\n");
382}
383
Simon Hormand9366582011-06-15 15:18:45 +0900384/* print a string of text buffer to <out>. The format is :
385 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
386 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
387 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
388 */
389static int dump_text(struct chunk *out, const char *buf, int bsize)
390{
391 unsigned char c;
392 int ptr = 0;
393
394 while (buf[ptr] && ptr < bsize) {
395 c = buf[ptr];
396 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
397 if (out->len > out->size - 1)
398 break;
399 out->str[out->len++] = c;
400 }
401 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
402 if (out->len > out->size - 2)
403 break;
404 out->str[out->len++] = '\\';
405 switch (c) {
406 case ' ': c = ' '; break;
407 case '\t': c = 't'; break;
408 case '\n': c = 'n'; break;
409 case '\r': c = 'r'; break;
410 case '\e': c = 'e'; break;
411 case '\\': c = '\\'; break;
412 }
413 out->str[out->len++] = c;
414 }
415 else {
416 if (out->len > out->size - 4)
417 break;
418 out->str[out->len++] = '\\';
419 out->str[out->len++] = 'x';
420 out->str[out->len++] = hextab[(c >> 4) & 0xF];
421 out->str[out->len++] = hextab[c & 0xF];
422 }
423 ptr++;
424 }
425
426 return ptr;
427}
428
429/* print a buffer in hexa.
430 * Print stopped if <bsize> is reached, or if no more place in the chunk.
431 */
432static int dump_binary(struct chunk *out, const char *buf, int bsize)
433{
434 unsigned char c;
435 int ptr = 0;
436
437 while (ptr < bsize) {
438 c = buf[ptr];
439
440 if (out->len > out->size - 2)
441 break;
442 out->str[out->len++] = hextab[(c >> 4) & 0xF];
443 out->str[out->len++] = hextab[c & 0xF];
444
445 ptr++;
446 }
447 return ptr;
448}
449
450/* Dump the status of a table to a stream interface's
451 * read buffer. It returns 0 if the output buffer is full
452 * and needs to be called again, otherwise non-zero.
453 */
454static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
455 struct proxy *proxy, struct proxy *target)
456{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200457 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900458
Willy Tarreau77804732012-10-29 16:14:26 +0100459 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900460 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
461
462 /* any other information should be dumped here */
463
Willy Tarreau290e63a2012-09-20 18:07:14 +0200464 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100465 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900466
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200467 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900468 return 0;
469
470 return 1;
471}
472
473/* Dump the a table entry to a stream interface's
474 * read buffer. It returns 0 if the output buffer is full
475 * and needs to be called again, otherwise non-zero.
476 */
477static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
478 struct proxy *proxy, struct stksess *entry)
479{
480 int dt;
481
Willy Tarreau77804732012-10-29 16:14:26 +0100482 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900483
484 if (proxy->table.type == STKTABLE_TYPE_IP) {
485 char addr[INET_ADDRSTRLEN];
486 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100487 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900488 }
489 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
490 char addr[INET6_ADDRSTRLEN];
491 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100492 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900493 }
494 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100495 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900496 }
497 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100498 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900499 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
500 }
501 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100502 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900503 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
504 }
505
Willy Tarreau77804732012-10-29 16:14:26 +0100506 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900507
508 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
509 void *ptr;
510
511 if (proxy->table.data_ofs[dt] == 0)
512 continue;
513 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100514 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900515 else
Willy Tarreau77804732012-10-29 16:14:26 +0100516 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900517
518 ptr = stktable_data_ptr(&proxy->table, entry, dt);
519 switch (stktable_data_types[dt].std_type) {
520 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100521 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900522 break;
523 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100524 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900525 break;
526 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100527 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900528 break;
529 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100530 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900531 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
532 proxy->table.data_arg[dt].u));
533 break;
534 }
535 }
Willy Tarreau77804732012-10-29 16:14:26 +0100536 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900537
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200538 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900539 return 0;
540
541 return 1;
542}
543
Willy Tarreaudec98142012-06-06 23:37:08 +0200544static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900545{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200546 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900547 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900548 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900549 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900550 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200551 long long value;
552 int data_type;
553 void *ptr;
554 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900555
Simon Hormand5b9fd92011-06-15 15:18:48 +0900556 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900557
558 if (!*args[4]) {
559 si->applet.ctx.cli.msg = "Key value expected\n";
560 si->applet.st0 = STAT_CLI_PRINT;
561 return;
562 }
563
Simon Hormanc5b89f62011-06-15 15:18:50 +0900564 switch (px->table.type) {
565 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900566 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100567 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900568 break;
569 case STKTABLE_TYPE_IPV6:
570 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100571 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900572 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900573 case STKTABLE_TYPE_INTEGER:
574 {
575 char *endptr;
576 unsigned long val;
577 errno = 0;
578 val = strtoul(args[4], &endptr, 10);
579 if ((errno == ERANGE && val == ULONG_MAX) ||
580 (errno != 0 && val == 0) || endptr == args[4] ||
581 val > 0xffffffff) {
582 si->applet.ctx.cli.msg = "Invalid key\n";
583 si->applet.st0 = STAT_CLI_PRINT;
584 return;
585 }
586 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100587 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900588 break;
589 }
590 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900591 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100592 static_table_key->key = args[4];
593 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900594 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900595 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200596 switch (action) {
597 case STAT_CLI_O_TAB:
598 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
599 break;
600 case STAT_CLI_O_CLR:
601 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
602 break;
603 default:
604 si->applet.ctx.cli.msg = "Unknown action\n";
605 break;
606 }
Simon Horman121f3052011-06-15 15:18:46 +0900607 si->applet.st0 = STAT_CLI_PRINT;
608 return;
609 }
610
611 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200612 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900613 si->applet.ctx.cli.msg = stats_permission_denied_msg;
614 si->applet.st0 = STAT_CLI_PRINT;
615 return;
616 }
617
Willy Tarreau07115412012-10-29 21:56:59 +0100618 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900619
Willy Tarreaudec98142012-06-06 23:37:08 +0200620 switch (action) {
621 case STAT_CLI_O_TAB:
622 if (!ts)
623 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100624 chunk_reset(&trash);
625 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900626 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100627 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900628 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200629
630 case STAT_CLI_O_CLR:
631 if (!ts)
632 return;
633 if (ts->ref_cnt) {
634 /* don't delete an entry which is currently referenced */
635 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
636 si->applet.st0 = STAT_CLI_PRINT;
637 return;
638 }
639 stksess_kill(&px->table, ts);
640 break;
Simon Horman17bce342011-06-15 15:18:47 +0900641
Willy Tarreau654694e2012-06-07 01:03:16 +0200642 case STAT_CLI_O_SET:
643 if (strncmp(args[5], "data.", 5) != 0) {
644 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
645 si->applet.st0 = STAT_CLI_PRINT;
646 return;
647 }
648
649 data_type = stktable_get_data_type(args[5] + 5);
650 if (data_type < 0) {
651 si->applet.ctx.cli.msg = "Unknown data type\n";
652 si->applet.st0 = STAT_CLI_PRINT;
653 return;
654 }
655
656 if (!px->table.data_ofs[data_type]) {
657 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
658 si->applet.st0 = STAT_CLI_PRINT;
659 return;
660 }
661
662 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
663 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
664 si->applet.st0 = STAT_CLI_PRINT;
665 return;
666 }
667
668 if (ts)
669 stktable_touch(&px->table, ts, 1);
670 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100671 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200672 if (!ts) {
673 /* don't delete an entry which is currently referenced */
674 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
675 si->applet.st0 = STAT_CLI_PRINT;
676 return;
677 }
678 stktable_store(&px->table, ts, 1);
679 }
680
681 ptr = stktable_data_ptr(&px->table, ts, data_type);
682 switch (stktable_data_types[data_type].std_type) {
683 case STD_T_SINT:
684 stktable_data_cast(ptr, std_t_sint) = value;
685 break;
686 case STD_T_UINT:
687 stktable_data_cast(ptr, std_t_uint) = value;
688 break;
689 case STD_T_ULL:
690 stktable_data_cast(ptr, std_t_ull) = value;
691 break;
692 case STD_T_FRQP:
693 /* We only reset the previous value so that it slowly fades out */
694 frqp = &stktable_data_cast(ptr, std_t_frqp);
695 frqp->curr_tick = now_ms;
696 frqp->prev_ctr = value;
697 frqp->curr_ctr = 0;
698 break;
699 }
700 break;
701
Willy Tarreaudec98142012-06-06 23:37:08 +0200702 default:
703 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900704 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200705 break;
Simon Horman121f3052011-06-15 15:18:46 +0900706 }
Simon Horman121f3052011-06-15 15:18:46 +0900707}
708
Willy Tarreau654694e2012-06-07 01:03:16 +0200709static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900710{
Willy Tarreau654694e2012-06-07 01:03:16 +0200711 if (action != STAT_CLI_O_TAB) {
712 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
713 si->applet.st0 = STAT_CLI_PRINT;
714 return;
715 }
716
Simon Hormand5b9fd92011-06-15 15:18:48 +0900717 /* condition on stored data value */
718 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
719 if (si->applet.ctx.table.data_type < 0) {
720 si->applet.ctx.cli.msg = "Unknown data type\n";
721 si->applet.st0 = STAT_CLI_PRINT;
722 return;
723 }
724
725 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
726 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
727 si->applet.st0 = STAT_CLI_PRINT;
728 return;
729 }
730
731 si->applet.ctx.table.data_op = get_std_op(args[4]);
732 if (si->applet.ctx.table.data_op < 0) {
733 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
734 si->applet.st0 = STAT_CLI_PRINT;
735 return;
736 }
737
738 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
739 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
740 si->applet.st0 = STAT_CLI_PRINT;
741 return;
742 }
743}
744
Willy Tarreaudec98142012-06-06 23:37:08 +0200745static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900746{
747 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200748 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200749 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900750 si->applet.ctx.table.proxy = NULL;
751 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200752 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900753
754 if (*args[2]) {
755 si->applet.ctx.table.target = find_stktable(args[2]);
756 if (!si->applet.ctx.table.target) {
757 si->applet.ctx.cli.msg = "No such table\n";
758 si->applet.st0 = STAT_CLI_PRINT;
759 return;
760 }
761 }
762 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200763 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900764 goto err_args;
765 return;
766 }
767
768 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200769 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900770 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200771 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900772 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900773 goto err_args;
774
775 return;
776
777err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200778 switch (action) {
779 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900780 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200781 break;
782 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900783 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200784 break;
785 default:
786 si->applet.ctx.cli.msg = "Unknown action\n";
787 break;
788 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900789 si->applet.st0 = STAT_CLI_PRINT;
790}
791
Willy Tarreau532a4502011-09-07 22:37:44 +0200792/* Expects to find a frontend named <arg> and returns it, otherwise displays various
793 * adequate error messages and returns NULL. This function also expects the session
794 * level to be admin.
795 */
796static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
797{
798 struct proxy *px;
799
Willy Tarreau290e63a2012-09-20 18:07:14 +0200800 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200801 si->applet.ctx.cli.msg = stats_permission_denied_msg;
802 si->applet.st0 = STAT_CLI_PRINT;
803 return NULL;
804 }
805
806 if (!*arg) {
807 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
808 si->applet.st0 = STAT_CLI_PRINT;
809 return NULL;
810 }
811
812 px = findproxy(arg, PR_CAP_FE);
813 if (!px) {
814 si->applet.ctx.cli.msg = "No such frontend.\n";
815 si->applet.st0 = STAT_CLI_PRINT;
816 return NULL;
817 }
818 return px;
819}
820
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200821/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
822 * and returns the pointer to the server. Otherwise, display adequate error messages
823 * and returns NULL. This function also expects the session level to be admin. Note:
824 * the <arg> is modified to remove the '/'.
825 */
826static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
827{
828 struct proxy *px;
829 struct server *sv;
830 char *line;
831
Willy Tarreau290e63a2012-09-20 18:07:14 +0200832 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200833 si->applet.ctx.cli.msg = stats_permission_denied_msg;
834 si->applet.st0 = STAT_CLI_PRINT;
835 return NULL;
836 }
837
838 /* split "backend/server" and make <line> point to server */
839 for (line = arg; *line; line++)
840 if (*line == '/') {
841 *line++ = '\0';
842 break;
843 }
844
845 if (!*line || !*arg) {
846 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
847 si->applet.st0 = STAT_CLI_PRINT;
848 return NULL;
849 }
850
851 if (!get_backend_server(arg, line, &px, &sv)) {
852 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
853 si->applet.st0 = STAT_CLI_PRINT;
854 return NULL;
855 }
856
857 if (px->state == PR_STSTOPPED) {
858 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
859 si->applet.st0 = STAT_CLI_PRINT;
860 return NULL;
861 }
862
863 return sv;
864}
865
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200866/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200867 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100868 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200869 * designating the function which will have to process the request, which can
870 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200871 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900872static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200873{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200874 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200875 char *args[MAX_STATS_ARGS + 1];
876 int arg;
877
878 while (isspace((unsigned char)*line))
879 line++;
880
881 arg = 0;
882 args[arg] = line;
883
884 while (*line && arg < MAX_STATS_ARGS) {
885 if (isspace((unsigned char)*line)) {
886 *line++ = '\0';
887
888 while (isspace((unsigned char)*line))
889 line++;
890
891 args[++arg] = line;
892 continue;
893 }
894
895 line++;
896 }
897
898 while (++arg <= MAX_STATS_ARGS)
899 args[arg] = line;
900
Willy Tarreau295a8372011-03-10 11:25:07 +0100901 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200902 if (strcmp(args[0], "show") == 0) {
903 if (strcmp(args[1], "stat") == 0) {
904 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100905 si->applet.ctx.stats.flags |= STAT_BOUND;
906 si->applet.ctx.stats.iid = atoi(args[2]);
907 si->applet.ctx.stats.type = atoi(args[3]);
908 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200909 }
910
Willy Tarreau295a8372011-03-10 11:25:07 +0100911 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
912 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200913 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100914 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200915 }
916 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100917 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
918 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200919 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100920 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200921 }
922 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200923 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200924 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100925 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100926 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200927 return 1;
928 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100929 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100930 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100931 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100932 si->applet.ctx.sess.target = NULL;
933 si->applet.ctx.sess.section = 0; /* start with session status */
934 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100935 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200936 }
937 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200938 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100939 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100940 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200941 return 1;
942 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200943 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100944 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200945 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100946 si->applet.ctx.errors.iid = -1;
947 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200948 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100949 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200950 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200951 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200952 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200953 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700954 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200955 return 0;
956 }
957 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200958 else if (strcmp(args[0], "clear") == 0) {
959 if (strcmp(args[1], "counters") == 0) {
960 struct proxy *px;
961 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200962 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200963 int clrall = 0;
964
965 if (strcmp(args[2], "all") == 0)
966 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200967
Willy Tarreau6162db22009-10-10 17:13:00 +0200968 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200969 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
970 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100971 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100972 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200973 return 1;
974 }
975
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200976 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100977 if (clrall) {
978 memset(&px->be_counters, 0, sizeof(px->be_counters));
979 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
980 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200981 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100982 px->be_counters.conn_max = 0;
983 px->be_counters.p.http.rps_max = 0;
984 px->be_counters.sps_max = 0;
985 px->be_counters.cps_max = 0;
986 px->be_counters.nbpend_max = 0;
987
988 px->fe_counters.conn_max = 0;
989 px->fe_counters.p.http.rps_max = 0;
990 px->fe_counters.sps_max = 0;
991 px->fe_counters.cps_max = 0;
992 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200993 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200994
995 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200996 if (clrall)
997 memset(&sv->counters, 0, sizeof(sv->counters));
998 else {
999 sv->counters.cur_sess_max = 0;
1000 sv->counters.nbpend_max = 0;
1001 sv->counters.sps_max = 0;
1002 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001003
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001004 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001005 if (li->counters) {
1006 if (clrall)
1007 memset(li->counters, 0, sizeof(*li->counters));
1008 else
1009 li->counters->conn_max = 0;
1010 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001011 }
1012
Willy Tarreau81c25d02011-09-07 15:17:21 +02001013 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001014 return 1;
1015 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001016 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001017 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001018 /* end of processing */
1019 return 1;
1020 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001021 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001022 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001023 return 0;
1024 }
1025 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001026 else if (strcmp(args[0], "get") == 0) {
1027 if (strcmp(args[1], "weight") == 0) {
1028 struct proxy *px;
1029 struct server *sv;
1030
1031 /* split "backend/server" and make <line> point to server */
1032 for (line = args[2]; *line; line++)
1033 if (*line == '/') {
1034 *line++ = '\0';
1035 break;
1036 }
1037
1038 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001039 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001040 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001041 return 1;
1042 }
1043
1044 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001045 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001046 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001047 return 1;
1048 }
1049
1050 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001051 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1052 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001053 return 1;
1054 }
1055 else { /* not "get weight" */
1056 return 0;
1057 }
1058 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001059 else if (strcmp(args[0], "set") == 0) {
1060 if (strcmp(args[1], "weight") == 0) {
1061 struct proxy *px;
1062 struct server *sv;
1063 int w;
1064
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001065 sv = expect_server_admin(s, si, args[2]);
1066 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001067 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001068 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001069
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001070 /* if the weight is terminated with '%', it is set relative to
1071 * the initial weight, otherwise it is absolute.
1072 */
1073 if (!*args[3]) {
1074 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001075 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001076 return 1;
1077 }
1078
Willy Tarreau4483d432009-10-10 19:30:08 +02001079 w = atoi(args[3]);
1080 if (strchr(args[3], '%') != NULL) {
1081 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001082 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001083 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001084 return 1;
1085 }
1086 w = sv->iweight * w / 100;
1087 }
1088 else {
1089 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001090 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001091 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001092 return 1;
1093 }
1094 }
1095
1096 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001097 si->applet.ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001098 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001099 return 1;
1100 }
1101
1102 sv->uweight = w;
1103
1104 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1105 /* we must take care of not pushing the server to full throttle during slow starts */
1106 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1107 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1108 else
1109 sv->eweight = BE_WEIGHT_SCALE;
1110 sv->eweight *= sv->uweight;
1111 } else {
1112 sv->eweight = sv->uweight;
1113 }
1114
1115 /* static LB algorithms are a bit harder to update */
1116 if (px->lbprm.update_server_eweight)
1117 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001118 else if (sv->eweight) {
1119 if (px->lbprm.set_server_status_up)
1120 px->lbprm.set_server_status_up(sv);
1121 }
1122 else {
1123 if (px->lbprm.set_server_status_down)
1124 px->lbprm.set_server_status_down(sv);
1125 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001126
1127 return 1;
1128 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001129 else if (strcmp(args[1], "timeout") == 0) {
1130 if (strcmp(args[2], "cli") == 0) {
1131 unsigned timeout;
1132 const char *res;
1133
1134 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001135 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001136 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001137 return 1;
1138 }
1139
1140 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1141 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001142 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001143 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001144 return 1;
1145 }
1146
1147 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1148 return 1;
1149 }
1150 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001151 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001152 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001153 return 1;
1154 }
1155 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001156 else if (strcmp(args[1], "maxconn") == 0) {
1157 if (strcmp(args[2], "frontend") == 0) {
1158 struct proxy *px;
1159 struct listener *l;
1160 int v;
1161
Willy Tarreau532a4502011-09-07 22:37:44 +02001162 px = expect_frontend_admin(s, si, args[3]);
1163 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001164 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001165
1166 if (!*args[4]) {
1167 si->applet.ctx.cli.msg = "Integer value expected.\n";
1168 si->applet.st0 = STAT_CLI_PRINT;
1169 return 1;
1170 }
1171
1172 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001173 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001174 si->applet.ctx.cli.msg = "Value out of range.\n";
1175 si->applet.st0 = STAT_CLI_PRINT;
1176 return 1;
1177 }
1178
1179 /* OK, the value is fine, so we assign it to the proxy and to all of
1180 * its listeners. The blocked ones will be dequeued.
1181 */
1182 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001183 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001184 l->maxconn = v;
1185 if (l->state == LI_FULL)
1186 resume_listener(l);
1187 }
1188
1189 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1190 dequeue_all_listeners(&s->fe->listener_queue);
1191
1192 return 1;
1193 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001194 else if (strcmp(args[2], "global") == 0) {
1195 int v;
1196
Willy Tarreau290e63a2012-09-20 18:07:14 +02001197 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001198 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1199 si->applet.st0 = STAT_CLI_PRINT;
1200 return 1;
1201 }
1202
1203 if (!*args[3]) {
1204 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1205 si->applet.st0 = STAT_CLI_PRINT;
1206 return 1;
1207 }
1208
1209 v = atoi(args[3]);
1210 if (v > global.hardmaxconn) {
1211 si->applet.ctx.cli.msg = "Value out of range.\n";
1212 si->applet.st0 = STAT_CLI_PRINT;
1213 return 1;
1214 }
1215
1216 /* check for unlimited values */
1217 if (v <= 0)
1218 v = global.hardmaxconn;
1219
1220 global.maxconn = v;
1221
1222 /* Dequeues all of the listeners waiting for a resource */
1223 if (!LIST_ISEMPTY(&global_listener_queue))
1224 dequeue_all_listeners(&global_listener_queue);
1225
1226 return 1;
1227 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001228 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001229 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001230 si->applet.st0 = STAT_CLI_PRINT;
1231 return 1;
1232 }
1233 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001234 else if (strcmp(args[1], "rate-limit") == 0) {
1235 if (strcmp(args[2], "connections") == 0) {
1236 if (strcmp(args[3], "global") == 0) {
1237 int v;
1238
Willy Tarreau290e63a2012-09-20 18:07:14 +02001239 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001240 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1241 si->applet.st0 = STAT_CLI_PRINT;
1242 return 1;
1243 }
1244
1245 if (!*args[4]) {
1246 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1247 si->applet.st0 = STAT_CLI_PRINT;
1248 return 1;
1249 }
1250
1251 v = atoi(args[4]);
1252 if (v < 0) {
1253 si->applet.ctx.cli.msg = "Value out of range.\n";
1254 si->applet.st0 = STAT_CLI_PRINT;
1255 return 1;
1256 }
1257
1258 global.cps_lim = v;
1259
1260 /* Dequeues all of the listeners waiting for a resource */
1261 if (!LIST_ISEMPTY(&global_listener_queue))
1262 dequeue_all_listeners(&global_listener_queue);
1263
1264 return 1;
1265 }
1266 else {
1267 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1268 si->applet.st0 = STAT_CLI_PRINT;
1269 return 1;
1270 }
1271 }
William Lallemandd85f9172012-11-09 17:05:39 +01001272 else if (strcmp(args[2], "http-compression") == 0) {
1273 if (strcmp(args[3], "global") == 0) {
1274 int v;
1275
1276 v = atoi(args[4]);
1277 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1278 }
1279 else {
1280 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1281 si->applet.st0 = STAT_CLI_PRINT;
1282 return 1;
1283 }
1284 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001285 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001286 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001287 si->applet.st0 = STAT_CLI_PRINT;
1288 return 1;
1289 }
1290 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001291 else if (strcmp(args[1], "table") == 0) {
1292 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1293 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001294 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001295 return 0;
1296 }
1297 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001298 else if (strcmp(args[0], "enable") == 0) {
1299 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001300 struct server *sv;
1301
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001302 sv = expect_server_admin(s, si, args[2]);
1303 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001304 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001305
Cyril Bontécd19e512010-01-31 22:34:03 +01001306 if (sv->state & SRV_MAINTAIN) {
1307 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001308 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001309 /* If this server tracks the status of another one,
1310 * we must restore the good status.
1311 */
Willy Tarreau44267702011-10-28 15:35:33 +02001312 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001313 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001314 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001315 } else {
1316 sv->state &= ~SRV_MAINTAIN;
1317 set_server_down(sv);
1318 }
1319 } else {
1320 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001321 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001322 }
1323 }
1324
Willy Tarreau532a4502011-09-07 22:37:44 +02001325 return 1;
1326 }
1327 else if (strcmp(args[1], "frontend") == 0) {
1328 struct proxy *px;
1329
1330 px = expect_frontend_admin(s, si, args[2]);
1331 if (!px)
1332 return 1;
1333
1334 if (px->state == PR_STSTOPPED) {
1335 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1336 si->applet.st0 = STAT_CLI_PRINT;
1337 return 1;
1338 }
1339
1340 if (px->state != PR_STPAUSED) {
1341 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1342 si->applet.st0 = STAT_CLI_PRINT;
1343 return 1;
1344 }
1345
1346 if (!resume_proxy(px)) {
1347 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1348 si->applet.st0 = STAT_CLI_PRINT;
1349 return 1;
1350 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001351 return 1;
1352 }
1353 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001354 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1355 si->applet.st0 = STAT_CLI_PRINT;
1356 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001357 }
1358 }
1359 else if (strcmp(args[0], "disable") == 0) {
1360 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001361 struct server *sv;
1362
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001363 sv = expect_server_admin(s, si, args[2]);
1364 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001365 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001366
Cyril Bontécd19e512010-01-31 22:34:03 +01001367 if (! (sv->state & SRV_MAINTAIN)) {
1368 /* Not already in maintenance, we can change the server state */
1369 sv->state |= SRV_MAINTAIN;
1370 set_server_down(sv);
1371 }
1372
Willy Tarreau532a4502011-09-07 22:37:44 +02001373 return 1;
1374 }
1375 else if (strcmp(args[1], "frontend") == 0) {
1376 struct proxy *px;
1377
1378 px = expect_frontend_admin(s, si, args[2]);
1379 if (!px)
1380 return 1;
1381
1382 if (px->state == PR_STSTOPPED) {
1383 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1384 si->applet.st0 = STAT_CLI_PRINT;
1385 return 1;
1386 }
1387
1388 if (px->state == PR_STPAUSED) {
1389 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1390 si->applet.st0 = STAT_CLI_PRINT;
1391 return 1;
1392 }
1393
1394 if (!pause_proxy(px)) {
1395 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1396 si->applet.st0 = STAT_CLI_PRINT;
1397 return 1;
1398 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001399 return 1;
1400 }
1401 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001402 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1403 si->applet.st0 = STAT_CLI_PRINT;
1404 return 1;
1405 }
1406 }
1407 else if (strcmp(args[0], "shutdown") == 0) {
1408 if (strcmp(args[1], "frontend") == 0) {
1409 struct proxy *px;
1410
1411 px = expect_frontend_admin(s, si, args[2]);
1412 if (!px)
1413 return 1;
1414
1415 if (px->state == PR_STSTOPPED) {
1416 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1417 si->applet.st0 = STAT_CLI_PRINT;
1418 return 1;
1419 }
1420
1421 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1422 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1423 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1424 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1425 stop_proxy(px);
1426 return 1;
1427 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001428 else if (strcmp(args[1], "session") == 0) {
1429 struct session *sess, *ptr;
1430
Willy Tarreau290e63a2012-09-20 18:07:14 +02001431 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001432 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1433 si->applet.st0 = STAT_CLI_PRINT;
1434 return 1;
1435 }
1436
1437 if (!*args[2]) {
1438 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1439 si->applet.st0 = STAT_CLI_PRINT;
1440 return 1;
1441 }
1442
1443 ptr = (void *)strtoul(args[2], NULL, 0);
1444
1445 /* first, look for the requested session in the session table */
1446 list_for_each_entry(sess, &sessions, list) {
1447 if (sess == ptr)
1448 break;
1449 }
1450
1451 /* do we have the session ? */
1452 if (sess != ptr) {
1453 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1454 si->applet.st0 = STAT_CLI_PRINT;
1455 return 1;
1456 }
1457
1458 session_shutdown(sess, SN_ERR_KILLED);
1459 return 1;
1460 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001461 else if (strcmp(args[1], "sessions") == 0) {
1462 if (strcmp(args[2], "server") == 0) {
1463 struct server *sv;
1464 struct session *sess, *sess_bck;
1465
1466 sv = expect_server_admin(s, si, args[3]);
1467 if (!sv)
1468 return 1;
1469
1470 /* kill all the session that are on this server */
1471 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1472 if (sess->srv_conn == sv)
1473 session_shutdown(sess, SN_ERR_KILLED);
1474
1475 return 1;
1476 }
1477 else {
1478 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1479 si->applet.st0 = STAT_CLI_PRINT;
1480 return 1;
1481 }
1482 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001483 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001484 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001485 si->applet.st0 = STAT_CLI_PRINT;
1486 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001487 }
1488 }
1489 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001490 return 0;
1491 }
1492 return 1;
1493}
1494
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001496 * used to processes I/O from/to the stats unix socket. The system relies on a
1497 * state machine handling requests and various responses. We read a request,
1498 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001499 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1500 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001501 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001502 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001503static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001504{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001505 struct channel *req = si->ob;
1506 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001507 int reql;
1508 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001509
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001510 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1511 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001512
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001514 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001515 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001516 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001517 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001518 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001519 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001520 /* Let's close for real now. We just close the request
1521 * side, the conditions below will complete if needed.
1522 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001523 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001524 break;
1525 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001526 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001527 /* ensure we have some output room left in the event we
1528 * would want to return some info right after parsing.
1529 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001530 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001531 break;
1532
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001533 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001534 if (reql <= 0) { /* closed or EOL not found */
1535 if (reql == 0)
1536 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001537 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 continue;
1539 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 /* seek for a possible semi-colon. If we find one, we
1542 * replace it with an LF and skip only this part.
1543 */
1544 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001545 if (trash.str[len] == ';') {
1546 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001547 reql = len + 1;
1548 break;
1549 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001550
Willy Tarreau816fc222009-10-04 07:36:58 +02001551 /* now it is time to check that we have a full line,
1552 * remove the trailing \n and possibly \r, then cut the
1553 * line.
1554 */
1555 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001556 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001557 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001558 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001559 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001560
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001561 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001562 len--;
1563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001564 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001565
Willy Tarreaubc4af052011-02-13 13:25:14 +01001566 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001567 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001568 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001569 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001570 continue;
1571 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001572 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001573 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001574 else if (strcmp(trash.str, "help") == 0 ||
1575 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001576 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001577 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001578 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001579 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001580 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001581 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001582 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001583 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001584 /* if prompt is disabled, print help on empty lines,
1585 * so that the user at least knows how to enable
1586 * prompt and find help.
1587 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001588 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001589 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001590 }
1591
1592 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001593 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001594 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001595 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001596 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001597 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001598 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001599 continue;
1600 }
1601
Willy Tarreaubc4af052011-02-13 13:25:14 +01001602 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001603 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001604 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001605 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001606 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001607 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001608 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001609 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001610 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001611 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001612 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001613 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001614 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001615 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001616 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001617 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001618 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001619 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001620 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001621 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001622 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001623 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001624 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001625 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001626 break;
1627 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001628
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001629 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001630 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001631 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001632 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001633 }
1634
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001635 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001636 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001637 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001638
1639 /* Now we close the output if one of the writers did so,
1640 * or if we're not in interactive mode and the request
1641 * buffer is empty. This still allows pipelined requests
1642 * to be sent in non-interactive mode.
1643 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001644 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001645 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001646 continue;
1647 }
1648
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001649 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001650 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001651 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001652 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001653
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001654 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001655 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1656 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001657 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001658 * and nothing more to consume. This is comparable to a broken pipe, so
1659 * we forward the close to the request side so that it flows upstream to
1660 * the client.
1661 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001662 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001663 }
1664
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001665 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001666 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1667 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1668 /* We have no more processing to do, and nothing more to send, and
1669 * the client side has closed. So we'll forward this state downstream
1670 * on the response buffer.
1671 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001672 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001673 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001674 }
1675
1676 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001677 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001678
1679 /* we don't want to expire timeouts while we're processing requests */
1680 si->ib->rex = TICK_ETERNITY;
1681 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001682
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001683 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001684 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 +02001685 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001686 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 +02001687
1688 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1689 /* check that we have released everything then unregister */
1690 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001691 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001692}
1693
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001694/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001695 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001696 * It returns 0 as long as it does not complete, non-zero upon completion.
1697 * Some states are not used but it makes the code more similar to other
1698 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001699 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001700static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001701{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001702 struct proxy *px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001703 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001704
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001705 chunk_reset(&trash);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001706
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001707 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001708 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001709 /* the function had not been called yet */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001710 si->conn->xprt_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001711 /* fall through */
1712
Willy Tarreau295a8372011-03-10 11:25:07 +01001713 case STAT_ST_HEAD:
1714 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001715 print_csv_header(&trash);
1716 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001717 return 0;
1718 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001719
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001720 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001721 /* fall through */
1722
Willy Tarreau295a8372011-03-10 11:25:07 +01001723 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001724 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001725 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001726 chunk_appendf(&trash,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001727 "Name: " PRODUCT_NAME "\n"
1728 "Version: " HAPROXY_VERSION "\n"
1729 "Release_date: " HAPROXY_DATE "\n"
1730 "Nbproc: %d\n"
1731 "Process_num: %d\n"
1732 "Pid: %d\n"
1733 "Uptime: %dd %dh%02dm%02ds\n"
1734 "Uptime_sec: %d\n"
1735 "Memmax_MB: %d\n"
1736 "Ulimit-n: %d\n"
1737 "Maxsock: %d\n"
1738 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001739 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001740 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001741 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001742 "PipesUsed: %d\n"
1743 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001744 "ConnRate: %d\n"
1745 "ConnRateLimit: %d\n"
1746 "MaxConnRate: %d\n"
William Lallemandd85f9172012-11-09 17:05:39 +01001747 "CompressBpsIn: %u\n"
1748 "CompressBpsOut: %u\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001749 "Tasks: %d\n"
1750 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001751 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001752 "node: %s\n"
1753 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001754 "",
1755 global.nbproc,
1756 relative_pid,
1757 pid,
1758 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1759 up,
1760 global.rlimit_memmax,
1761 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001762 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001763 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001764 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
William Lallemandd85f9172012-11-09 17:05:39 +01001765 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
Willy Tarreau45a12512011-09-10 16:56:42 +02001766 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001767 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001768 );
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001769 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001770 return 0;
1771 }
1772
Willy Tarreau295a8372011-03-10 11:25:07 +01001773 si->applet.ctx.stats.px = proxy;
1774 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1775 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001776 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001777 /* fall through */
1778
Willy Tarreau295a8372011-03-10 11:25:07 +01001779 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001780 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001781 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1782 while (si->applet.ctx.stats.px) {
1783 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02001784 /* skip the disabled proxies, global frontend and non-networked ones */
1785 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001786 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001787 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001788 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001789 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001790
Willy Tarreau295a8372011-03-10 11:25:07 +01001791 si->applet.ctx.stats.px = px->next;
1792 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001793 }
1794 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001795 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001796
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001797 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001798 /* fall through */
1799
Willy Tarreau295a8372011-03-10 11:25:07 +01001800 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001801 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001802 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001803
Willy Tarreau295a8372011-03-10 11:25:07 +01001804 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001805 return 1;
1806
1807 default:
1808 /* unknown state ! */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001809 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001810 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001811 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001812}
1813
1814
Cyril Bonté70be45d2010-10-12 00:14:35 +02001815/* We don't want to land on the posted stats page because a refresh will
1816 * repost the data. We don't want this to happen on accident so we redirect
1817 * the browse to the stats page with a GET.
1818 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001819static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001820{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001821 struct session *s = si->conn->xprt_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001823 chunk_reset(&trash);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001824
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001825 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001826 case STAT_ST_INIT:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001827 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02001828 "HTTP/1.0 303 See Other\r\n"
1829 "Cache-Control: no-cache\r\n"
1830 "Content-Type: text/plain\r\n"
1831 "Connection: close\r\n"
1832 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001833 uri->uri_prefix,
1834 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1835 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1836 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1837 stat_status_codes[si->applet.ctx.stats.st_code] :
1838 stat_status_codes[STAT_STATUS_UNKN]);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001839 chunk_appendf(&trash, "\r\n\r\n");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001840
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001841 if (bi_putchk(si->ib, &trash) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001842 return 0;
1843
1844 s->txn.status = 303;
1845
1846 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1847 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1848 if (!(s->flags & SN_FINST_MASK))
1849 s->flags |= SN_FINST_R;
1850
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001851 si->conn->xprt_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001852 return 1;
1853 }
1854 return 1;
1855}
1856
1857
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001858/* This I/O handler runs as an applet embedded in a stream interface. It is
1859 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001860 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001861 * automatically unregisters itself once transfer is complete.
1862 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001863static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001864{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001865 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001866 struct channel *req = si->ob;
1867 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001868
1869 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1870 goto out;
1871
1872 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001873 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001874 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001875
Willy Tarreaubc4af052011-02-13 13:25:14 +01001876 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001877 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001878 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001879 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001880 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001881 }
1882 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001883 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001884 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001885 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001886 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001887 }
1888 }
1889
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001890 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001891 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001892
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001893 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001894 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001895 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001896 }
1897
1898 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001899 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001900
1901 /* we don't want to expire timeouts while we're processing requests */
1902 si->ib->rex = TICK_ETERNITY;
1903 si->ob->wex = TICK_ETERNITY;
1904
1905 out:
1906 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1907 /* check that we have released everything then unregister */
1908 stream_int_unregister_handler(si);
1909 }
1910}
1911
1912
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001913/* This function dumps statistics in HTTP format onto the stream interface's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001914 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001915 * properly set. It returns 0 if it had to stop writing data and an I/O is
1916 * needed, 1 if the dump is finished and the session must be closed, or -1
1917 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001918 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001919static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001920{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001921 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001922 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001923 struct proxy *px;
Willy Tarreau91861262007-10-17 17:06:05 +02001924 unsigned int up;
1925
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001926 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02001927
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001928 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001929 case STAT_ST_INIT:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001930 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02001931 "HTTP/1.0 200 OK\r\n"
1932 "Cache-Control: no-cache\r\n"
1933 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001934 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001935 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001936
Willy Tarreau295a8372011-03-10 11:25:07 +01001937 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001938 chunk_appendf(&trash, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001939 uri->refresh);
1940
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001941 chunk_appendf(&trash, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001942
1943 s->txn.status = 200;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001944 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001945 return 0;
1946
Willy Tarreau91861262007-10-17 17:06:05 +02001947 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1948 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1949 if (!(s->flags & SN_FINST_MASK))
1950 s->flags |= SN_FINST_R;
1951
1952 if (s->txn.meth == HTTP_METH_HEAD) {
1953 /* that's all we return in case of HEAD request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001954 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001955 return 1;
1956 }
1957
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001958 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001959 /* fall through */
1960
Willy Tarreau295a8372011-03-10 11:25:07 +01001961 case STAT_ST_HEAD:
1962 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001963 /* WARNING! This must fit in the first buffer !!! */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001964 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001965 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1966 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001967 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001968 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1969 "<style type=\"text/css\"><!--\n"
1970 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001971 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001972 " font-size: 12px;"
1973 " font-weight: normal;"
1974 " color: black;"
1975 " background: white;"
1976 "}\n"
1977 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001978 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001979 "}\n"
1980 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001981 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001982 " margin-bottom: 0.5em;"
1983 "}\n"
1984 "h2 {"
1985 " font-family: helvetica, arial;"
1986 " font-size: x-large;"
1987 " font-weight: bold;"
1988 " font-style: italic;"
1989 " color: #6020a0;"
1990 " margin-top: 0em;"
1991 " margin-bottom: 0em;"
1992 "}\n"
1993 "h3 {"
1994 " font-family: helvetica, arial;"
1995 " font-size: 16px;"
1996 " font-weight: bold;"
1997 " color: #b00040;"
1998 " background: #e8e8d0;"
1999 " margin-top: 0em;"
2000 " margin-bottom: 0em;"
2001 "}\n"
2002 "li {"
2003 " margin-top: 0.25em;"
2004 " margin-right: 2em;"
2005 "}\n"
2006 ".hr {margin-top: 0.25em;"
2007 " border-color: black;"
2008 " border-bottom-style: solid;"
2009 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002010 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002011 ".total {background: #20D0D0;color: #ffff80;}\n"
2012 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002013 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002014 ".backend {background: #e8e8d0;}\n"
2015 ".active0 {background: #ff9090;}\n"
2016 ".active1 {background: #ffd020;}\n"
2017 ".active2 {background: #ffffa0;}\n"
2018 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002019 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
2020 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
2021 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002022 ".backup0 {background: #ff9090;}\n"
2023 ".backup1 {background: #ff80ff;}\n"
2024 ".backup2 {background: #c060ff;}\n"
2025 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002026 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
2027 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
2028 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01002029 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02002030 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002031 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002032 "a.px:link {color: #ffff40; text-decoration: none;}"
2033 "a.px:visited {color: #ffff40; text-decoration: none;}"
2034 "a.px:hover {color: #ffffff; text-decoration: none;}"
2035 "a.lfsb:link {color: #000000; text-decoration: none;}"
2036 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2037 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2038 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002039 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002040 "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"
2041 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002042 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002043 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02002044 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002045 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002046 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002047 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2048 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2049 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002050 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002051 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002052 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002053 (uri->flags&ST_SHNODE) ? " on " : "",
2054 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002055 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002056 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002057 print_csv_header(&trash);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002058 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002059 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002060 return 0;
2061
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002062 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002063 /* fall through */
2064
Willy Tarreau295a8372011-03-10 11:25:07 +01002065 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002066 up = (now.tv_sec - start_date.tv_sec);
2067
2068 /* WARNING! this has to fit the first packet too.
2069 * We are around 3.5 kB, add adding entries will
2070 * become tricky if we want to support 4kB buffers !
2071 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002072 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002073 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002074 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2075 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002076 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002077 "<hr width=\"100%%\" class=\"hr\">\n"
2078 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002079 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002080 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002081 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002082 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2083 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002084 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002085 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002086 "</td><td align=\"center\" nowrap>\n"
2087 "<table class=\"lgd\"><tr>\n"
2088 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2089 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2090 "</tr><tr>\n"
2091 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2092 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2093 "</tr><tr>\n"
2094 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2095 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2096 "</tr><tr>\n"
2097 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002098 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002099 "</tr><tr>\n"
2100 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002101 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002102 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002103 "</td>"
2104 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2105 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2106 "",
2107 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002108 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2109 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2110 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002111 up / 86400, (up % 86400) / 3600,
2112 (up % 3600) / 60, (up % 60),
2113 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2114 global.rlimit_memmax ? " MB" : "",
2115 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002116 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002117 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002118 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002119 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002120
Willy Tarreau295a8372011-03-10 11:25:07 +01002121 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002122 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002123 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2124 uri->uri_prefix,
2125 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002126 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002127 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002128 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002129 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2130 uri->uri_prefix,
2131 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002132 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002133
Willy Tarreau55bb8452007-10-17 18:44:57 +02002134 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002135 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002136 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002137 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2138 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002139 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002140 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002141 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002142 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002143 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2144 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002145 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002146 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002147 }
Willy Tarreau91861262007-10-17 17:06:05 +02002148
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002149 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002150 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2151 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002152 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2153 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002154
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002155 chunk_appendf(&trash,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002156 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2157 uri->uri_prefix,
2158 (uri->refresh > 0) ? ";norefresh" : "");
2159
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002160 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002161 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002162 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002163 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002164 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2165 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2166 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2167 "</ul>"
2168 "</td>"
2169 "</tr></table>\n"
2170 ""
2171 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002172
Willy Tarreau295a8372011-03-10 11:25:07 +01002173 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002174 switch (si->applet.ctx.stats.st_code) {
2175 case STAT_STATUS_DONE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002176 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002177 "<p><div class=active3>"
2178 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2179 "Action processed successfully."
2180 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002181 break;
2182 case STAT_STATUS_NONE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002183 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002184 "<p><div class=active2>"
2185 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2186 "Nothing has changed."
2187 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002188 break;
2189 case STAT_STATUS_PART:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002190 chunk_appendf(&trash,
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002191 "<p><div class=active2>"
2192 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2193 "Action partially processed.<br>"
2194 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2195 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002196 break;
2197 case STAT_STATUS_ERRP:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002198 chunk_appendf(&trash,
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002199 "<p><div class=active0>"
2200 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2201 "Action not processed because of invalid parameters."
2202 "<ul>"
2203 "<li>The action is maybe unknown.</li>"
2204 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2205 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2206 "</ul>"
2207 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002208 break;
2209 case STAT_STATUS_EXCD:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002210 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002211 "<p><div class=active0>"
2212 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2213 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2214 "You should retry with less servers at a time.</b>"
2215 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002216 break;
2217 case STAT_STATUS_DENY:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002218 chunk_appendf(&trash,
Cyril Bonté474be412010-10-12 00:14:36 +02002219 "<p><div class=active0>"
2220 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2221 "<b>Action denied.</b>"
2222 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002223 break;
2224 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002225 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002226 "<p><div class=active6>"
2227 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2228 "Unexpected result."
2229 "</div>\n", uri->uri_prefix);
2230 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002231 chunk_appendf(&trash,"<p>\n");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002232 }
2233
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002234 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002235 return 0;
2236 }
Willy Tarreau91861262007-10-17 17:06:05 +02002237
Willy Tarreau295a8372011-03-10 11:25:07 +01002238 si->applet.ctx.stats.px = proxy;
2239 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002240 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002241 /* fall through */
2242
Willy Tarreau295a8372011-03-10 11:25:07 +01002243 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002244 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002245 while (si->applet.ctx.stats.px) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002246 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002247 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002248 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02002249 /* skip the disabled proxies, global frontend and non-networked ones */
2250 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002251 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002252 return 0;
2253
Willy Tarreau295a8372011-03-10 11:25:07 +01002254 si->applet.ctx.stats.px = px->next;
2255 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002256 }
2257 /* here, we just have reached the last proxy */
2258
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002259 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002260 /* fall through */
2261
Willy Tarreau295a8372011-03-10 11:25:07 +01002262 case STAT_ST_END:
2263 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002264 chunk_appendf(&trash, "</body></html>\n");
2265 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002266 return 0;
2267 }
Willy Tarreau91861262007-10-17 17:06:05 +02002268
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002269 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002270 /* fall through */
2271
Willy Tarreau295a8372011-03-10 11:25:07 +01002272 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002273 return 1;
2274
2275 default:
2276 /* unknown state ! */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002277 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002278 return -1;
2279 }
2280}
2281
2282
2283/*
2284 * Dumps statistics for a proxy.
2285 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2286 * ot non-zero if everything completed.
2287 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002288static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002289{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002290 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002291 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002292 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002293 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002294
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002295 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002296
Willy Tarreau295a8372011-03-10 11:25:07 +01002297 switch (si->applet.ctx.stats.px_st) {
2298 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002299 /* we are on a new proxy */
2300
2301 if (uri && uri->scope) {
2302 /* we have a limited scope, we have to check the proxy name */
2303 struct stat_scope *scope;
2304 int len;
2305
2306 len = strlen(px->id);
2307 scope = uri->scope;
2308
2309 while (scope) {
2310 /* match exact proxy name */
2311 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2312 break;
2313
2314 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002315 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002316 break;
2317 scope = scope->next;
2318 }
2319
2320 /* proxy name not found : don't dump anything */
2321 if (scope == NULL)
2322 return 1;
2323 }
2324
Willy Tarreau295a8372011-03-10 11:25:07 +01002325 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2326 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002327 return 1;
2328
Willy Tarreau295a8372011-03-10 11:25:07 +01002329 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002330 /* fall through */
2331
Willy Tarreau295a8372011-03-10 11:25:07 +01002332 case STAT_PX_ST_TH:
2333 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2334 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002335 /* A form to enable/disable this proxy servers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002336 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002337 "<form action=\"%s\" method=\"post\">",
2338 uri->uri_prefix);
2339 }
2340
Willy Tarreau55bb8452007-10-17 18:44:57 +02002341 /* print a new table */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002342 chunk_appendf(&trash,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002343 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002344 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002345 "<th class=\"pxname\" width=\"10%%\"");
2346
2347 if (uri->flags&ST_SHLGNDS) {
2348 /* cap, mode, id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002349 chunk_appendf(&trash, " title=\"cap: %s, mode: %s, id: %d",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002350 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2351 px->uuid);
2352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002353 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002354 }
2355
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002356 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002357 ">%s<a name=\"%s\"></a>"
2358 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002359 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002360 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002361 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002362 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002363 "<tr class=\"titre\">",
2364 (uri->flags & ST_SHLGNDS)?"<u>":"",
2365 px->id, px->id, px->id,
2366 (uri->flags & ST_SHLGNDS)?"</u>":"",
2367 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2368
Willy Tarreau295a8372011-03-10 11:25:07 +01002369 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002370 /* Column heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002371 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002372 }
2373
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002374 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002375 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002376 "<th colspan=3>Queue</th>"
2377 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002378 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002379 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002380 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002381 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002382 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002383 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002384 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002385 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002386 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002387 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002388 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002389 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2390 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002391 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002392
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002393 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002394 return 0;
2395 }
Willy Tarreau91861262007-10-17 17:06:05 +02002396
Willy Tarreau295a8372011-03-10 11:25:07 +01002397 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002398 /* fall through */
2399
Willy Tarreau295a8372011-03-10 11:25:07 +01002400 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002401 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002402 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002403 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2404 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002405 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002406 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002407 "<tr class=\"frontend\">");
2408
Willy Tarreau295a8372011-03-10 11:25:07 +01002409 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002410 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002411 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002412 }
2413
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002414 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002415 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002416 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002417 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2418 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002419 "",
2420 px->id, px->id);
2421
2422 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002423 chunk_appendf(&trash,
Willy Tarreaub44939a2010-02-26 11:35:39 +01002424 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002425 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002426 "",
2427 read_freq_ctr(&px->fe_req_per_sec),
2428 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002429 px->fe_counters.p.http.rps_max,
2430 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002431 LIM2A2(px->fe_sps_lim, "-"));
2432 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002433 chunk_appendf(&trash,
Willy Tarreaub44939a2010-02-26 11:35:39 +01002434 /* sessions rate : current, max, limit */
2435 "<td>%s</td><td>%s</td><td>%s</td>"
2436 "",
2437 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002438 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002439 }
2440
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002441 chunk_appendf(&trash,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002442 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002443 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002444 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002445 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002446 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002447
2448 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2449 if (px->mode == PR_MODE_HTTP) {
2450 int i;
2451
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002452 chunk_appendf(&trash, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002453
2454 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002455 chunk_appendf(&trash, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002456
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002457 chunk_appendf(&trash, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2458 chunk_appendf(&trash, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002459 }
2460
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002461 chunk_appendf(&trash,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002462 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002463 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002464 /* bytes : in, out */
2465 "<td>%s</td><td>%s</td>"
2466 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002467 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002468 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002469 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002470 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002471
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002472 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002473 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002474 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002475 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002476 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002477 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002478 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002479 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002480 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002481 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002482 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002483 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002484 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2485 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002486 px->state == PR_STREADY ? "OPEN" :
2487 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002488 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002489 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002490 /* pxid, name, queue cur, queue max, */
2491 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002492 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002493 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002494 /* bytes : in, out */
2495 "%lld,%lld,"
2496 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002497 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002498 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002499 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002500 /* warnings: retries, redispatches */
2501 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002502 /* server status : reflect frontend status */
2503 "%s,"
2504 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002505 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002506 /* pid, iid, sid, throttle, lbtot, tracked, type */
2507 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002508 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002509 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002510 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002511 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002512 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002513 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2514 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2515 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2516 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002517 px->state == PR_STREADY ? "OPEN" :
2518 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002519 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002520 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002521 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002522
2523 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2524 if (px->mode == PR_MODE_HTTP) {
2525 int i;
2526
2527 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002528 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002529
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002530 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002531 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002532 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002533 }
2534
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002535 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002536 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002537
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002538 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002539 chunk_appendf(&trash, "%u,%u,%lld,",
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002540 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002541 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002542
Willy Tarreauae526782010-03-04 20:34:23 +01002543 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002544 chunk_appendf(&trash, ",,");
Willy Tarreauae526782010-03-04 20:34:23 +01002545
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002546 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002547 chunk_appendf(&trash, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002548 }
Willy Tarreau91861262007-10-17 17:06:05 +02002549
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002550 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002551 return 0;
2552 }
2553
Willy Tarreau4348fad2012-09-20 16:48:07 +02002554 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002555 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002556 /* fall through */
2557
Willy Tarreau295a8372011-03-10 11:25:07 +01002558 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002559 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002560 for (; si->applet.ctx.stats.l != &px->conf.listeners; si->applet.ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002561 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002562 return 0;
2563
Willy Tarreau4348fad2012-09-20 16:48:07 +02002564 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002565 if (!l->counters)
2566 continue;
2567
Willy Tarreau295a8372011-03-10 11:25:07 +01002568 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2569 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002570 break;
2571
Willy Tarreau295a8372011-03-10 11:25:07 +01002572 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002573 continue;
2574 }
2575
Willy Tarreau295a8372011-03-10 11:25:07 +01002576 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002577 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002578 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002579 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002580 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002582 chunk_appendf(&trash, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002583
2584 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002585 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002586 int port;
2587
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002588 chunk_appendf(&trash, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002589
Willy Tarreau631f01c2011-09-05 00:36:48 +02002590 port = get_host_port(&l->addr);
2591 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2592 case AF_INET:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002593 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
Willy Tarreau631f01c2011-09-05 00:36:48 +02002594 break;
2595 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002596 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
Willy Tarreau631f01c2011-09-05 00:36:48 +02002597 break;
2598 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002599 chunk_appendf(&trash, "unix, ");
Willy Tarreau631f01c2011-09-05 00:36:48 +02002600 break;
2601 case -1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002602 chunk_appendf(&trash, "(%s), ", strerror(errno));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002603 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002604 }
2605
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002606 /* id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002607 chunk_appendf(&trash, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002608 }
2609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002610 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002611 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002612 ">%s<a name=\"%s/+%s\"></a>"
2613 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002614 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002615 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002616 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002617 "<td>%s</td><td>%s</td><td>%s</td>"
2618 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002619 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002620 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002621 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002622 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002623 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002624 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002625 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2626 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2627
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002628 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002629 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002630 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002631 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002632 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002633 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002634 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002635 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002636 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002637 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002638 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002639 "",
2640 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2641 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002642 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002643 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002644 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002645 /* pxid, name, queue cur, queue max, */
2646 "%s,%s,,,"
2647 /* sessions: current, max, limit, total */
2648 "%d,%d,%d,%lld,"
2649 /* bytes: in, out */
2650 "%lld,%lld,"
2651 /* denied: req, resp */
2652 "%lld,%lld,"
2653 /* errors: request, connect, response */
2654 "%lld,,,"
2655 /* warnings: retries, redispatches */
2656 ",,"
2657 /* server status: reflect listener status */
2658 "%s,"
2659 /* rest of server: nothing */
2660 ",,,,,,,,"
2661 /* pid, iid, sid, throttle, lbtot, tracked, type */
2662 "%d,%d,%d,,,,%d,"
2663 /* rate, rate_lim, rate_max */
2664 ",,,"
2665 /* check_status, check_code, check_duration */
2666 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002667 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2668 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002669 /* failed health analyses */
2670 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002671 /* requests : req_rate, req_rate_max, req_tot, */
2672 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002673 /* errors: cli_aborts, srv_aborts */
2674 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002675 "\n",
2676 px->id, l->name,
2677 l->nbconn, l->counters->conn_max,
2678 l->maxconn, l->counters->cum_conn,
2679 l->counters->bytes_in, l->counters->bytes_out,
2680 l->counters->denied_req, l->counters->denied_resp,
2681 l->counters->failed_req,
2682 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2683 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2684 }
2685
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002686 if (bi_putchk(rep, &trash) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002687 return 0;
2688 }
2689
Willy Tarreau295a8372011-03-10 11:25:07 +01002690 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2691 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002692 /* fall through */
2693
Willy Tarreau295a8372011-03-10 11:25:07 +01002694 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002695 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002696 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002697 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 +02002698
Willy Tarreau9b28e032012-10-12 23:49:43 +02002699 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002700 return 0;
2701
Willy Tarreau295a8372011-03-10 11:25:07 +01002702 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002703
Willy Tarreau295a8372011-03-10 11:25:07 +01002704 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2705 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002706 break;
2707
Willy Tarreau295a8372011-03-10 11:25:07 +01002708 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002709 continue;
2710 }
2711
Willy Tarreau44267702011-10-28 15:35:33 +02002712 if (sv->track)
2713 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002714 else
2715 svs = sv;
2716
Willy Tarreau91861262007-10-17 17:06:05 +02002717 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002718 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002719 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002720 else if (svs->state & SRV_RUNNING) {
2721 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002722 sv_state = 3; /* UP */
2723 else
2724 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002725
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002726 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002727 sv_state += 2;
2728 }
Willy Tarreau91861262007-10-17 17:06:05 +02002729 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002730 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002731 sv_state = 1; /* going up */
2732 else
2733 sv_state = 0; /* DOWN */
2734
Willy Tarreau295a8372011-03-10 11:25:07 +01002735 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002736 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002737 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002738 continue;
2739 }
2740
Willy Tarreau295a8372011-03-10 11:25:07 +01002741 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002742 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2743 "UP %d/%d &darr;", "UP",
2744 "NOLB %d/%d &darr;", "NOLB",
2745 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002746 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002747 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002748 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002749 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002750 );
2751 }
2752 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002753 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002754 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002755 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002756 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2757 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002758
Willy Tarreau295a8372011-03-10 11:25:07 +01002759 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002760 chunk_appendf(&trash,
Cyril Bonté474be412010-10-12 00:14:36 +02002761 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2762 sv->id);
2763 }
2764
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002765 chunk_appendf(&trash, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002766
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002767 if (uri->flags&ST_SHLGNDS) {
2768 char str[INET6_ADDRSTRLEN];
2769
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002770 chunk_appendf(&trash, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002771
Willy Tarreau631f01c2011-09-05 00:36:48 +02002772 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002773 case AF_INET:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002774 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002775 break;
2776 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002777 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002778 break;
2779 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002780 chunk_appendf(&trash, "unix, ");
Willy Tarreau631f01c2011-09-05 00:36:48 +02002781 break;
2782 case -1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002783 chunk_appendf(&trash, "(%s), ", strerror(errno));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002784 break;
2785 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002786 break;
2787 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002788
2789 /* id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002790 chunk_appendf(&trash, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002791
2792 /* cookie */
2793 if (sv->cookie) {
2794 struct chunk src;
2795
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002796 chunk_appendf(&trash, ", cookie: '");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002797
2798 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002799 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002800
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002801 chunk_appendf(&trash, "'");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002802 }
2803
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002804 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002805 }
2806
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002807 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002808 ">%s<a name=\"%s/%s\"></a>"
2809 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002810 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002811 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002812 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002813 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002814 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002815 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002816 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002817 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002818 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002819 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002820 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002821 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2822 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002823 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2824
2825 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2826 if (px->mode == PR_MODE_HTTP) {
2827 int i;
2828
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002829 chunk_appendf(&trash, " title=\"rsp codes:");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002830
2831 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002832 chunk_appendf(&trash, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002833
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002834 chunk_appendf(&trash, " other=%lld\"", sv->counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002835 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002836
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002837 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002838 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002839 ">%s%s%s</td><td>%s</td>",
2840 (px->mode == PR_MODE_HTTP)?"<u>":"",
2841 U2H0(sv->counters.cum_sess),
2842 (px->mode == PR_MODE_HTTP)?"</u>":"",
2843 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002844
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002845 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002846 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002847 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002848 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002849 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002850 /* errors : request, connect */
2851 "<td></td><td>%s</td>"
2852 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002853 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002854 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002855 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002856 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002857 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2858 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002859 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002860 sv->counters.cli_aborts,
2861 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002862 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002863 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002864
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002865 /* status, lest check */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002866 chunk_appendf(&trash, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002867
Cyril Bontécd19e512010-01-31 22:34:03 +01002868 if (sv->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002869 chunk_appendf(&trash, "%s ",
Cyril Bontécd19e512010-01-31 22:34:03 +01002870 human_time(now.tv_sec - sv->last_change, 1));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002871 chunk_appendf(&trash, "MAINT");
Cyril Bontécd19e512010-01-31 22:34:03 +01002872 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002873 else if (svs != sv && svs->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002874 chunk_appendf(&trash, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002875 human_time(now.tv_sec - svs->last_change, 1));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002876 chunk_appendf(&trash, "MAINT(via)");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002877 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002878 else if (svs->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002879 chunk_appendf(&trash, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002880 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002881
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002882 chunk_appendf(&trash,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002883 srv_hlt_st[sv_state],
2884 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2885 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002886 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002887
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002888 if (sv->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002889 chunk_appendf(&trash, "</td><td class=ac title=\"%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002890 get_check_status_description(sv->check.status));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002891
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002892 if (*sv->check.desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002893 struct chunk src;
2894
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002895 chunk_appendf(&trash, ": ");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002896
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002897 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002898 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002899 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002900
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002901 chunk_appendf(&trash, "\"><u> %s%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002902 tv_iszero(&sv->check.start)?"":"* ",
2903 get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002904
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002905 if (sv->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002906 chunk_appendf(&trash, "/%d", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002907
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002908 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002909 chunk_appendf(&trash, " in %lums</u>", sv->check.duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002910 } else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002911 chunk_appendf(&trash, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002912
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002913 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002914 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002915 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002916 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002917 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002918 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002919 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002920 (sv->state & SRV_BACKUP) ? "-" : "Y",
2921 (sv->state & SRV_BACKUP) ? "Y" : "-");
2922
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002923 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002924 if (sv->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002925 chunk_appendf(&trash, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002926 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2927
2928 if (svs->observe)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002929 chunk_appendf(&trash, "/%lld", svs->counters.failed_hana);
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002930
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002931 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002932 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002933 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002934 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002935 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002936 } else if (sv != svs)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002937 chunk_appendf(&trash,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002938 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2939 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002940 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002941 chunk_appendf(&trash,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002942 "<td colspan=3></td>");
2943
2944 /* throttle */
2945 if ((sv->state & SRV_WARMINGUP) &&
2946 now.tv_sec < sv->last_change + sv->slowstart &&
2947 now.tv_sec >= sv->last_change) {
2948 unsigned int ratio;
2949 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002950 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002951 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002952 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002953 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002954 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002955 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002956 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002957 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2958 "UP %d/%d,", "UP,",
2959 "NOLB %d/%d,", "NOLB,",
2960 "no check," };
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002961 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002962 /* pxid, name */
2963 "%s,%s,"
2964 /* queue : current, max */
2965 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002966 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002967 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002968 /* bytes : in, out */
2969 "%lld,%lld,"
2970 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002971 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002972 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002973 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002974 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002975 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002976 "",
2977 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002978 sv->nbpend, sv->counters.nbpend_max,
2979 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002980 sv->counters.bytes_in, sv->counters.bytes_out,
2981 sv->counters.failed_secu,
2982 sv->counters.failed_conns, sv->counters.failed_resp,
2983 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002984
Willy Tarreau55bb8452007-10-17 18:44:57 +02002985 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002986 if (sv->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002987 chunk_appendf(&trash, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002988 }
2989 else if (svs != sv && svs->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002990 chunk_appendf(&trash, "MAINT(via),");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002991 }
2992 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002993 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002994 srv_hlt_st[sv_state],
2995 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2996 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2997 }
Willy Tarreau91861262007-10-17 17:06:05 +02002998
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002999 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003000 /* weight, active, backup */
3001 "%d,%d,%d,"
3002 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01003003 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003004 (sv->state & SRV_BACKUP) ? 0 : 1,
3005 (sv->state & SRV_BACKUP) ? 1 : 0);
3006
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003007 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003008 if (sv->state & SRV_CHECKED)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003009 chunk_appendf(&trash,
Willy Tarreau3b88d442009-04-11 20:44:08 +02003010 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003011 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003012 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003013 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003014 chunk_appendf(&trash,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003015 ",,,,");
3016
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003017 /* queue limit, pid, iid, sid, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003018 chunk_appendf(&trash,
Willy Tarreaudcd47712007-11-04 23:35:08 +01003019 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003020 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01003021 LIM2A0(sv->maxqueue, ""),
3022 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003023
3024 /* throttle */
3025 if ((sv->state & SRV_WARMINGUP) &&
3026 now.tv_sec < sv->last_change + sv->slowstart &&
3027 now.tv_sec >= sv->last_change) {
3028 unsigned int ratio;
3029 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003030 chunk_appendf(&trash, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003031 }
3032
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003033 /* sessions: lbtot */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003034 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003035
3036 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02003037 if (sv->track)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003038 chunk_appendf(&trash, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02003039 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003040 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003041 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003042
Willy Tarreau7f062c42009-03-05 18:43:00 +01003043 /* type */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003044 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003045
3046 /* rate */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003047 chunk_appendf(&trash, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003048 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003049 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003050
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003051 if (sv->state & SRV_CHECKED) {
3052 /* check_status */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003053 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003054
3055 /* check_code */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003056 if (sv->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003057 chunk_appendf(&trash, "%u,", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003058 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003059 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003060
3061 /* check_duration */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003062 if (sv->check.status >= HCHK_STATUS_CHECKED)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003063 chunk_appendf(&trash, "%lu,", sv->check.duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003064 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003065 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003066
3067 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003068 chunk_appendf(&trash, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003069 }
3070
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003071 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3072 if (px->mode == PR_MODE_HTTP) {
3073 int i;
3074
3075 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003076 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003077
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003078 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003079 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003080 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003081 }
3082
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003083 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003084 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003085
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003086 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003087 chunk_appendf(&trash, ",,,");
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003088
Willy Tarreauae526782010-03-04 20:34:23 +01003089 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003090 chunk_appendf(&trash, "%lld,%lld,",
Willy Tarreauae526782010-03-04 20:34:23 +01003091 sv->counters.cli_aborts, sv->counters.srv_aborts);
3092
Willy Tarreau7f062c42009-03-05 18:43:00 +01003093 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003094 chunk_appendf(&trash, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003095 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003096 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003097 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003098 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003099
Willy Tarreau295a8372011-03-10 11:25:07 +01003100 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003101 /* fall through */
3102
Willy Tarreau295a8372011-03-10 11:25:07 +01003103 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003104 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003105 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003106 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3107 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003108 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003109 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003110 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003111 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003112 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003113 chunk_appendf(&trash, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003114
3115 if (uri->flags&ST_SHLGNDS) {
3116 /* balancing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003117 chunk_appendf(&trash, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003118 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003119
3120 /* cookie */
3121 if (px->cookie_name) {
3122 struct chunk src;
3123
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003124 chunk_appendf(&trash, ", cookie: '");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003125
3126 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003127 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003128
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003129 chunk_appendf(&trash, "'");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003130 }
3131
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003132 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003133
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003134 }
3135
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003136 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003137 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003138 ">%s<a name=\"%s/Backend\"></a>"
3139 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003140 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003141 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003142 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003143 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003144 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003145 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003146 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003147 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003148 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3149 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003150
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003151 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003152 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003153 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003154 "<td"
3155 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003156 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003157
3158 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3159 if (px->mode == PR_MODE_HTTP) {
3160 int i;
3161
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003162 chunk_appendf(&trash, " title=\"rsp codes:");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003163
3164 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003165 chunk_appendf(&trash, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003167 chunk_appendf(&trash, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003168 }
3169
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003170 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003171 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003172 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003173 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003174 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003175 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003176 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003177 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003178 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003179 U2H7(px->be_counters.cum_lbconn),
3180 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003181
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003182 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02003183 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003184 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003185 /* errors : request, connect */
3186 "<td></td><td>%s</td>"
3187 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003188 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003189 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003190 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003191 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003192 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003193 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003194 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003195 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3196 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003197 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003198 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3199 U2H2(px->be_counters.failed_conns),
3200 px->be_counters.cli_aborts,
3201 px->be_counters.srv_aborts,
3202 U2H5(px->be_counters.failed_resp),
3203 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003204 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003205 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3206 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003207 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003208 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003209
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003210 chunk_appendf(&trash,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003211 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003212 "<td class=ac>&nbsp;</td><td>%d</td>"
3213 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003214 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003215 "</tr>",
3216 px->down_trans,
3217 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003218 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003219 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003220 /* pxid, name */
3221 "%s,BACKEND,"
3222 /* queue : current, max */
3223 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003224 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003225 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003226 /* bytes : in, out */
3227 "%lld,%lld,"
3228 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003229 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003230 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003231 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003232 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003233 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003234 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003235 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003236 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003237 * active and backups. */
3238 "%s,"
3239 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003240 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003241 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003242 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003243 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003244 /* rate, rate_lim, rate_max, */
3245 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003246 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003247 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003248 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003249 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3250 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3251 px->be_counters.bytes_in, px->be_counters.bytes_out,
3252 px->be_counters.denied_req, px->be_counters.denied_resp,
3253 px->be_counters.failed_conns, px->be_counters.failed_resp,
3254 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003255 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003256 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003257 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003258 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003259 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003260 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003261 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003262 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003263 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003264
3265 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3266 if (px->mode == PR_MODE_HTTP) {
3267 int i;
3268
3269 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003270 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003271
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003272 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003273 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003274 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003275 }
3276
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003277 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003278 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003279
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003280 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003281 chunk_appendf(&trash, ",,,");
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003282
Willy Tarreauae526782010-03-04 20:34:23 +01003283 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003284 chunk_appendf(&trash, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003285 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003286
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003287 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003288 chunk_appendf(&trash, "\n");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003289
Willy Tarreau55bb8452007-10-17 18:44:57 +02003290 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003291 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003292 return 0;
3293 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003294
Willy Tarreau295a8372011-03-10 11:25:07 +01003295 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003296 /* fall through */
3297
Willy Tarreau295a8372011-03-10 11:25:07 +01003298 case STAT_PX_ST_END:
3299 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003300 chunk_appendf(&trash, "</table>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003301
Willy Tarreau295a8372011-03-10 11:25:07 +01003302 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003303 /* close the form used to enable/disable this proxy servers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003304 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003305 "Choose the action to perform on the checked servers : "
3306 "<select name=action>"
3307 "<option value=\"\"></option>"
3308 "<option value=\"disable\">Disable</option>"
3309 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003310 "<option value=\"stop\">Soft Stop</option>"
3311 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003312 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003313 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003314 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003315 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3316 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003317 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003318 }
3319
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003320 chunk_appendf(&trash, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003321
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003322 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003323 return 0;
3324 }
Willy Tarreau91861262007-10-17 17:06:05 +02003325
Willy Tarreau295a8372011-03-10 11:25:07 +01003326 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003327 /* fall through */
3328
Willy Tarreau295a8372011-03-10 11:25:07 +01003329 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003330 return 1;
3331
3332 default:
3333 /* unknown state, we should put an abort() here ! */
3334 return 1;
3335 }
3336}
3337
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003338/* This function dumps a complete session state onto the stream interface's
3339 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3340 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003341 * 0 if the output buffer is full and it needs to be called again, otherwise
3342 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003343 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003344static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003345{
3346 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003347 struct session *sess;
3348 extern const char *monthname[12];
3349 char pn[INET6_ADDRSTRLEN];
3350
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003351 chunk_reset(&trash);
Willy Tarreau295a8372011-03-10 11:25:07 +01003352 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003353
Willy Tarreau295a8372011-03-10 11:25:07 +01003354 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003355 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003356 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3357 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003358 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003359 si->applet.ctx.sess.target = NULL;
3360 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003361 return 1;
3362 }
3363
Willy Tarreau295a8372011-03-10 11:25:07 +01003364 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003365 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003366 si->applet.ctx.sess.uid = sess->uniq_id;
3367 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003368 /* fall through */
3369
3370 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003371 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003372 "%p: id=%u, proto=%s",
3373 sess,
3374 sess->uniq_id,
3375 sess->listener->proto->name);
3376
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003377 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003378 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003379 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003380 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003381 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003382 break;
3383 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003384 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003385 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003386 default:
3387 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003388 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003389 break;
3390 }
3391
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003392 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003393 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003394 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003396 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003397 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003398 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3399 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3400 sess->listener ? sess->listener->luid : 0);
3401
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003402 conn_get_to_addr(sess->si[0].conn);
3403 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003404 case AF_INET:
3405 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003406 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003407 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003408 break;
3409 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003410 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003411 break;
3412 default:
3413 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003414 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003415 break;
3416 }
3417
Willy Tarreau50943332011-09-02 17:33:05 +02003418 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003419 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003420 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003421 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003422 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3423 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003424 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003425
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003426 conn_get_from_addr(sess->si[1].conn);
3427 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003428 case AF_INET:
3429 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003430 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003431 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003432 break;
3433 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003434 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003435 break;
3436 default:
3437 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003438 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003439 break;
3440 }
3441
3442 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003443 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003444 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003445 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3446 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003447 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003448 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003449
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003450 conn_get_to_addr(sess->si[1].conn);
3451 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003452 case AF_INET:
3453 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003454 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003455 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003456 break;
3457 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003458 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003459 break;
3460 default:
3461 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003462 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003463 break;
3464 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003465
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003466 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003467 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3468 sess->task,
3469 sess->task->state,
3470 sess->task->nice, sess->task->calls,
3471 sess->task->expire ?
3472 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3473 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3474 TICKS_TO_MS(1000)) : "<NEVER>",
3475 task_in_rq(sess->task) ? ", running" : "");
3476
3477 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003478 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003479 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3480 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3481 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3482 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3483
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003484 chunk_appendf(&trash,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003485 " si[0]=%p (state=%d flags=0x%02x conn=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003486 &sess->si[0],
3487 sess->si[0].state,
3488 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003489 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003490 sess->si[0].exp ?
3491 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3492 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3493 TICKS_TO_MS(1000)) : "<NEVER>",
3494 sess->si[0].err_type);
3495
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003496 chunk_appendf(&trash,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003497 " si[1]=%p (state=%d flags=0x%02x conn=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003498 &sess->si[1],
3499 sess->si[1].state,
3500 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003501 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003502 sess->si[1].exp ?
3503 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3504 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3505 TICKS_TO_MS(1000)) : "<NEVER>",
3506 sess->si[1].err_type);
3507
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003508 chunk_appendf(&trash,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003509 " lconn=%p (ctrl=%p(%s) xprt=%p(%s) data=%p(%s) fd=%d target=%d flags=0x%08x)\n",
3510 sess->si[0].conn,
3511 sess->si[0].conn->ctrl,
3512 sess->si[0].conn->ctrl ? sess->si[0].conn->ctrl->name : "NONE",
3513 sess->si[0].conn->xprt,
3514 (sess->si[0].conn->xprt == NULL) ? "NONE" :
3515 (sess->si[0].conn->xprt == &raw_sock) ? "RAW" :
3516#ifdef USE_OPENSSL
3517 (sess->si[0].conn->xprt == &ssl_sock) ? "SSL" :
3518#endif
3519 "????",
3520 sess->si[0].conn->data,
3521 (sess->si[0].conn->data == NULL) ? "NONE" :
3522 (sess->si[0].conn->data == &sess_conn_cb) ? "SESS" :
3523 (sess->si[0].conn->data == &si_conn_cb) ? "STRM" :
3524 (sess->si[0].conn->data == &check_conn_cb) ? "CHCK" : "????",
3525 sess->si[0].conn->t.sock.fd,
3526 sess->si[0].conn->target ? *sess->si[0].conn->target : 0,
3527 sess->si[0].conn->flags);
3528
3529 chunk_appendf(&trash,
3530 " rconn=%p (ctrl=%p(%s) xprt=%p(%s) data=%p(%s) fd=%d target=%d flags=0x%08x)\n",
3531 sess->si[1].conn,
3532 sess->si[1].conn->ctrl,
3533 sess->si[1].conn->ctrl ? sess->si[1].conn->ctrl->name : "NONE",
3534 sess->si[1].conn->xprt,
3535 (sess->si[1].conn->xprt == NULL) ? "NONE" :
3536 (sess->si[1].conn->xprt == &raw_sock) ? "RAW" :
3537#ifdef USE_OPENSSL
3538 (sess->si[1].conn->xprt == &ssl_sock) ? "SSL" :
3539#endif
3540 "UNKNOWN",
3541 sess->si[1].conn->data,
3542 (sess->si[1].conn->data == NULL) ? "NONE" :
3543 (sess->si[1].conn->data == &sess_conn_cb) ? "SESS" :
3544 (sess->si[1].conn->data == &si_conn_cb) ? "STRM" :
3545 (sess->si[1].conn->data == &check_conn_cb) ? "CHCK" : "????",
3546 sess->si[1].conn->t.sock.fd,
3547 sess->si[1].conn->target ? *sess->si[1].conn->target : 0,
3548 sess->si[1].conn->flags);
3549
3550 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003551 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3552 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3553 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3554
3555
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003556 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003557 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003558 " an_exp=%s",
3559 sess->req,
3560 sess->req->flags, sess->req->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003561 sess->req->buf->i, sess->req->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003562 sess->req->pipe ? sess->req->pipe->data : 0,
3563 sess->req->to_forward,
3564 sess->req->analyse_exp ?
3565 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3566 TICKS_TO_MS(1000)) : "<NEVER>");
3567
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003568 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003569 " rex=%s",
3570 sess->req->rex ?
3571 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3572 TICKS_TO_MS(1000)) : "<NEVER>");
3573
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003574 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003575 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003576 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003577 sess->req->wex ?
3578 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3579 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003580 sess->req->buf->data,
3581 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003582 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003583 sess->req->total);
3584
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003585 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003586 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003587 " an_exp=%s",
3588 sess->rep,
3589 sess->rep->flags, sess->rep->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003590 sess->rep->buf->i, sess->rep->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003591 sess->rep->pipe ? sess->rep->pipe->data : 0,
3592 sess->rep->to_forward,
3593 sess->rep->analyse_exp ?
3594 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3595 TICKS_TO_MS(1000)) : "<NEVER>");
3596
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003597 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003598 " rex=%s",
3599 sess->rep->rex ?
3600 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3601 TICKS_TO_MS(1000)) : "<NEVER>");
3602
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003603 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003604 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003605 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003606 sess->rep->wex ?
3607 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3608 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003609 sess->rep->buf->data,
3610 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003611 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003612 sess->rep->total);
3613
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003614 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003615 return 0;
3616
3617 /* use other states to dump the contents */
3618 }
3619 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003620 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003621 return 1;
3622}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003623
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003624/* This function dumps all sessions' states onto the stream interface's
3625 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003626 * properly set. It returns 0 if the output buffer is full and it needs
3627 * to be called again, otherwise non-zero. It is designed to be called
3628 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003629 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003630static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003631{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003632 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003633 /* If we're forced to shut down, we might have to remove our
3634 * reference to the last session being dumped.
3635 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003636 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003637 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3638 LIST_DEL(&si->applet.ctx.sess.bref.users);
3639 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003640 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003641 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003642 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003643 }
3644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003645 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003646
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003647 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003648 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003649 /* the function had not been called yet, let's prepare the
3650 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003651 * pointer to the first in the global list. When a target
3652 * session is being destroyed, it is responsible for updating
3653 * this pointer. We know we have reached the end when this
3654 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003655 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003656 LIST_INIT(&si->applet.ctx.sess.bref.users);
3657 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003658 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003659 /* fall through */
3660
Willy Tarreau295a8372011-03-10 11:25:07 +01003661 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003662 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003663 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3664 LIST_DEL(&si->applet.ctx.sess.bref.users);
3665 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003666 }
3667
3668 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003669 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003670 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003671 struct session *curr_sess;
3672
Willy Tarreau295a8372011-03-10 11:25:07 +01003673 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003674
Willy Tarreau295a8372011-03-10 11:25:07 +01003675 if (si->applet.ctx.sess.target) {
3676 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003677 goto next_sess;
3678
Willy Tarreau295a8372011-03-10 11:25:07 +01003679 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003680 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003681 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003682 return 0;
3683
3684 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003685 LIST_DEL(&si->applet.ctx.sess.bref.users);
3686 LIST_INIT(&si->applet.ctx.sess.bref.users);
3687 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003688 break;
3689 }
3690
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003691 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003692 "%p: proto=%s",
3693 curr_sess,
3694 curr_sess->listener->proto->name);
3695
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003696
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003697 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003698 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003699 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003700 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003701 " src=%s:%d fe=%s be=%s srv=%s",
3702 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003703 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003704 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003705 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003706 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003707 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003708 break;
3709 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003710 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003711 " src=unix:%d fe=%s be=%s srv=%s",
3712 curr_sess->listener->luid,
3713 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003714 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003715 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003716 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003717 break;
3718 }
3719
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003720 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003721 " ts=%02x age=%s calls=%d",
3722 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003723 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3724 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003725
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003726 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003727 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003728 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003729 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003730 curr_sess->req->analysers,
3731 curr_sess->req->rex ?
3732 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3733 TICKS_TO_MS(1000)) : "");
3734
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003735 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003736 ",wx=%s",
3737 curr_sess->req->wex ?
3738 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3739 TICKS_TO_MS(1000)) : "");
3740
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003741 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003742 ",ax=%s]",
3743 curr_sess->req->analyse_exp ?
3744 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3745 TICKS_TO_MS(1000)) : "");
3746
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003747 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003748 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003749 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003750 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003751 curr_sess->rep->analysers,
3752 curr_sess->rep->rex ?
3753 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3754 TICKS_TO_MS(1000)) : "");
3755
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003756 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003757 ",wx=%s",
3758 curr_sess->rep->wex ?
3759 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3760 TICKS_TO_MS(1000)) : "");
3761
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003762 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003763 ",ax=%s]",
3764 curr_sess->rep->analyse_exp ?
3765 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3766 TICKS_TO_MS(1000)) : "");
3767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003768 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003769 " s0=[%d,%1xh,fd=%d,ex=%s]",
3770 curr_sess->si[0].state,
3771 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003772 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003773 curr_sess->si[0].exp ?
3774 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3775 TICKS_TO_MS(1000)) : "");
3776
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003777 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003778 " s1=[%d,%1xh,fd=%d,ex=%s]",
3779 curr_sess->si[1].state,
3780 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003781 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003782 curr_sess->si[1].exp ?
3783 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3784 TICKS_TO_MS(1000)) : "");
3785
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003786 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003787 " exp=%s",
3788 curr_sess->task->expire ?
3789 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3790 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003791 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003792 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003793
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003794 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003795
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003796 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003797 /* let's try again later from this session. We add ourselves into
3798 * this session's users so that it can remove us upon termination.
3799 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003800 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003801 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003802 }
3803
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003804 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003805 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003806 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003807
Willy Tarreau295a8372011-03-10 11:25:07 +01003808 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003809 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003810 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003811 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003812 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003813 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003814
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003815 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003816 return 0;
3817
Willy Tarreau295a8372011-03-10 11:25:07 +01003818 si->applet.ctx.sess.target = NULL;
3819 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003820 return 1;
3821 }
3822
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003823 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003824 /* fall through */
3825
3826 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003827 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003828 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003829 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003830}
3831
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003832/* This function dumps all tables' states onto the stream interface's
3833 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003834 * properly set. It returns 0 if the output buffer is full and it needs
3835 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003836 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003837static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003838{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003839 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003840 struct ebmb_node *eb;
3841 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003842 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003843
3844 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003845 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003846 * - STAT_ST_INIT : the first call
3847 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003848 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003849 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003850 * and the entry pointer points to the next entry to be dumped,
3851 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003852 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003853 * data though.
3854 */
3855
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003856 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003857 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003858 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003859 si->applet.ctx.table.entry->ref_cnt--;
3860 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003861 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003862 return 1;
3863 }
3864
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003865 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003866
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003867 while (si->conn->xprt_st != STAT_ST_FIN) {
3868 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003869 case STAT_ST_INIT:
3870 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3871 if (!si->applet.ctx.table.proxy)
3872 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003873
Willy Tarreau295a8372011-03-10 11:25:07 +01003874 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003875 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003876 break;
3877
Willy Tarreau295a8372011-03-10 11:25:07 +01003878 case STAT_ST_INFO:
3879 if (!si->applet.ctx.table.proxy ||
3880 (si->applet.ctx.table.target &&
3881 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003882 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003883 break;
3884 }
3885
Willy Tarreau295a8372011-03-10 11:25:07 +01003886 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003887 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09003888 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003889 return 0;
3890
Willy Tarreau295a8372011-03-10 11:25:07 +01003891 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02003892 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003893 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003894 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003895 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003896 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3897 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003898 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003899 break;
3900 }
3901 }
3902 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003903 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003904 break;
3905
Willy Tarreau295a8372011-03-10 11:25:07 +01003906 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003907 skip_entry = false;
3908
Willy Tarreau295a8372011-03-10 11:25:07 +01003909 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003910 /* we're filtering on some data contents */
3911 void *ptr;
3912 long long data;
3913
Willy Tarreau295a8372011-03-10 11:25:07 +01003914 dt = si->applet.ctx.table.data_type;
3915 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3916 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003917 dt);
3918
3919 data = 0;
3920 switch (stktable_data_types[dt].std_type) {
3921 case STD_T_SINT:
3922 data = stktable_data_cast(ptr, std_t_sint);
3923 break;
3924 case STD_T_UINT:
3925 data = stktable_data_cast(ptr, std_t_uint);
3926 break;
3927 case STD_T_ULL:
3928 data = stktable_data_cast(ptr, std_t_ull);
3929 break;
3930 case STD_T_FRQP:
3931 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003932 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003933 break;
3934 }
3935
3936 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003937 if ((data < si->applet.ctx.table.value &&
3938 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3939 si->applet.ctx.table.data_op == STD_OP_GT ||
3940 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3941 (data == si->applet.ctx.table.value &&
3942 (si->applet.ctx.table.data_op == STD_OP_NE ||
3943 si->applet.ctx.table.data_op == STD_OP_GT ||
3944 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3945 (data > si->applet.ctx.table.value &&
3946 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3947 si->applet.ctx.table.data_op == STD_OP_LT ||
3948 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003949 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003950 }
3951
Simon Hormanc88b8872011-06-15 15:18:49 +09003952 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003954 si->applet.ctx.table.entry))
3955 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003956
Willy Tarreau295a8372011-03-10 11:25:07 +01003957 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003958
Willy Tarreau295a8372011-03-10 11:25:07 +01003959 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003960 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003961 struct stksess *old = si->applet.ctx.table.entry;
3962 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003963 if (show)
3964 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3965 else
3966 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003967 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003968 break;
3969 }
3970
Simon Hormanc88b8872011-06-15 15:18:49 +09003971
3972 if (show)
3973 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3974 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3975 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3976
Willy Tarreau295a8372011-03-10 11:25:07 +01003977 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003978 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003979 break;
3980
Willy Tarreau295a8372011-03-10 11:25:07 +01003981 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003982 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003983 break;
3984 }
3985 }
3986 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003987}
3988
Willy Tarreaud426a182010-03-05 14:58:26 +01003989/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003990 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3991 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3992 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3993 * lines are respected within the limit of 70 output chars. Lines that are
3994 * continuation of a previous truncated line begin with "+" instead of " "
3995 * after the offset. The new pointer is returned.
3996 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003997static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3998 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003999{
4000 int end;
4001 unsigned char c;
4002
4003 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004004 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004005 return ptr;
4006
Willy Tarreau77804732012-10-29 16:14:26 +01004007 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004008
Willy Tarreaud426a182010-03-05 14:58:26 +01004009 while (ptr < len && ptr < bsize) {
4010 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004011 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004012 if (out->len > end - 2)
4013 break;
4014 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004015 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004016 if (out->len > end - 3)
4017 break;
4018 out->str[out->len++] = '\\';
4019 switch (c) {
4020 case '\t': c = 't'; break;
4021 case '\n': c = 'n'; break;
4022 case '\r': c = 'r'; break;
4023 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004024 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004025 }
4026 out->str[out->len++] = c;
4027 } else {
4028 if (out->len > end - 5)
4029 break;
4030 out->str[out->len++] = '\\';
4031 out->str[out->len++] = 'x';
4032 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4033 out->str[out->len++] = hextab[c & 0xF];
4034 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004035 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004036 /* we had a line break, let's return now */
4037 out->str[out->len++] = '\n';
4038 *line = ptr;
4039 return ptr;
4040 }
4041 }
4042 /* we have an incomplete line, we return it as-is */
4043 out->str[out->len++] = '\n';
4044 return ptr;
4045}
4046
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004047/* This function dumps all captured errors onto the stream interface's
4048 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004049 * properly set. It returns 0 if the output buffer is full and it needs
4050 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004051 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004052static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004053{
4054 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004055
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004056 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004057 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004059 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004060
Willy Tarreau295a8372011-03-10 11:25:07 +01004061 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004062 /* the function had not been called yet, let's prepare the
4063 * buffer for a response.
4064 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004065 struct tm tm;
4066
4067 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004068 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004069 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4070 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4071 error_snapshot_id);
4072
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004073 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004074 /* Socket buffer full. Let's try again later from the same point */
4075 return 0;
4076 }
4077
Willy Tarreau295a8372011-03-10 11:25:07 +01004078 si->applet.ctx.errors.px = proxy;
4079 si->applet.ctx.errors.buf = 0;
4080 si->applet.ctx.errors.bol = 0;
4081 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004082 }
4083
4084 /* we have two inner loops here, one for the proxy, the other one for
4085 * the buffer.
4086 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004087 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004088 struct error_snapshot *es;
4089
Willy Tarreau295a8372011-03-10 11:25:07 +01004090 if (si->applet.ctx.errors.buf == 0)
4091 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004092 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004093 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004094
4095 if (!es->when.tv_sec)
4096 goto next;
4097
Willy Tarreau295a8372011-03-10 11:25:07 +01004098 if (si->applet.ctx.errors.iid >= 0 &&
4099 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4100 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004101 goto next;
4102
Willy Tarreau295a8372011-03-10 11:25:07 +01004103 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004104 /* just print headers now */
4105
4106 char pn[INET6_ADDRSTRLEN];
4107 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004108 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004109
4110 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004111 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004112 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004113 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004114
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004115 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4116 case AF_INET:
4117 case AF_INET6:
4118 port = get_host_port(&es->src);
4119 break;
4120 default:
4121 port = 0;
4122 }
4123
Willy Tarreau295a8372011-03-10 11:25:07 +01004124 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004125 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004126 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004127 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004128 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004129 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004130 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4131 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004132 break;
4133 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004134 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004135 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004136 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004137 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004138 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004139 break;
4140 }
4141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004142 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004143 ", server %s (#%d), event #%u\n"
4144 " src %s:%d, session #%d, session flags 0x%08x\n"
4145 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4146 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4147 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4148 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4149 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4150 es->ev_id,
4151 pn, port, es->sid, es->s_flags,
4152 es->state, es->m_flags, es->t_flags,
4153 es->m_clen, es->m_blen,
4154 es->b_flags, es->b_out, es->b_tot,
4155 es->len, es->b_wrap, es->pos);
4156
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004157 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004158 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004159 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004160 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004161 si->applet.ctx.errors.ptr = 0;
4162 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004163 }
4164
Willy Tarreau295a8372011-03-10 11:25:07 +01004165 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004166 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004167 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004168 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004169 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004170 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004171 goto next;
4172 }
4173
4174 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004175 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004176 int newptr;
4177 int newline;
4178
Willy Tarreau295a8372011-03-10 11:25:07 +01004179 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004180 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
Willy Tarreau295a8372011-03-10 11:25:07 +01004181 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004182 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004183
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004184 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004185 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004186 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004187 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004188 si->applet.ctx.errors.ptr = newptr;
4189 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004190 };
4191 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004192 si->applet.ctx.errors.bol = 0;
4193 si->applet.ctx.errors.ptr = -1;
4194 si->applet.ctx.errors.buf++;
4195 if (si->applet.ctx.errors.buf > 1) {
4196 si->applet.ctx.errors.buf = 0;
4197 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004198 }
4199 }
4200
4201 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004202 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004203}
4204
Willy Tarreaud5781202012-09-22 19:32:35 +02004205/* parse the "level" argument on the bind lines */
4206static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4207{
4208 if (!*args[cur_arg + 1]) {
4209 memprintf(err, "'%s' : missing level", args[cur_arg]);
4210 return ERR_ALERT | ERR_FATAL;
4211 }
4212
4213 if (!strcmp(args[cur_arg+1], "user"))
4214 conf->level = ACCESS_LVL_USER;
4215 else if (!strcmp(args[cur_arg+1], "operator"))
4216 conf->level = ACCESS_LVL_OPER;
4217 else if (!strcmp(args[cur_arg+1], "admin"))
4218 conf->level = ACCESS_LVL_ADMIN;
4219 else {
4220 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4221 args[cur_arg], args[cur_arg+1]);
4222 return ERR_ALERT | ERR_FATAL;
4223 }
4224
4225 return 0;
4226}
4227
Willy Tarreaub24281b2011-02-13 13:16:36 +01004228struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004229 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004230 .name = "<STATS>", /* used for logging */
4231 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004232 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004233};
4234
Simon Horman9bd2c732011-06-15 15:18:44 +09004235static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004236 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004237 .name = "<CLI>", /* used for logging */
4238 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004239 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004240};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004241
Willy Tarreau10522fd2008-07-09 20:12:41 +02004242static struct cfg_kw_list cfg_kws = {{ },{
4243 { CFG_GLOBAL, "stats", stats_parse_global },
4244 { 0, NULL, NULL },
4245}};
4246
Willy Tarreaud5781202012-09-22 19:32:35 +02004247static struct bind_kw_list bind_kws = { "STAT", { }, {
4248 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4249 { NULL, NULL, 0 },
4250}};
4251
Willy Tarreau10522fd2008-07-09 20:12:41 +02004252__attribute__((constructor))
4253static void __dumpstats_module_init(void)
4254{
4255 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004256 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004257}
4258
Willy Tarreau91861262007-10-17 17:06:05 +02004259/*
4260 * Local variables:
4261 * c-indent-level: 8
4262 * c-basic-offset: 8
4263 * End:
4264 */