blob: db82005eef5766675b23d9c11fe0b32223a3ff90 [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>
William Lallemande3a7d992012-11-20 11:25:20 +010045#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_raw_to_buffer(struct stream_interface *si);
65static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
66static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090068static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090069static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
71
72static struct si_applet cli_applet;
73
74static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020075 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020076 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020077 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020078 " help : this message\n"
79 " prompt : toggle interactive mode with prompt\n"
80 " quit : disconnect\n"
81 " show info : report information about the running process\n"
82 " show stat : report counters for each proxy and server\n"
83 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010084 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020085 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020086 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020087 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +020088 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010089 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020090 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020091 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020092 " disable : put a server or frontend in maintenance mode\n"
93 " enable : re-enable a server or frontend which is in maintenance mode\n"
94 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020095 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020096
Simon Horman9bd2c732011-06-15 15:18:44 +090097static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020098 "Permission denied\n"
99 "";
100
Willy Tarreau295a8372011-03-10 11:25:07 +0100101/* data transmission states for the stats responses */
102enum {
103 STAT_ST_INIT = 0,
104 STAT_ST_HEAD,
105 STAT_ST_INFO,
106 STAT_ST_LIST,
107 STAT_ST_END,
108 STAT_ST_FIN,
109};
110
111/* data transmission states for the stats responses inside a proxy */
112enum {
113 STAT_PX_ST_INIT = 0,
114 STAT_PX_ST_TH,
115 STAT_PX_ST_FE,
116 STAT_PX_ST_LI,
117 STAT_PX_ST_SV,
118 STAT_PX_ST_BE,
119 STAT_PX_ST_END,
120 STAT_PX_ST_FIN,
121};
122
Cyril Bonté19979e12012-04-04 12:57:21 +0200123extern const char *stat_status_codes[];
124
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200125/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200126 * a new stats socket. It returns a positive value upon success, 0 if the connection
127 * needs to be closed and ignored, or a negative value upon critical failure.
128 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900129static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200130{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200131 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100132 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100133 s->target = s->si[1].conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200134 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100135 s->si[1].applet.st1 = 0;
136 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200137
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200138 tv_zero(&s->logs.tv_request);
139 s->logs.t_queue = 0;
140 s->logs.t_connect = 0;
141 s->logs.t_data = 0;
142 s->logs.t_close = 0;
143 s->logs.bytes_in = s->logs.bytes_out = 0;
144 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
145 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200146
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200147 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200148
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200149 if (s->listener->timeout) {
150 s->req->rto = *s->listener->timeout;
151 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200152 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200153 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200154}
155
Willy Tarreau07e9e642010-08-17 21:48:17 +0200156/* allocate a new stats frontend named <name>, and return it
157 * (or NULL in case of lack of memory).
158 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200159static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200160{
161 struct proxy *fe;
162
163 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
164 if (!fe)
165 return NULL;
166
Willy Tarreau237250c2011-07-29 01:49:03 +0200167 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200168 fe->next = proxy;
169 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200170 fe->last_change = now.tv_sec;
171 fe->id = strdup("GLOBAL");
172 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200173 fe->maxconn = 10; /* default to 10 concurrent connections */
174 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200175 fe->conf.file = strdup(file);
176 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200177 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200178
179 /* the stats frontend is the only one able to assign ID #0 */
180 fe->conf.id.key = fe->uuid = 0;
181 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200182 return fe;
183}
184
Willy Tarreaufbee7132007-10-18 13:53:22 +0200185/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200186 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
187 * error message into the <err> buffer which will be preallocated. The trailing
188 * '\n' must not be written. The function must be called with <args> pointing to
189 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200190 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200191static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200192 struct proxy *defpx, const char *file, int line,
193 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200195 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200196 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200197
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200198 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200199 int cur_arg;
200
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200201 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200202 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 +0200203 return -1;
204 }
205
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200207 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200208 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200209 return -1;
210 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200211 }
212
Willy Tarreau4348fad2012-09-20 16:48:07 +0200213 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200214 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200215
Willy Tarreauc53d4222012-09-20 20:19:28 +0200216 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
217 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
218 file, line, args[0], args[1], err && *err ? *err : "error");
219 return -1;
220 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200221
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200222 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200223 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200224 static int bind_dumped;
225 struct bind_kw *kw;
226
227 kw = bind_find_kw(args[cur_arg]);
228 if (kw) {
229 if (!kw->parse) {
230 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
231 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200232 return -1;
233 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200234
235 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
236 if (err && *err)
237 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
238 else
239 memprintf(err, "'%s %s' : error encountered while processing '%s'",
240 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200241 return -1;
242 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200243
244 cur_arg += 1 + kw->skip;
245 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200246 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200247
248 if (!bind_dumped) {
249 bind_dump_kws(err);
250 indent_msg(err, 4);
251 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200252 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200253
254 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
255 args[0], args[1], args[cur_arg],
256 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
257 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200258 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100259
Willy Tarreauc53d4222012-09-20 20:19:28 +0200260 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
261 l->maxconn = global.stats_fe->maxconn;
262 l->backlog = global.stats_fe->backlog;
263 l->timeout = &global.stats_fe->timeout.client;
264 l->accept = session_accept;
265 l->handler = process_session;
266 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
267 l->nice = -64; /* we want to boost priority for local stats */
268 global.maxsock += l->maxconn;
269 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200270 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200271 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100272 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200273 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100274
275 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100277 return -1;
278 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100280 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 return -1;
283 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200284 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200285 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200287 return -1;
288 }
289 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200290 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 else if (!strcmp(args[1], "maxconn")) {
293 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200294
295 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200296 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 return -1;
298 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200299
300 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200301 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200303 return -1;
304 }
305 }
306 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200308 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
309 int cur_arg = 2;
310 unsigned int set = 0;
311
312 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100313 unsigned int low, high;
314
Willy Tarreau35b7b162012-10-22 23:17:18 +0200315 if (strcmp(args[cur_arg], "all") == 0) {
316 set = 0;
317 break;
318 }
319 else if (strcmp(args[cur_arg], "odd") == 0) {
320 set |= 0x55555555;
321 }
322 else if (strcmp(args[cur_arg], "even") == 0) {
323 set |= 0xAAAAAAAA;
324 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100325 else if (isdigit(*args[cur_arg])) {
326 char *dash = strchr(args[cur_arg], '-');
327
328 low = high = str2uic(args[cur_arg]);
329 if (dash)
330 high = str2uic(dash + 1);
331
332 if (high < low) {
333 unsigned int swap = low;
334 low = high;
335 high = swap;
336 }
337
338 if (low < 1 || high > 32) {
339 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
340 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200341 return -1;
342 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100343
344 while (low <= high)
345 set |= 1 << (low++ - 1);
346 }
347 else {
348 memprintf(err,
349 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
350 args[0], args[1]);
351 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200352 }
353 cur_arg++;
354 }
355 global.stats_fe->bind_proc = set;
356 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200357 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200358 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359 return -1;
360 }
361 return 0;
362}
363
Simon Horman9bd2c732011-06-15 15:18:44 +0900364static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100365{
Willy Tarreau77804732012-10-29 16:14:26 +0100366 return chunk_appendf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100367 "# pxname,svname,"
368 "qcur,qmax,"
369 "scur,smax,slim,stot,"
370 "bin,bout,"
371 "dreq,dresp,"
372 "ereq,econ,eresp,"
373 "wretr,wredis,"
374 "status,weight,act,bck,"
375 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200376 "pid,iid,sid,throttle,lbtot,tracked,type,"
377 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200378 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100379 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100380 "req_rate,req_rate_max,req_tot,"
381 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100382 "\n");
383}
384
Simon Hormand9366582011-06-15 15:18:45 +0900385/* print a string of text buffer to <out>. The format is :
386 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
387 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
388 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
389 */
390static int dump_text(struct chunk *out, const char *buf, int bsize)
391{
392 unsigned char c;
393 int ptr = 0;
394
395 while (buf[ptr] && ptr < bsize) {
396 c = buf[ptr];
397 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
398 if (out->len > out->size - 1)
399 break;
400 out->str[out->len++] = c;
401 }
402 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
403 if (out->len > out->size - 2)
404 break;
405 out->str[out->len++] = '\\';
406 switch (c) {
407 case ' ': c = ' '; break;
408 case '\t': c = 't'; break;
409 case '\n': c = 'n'; break;
410 case '\r': c = 'r'; break;
411 case '\e': c = 'e'; break;
412 case '\\': c = '\\'; break;
413 }
414 out->str[out->len++] = c;
415 }
416 else {
417 if (out->len > out->size - 4)
418 break;
419 out->str[out->len++] = '\\';
420 out->str[out->len++] = 'x';
421 out->str[out->len++] = hextab[(c >> 4) & 0xF];
422 out->str[out->len++] = hextab[c & 0xF];
423 }
424 ptr++;
425 }
426
427 return ptr;
428}
429
430/* print a buffer in hexa.
431 * Print stopped if <bsize> is reached, or if no more place in the chunk.
432 */
433static int dump_binary(struct chunk *out, const char *buf, int bsize)
434{
435 unsigned char c;
436 int ptr = 0;
437
438 while (ptr < bsize) {
439 c = buf[ptr];
440
441 if (out->len > out->size - 2)
442 break;
443 out->str[out->len++] = hextab[(c >> 4) & 0xF];
444 out->str[out->len++] = hextab[c & 0xF];
445
446 ptr++;
447 }
448 return ptr;
449}
450
451/* Dump the status of a table to a stream interface's
452 * read buffer. It returns 0 if the output buffer is full
453 * and needs to be called again, otherwise non-zero.
454 */
455static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
456 struct proxy *proxy, struct proxy *target)
457{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200458 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900459
Willy Tarreau77804732012-10-29 16:14:26 +0100460 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900461 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
462
463 /* any other information should be dumped here */
464
Willy Tarreau290e63a2012-09-20 18:07:14 +0200465 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100466 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900467
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200468 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900469 return 0;
470
471 return 1;
472}
473
474/* Dump the a table entry to a stream interface's
475 * read buffer. It returns 0 if the output buffer is full
476 * and needs to be called again, otherwise non-zero.
477 */
478static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
479 struct proxy *proxy, struct stksess *entry)
480{
481 int dt;
482
Willy Tarreau77804732012-10-29 16:14:26 +0100483 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900484
485 if (proxy->table.type == STKTABLE_TYPE_IP) {
486 char addr[INET_ADDRSTRLEN];
487 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100488 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900489 }
490 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
491 char addr[INET6_ADDRSTRLEN];
492 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100493 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900494 }
495 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100496 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900497 }
498 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100499 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900500 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
501 }
502 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100503 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900504 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
505 }
506
Willy Tarreau77804732012-10-29 16:14:26 +0100507 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900508
509 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
510 void *ptr;
511
512 if (proxy->table.data_ofs[dt] == 0)
513 continue;
514 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100515 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900516 else
Willy Tarreau77804732012-10-29 16:14:26 +0100517 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900518
519 ptr = stktable_data_ptr(&proxy->table, entry, dt);
520 switch (stktable_data_types[dt].std_type) {
521 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100522 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900523 break;
524 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100525 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900526 break;
527 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100528 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900529 break;
530 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900532 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
533 proxy->table.data_arg[dt].u));
534 break;
535 }
536 }
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900538
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200539 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900540 return 0;
541
542 return 1;
543}
544
Willy Tarreaudec98142012-06-06 23:37:08 +0200545static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900546{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200547 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900548 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900549 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900550 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900551 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200552 long long value;
553 int data_type;
554 void *ptr;
555 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900556
Simon Hormand5b9fd92011-06-15 15:18:48 +0900557 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900558
559 if (!*args[4]) {
560 si->applet.ctx.cli.msg = "Key value expected\n";
561 si->applet.st0 = STAT_CLI_PRINT;
562 return;
563 }
564
Simon Hormanc5b89f62011-06-15 15:18:50 +0900565 switch (px->table.type) {
566 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900567 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100568 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900569 break;
570 case STKTABLE_TYPE_IPV6:
571 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100572 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900573 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900574 case STKTABLE_TYPE_INTEGER:
575 {
576 char *endptr;
577 unsigned long val;
578 errno = 0;
579 val = strtoul(args[4], &endptr, 10);
580 if ((errno == ERANGE && val == ULONG_MAX) ||
581 (errno != 0 && val == 0) || endptr == args[4] ||
582 val > 0xffffffff) {
583 si->applet.ctx.cli.msg = "Invalid key\n";
584 si->applet.st0 = STAT_CLI_PRINT;
585 return;
586 }
587 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100588 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900589 break;
590 }
591 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900592 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100593 static_table_key->key = args[4];
594 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900595 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900596 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200597 switch (action) {
598 case STAT_CLI_O_TAB:
599 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
600 break;
601 case STAT_CLI_O_CLR:
602 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
603 break;
604 default:
605 si->applet.ctx.cli.msg = "Unknown action\n";
606 break;
607 }
Simon Horman121f3052011-06-15 15:18:46 +0900608 si->applet.st0 = STAT_CLI_PRINT;
609 return;
610 }
611
612 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200613 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900614 si->applet.ctx.cli.msg = stats_permission_denied_msg;
615 si->applet.st0 = STAT_CLI_PRINT;
616 return;
617 }
618
Willy Tarreau07115412012-10-29 21:56:59 +0100619 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900620
Willy Tarreaudec98142012-06-06 23:37:08 +0200621 switch (action) {
622 case STAT_CLI_O_TAB:
623 if (!ts)
624 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100625 chunk_reset(&trash);
626 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900627 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100628 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900629 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200630
631 case STAT_CLI_O_CLR:
632 if (!ts)
633 return;
634 if (ts->ref_cnt) {
635 /* don't delete an entry which is currently referenced */
636 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
637 si->applet.st0 = STAT_CLI_PRINT;
638 return;
639 }
640 stksess_kill(&px->table, ts);
641 break;
Simon Horman17bce342011-06-15 15:18:47 +0900642
Willy Tarreau654694e2012-06-07 01:03:16 +0200643 case STAT_CLI_O_SET:
644 if (strncmp(args[5], "data.", 5) != 0) {
645 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
646 si->applet.st0 = STAT_CLI_PRINT;
647 return;
648 }
649
650 data_type = stktable_get_data_type(args[5] + 5);
651 if (data_type < 0) {
652 si->applet.ctx.cli.msg = "Unknown data type\n";
653 si->applet.st0 = STAT_CLI_PRINT;
654 return;
655 }
656
657 if (!px->table.data_ofs[data_type]) {
658 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
659 si->applet.st0 = STAT_CLI_PRINT;
660 return;
661 }
662
663 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
664 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
665 si->applet.st0 = STAT_CLI_PRINT;
666 return;
667 }
668
669 if (ts)
670 stktable_touch(&px->table, ts, 1);
671 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100672 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200673 if (!ts) {
674 /* don't delete an entry which is currently referenced */
675 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
676 si->applet.st0 = STAT_CLI_PRINT;
677 return;
678 }
679 stktable_store(&px->table, ts, 1);
680 }
681
682 ptr = stktable_data_ptr(&px->table, ts, data_type);
683 switch (stktable_data_types[data_type].std_type) {
684 case STD_T_SINT:
685 stktable_data_cast(ptr, std_t_sint) = value;
686 break;
687 case STD_T_UINT:
688 stktable_data_cast(ptr, std_t_uint) = value;
689 break;
690 case STD_T_ULL:
691 stktable_data_cast(ptr, std_t_ull) = value;
692 break;
693 case STD_T_FRQP:
694 /* We only reset the previous value so that it slowly fades out */
695 frqp = &stktable_data_cast(ptr, std_t_frqp);
696 frqp->curr_tick = now_ms;
697 frqp->prev_ctr = value;
698 frqp->curr_ctr = 0;
699 break;
700 }
701 break;
702
Willy Tarreaudec98142012-06-06 23:37:08 +0200703 default:
704 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900705 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200706 break;
Simon Horman121f3052011-06-15 15:18:46 +0900707 }
Simon Horman121f3052011-06-15 15:18:46 +0900708}
709
Willy Tarreau654694e2012-06-07 01:03:16 +0200710static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900711{
Willy Tarreau654694e2012-06-07 01:03:16 +0200712 if (action != STAT_CLI_O_TAB) {
713 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
714 si->applet.st0 = STAT_CLI_PRINT;
715 return;
716 }
717
Simon Hormand5b9fd92011-06-15 15:18:48 +0900718 /* condition on stored data value */
719 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
720 if (si->applet.ctx.table.data_type < 0) {
721 si->applet.ctx.cli.msg = "Unknown data type\n";
722 si->applet.st0 = STAT_CLI_PRINT;
723 return;
724 }
725
726 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
727 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
728 si->applet.st0 = STAT_CLI_PRINT;
729 return;
730 }
731
732 si->applet.ctx.table.data_op = get_std_op(args[4]);
733 if (si->applet.ctx.table.data_op < 0) {
734 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
735 si->applet.st0 = STAT_CLI_PRINT;
736 return;
737 }
738
739 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
740 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
741 si->applet.st0 = STAT_CLI_PRINT;
742 return;
743 }
744}
745
Willy Tarreaudec98142012-06-06 23:37:08 +0200746static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900747{
748 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200749 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200750 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900751 si->applet.ctx.table.proxy = NULL;
752 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200753 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900754
755 if (*args[2]) {
756 si->applet.ctx.table.target = find_stktable(args[2]);
757 if (!si->applet.ctx.table.target) {
758 si->applet.ctx.cli.msg = "No such table\n";
759 si->applet.st0 = STAT_CLI_PRINT;
760 return;
761 }
762 }
763 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200764 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900765 goto err_args;
766 return;
767 }
768
769 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200770 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900771 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200772 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900773 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900774 goto err_args;
775
776 return;
777
778err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200779 switch (action) {
780 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900781 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 +0200782 break;
783 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900784 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 +0200785 break;
786 default:
787 si->applet.ctx.cli.msg = "Unknown action\n";
788 break;
789 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900790 si->applet.st0 = STAT_CLI_PRINT;
791}
792
Willy Tarreau532a4502011-09-07 22:37:44 +0200793/* Expects to find a frontend named <arg> and returns it, otherwise displays various
794 * adequate error messages and returns NULL. This function also expects the session
795 * level to be admin.
796 */
797static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
798{
799 struct proxy *px;
800
Willy Tarreau290e63a2012-09-20 18:07:14 +0200801 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200802 si->applet.ctx.cli.msg = stats_permission_denied_msg;
803 si->applet.st0 = STAT_CLI_PRINT;
804 return NULL;
805 }
806
807 if (!*arg) {
808 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
809 si->applet.st0 = STAT_CLI_PRINT;
810 return NULL;
811 }
812
813 px = findproxy(arg, PR_CAP_FE);
814 if (!px) {
815 si->applet.ctx.cli.msg = "No such frontend.\n";
816 si->applet.st0 = STAT_CLI_PRINT;
817 return NULL;
818 }
819 return px;
820}
821
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200822/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
823 * and returns the pointer to the server. Otherwise, display adequate error messages
824 * and returns NULL. This function also expects the session level to be admin. Note:
825 * the <arg> is modified to remove the '/'.
826 */
827static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
828{
829 struct proxy *px;
830 struct server *sv;
831 char *line;
832
Willy Tarreau290e63a2012-09-20 18:07:14 +0200833 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200834 si->applet.ctx.cli.msg = stats_permission_denied_msg;
835 si->applet.st0 = STAT_CLI_PRINT;
836 return NULL;
837 }
838
839 /* split "backend/server" and make <line> point to server */
840 for (line = arg; *line; line++)
841 if (*line == '/') {
842 *line++ = '\0';
843 break;
844 }
845
846 if (!*line || !*arg) {
847 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
848 si->applet.st0 = STAT_CLI_PRINT;
849 return NULL;
850 }
851
852 if (!get_backend_server(arg, line, &px, &sv)) {
853 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
854 si->applet.st0 = STAT_CLI_PRINT;
855 return NULL;
856 }
857
858 if (px->state == PR_STSTOPPED) {
859 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
860 si->applet.st0 = STAT_CLI_PRINT;
861 return NULL;
862 }
863
864 return sv;
865}
866
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200867/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200868 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100869 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200870 * designating the function which will have to process the request, which can
871 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200872 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900873static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200874{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200875 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200876 char *args[MAX_STATS_ARGS + 1];
877 int arg;
878
879 while (isspace((unsigned char)*line))
880 line++;
881
882 arg = 0;
883 args[arg] = line;
884
885 while (*line && arg < MAX_STATS_ARGS) {
886 if (isspace((unsigned char)*line)) {
887 *line++ = '\0';
888
889 while (isspace((unsigned char)*line))
890 line++;
891
892 args[++arg] = line;
893 continue;
894 }
895
896 line++;
897 }
898
899 while (++arg <= MAX_STATS_ARGS)
900 args[arg] = line;
901
Willy Tarreau295a8372011-03-10 11:25:07 +0100902 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200903 if (strcmp(args[0], "show") == 0) {
904 if (strcmp(args[1], "stat") == 0) {
905 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100906 si->applet.ctx.stats.flags |= STAT_BOUND;
907 si->applet.ctx.stats.iid = atoi(args[2]);
908 si->applet.ctx.stats.type = atoi(args[3]);
909 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200910 }
911
Willy Tarreau295a8372011-03-10 11:25:07 +0100912 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
913 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200914 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100915 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200916 }
917 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100918 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
919 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200920 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100921 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 }
923 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200924 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200925 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100926 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100927 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200928 return 1;
929 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100930 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100931 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100932 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100933 si->applet.ctx.sess.target = NULL;
934 si->applet.ctx.sess.section = 0; /* start with session status */
935 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100936 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200937 }
938 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200939 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100940 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100941 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200942 return 1;
943 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200944 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100945 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200946 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100947 si->applet.ctx.errors.iid = -1;
948 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200949 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200951 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200952 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200953 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200954 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700955 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200956 return 0;
957 }
958 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200959 else if (strcmp(args[0], "clear") == 0) {
960 if (strcmp(args[1], "counters") == 0) {
961 struct proxy *px;
962 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200963 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200964 int clrall = 0;
965
966 if (strcmp(args[2], "all") == 0)
967 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200968
Willy Tarreau6162db22009-10-10 17:13:00 +0200969 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200970 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
971 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100972 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100973 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200974 return 1;
975 }
976
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200977 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100978 if (clrall) {
979 memset(&px->be_counters, 0, sizeof(px->be_counters));
980 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
981 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200982 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100983 px->be_counters.conn_max = 0;
984 px->be_counters.p.http.rps_max = 0;
985 px->be_counters.sps_max = 0;
986 px->be_counters.cps_max = 0;
987 px->be_counters.nbpend_max = 0;
988
989 px->fe_counters.conn_max = 0;
990 px->fe_counters.p.http.rps_max = 0;
991 px->fe_counters.sps_max = 0;
992 px->fe_counters.cps_max = 0;
993 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200994 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200995
996 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200997 if (clrall)
998 memset(&sv->counters, 0, sizeof(sv->counters));
999 else {
1000 sv->counters.cur_sess_max = 0;
1001 sv->counters.nbpend_max = 0;
1002 sv->counters.sps_max = 0;
1003 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001004
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001005 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001006 if (li->counters) {
1007 if (clrall)
1008 memset(li->counters, 0, sizeof(*li->counters));
1009 else
1010 li->counters->conn_max = 0;
1011 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001012 }
1013
Willy Tarreau81c25d02011-09-07 15:17:21 +02001014 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001015 return 1;
1016 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001017 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001018 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001019 /* end of processing */
1020 return 1;
1021 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001022 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001023 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001024 return 0;
1025 }
1026 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001027 else if (strcmp(args[0], "get") == 0) {
1028 if (strcmp(args[1], "weight") == 0) {
1029 struct proxy *px;
1030 struct server *sv;
1031
1032 /* split "backend/server" and make <line> point to server */
1033 for (line = args[2]; *line; line++)
1034 if (*line == '/') {
1035 *line++ = '\0';
1036 break;
1037 }
1038
1039 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001040 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001041 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001042 return 1;
1043 }
1044
1045 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001046 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001047 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001048 return 1;
1049 }
1050
1051 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001052 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1053 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001054 return 1;
1055 }
1056 else { /* not "get weight" */
1057 return 0;
1058 }
1059 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001060 else if (strcmp(args[0], "set") == 0) {
1061 if (strcmp(args[1], "weight") == 0) {
1062 struct proxy *px;
1063 struct server *sv;
1064 int w;
1065
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001066 sv = expect_server_admin(s, si, args[2]);
1067 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001068 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001069 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001070
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001071 /* if the weight is terminated with '%', it is set relative to
1072 * the initial weight, otherwise it is absolute.
1073 */
1074 if (!*args[3]) {
1075 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001076 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001077 return 1;
1078 }
1079
Willy Tarreau4483d432009-10-10 19:30:08 +02001080 w = atoi(args[3]);
1081 if (strchr(args[3], '%') != NULL) {
1082 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001083 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001084 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001085 return 1;
1086 }
1087 w = sv->iweight * w / 100;
1088 }
1089 else {
1090 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001091 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001092 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001093 return 1;
1094 }
1095 }
1096
1097 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001098 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 +01001099 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001100 return 1;
1101 }
1102
1103 sv->uweight = w;
1104
1105 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1106 /* we must take care of not pushing the server to full throttle during slow starts */
1107 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1108 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1109 else
1110 sv->eweight = BE_WEIGHT_SCALE;
1111 sv->eweight *= sv->uweight;
1112 } else {
1113 sv->eweight = sv->uweight;
1114 }
1115
1116 /* static LB algorithms are a bit harder to update */
1117 if (px->lbprm.update_server_eweight)
1118 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001119 else if (sv->eweight) {
1120 if (px->lbprm.set_server_status_up)
1121 px->lbprm.set_server_status_up(sv);
1122 }
1123 else {
1124 if (px->lbprm.set_server_status_down)
1125 px->lbprm.set_server_status_down(sv);
1126 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001127
1128 return 1;
1129 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001130 else if (strcmp(args[1], "timeout") == 0) {
1131 if (strcmp(args[2], "cli") == 0) {
1132 unsigned timeout;
1133 const char *res;
1134
1135 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001136 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001137 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001138 return 1;
1139 }
1140
1141 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1142 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001143 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001144 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001145 return 1;
1146 }
1147
1148 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1149 return 1;
1150 }
1151 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001152 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001153 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001154 return 1;
1155 }
1156 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001157 else if (strcmp(args[1], "maxconn") == 0) {
1158 if (strcmp(args[2], "frontend") == 0) {
1159 struct proxy *px;
1160 struct listener *l;
1161 int v;
1162
Willy Tarreau532a4502011-09-07 22:37:44 +02001163 px = expect_frontend_admin(s, si, args[3]);
1164 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001165 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001166
1167 if (!*args[4]) {
1168 si->applet.ctx.cli.msg = "Integer value expected.\n";
1169 si->applet.st0 = STAT_CLI_PRINT;
1170 return 1;
1171 }
1172
1173 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001174 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001175 si->applet.ctx.cli.msg = "Value out of range.\n";
1176 si->applet.st0 = STAT_CLI_PRINT;
1177 return 1;
1178 }
1179
1180 /* OK, the value is fine, so we assign it to the proxy and to all of
1181 * its listeners. The blocked ones will be dequeued.
1182 */
1183 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001184 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001185 l->maxconn = v;
1186 if (l->state == LI_FULL)
1187 resume_listener(l);
1188 }
1189
1190 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1191 dequeue_all_listeners(&s->fe->listener_queue);
1192
1193 return 1;
1194 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001195 else if (strcmp(args[2], "global") == 0) {
1196 int v;
1197
Willy Tarreau290e63a2012-09-20 18:07:14 +02001198 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001199 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1200 si->applet.st0 = STAT_CLI_PRINT;
1201 return 1;
1202 }
1203
1204 if (!*args[3]) {
1205 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1206 si->applet.st0 = STAT_CLI_PRINT;
1207 return 1;
1208 }
1209
1210 v = atoi(args[3]);
1211 if (v > global.hardmaxconn) {
1212 si->applet.ctx.cli.msg = "Value out of range.\n";
1213 si->applet.st0 = STAT_CLI_PRINT;
1214 return 1;
1215 }
1216
1217 /* check for unlimited values */
1218 if (v <= 0)
1219 v = global.hardmaxconn;
1220
1221 global.maxconn = v;
1222
1223 /* Dequeues all of the listeners waiting for a resource */
1224 if (!LIST_ISEMPTY(&global_listener_queue))
1225 dequeue_all_listeners(&global_listener_queue);
1226
1227 return 1;
1228 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001229 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001230 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001231 si->applet.st0 = STAT_CLI_PRINT;
1232 return 1;
1233 }
1234 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001235 else if (strcmp(args[1], "rate-limit") == 0) {
1236 if (strcmp(args[2], "connections") == 0) {
1237 if (strcmp(args[3], "global") == 0) {
1238 int v;
1239
Willy Tarreau290e63a2012-09-20 18:07:14 +02001240 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001241 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1242 si->applet.st0 = STAT_CLI_PRINT;
1243 return 1;
1244 }
1245
1246 if (!*args[4]) {
1247 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1248 si->applet.st0 = STAT_CLI_PRINT;
1249 return 1;
1250 }
1251
1252 v = atoi(args[4]);
1253 if (v < 0) {
1254 si->applet.ctx.cli.msg = "Value out of range.\n";
1255 si->applet.st0 = STAT_CLI_PRINT;
1256 return 1;
1257 }
1258
1259 global.cps_lim = v;
1260
1261 /* Dequeues all of the listeners waiting for a resource */
1262 if (!LIST_ISEMPTY(&global_listener_queue))
1263 dequeue_all_listeners(&global_listener_queue);
1264
1265 return 1;
1266 }
1267 else {
1268 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1269 si->applet.st0 = STAT_CLI_PRINT;
1270 return 1;
1271 }
1272 }
William Lallemandd85f9172012-11-09 17:05:39 +01001273 else if (strcmp(args[2], "http-compression") == 0) {
1274 if (strcmp(args[3], "global") == 0) {
1275 int v;
1276
Willy Tarreau85d47f92012-11-21 00:29:50 +01001277 if (!*args[4]) {
1278 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1279 si->applet.st0 = STAT_CLI_PRINT;
1280 return 1;
1281 }
1282
William Lallemandd85f9172012-11-09 17:05:39 +01001283 v = atoi(args[4]);
1284 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1285 }
1286 else {
1287 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1288 si->applet.st0 = STAT_CLI_PRINT;
1289 return 1;
1290 }
1291 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001292 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001293 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001294 si->applet.st0 = STAT_CLI_PRINT;
1295 return 1;
1296 }
1297 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001298 else if (strcmp(args[1], "table") == 0) {
1299 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1300 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001301 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001302 return 0;
1303 }
1304 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001305 else if (strcmp(args[0], "enable") == 0) {
1306 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001307 struct server *sv;
1308
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001309 sv = expect_server_admin(s, si, args[2]);
1310 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001311 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001312
Cyril Bontécd19e512010-01-31 22:34:03 +01001313 if (sv->state & SRV_MAINTAIN) {
1314 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001315 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001316 /* If this server tracks the status of another one,
1317 * we must restore the good status.
1318 */
Willy Tarreau44267702011-10-28 15:35:33 +02001319 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001320 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 } else {
1323 sv->state &= ~SRV_MAINTAIN;
1324 set_server_down(sv);
1325 }
1326 } else {
1327 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001328 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001329 }
1330 }
1331
Willy Tarreau532a4502011-09-07 22:37:44 +02001332 return 1;
1333 }
1334 else if (strcmp(args[1], "frontend") == 0) {
1335 struct proxy *px;
1336
1337 px = expect_frontend_admin(s, si, args[2]);
1338 if (!px)
1339 return 1;
1340
1341 if (px->state == PR_STSTOPPED) {
1342 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1343 si->applet.st0 = STAT_CLI_PRINT;
1344 return 1;
1345 }
1346
1347 if (px->state != PR_STPAUSED) {
1348 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1349 si->applet.st0 = STAT_CLI_PRINT;
1350 return 1;
1351 }
1352
1353 if (!resume_proxy(px)) {
1354 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1355 si->applet.st0 = STAT_CLI_PRINT;
1356 return 1;
1357 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001358 return 1;
1359 }
1360 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001361 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1362 si->applet.st0 = STAT_CLI_PRINT;
1363 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001364 }
1365 }
1366 else if (strcmp(args[0], "disable") == 0) {
1367 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001368 struct server *sv;
1369
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001370 sv = expect_server_admin(s, si, args[2]);
1371 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001372 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001373
Cyril Bontécd19e512010-01-31 22:34:03 +01001374 if (! (sv->state & SRV_MAINTAIN)) {
1375 /* Not already in maintenance, we can change the server state */
1376 sv->state |= SRV_MAINTAIN;
1377 set_server_down(sv);
1378 }
1379
Willy Tarreau532a4502011-09-07 22:37:44 +02001380 return 1;
1381 }
1382 else if (strcmp(args[1], "frontend") == 0) {
1383 struct proxy *px;
1384
1385 px = expect_frontend_admin(s, si, args[2]);
1386 if (!px)
1387 return 1;
1388
1389 if (px->state == PR_STSTOPPED) {
1390 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1391 si->applet.st0 = STAT_CLI_PRINT;
1392 return 1;
1393 }
1394
1395 if (px->state == PR_STPAUSED) {
1396 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1397 si->applet.st0 = STAT_CLI_PRINT;
1398 return 1;
1399 }
1400
1401 if (!pause_proxy(px)) {
1402 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1403 si->applet.st0 = STAT_CLI_PRINT;
1404 return 1;
1405 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001406 return 1;
1407 }
1408 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001409 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1410 si->applet.st0 = STAT_CLI_PRINT;
1411 return 1;
1412 }
1413 }
1414 else if (strcmp(args[0], "shutdown") == 0) {
1415 if (strcmp(args[1], "frontend") == 0) {
1416 struct proxy *px;
1417
1418 px = expect_frontend_admin(s, si, args[2]);
1419 if (!px)
1420 return 1;
1421
1422 if (px->state == PR_STSTOPPED) {
1423 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1424 si->applet.st0 = STAT_CLI_PRINT;
1425 return 1;
1426 }
1427
1428 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1429 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1430 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1431 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1432 stop_proxy(px);
1433 return 1;
1434 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001435 else if (strcmp(args[1], "session") == 0) {
1436 struct session *sess, *ptr;
1437
Willy Tarreau290e63a2012-09-20 18:07:14 +02001438 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001439 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1440 si->applet.st0 = STAT_CLI_PRINT;
1441 return 1;
1442 }
1443
1444 if (!*args[2]) {
1445 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1446 si->applet.st0 = STAT_CLI_PRINT;
1447 return 1;
1448 }
1449
1450 ptr = (void *)strtoul(args[2], NULL, 0);
1451
1452 /* first, look for the requested session in the session table */
1453 list_for_each_entry(sess, &sessions, list) {
1454 if (sess == ptr)
1455 break;
1456 }
1457
1458 /* do we have the session ? */
1459 if (sess != ptr) {
1460 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1461 si->applet.st0 = STAT_CLI_PRINT;
1462 return 1;
1463 }
1464
1465 session_shutdown(sess, SN_ERR_KILLED);
1466 return 1;
1467 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001468 else if (strcmp(args[1], "sessions") == 0) {
1469 if (strcmp(args[2], "server") == 0) {
1470 struct server *sv;
1471 struct session *sess, *sess_bck;
1472
1473 sv = expect_server_admin(s, si, args[3]);
1474 if (!sv)
1475 return 1;
1476
1477 /* kill all the session that are on this server */
1478 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1479 if (sess->srv_conn == sv)
1480 session_shutdown(sess, SN_ERR_KILLED);
1481
1482 return 1;
1483 }
1484 else {
1485 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1486 si->applet.st0 = STAT_CLI_PRINT;
1487 return 1;
1488 }
1489 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001490 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001491 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001492 si->applet.st0 = STAT_CLI_PRINT;
1493 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001494 }
1495 }
1496 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001497 return 0;
1498 }
1499 return 1;
1500}
1501
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001502/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001503 * used to processes I/O from/to the stats unix socket. The system relies on a
1504 * state machine handling requests and various responses. We read a request,
1505 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001506 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1507 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001508 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001509 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001510static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001511{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001512 struct channel *req = si->ob;
1513 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001514 int reql;
1515 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001516
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001517 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1518 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001519
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001520 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001521 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001522 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001523 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001524 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001526 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001527 /* Let's close for real now. We just close the request
1528 * side, the conditions below will complete if needed.
1529 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001530 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001531 break;
1532 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001533 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001534 /* ensure we have some output room left in the event we
1535 * would want to return some info right after parsing.
1536 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001537 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001538 break;
1539
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001540 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 if (reql <= 0) { /* closed or EOL not found */
1542 if (reql == 0)
1543 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 continue;
1546 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001547
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001548 /* seek for a possible semi-colon. If we find one, we
1549 * replace it with an LF and skip only this part.
1550 */
1551 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001552 if (trash.str[len] == ';') {
1553 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001554 reql = len + 1;
1555 break;
1556 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001557
Willy Tarreau816fc222009-10-04 07:36:58 +02001558 /* now it is time to check that we have a full line,
1559 * remove the trailing \n and possibly \r, then cut the
1560 * line.
1561 */
1562 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001563 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001564 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001565 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001566 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001567
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001568 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001569 len--;
1570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001571 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572
Willy Tarreaubc4af052011-02-13 13:25:14 +01001573 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001574 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001575 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001576 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001577 continue;
1578 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001579 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001580 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001581 else if (strcmp(trash.str, "help") == 0 ||
1582 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001583 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001584 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001585 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001586 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001587 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001588 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001589 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001590 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001591 /* if prompt is disabled, print help on empty lines,
1592 * so that the user at least knows how to enable
1593 * prompt and find help.
1594 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001595 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001596 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597 }
1598
1599 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001600 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001601 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001602 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001603 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001604 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001605 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001606 continue;
1607 }
1608
Willy Tarreaubc4af052011-02-13 13:25:14 +01001609 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001610 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001611 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001612 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001613 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001614 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001615 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001616 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001617 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001618 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001619 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001620 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001622 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001623 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001624 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001625 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001626 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001627 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001628 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001629 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001630 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001631 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001632 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001633 break;
1634 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001635
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001636 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001637 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001638 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001639 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001640 }
1641
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001642 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001643 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001644 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001645
1646 /* Now we close the output if one of the writers did so,
1647 * or if we're not in interactive mode and the request
1648 * buffer is empty. This still allows pipelined requests
1649 * to be sent in non-interactive mode.
1650 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001651 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001652 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001653 continue;
1654 }
1655
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001656 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001657 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001658 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001659 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001660
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001661 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001662 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1663 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001664 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001665 * and nothing more to consume. This is comparable to a broken pipe, so
1666 * we forward the close to the request side so that it flows upstream to
1667 * the client.
1668 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001669 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001670 }
1671
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001672 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001673 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1674 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1675 /* We have no more processing to do, and nothing more to send, and
1676 * the client side has closed. So we'll forward this state downstream
1677 * on the response buffer.
1678 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001679 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001680 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001681 }
1682
1683 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001684 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001685
1686 /* we don't want to expire timeouts while we're processing requests */
1687 si->ib->rex = TICK_ETERNITY;
1688 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001689
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001690 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001691 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 +02001692 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001693 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 +02001694
1695 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1696 /* check that we have released everything then unregister */
1697 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001698 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001699}
1700
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001701/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001702 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001703 * It returns 0 as long as it does not complete, non-zero upon completion.
1704 * Some states are not used but it makes the code more similar to other
1705 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001706 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001707static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001708{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001709 struct proxy *px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001710 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001712 chunk_reset(&trash);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001713
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001714 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001715 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001716 /* the function had not been called yet */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001717 si->conn->xprt_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001718 /* fall through */
1719
Willy Tarreau295a8372011-03-10 11:25:07 +01001720 case STAT_ST_HEAD:
1721 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001722 print_csv_header(&trash);
1723 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001724 return 0;
1725 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001726
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001727 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001728 /* fall through */
1729
Willy Tarreau295a8372011-03-10 11:25:07 +01001730 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001731 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001732 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001733 chunk_appendf(&trash,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001734 "Name: " PRODUCT_NAME "\n"
1735 "Version: " HAPROXY_VERSION "\n"
1736 "Release_date: " HAPROXY_DATE "\n"
1737 "Nbproc: %d\n"
1738 "Process_num: %d\n"
1739 "Pid: %d\n"
1740 "Uptime: %dd %dh%02dm%02ds\n"
1741 "Uptime_sec: %d\n"
1742 "Memmax_MB: %d\n"
1743 "Ulimit-n: %d\n"
1744 "Maxsock: %d\n"
1745 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001746 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001747 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001748 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001749 "PipesUsed: %d\n"
1750 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001751 "ConnRate: %d\n"
1752 "ConnRateLimit: %d\n"
1753 "MaxConnRate: %d\n"
William Lallemandd85f9172012-11-09 17:05:39 +01001754 "CompressBpsIn: %u\n"
1755 "CompressBpsOut: %u\n"
William Lallemand096f5542012-11-19 17:26:05 +01001756 "CompressBpsRateLim: %u\n"
William Lallemande3a7d992012-11-20 11:25:20 +01001757#ifdef USE_ZLIB
1758 "ZlibMemUsage: %ld\n"
1759 "MaxZlibMemUsage: %ld\n"
1760#endif
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001761 "Tasks: %d\n"
1762 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001763 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001764 "node: %s\n"
1765 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001766 "",
1767 global.nbproc,
1768 relative_pid,
1769 pid,
1770 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1771 up,
1772 global.rlimit_memmax,
1773 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001774 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001775 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001776 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
William Lallemandd85f9172012-11-09 17:05:39 +01001777 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
William Lallemand096f5542012-11-19 17:26:05 +01001778 global.comp_rate_lim,
William Lallemande3a7d992012-11-20 11:25:20 +01001779#ifdef USE_ZLIB
1780 zlib_used_memory, global.maxzlibmem,
1781#endif
Willy Tarreau45a12512011-09-10 16:56:42 +02001782 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001783 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001784 );
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001785 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001786 return 0;
1787 }
1788
Willy Tarreau295a8372011-03-10 11:25:07 +01001789 si->applet.ctx.stats.px = proxy;
1790 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1791 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001792 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001793 /* fall through */
1794
Willy Tarreau295a8372011-03-10 11:25:07 +01001795 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001796 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001797 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1798 while (si->applet.ctx.stats.px) {
1799 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02001800 /* skip the disabled proxies, global frontend and non-networked ones */
1801 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001802 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001803 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001804 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001805 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001806
Willy Tarreau295a8372011-03-10 11:25:07 +01001807 si->applet.ctx.stats.px = px->next;
1808 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001809 }
1810 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001811 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001812
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001813 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001814 /* fall through */
1815
Willy Tarreau295a8372011-03-10 11:25:07 +01001816 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001817 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001818 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001819
Willy Tarreau295a8372011-03-10 11:25:07 +01001820 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001821 return 1;
1822
1823 default:
1824 /* unknown state ! */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001825 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001826 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001827 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001828}
1829
1830
Cyril Bonté70be45d2010-10-12 00:14:35 +02001831/* We don't want to land on the posted stats page because a refresh will
1832 * repost the data. We don't want this to happen on accident so we redirect
1833 * the browse to the stats page with a GET.
1834 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001835static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001836{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001837 struct session *s = si->conn->xprt_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001838
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001839 chunk_reset(&trash);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001840
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001841 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001842 case STAT_ST_INIT:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001843 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02001844 "HTTP/1.0 303 See Other\r\n"
1845 "Cache-Control: no-cache\r\n"
1846 "Content-Type: text/plain\r\n"
1847 "Connection: close\r\n"
1848 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001849 uri->uri_prefix,
1850 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1851 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1852 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1853 stat_status_codes[si->applet.ctx.stats.st_code] :
1854 stat_status_codes[STAT_STATUS_UNKN]);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001855 chunk_appendf(&trash, "\r\n\r\n");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001856
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001857 if (bi_putchk(si->ib, &trash) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001858 return 0;
1859
1860 s->txn.status = 303;
1861
1862 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1863 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1864 if (!(s->flags & SN_FINST_MASK))
1865 s->flags |= SN_FINST_R;
1866
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001867 si->conn->xprt_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001868 return 1;
1869 }
1870 return 1;
1871}
1872
1873
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001874/* This I/O handler runs as an applet embedded in a stream interface. It is
1875 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001876 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001877 * automatically unregisters itself once transfer is complete.
1878 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001879static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001880{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001881 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001882 struct channel *req = si->ob;
1883 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001884
1885 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1886 goto out;
1887
1888 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001889 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001890 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001891
Willy Tarreaubc4af052011-02-13 13:25:14 +01001892 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001893 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001894 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001895 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001896 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001897 }
1898 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001899 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001900 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001901 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001902 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001903 }
1904 }
1905
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001906 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001907 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001908
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001909 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001910 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001911 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001912 }
1913
1914 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001915 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001916
1917 /* we don't want to expire timeouts while we're processing requests */
1918 si->ib->rex = TICK_ETERNITY;
1919 si->ob->wex = TICK_ETERNITY;
1920
1921 out:
1922 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1923 /* check that we have released everything then unregister */
1924 stream_int_unregister_handler(si);
1925 }
1926}
1927
1928
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001929/* This function dumps statistics in HTTP format onto the stream interface's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001930 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001931 * properly set. It returns 0 if it had to stop writing data and an I/O is
1932 * needed, 1 if the dump is finished and the session must be closed, or -1
1933 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001934 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001935static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001936{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001937 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001938 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001939 struct proxy *px;
Willy Tarreau91861262007-10-17 17:06:05 +02001940 unsigned int up;
1941
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001942 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02001943
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001944 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001945 case STAT_ST_INIT:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001946 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02001947 "HTTP/1.0 200 OK\r\n"
1948 "Cache-Control: no-cache\r\n"
1949 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001950 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001951 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001952
Willy Tarreau295a8372011-03-10 11:25:07 +01001953 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001954 chunk_appendf(&trash, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001955 uri->refresh);
1956
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001957 chunk_appendf(&trash, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001958
1959 s->txn.status = 200;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001960 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001961 return 0;
1962
Willy Tarreau91861262007-10-17 17:06:05 +02001963 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1964 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1965 if (!(s->flags & SN_FINST_MASK))
1966 s->flags |= SN_FINST_R;
1967
1968 if (s->txn.meth == HTTP_METH_HEAD) {
1969 /* that's all we return in case of HEAD request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001970 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001971 return 1;
1972 }
1973
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001974 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001975 /* fall through */
1976
Willy Tarreau295a8372011-03-10 11:25:07 +01001977 case STAT_ST_HEAD:
1978 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001979 /* WARNING! This must fit in the first buffer !!! */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001980 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001981 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1982 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001983 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001984 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1985 "<style type=\"text/css\"><!--\n"
1986 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001987 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001988 " font-size: 12px;"
1989 " font-weight: normal;"
1990 " color: black;"
1991 " background: white;"
1992 "}\n"
1993 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001994 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001995 "}\n"
1996 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001997 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001998 " margin-bottom: 0.5em;"
1999 "}\n"
2000 "h2 {"
2001 " font-family: helvetica, arial;"
2002 " font-size: x-large;"
2003 " font-weight: bold;"
2004 " font-style: italic;"
2005 " color: #6020a0;"
2006 " margin-top: 0em;"
2007 " margin-bottom: 0em;"
2008 "}\n"
2009 "h3 {"
2010 " font-family: helvetica, arial;"
2011 " font-size: 16px;"
2012 " font-weight: bold;"
2013 " color: #b00040;"
2014 " background: #e8e8d0;"
2015 " margin-top: 0em;"
2016 " margin-bottom: 0em;"
2017 "}\n"
2018 "li {"
2019 " margin-top: 0.25em;"
2020 " margin-right: 2em;"
2021 "}\n"
2022 ".hr {margin-top: 0.25em;"
2023 " border-color: black;"
2024 " border-bottom-style: solid;"
2025 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002026 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002027 ".total {background: #20D0D0;color: #ffff80;}\n"
2028 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002029 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002030 ".backend {background: #e8e8d0;}\n"
2031 ".active0 {background: #ff9090;}\n"
2032 ".active1 {background: #ffd020;}\n"
2033 ".active2 {background: #ffffa0;}\n"
2034 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002035 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
2036 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
2037 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002038 ".backup0 {background: #ff9090;}\n"
2039 ".backup1 {background: #ff80ff;}\n"
2040 ".backup2 {background: #c060ff;}\n"
2041 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002042 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
2043 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
2044 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01002045 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02002046 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002047 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002048 "a.px:link {color: #ffff40; text-decoration: none;}"
2049 "a.px:visited {color: #ffff40; text-decoration: none;}"
2050 "a.px:hover {color: #ffffff; text-decoration: none;}"
2051 "a.lfsb:link {color: #000000; text-decoration: none;}"
2052 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2053 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2054 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002055 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002056 "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"
2057 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002058 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002059 "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 +02002060 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002061 "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 +02002062 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002063 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2064 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2065 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002066 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002067 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002068 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002069 (uri->flags&ST_SHNODE) ? " on " : "",
2070 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002071 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002072 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002073 print_csv_header(&trash);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002074 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002075 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002076 return 0;
2077
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002078 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002079 /* fall through */
2080
Willy Tarreau295a8372011-03-10 11:25:07 +01002081 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002082 up = (now.tv_sec - start_date.tv_sec);
2083
2084 /* WARNING! this has to fit the first packet too.
2085 * We are around 3.5 kB, add adding entries will
2086 * become tricky if we want to support 4kB buffers !
2087 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002088 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002089 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002090 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2091 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002092 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002093 "<hr width=\"100%%\" class=\"hr\">\n"
2094 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002095 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002096 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002097 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002098 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2099 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002100 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002101 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002102 "</td><td align=\"center\" nowrap>\n"
2103 "<table class=\"lgd\"><tr>\n"
2104 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2105 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2106 "</tr><tr>\n"
2107 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2108 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2109 "</tr><tr>\n"
2110 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2111 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2112 "</tr><tr>\n"
2113 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002114 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002115 "</tr><tr>\n"
2116 "<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 +02002117 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002118 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002119 "</td>"
2120 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2121 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2122 "",
2123 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002124 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2125 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2126 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002127 up / 86400, (up % 86400) / 3600,
2128 (up % 3600) / 60, (up % 60),
2129 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2130 global.rlimit_memmax ? " MB" : "",
2131 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002132 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002133 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002134 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002135 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002136
Willy Tarreau295a8372011-03-10 11:25:07 +01002137 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002138 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002139 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2140 uri->uri_prefix,
2141 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002142 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002143 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002144 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002145 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2146 uri->uri_prefix,
2147 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002148 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002149
Willy Tarreau55bb8452007-10-17 18:44:57 +02002150 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002151 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002152 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002153 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2154 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002155 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002156 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002157 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002158 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002159 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2160 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002161 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002162 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002163 }
Willy Tarreau91861262007-10-17 17:06:05 +02002164
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002165 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002166 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2167 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002168 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2169 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002171 chunk_appendf(&trash,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002172 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2173 uri->uri_prefix,
2174 (uri->refresh > 0) ? ";norefresh" : "");
2175
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002176 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002177 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002178 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002179 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002180 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2181 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2182 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2183 "</ul>"
2184 "</td>"
2185 "</tr></table>\n"
2186 ""
2187 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002188
Willy Tarreau295a8372011-03-10 11:25:07 +01002189 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002190 switch (si->applet.ctx.stats.st_code) {
2191 case STAT_STATUS_DONE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002192 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002193 "<p><div class=active3>"
2194 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2195 "Action processed successfully."
2196 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002197 break;
2198 case STAT_STATUS_NONE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002199 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002200 "<p><div class=active2>"
2201 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2202 "Nothing has changed."
2203 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002204 break;
2205 case STAT_STATUS_PART:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002206 chunk_appendf(&trash,
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002207 "<p><div class=active2>"
2208 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2209 "Action partially processed.<br>"
2210 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2211 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002212 break;
2213 case STAT_STATUS_ERRP:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002214 chunk_appendf(&trash,
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002215 "<p><div class=active0>"
2216 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2217 "Action not processed because of invalid parameters."
2218 "<ul>"
2219 "<li>The action is maybe unknown.</li>"
2220 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2221 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2222 "</ul>"
2223 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002224 break;
2225 case STAT_STATUS_EXCD:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002226 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002227 "<p><div class=active0>"
2228 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2229 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2230 "You should retry with less servers at a time.</b>"
2231 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002232 break;
2233 case STAT_STATUS_DENY:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002234 chunk_appendf(&trash,
Cyril Bonté474be412010-10-12 00:14:36 +02002235 "<p><div class=active0>"
2236 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2237 "<b>Action denied.</b>"
2238 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002239 break;
2240 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002241 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002242 "<p><div class=active6>"
2243 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2244 "Unexpected result."
2245 "</div>\n", uri->uri_prefix);
2246 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002247 chunk_appendf(&trash,"<p>\n");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002248 }
2249
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002250 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002251 return 0;
2252 }
Willy Tarreau91861262007-10-17 17:06:05 +02002253
Willy Tarreau295a8372011-03-10 11:25:07 +01002254 si->applet.ctx.stats.px = proxy;
2255 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002256 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002257 /* fall through */
2258
Willy Tarreau295a8372011-03-10 11:25:07 +01002259 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002260 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002261 while (si->applet.ctx.stats.px) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002262 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002263 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002264 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02002265 /* skip the disabled proxies, global frontend and non-networked ones */
2266 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002267 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002268 return 0;
2269
Willy Tarreau295a8372011-03-10 11:25:07 +01002270 si->applet.ctx.stats.px = px->next;
2271 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002272 }
2273 /* here, we just have reached the last proxy */
2274
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002275 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002276 /* fall through */
2277
Willy Tarreau295a8372011-03-10 11:25:07 +01002278 case STAT_ST_END:
2279 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002280 chunk_appendf(&trash, "</body></html>\n");
2281 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002282 return 0;
2283 }
Willy Tarreau91861262007-10-17 17:06:05 +02002284
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002285 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002286 /* fall through */
2287
Willy Tarreau295a8372011-03-10 11:25:07 +01002288 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002289 return 1;
2290
2291 default:
2292 /* unknown state ! */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002293 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002294 return -1;
2295 }
2296}
2297
2298
2299/*
2300 * Dumps statistics for a proxy.
2301 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2302 * ot non-zero if everything completed.
2303 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002304static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002305{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002306 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002307 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002308 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002309 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002310
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002311 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002312
Willy Tarreau295a8372011-03-10 11:25:07 +01002313 switch (si->applet.ctx.stats.px_st) {
2314 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002315 /* we are on a new proxy */
2316
2317 if (uri && uri->scope) {
2318 /* we have a limited scope, we have to check the proxy name */
2319 struct stat_scope *scope;
2320 int len;
2321
2322 len = strlen(px->id);
2323 scope = uri->scope;
2324
2325 while (scope) {
2326 /* match exact proxy name */
2327 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2328 break;
2329
2330 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002331 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002332 break;
2333 scope = scope->next;
2334 }
2335
2336 /* proxy name not found : don't dump anything */
2337 if (scope == NULL)
2338 return 1;
2339 }
2340
Willy Tarreau295a8372011-03-10 11:25:07 +01002341 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2342 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002343 return 1;
2344
Willy Tarreau295a8372011-03-10 11:25:07 +01002345 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002346 /* fall through */
2347
Willy Tarreau295a8372011-03-10 11:25:07 +01002348 case STAT_PX_ST_TH:
2349 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2350 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002351 /* A form to enable/disable this proxy servers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002352 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002353 "<form action=\"%s\" method=\"post\">",
2354 uri->uri_prefix);
2355 }
2356
Willy Tarreau55bb8452007-10-17 18:44:57 +02002357 /* print a new table */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002358 chunk_appendf(&trash,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002359 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002360 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002361 "<th class=\"pxname\" width=\"10%%\"");
2362
2363 if (uri->flags&ST_SHLGNDS) {
2364 /* cap, mode, id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002365 chunk_appendf(&trash, " title=\"cap: %s, mode: %s, id: %d",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002366 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2367 px->uuid);
2368
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002369 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002370 }
2371
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002372 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002373 ">%s<a name=\"%s\"></a>"
2374 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002375 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002376 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002377 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002378 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002379 "<tr class=\"titre\">",
2380 (uri->flags & ST_SHLGNDS)?"<u>":"",
2381 px->id, px->id, px->id,
2382 (uri->flags & ST_SHLGNDS)?"</u>":"",
2383 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2384
Willy Tarreau295a8372011-03-10 11:25:07 +01002385 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002386 /* Column heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002387 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002388 }
2389
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002390 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002391 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002392 "<th colspan=3>Queue</th>"
2393 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002394 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002395 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002396 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002397 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002398 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002399 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002400 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002401 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002402 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002403 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002404 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002405 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2406 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002407 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002409 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002410 return 0;
2411 }
Willy Tarreau91861262007-10-17 17:06:05 +02002412
Willy Tarreau295a8372011-03-10 11:25:07 +01002413 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002414 /* fall through */
2415
Willy Tarreau295a8372011-03-10 11:25:07 +01002416 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002417 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002418 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002419 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2420 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002421 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002422 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002423 "<tr class=\"frontend\">");
2424
Willy Tarreau295a8372011-03-10 11:25:07 +01002425 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002426 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002427 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002428 }
2429
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002430 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002431 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002432 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002433 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2434 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002435 "",
2436 px->id, px->id);
2437
2438 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002439 chunk_appendf(&trash,
Willy Tarreaub44939a2010-02-26 11:35:39 +01002440 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002441 "<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 +01002442 "",
2443 read_freq_ctr(&px->fe_req_per_sec),
2444 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002445 px->fe_counters.p.http.rps_max,
2446 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002447 LIM2A2(px->fe_sps_lim, "-"));
2448 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002449 chunk_appendf(&trash,
Willy Tarreaub44939a2010-02-26 11:35:39 +01002450 /* sessions rate : current, max, limit */
2451 "<td>%s</td><td>%s</td><td>%s</td>"
2452 "",
2453 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002454 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002455 }
2456
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002457 chunk_appendf(&trash,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002458 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002459 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002460 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002461 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002462 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002463
2464 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2465 if (px->mode == PR_MODE_HTTP) {
2466 int i;
2467
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002468 chunk_appendf(&trash, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002469
2470 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002471 chunk_appendf(&trash, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002472
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002473 chunk_appendf(&trash, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2474 chunk_appendf(&trash, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002475 }
2476
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002477 chunk_appendf(&trash,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002478 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002479 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002480 /* bytes : in, out */
2481 "<td>%s</td><td>%s</td>"
2482 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002483 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002484 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002485 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002486 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002487
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002488 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002489 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002490 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002491 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002492 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002493 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002494 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002495 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002496 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002497 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002498 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002499 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002500 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2501 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002502 px->state == PR_STREADY ? "OPEN" :
2503 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002504 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002505 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002506 /* pxid, name, queue cur, queue max, */
2507 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002508 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002509 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002510 /* bytes : in, out */
2511 "%lld,%lld,"
2512 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002513 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002514 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002515 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002516 /* warnings: retries, redispatches */
2517 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002518 /* server status : reflect frontend status */
2519 "%s,"
2520 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002521 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002522 /* pid, iid, sid, throttle, lbtot, tracked, type */
2523 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002524 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002525 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002526 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002527 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002528 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002529 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2530 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2531 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2532 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002533 px->state == PR_STREADY ? "OPEN" :
2534 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002535 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002536 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002537 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002538
2539 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2540 if (px->mode == PR_MODE_HTTP) {
2541 int i;
2542
2543 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002544 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002545
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002546 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002547 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002548 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002549 }
2550
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002551 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002552 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002553
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002554 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002555 chunk_appendf(&trash, "%u,%u,%lld,",
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002556 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002557 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002558
Willy Tarreauae526782010-03-04 20:34:23 +01002559 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002560 chunk_appendf(&trash, ",,");
Willy Tarreauae526782010-03-04 20:34:23 +01002561
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002562 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002563 chunk_appendf(&trash, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002564 }
Willy Tarreau91861262007-10-17 17:06:05 +02002565
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002566 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002567 return 0;
2568 }
2569
Willy Tarreau4348fad2012-09-20 16:48:07 +02002570 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002571 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002572 /* fall through */
2573
Willy Tarreau295a8372011-03-10 11:25:07 +01002574 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002575 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002576 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 +02002577 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002578 return 0;
2579
Willy Tarreau4348fad2012-09-20 16:48:07 +02002580 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002581 if (!l->counters)
2582 continue;
2583
Willy Tarreau295a8372011-03-10 11:25:07 +01002584 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2585 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002586 break;
2587
Willy Tarreau295a8372011-03-10 11:25:07 +01002588 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002589 continue;
2590 }
2591
Willy Tarreau295a8372011-03-10 11:25:07 +01002592 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002593 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002594 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002595 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002596 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002597 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002598 chunk_appendf(&trash, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002599
2600 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002601 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002602 int port;
2603
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002604 chunk_appendf(&trash, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002605
Willy Tarreau631f01c2011-09-05 00:36:48 +02002606 port = get_host_port(&l->addr);
2607 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2608 case AF_INET:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002609 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
Willy Tarreau631f01c2011-09-05 00:36:48 +02002610 break;
2611 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002612 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
Willy Tarreau631f01c2011-09-05 00:36:48 +02002613 break;
2614 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002615 chunk_appendf(&trash, "unix, ");
Willy Tarreau631f01c2011-09-05 00:36:48 +02002616 break;
2617 case -1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002618 chunk_appendf(&trash, "(%s), ", strerror(errno));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002619 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002620 }
2621
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002622 /* id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002623 chunk_appendf(&trash, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002624 }
2625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002626 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002627 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002628 ">%s<a name=\"%s/+%s\"></a>"
2629 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002630 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002631 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002632 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002633 "<td>%s</td><td>%s</td><td>%s</td>"
2634 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002635 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002636 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002637 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002638 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002639 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002640 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2642 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2643
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002644 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002645 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002646 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002647 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002648 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002649 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002650 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002651 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002652 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002653 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002654 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002655 "",
2656 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2657 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002658 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002659 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002660 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002661 /* pxid, name, queue cur, queue max, */
2662 "%s,%s,,,"
2663 /* sessions: current, max, limit, total */
2664 "%d,%d,%d,%lld,"
2665 /* bytes: in, out */
2666 "%lld,%lld,"
2667 /* denied: req, resp */
2668 "%lld,%lld,"
2669 /* errors: request, connect, response */
2670 "%lld,,,"
2671 /* warnings: retries, redispatches */
2672 ",,"
2673 /* server status: reflect listener status */
2674 "%s,"
2675 /* rest of server: nothing */
2676 ",,,,,,,,"
2677 /* pid, iid, sid, throttle, lbtot, tracked, type */
2678 "%d,%d,%d,,,,%d,"
2679 /* rate, rate_lim, rate_max */
2680 ",,,"
2681 /* check_status, check_code, check_duration */
2682 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002683 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2684 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002685 /* failed health analyses */
2686 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002687 /* requests : req_rate, req_rate_max, req_tot, */
2688 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002689 /* errors: cli_aborts, srv_aborts */
2690 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002691 "\n",
2692 px->id, l->name,
2693 l->nbconn, l->counters->conn_max,
2694 l->maxconn, l->counters->cum_conn,
2695 l->counters->bytes_in, l->counters->bytes_out,
2696 l->counters->denied_req, l->counters->denied_resp,
2697 l->counters->failed_req,
2698 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2699 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2700 }
2701
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002702 if (bi_putchk(rep, &trash) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002703 return 0;
2704 }
2705
Willy Tarreau295a8372011-03-10 11:25:07 +01002706 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2707 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002708 /* fall through */
2709
Willy Tarreau295a8372011-03-10 11:25:07 +01002710 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002711 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002712 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002713 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 +02002714
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002716 return 0;
2717
Willy Tarreau295a8372011-03-10 11:25:07 +01002718 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002719
Willy Tarreau295a8372011-03-10 11:25:07 +01002720 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2721 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002722 break;
2723
Willy Tarreau295a8372011-03-10 11:25:07 +01002724 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002725 continue;
2726 }
2727
Willy Tarreau44267702011-10-28 15:35:33 +02002728 if (sv->track)
2729 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002730 else
2731 svs = sv;
2732
Willy Tarreau91861262007-10-17 17:06:05 +02002733 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002734 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002735 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002736 else if (svs->state & SRV_RUNNING) {
2737 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002738 sv_state = 3; /* UP */
2739 else
2740 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002741
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002742 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002743 sv_state += 2;
2744 }
Willy Tarreau91861262007-10-17 17:06:05 +02002745 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002746 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002747 sv_state = 1; /* going up */
2748 else
2749 sv_state = 0; /* DOWN */
2750
Willy Tarreau295a8372011-03-10 11:25:07 +01002751 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002752 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002753 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002754 continue;
2755 }
2756
Willy Tarreau295a8372011-03-10 11:25:07 +01002757 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002758 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2759 "UP %d/%d &darr;", "UP",
2760 "NOLB %d/%d &darr;", "NOLB",
2761 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002762 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002763 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002764 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002765 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002766 );
2767 }
2768 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002769 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002770 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002771 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002772 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2773 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002774
Willy Tarreau295a8372011-03-10 11:25:07 +01002775 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002776 chunk_appendf(&trash,
Cyril Bonté474be412010-10-12 00:14:36 +02002777 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2778 sv->id);
2779 }
2780
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002781 chunk_appendf(&trash, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002782
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002783 if (uri->flags&ST_SHLGNDS) {
2784 char str[INET6_ADDRSTRLEN];
2785
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002786 chunk_appendf(&trash, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002787
Willy Tarreau631f01c2011-09-05 00:36:48 +02002788 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002789 case AF_INET:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002790 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002791 break;
2792 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002793 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002794 break;
2795 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002796 chunk_appendf(&trash, "unix, ");
Willy Tarreau631f01c2011-09-05 00:36:48 +02002797 break;
2798 case -1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002799 chunk_appendf(&trash, "(%s), ", strerror(errno));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002800 break;
2801 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002802 break;
2803 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002804
2805 /* id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002806 chunk_appendf(&trash, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002807
2808 /* cookie */
2809 if (sv->cookie) {
2810 struct chunk src;
2811
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002812 chunk_appendf(&trash, ", cookie: '");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002813
2814 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002815 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002816
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002817 chunk_appendf(&trash, "'");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002818 }
2819
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002820 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002821 }
2822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002823 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002824 ">%s<a name=\"%s/%s\"></a>"
2825 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002826 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002827 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002828 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002829 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002830 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002831 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002832 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002833 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002834 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002835 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002836 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002837 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2838 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002839 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2840
2841 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2842 if (px->mode == PR_MODE_HTTP) {
2843 int i;
2844
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002845 chunk_appendf(&trash, " title=\"rsp codes:");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002846
2847 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002848 chunk_appendf(&trash, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002849
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002850 chunk_appendf(&trash, " other=%lld\"", sv->counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002851 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002852
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002853 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002854 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002855 ">%s%s%s</td><td>%s</td>",
2856 (px->mode == PR_MODE_HTTP)?"<u>":"",
2857 U2H0(sv->counters.cum_sess),
2858 (px->mode == PR_MODE_HTTP)?"</u>":"",
2859 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002860
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002861 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002862 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002863 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002864 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002865 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002866 /* errors : request, connect */
2867 "<td></td><td>%s</td>"
2868 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002869 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002870 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002871 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002872 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002873 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2874 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002875 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002876 sv->counters.cli_aborts,
2877 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002878 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002879 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002880
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002881 /* status, lest check */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002882 chunk_appendf(&trash, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002883
Cyril Bontécd19e512010-01-31 22:34:03 +01002884 if (sv->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002885 chunk_appendf(&trash, "%s ",
Cyril Bontécd19e512010-01-31 22:34:03 +01002886 human_time(now.tv_sec - sv->last_change, 1));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002887 chunk_appendf(&trash, "MAINT");
Cyril Bontécd19e512010-01-31 22:34:03 +01002888 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002889 else if (svs != sv && svs->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002890 chunk_appendf(&trash, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002891 human_time(now.tv_sec - svs->last_change, 1));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002892 chunk_appendf(&trash, "MAINT(via)");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002893 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002894 else if (svs->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002895 chunk_appendf(&trash, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002896 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002897
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002898 chunk_appendf(&trash,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002899 srv_hlt_st[sv_state],
2900 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2901 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002902 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002903
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002904 if (sv->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002905 chunk_appendf(&trash, "</td><td class=ac title=\"%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002906 get_check_status_description(sv->check.status));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002907
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002908 if (*sv->check.desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002909 struct chunk src;
2910
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002911 chunk_appendf(&trash, ": ");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002912
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002913 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002914 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002915 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002916
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002917 chunk_appendf(&trash, "\"><u> %s%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002918 tv_iszero(&sv->check.start)?"":"* ",
2919 get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002920
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002921 if (sv->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002922 chunk_appendf(&trash, "/%d", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002923
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002924 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002925 chunk_appendf(&trash, " in %lums</u>", sv->check.duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002926 } else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002927 chunk_appendf(&trash, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002929 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002930 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002931 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002932 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002933 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002934 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002935 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002936 (sv->state & SRV_BACKUP) ? "-" : "Y",
2937 (sv->state & SRV_BACKUP) ? "Y" : "-");
2938
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002939 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002940 if (sv->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002941 chunk_appendf(&trash, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002942 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2943
2944 if (svs->observe)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002945 chunk_appendf(&trash, "/%lld", svs->counters.failed_hana);
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002946
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002947 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002948 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002949 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002950 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002951 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002952 } else if (sv != svs)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002953 chunk_appendf(&trash,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002954 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2955 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002956 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002957 chunk_appendf(&trash,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002958 "<td colspan=3></td>");
2959
2960 /* throttle */
2961 if ((sv->state & SRV_WARMINGUP) &&
2962 now.tv_sec < sv->last_change + sv->slowstart &&
2963 now.tv_sec >= sv->last_change) {
2964 unsigned int ratio;
2965 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002966 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002967 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002968 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002969 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002970 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002971 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002972 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002973 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2974 "UP %d/%d,", "UP,",
2975 "NOLB %d/%d,", "NOLB,",
2976 "no check," };
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002977 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002978 /* pxid, name */
2979 "%s,%s,"
2980 /* queue : current, max */
2981 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002982 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002983 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002984 /* bytes : in, out */
2985 "%lld,%lld,"
2986 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002987 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002988 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002989 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002990 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002991 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002992 "",
2993 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002994 sv->nbpend, sv->counters.nbpend_max,
2995 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002996 sv->counters.bytes_in, sv->counters.bytes_out,
2997 sv->counters.failed_secu,
2998 sv->counters.failed_conns, sv->counters.failed_resp,
2999 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003000
Willy Tarreau55bb8452007-10-17 18:44:57 +02003001 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01003002 if (sv->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003003 chunk_appendf(&trash, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01003004 }
3005 else if (svs != sv && svs->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003006 chunk_appendf(&trash, "MAINT(via),");
Cyril Bonté0dae5852010-02-03 00:26:28 +01003007 }
3008 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003009 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01003010 srv_hlt_st[sv_state],
3011 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
3012 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
3013 }
Willy Tarreau91861262007-10-17 17:06:05 +02003014
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003015 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003016 /* weight, active, backup */
3017 "%d,%d,%d,"
3018 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01003019 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003020 (sv->state & SRV_BACKUP) ? 0 : 1,
3021 (sv->state & SRV_BACKUP) ? 1 : 0);
3022
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003023 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02003024 if (sv->state & SRV_CHECKED)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003025 chunk_appendf(&trash,
Willy Tarreau3b88d442009-04-11 20:44:08 +02003026 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003027 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003028 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003029 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003030 chunk_appendf(&trash,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003031 ",,,,");
3032
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003033 /* queue limit, pid, iid, sid, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003034 chunk_appendf(&trash,
Willy Tarreaudcd47712007-11-04 23:35:08 +01003035 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003036 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01003037 LIM2A0(sv->maxqueue, ""),
3038 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003039
3040 /* throttle */
3041 if ((sv->state & SRV_WARMINGUP) &&
3042 now.tv_sec < sv->last_change + sv->slowstart &&
3043 now.tv_sec >= sv->last_change) {
3044 unsigned int ratio;
3045 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003046 chunk_appendf(&trash, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003047 }
3048
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003049 /* sessions: lbtot */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003050 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003051
3052 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02003053 if (sv->track)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003054 chunk_appendf(&trash, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02003055 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003056 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003057 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003058
Willy Tarreau7f062c42009-03-05 18:43:00 +01003059 /* type */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003060 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003061
3062 /* rate */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003063 chunk_appendf(&trash, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003064 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003065 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003066
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003067 if (sv->state & SRV_CHECKED) {
3068 /* check_status */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003069 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003070
3071 /* check_code */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003072 if (sv->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003073 chunk_appendf(&trash, "%u,", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003074 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003075 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003076
3077 /* check_duration */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003078 if (sv->check.status >= HCHK_STATUS_CHECKED)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003079 chunk_appendf(&trash, "%lu,", sv->check.duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003080 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003081 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003082
3083 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003084 chunk_appendf(&trash, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003085 }
3086
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003087 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3088 if (px->mode == PR_MODE_HTTP) {
3089 int i;
3090
3091 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003092 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003093
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003094 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003095 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003096 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003097 }
3098
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003099 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003100 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003101
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003102 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003103 chunk_appendf(&trash, ",,,");
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003104
Willy Tarreauae526782010-03-04 20:34:23 +01003105 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003106 chunk_appendf(&trash, "%lld,%lld,",
Willy Tarreauae526782010-03-04 20:34:23 +01003107 sv->counters.cli_aborts, sv->counters.srv_aborts);
3108
Willy Tarreau7f062c42009-03-05 18:43:00 +01003109 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003110 chunk_appendf(&trash, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003111 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003112 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003113 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003114 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003115
Willy Tarreau295a8372011-03-10 11:25:07 +01003116 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003117 /* fall through */
3118
Willy Tarreau295a8372011-03-10 11:25:07 +01003119 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003120 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003121 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003122 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3123 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003124 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003125 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003126 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003127 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003128 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003129 chunk_appendf(&trash, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003130
3131 if (uri->flags&ST_SHLGNDS) {
3132 /* balancing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003133 chunk_appendf(&trash, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003134 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003135
3136 /* cookie */
3137 if (px->cookie_name) {
3138 struct chunk src;
3139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003140 chunk_appendf(&trash, ", cookie: '");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003141
3142 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003143 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003144
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003145 chunk_appendf(&trash, "'");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003146 }
3147
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003148 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003149
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003150 }
3151
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003152 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003153 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003154 ">%s<a name=\"%s/Backend\"></a>"
3155 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003156 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003157 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003158 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003159 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003160 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003161 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003162 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003163 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003164 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3165 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003167 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003168 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003169 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003170 "<td"
3171 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003172 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003173
3174 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3175 if (px->mode == PR_MODE_HTTP) {
3176 int i;
3177
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003178 chunk_appendf(&trash, " title=\"rsp codes:");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003179
3180 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003181 chunk_appendf(&trash, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003182
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003183 chunk_appendf(&trash, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003184 }
3185
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003186 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003187 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003188 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003189 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003190 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003191 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003192 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003193 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003194 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003195 U2H7(px->be_counters.cum_lbconn),
3196 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003197
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003198 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02003199 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003200 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003201 /* errors : request, connect */
3202 "<td></td><td>%s</td>"
3203 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003204 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003205 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003206 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003207 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003208 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003209 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003210 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003211 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3212 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003213 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003214 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3215 U2H2(px->be_counters.failed_conns),
3216 px->be_counters.cli_aborts,
3217 px->be_counters.srv_aborts,
3218 U2H5(px->be_counters.failed_resp),
3219 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003220 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003221 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3222 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003223 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003224 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003225
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003226 chunk_appendf(&trash,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003227 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003228 "<td class=ac>&nbsp;</td><td>%d</td>"
3229 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003230 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003231 "</tr>",
3232 px->down_trans,
3233 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003234 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003235 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003236 /* pxid, name */
3237 "%s,BACKEND,"
3238 /* queue : current, max */
3239 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003240 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003241 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003242 /* bytes : in, out */
3243 "%lld,%lld,"
3244 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003245 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003246 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003247 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003248 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003249 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003250 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003251 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003252 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003253 * active and backups. */
3254 "%s,"
3255 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003256 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003257 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003258 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003259 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003260 /* rate, rate_lim, rate_max, */
3261 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003262 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003263 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003264 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003265 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3266 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3267 px->be_counters.bytes_in, px->be_counters.bytes_out,
3268 px->be_counters.denied_req, px->be_counters.denied_resp,
3269 px->be_counters.failed_conns, px->be_counters.failed_resp,
3270 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003271 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003272 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003273 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003274 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003275 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003276 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003277 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003278 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003279 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003280
3281 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3282 if (px->mode == PR_MODE_HTTP) {
3283 int i;
3284
3285 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003286 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003288 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003289 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003290 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003291 }
3292
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003293 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003294 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003295
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003296 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003297 chunk_appendf(&trash, ",,,");
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003298
Willy Tarreauae526782010-03-04 20:34:23 +01003299 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003300 chunk_appendf(&trash, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003301 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003302
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003303 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003304 chunk_appendf(&trash, "\n");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003305
Willy Tarreau55bb8452007-10-17 18:44:57 +02003306 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003307 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003308 return 0;
3309 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003310
Willy Tarreau295a8372011-03-10 11:25:07 +01003311 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003312 /* fall through */
3313
Willy Tarreau295a8372011-03-10 11:25:07 +01003314 case STAT_PX_ST_END:
3315 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003316 chunk_appendf(&trash, "</table>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003317
Willy Tarreau295a8372011-03-10 11:25:07 +01003318 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003319 /* close the form used to enable/disable this proxy servers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003320 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003321 "Choose the action to perform on the checked servers : "
3322 "<select name=action>"
3323 "<option value=\"\"></option>"
3324 "<option value=\"disable\">Disable</option>"
3325 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003326 "<option value=\"stop\">Soft Stop</option>"
3327 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003328 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003329 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003330 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003331 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3332 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003333 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003334 }
3335
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003336 chunk_appendf(&trash, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003337
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003338 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003339 return 0;
3340 }
Willy Tarreau91861262007-10-17 17:06:05 +02003341
Willy Tarreau295a8372011-03-10 11:25:07 +01003342 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003343 /* fall through */
3344
Willy Tarreau295a8372011-03-10 11:25:07 +01003345 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003346 return 1;
3347
3348 default:
3349 /* unknown state, we should put an abort() here ! */
3350 return 1;
3351 }
3352}
3353
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003354/* This function dumps a complete session state onto the stream interface's
3355 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3356 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003357 * 0 if the output buffer is full and it needs to be called again, otherwise
3358 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003359 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003360static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003361{
3362 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003363 struct session *sess;
3364 extern const char *monthname[12];
3365 char pn[INET6_ADDRSTRLEN];
3366
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003367 chunk_reset(&trash);
Willy Tarreau295a8372011-03-10 11:25:07 +01003368 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003369
Willy Tarreau295a8372011-03-10 11:25:07 +01003370 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003371 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003372 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3373 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003374 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003375 si->applet.ctx.sess.target = NULL;
3376 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003377 return 1;
3378 }
3379
Willy Tarreau295a8372011-03-10 11:25:07 +01003380 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003381 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003382 si->applet.ctx.sess.uid = sess->uniq_id;
3383 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003384 /* fall through */
3385
3386 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003387 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003388 "%p: id=%u, proto=%s",
3389 sess,
3390 sess->uniq_id,
3391 sess->listener->proto->name);
3392
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003393 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003394 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003395 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003396 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003397 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003398 break;
3399 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003400 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003401 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003402 default:
3403 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003404 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003405 break;
3406 }
3407
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003408 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003409 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003410 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003411
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003412 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003413 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003414 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3415 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3416 sess->listener ? sess->listener->luid : 0);
3417
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003418 conn_get_to_addr(sess->si[0].conn);
3419 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003420 case AF_INET:
3421 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003422 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003423 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003424 break;
3425 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003426 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003427 break;
3428 default:
3429 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003430 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003431 break;
3432 }
3433
Willy Tarreau50943332011-09-02 17:33:05 +02003434 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003435 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003436 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003437 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003438 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3439 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003440 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003441
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003442 conn_get_from_addr(sess->si[1].conn);
3443 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003444 case AF_INET:
3445 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003446 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003447 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003448 break;
3449 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003450 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003451 break;
3452 default:
3453 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003454 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003455 break;
3456 }
3457
3458 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003459 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003460 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003461 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3462 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003463 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003464 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003465
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003466 conn_get_to_addr(sess->si[1].conn);
3467 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003468 case AF_INET:
3469 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003470 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003471 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003472 break;
3473 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003474 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003475 break;
3476 default:
3477 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003478 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003479 break;
3480 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003481
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003482 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003483 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3484 sess->task,
3485 sess->task->state,
3486 sess->task->nice, sess->task->calls,
3487 sess->task->expire ?
3488 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3489 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3490 TICKS_TO_MS(1000)) : "<NEVER>",
3491 task_in_rq(sess->task) ? ", running" : "");
3492
3493 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003494 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003495 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3496 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3497 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3498 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3499
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003500 chunk_appendf(&trash,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003501 " si[0]=%p (state=%d flags=0x%02x conn=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003502 &sess->si[0],
3503 sess->si[0].state,
3504 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003505 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003506 sess->si[0].exp ?
3507 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3508 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3509 TICKS_TO_MS(1000)) : "<NEVER>",
3510 sess->si[0].err_type);
3511
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003512 chunk_appendf(&trash,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003513 " si[1]=%p (state=%d flags=0x%02x conn=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003514 &sess->si[1],
3515 sess->si[1].state,
3516 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003517 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003518 sess->si[1].exp ?
3519 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3520 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3521 TICKS_TO_MS(1000)) : "<NEVER>",
3522 sess->si[1].err_type);
3523
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003524 chunk_appendf(&trash,
Willy Tarreau1feca012012-11-19 18:15:19 +01003525 " lconn=%p (ctrl=%p(%s) xprt=%p(%s) data=%p(%s) target=%d flags=0x%08x fd=%d fd_spec=%02x)\n",
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003526 sess->si[0].conn,
3527 sess->si[0].conn->ctrl,
3528 sess->si[0].conn->ctrl ? sess->si[0].conn->ctrl->name : "NONE",
3529 sess->si[0].conn->xprt,
3530 (sess->si[0].conn->xprt == NULL) ? "NONE" :
3531 (sess->si[0].conn->xprt == &raw_sock) ? "RAW" :
3532#ifdef USE_OPENSSL
3533 (sess->si[0].conn->xprt == &ssl_sock) ? "SSL" :
3534#endif
3535 "????",
3536 sess->si[0].conn->data,
3537 (sess->si[0].conn->data == NULL) ? "NONE" :
3538 (sess->si[0].conn->data == &sess_conn_cb) ? "SESS" :
3539 (sess->si[0].conn->data == &si_conn_cb) ? "STRM" :
3540 (sess->si[0].conn->data == &check_conn_cb) ? "CHCK" : "????",
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003541 sess->si[0].conn->target ? *sess->si[0].conn->target : 0,
Willy Tarreau1feca012012-11-19 18:15:19 +01003542 sess->si[0].conn->flags,
3543 sess->si[0].conn->t.sock.fd,
3544 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0);
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003545
3546 chunk_appendf(&trash,
Willy Tarreau1feca012012-11-19 18:15:19 +01003547 " rconn=%p (ctrl=%p(%s) xprt=%p(%s) data=%p(%s) target=%d flags=0x%08x fd=%d fd_spec=%02x)\n",
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003548 sess->si[1].conn,
3549 sess->si[1].conn->ctrl,
3550 sess->si[1].conn->ctrl ? sess->si[1].conn->ctrl->name : "NONE",
3551 sess->si[1].conn->xprt,
3552 (sess->si[1].conn->xprt == NULL) ? "NONE" :
3553 (sess->si[1].conn->xprt == &raw_sock) ? "RAW" :
3554#ifdef USE_OPENSSL
3555 (sess->si[1].conn->xprt == &ssl_sock) ? "SSL" :
3556#endif
3557 "UNKNOWN",
3558 sess->si[1].conn->data,
3559 (sess->si[1].conn->data == NULL) ? "NONE" :
3560 (sess->si[1].conn->data == &sess_conn_cb) ? "SESS" :
3561 (sess->si[1].conn->data == &si_conn_cb) ? "STRM" :
3562 (sess->si[1].conn->data == &check_conn_cb) ? "CHCK" : "????",
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003563 sess->si[1].conn->target ? *sess->si[1].conn->target : 0,
Willy Tarreau1feca012012-11-19 18:15:19 +01003564 sess->si[1].conn->flags,
3565 sess->si[1].conn->t.sock.fd,
3566 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0);
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003567
3568 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003569 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3570 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3571 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3572
3573
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003574 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003575 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003576 " an_exp=%s",
3577 sess->req,
3578 sess->req->flags, sess->req->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003579 sess->req->buf->i, sess->req->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003580 sess->req->pipe ? sess->req->pipe->data : 0,
3581 sess->req->to_forward,
3582 sess->req->analyse_exp ?
3583 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3584 TICKS_TO_MS(1000)) : "<NEVER>");
3585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003586 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003587 " rex=%s",
3588 sess->req->rex ?
3589 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3590 TICKS_TO_MS(1000)) : "<NEVER>");
3591
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003592 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003593 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003594 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003595 sess->req->wex ?
3596 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3597 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003598 sess->req->buf->data,
3599 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003600 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003601 sess->req->total);
3602
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003603 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003604 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003605 " an_exp=%s",
3606 sess->rep,
3607 sess->rep->flags, sess->rep->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003608 sess->rep->buf->i, sess->rep->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003609 sess->rep->pipe ? sess->rep->pipe->data : 0,
3610 sess->rep->to_forward,
3611 sess->rep->analyse_exp ?
3612 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3613 TICKS_TO_MS(1000)) : "<NEVER>");
3614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003615 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003616 " rex=%s",
3617 sess->rep->rex ?
3618 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3619 TICKS_TO_MS(1000)) : "<NEVER>");
3620
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003621 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003622 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003623 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003624 sess->rep->wex ?
3625 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3626 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003627 sess->rep->buf->data,
3628 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003629 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003630 sess->rep->total);
3631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003632 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003633 return 0;
3634
3635 /* use other states to dump the contents */
3636 }
3637 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003638 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003639 return 1;
3640}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003641
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003642/* This function dumps all sessions' states onto the stream interface's
3643 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003644 * properly set. It returns 0 if the output buffer is full and it needs
3645 * to be called again, otherwise non-zero. It is designed to be called
3646 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003647 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003648static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003649{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003650 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003651 /* If we're forced to shut down, we might have to remove our
3652 * reference to the last session being dumped.
3653 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003654 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003655 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3656 LIST_DEL(&si->applet.ctx.sess.bref.users);
3657 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003658 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003659 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003660 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003661 }
3662
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003663 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003664
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003665 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003666 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003667 /* the function had not been called yet, let's prepare the
3668 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003669 * pointer to the first in the global list. When a target
3670 * session is being destroyed, it is responsible for updating
3671 * this pointer. We know we have reached the end when this
3672 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003673 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003674 LIST_INIT(&si->applet.ctx.sess.bref.users);
3675 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003676 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003677 /* fall through */
3678
Willy Tarreau295a8372011-03-10 11:25:07 +01003679 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003680 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003681 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3682 LIST_DEL(&si->applet.ctx.sess.bref.users);
3683 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003684 }
3685
3686 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003687 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003688 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003689 struct session *curr_sess;
3690
Willy Tarreau295a8372011-03-10 11:25:07 +01003691 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003692
Willy Tarreau295a8372011-03-10 11:25:07 +01003693 if (si->applet.ctx.sess.target) {
3694 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003695 goto next_sess;
3696
Willy Tarreau295a8372011-03-10 11:25:07 +01003697 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003698 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003699 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003700 return 0;
3701
3702 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003703 LIST_DEL(&si->applet.ctx.sess.bref.users);
3704 LIST_INIT(&si->applet.ctx.sess.bref.users);
3705 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003706 break;
3707 }
3708
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003709 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003710 "%p: proto=%s",
3711 curr_sess,
3712 curr_sess->listener->proto->name);
3713
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003714
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003715 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003716 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003717 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003718 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003719 " src=%s:%d fe=%s be=%s srv=%s",
3720 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003721 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003722 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003723 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003724 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003725 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003726 break;
3727 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003728 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003729 " src=unix:%d fe=%s be=%s srv=%s",
3730 curr_sess->listener->luid,
3731 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003732 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003733 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003734 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003735 break;
3736 }
3737
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003738 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003739 " ts=%02x age=%s calls=%d",
3740 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003741 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3742 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003743
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003744 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003745 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003746 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003747 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003748 curr_sess->req->analysers,
3749 curr_sess->req->rex ?
3750 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3751 TICKS_TO_MS(1000)) : "");
3752
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003753 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003754 ",wx=%s",
3755 curr_sess->req->wex ?
3756 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3757 TICKS_TO_MS(1000)) : "");
3758
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003759 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003760 ",ax=%s]",
3761 curr_sess->req->analyse_exp ?
3762 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3763 TICKS_TO_MS(1000)) : "");
3764
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003765 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003766 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003767 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003768 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003769 curr_sess->rep->analysers,
3770 curr_sess->rep->rex ?
3771 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3772 TICKS_TO_MS(1000)) : "");
3773
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003774 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003775 ",wx=%s",
3776 curr_sess->rep->wex ?
3777 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3778 TICKS_TO_MS(1000)) : "");
3779
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003780 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003781 ",ax=%s]",
3782 curr_sess->rep->analyse_exp ?
3783 human_time(TICKS_TO_MS(curr_sess->rep->analyse_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 " s0=[%d,%1xh,fd=%d,ex=%s]",
3788 curr_sess->si[0].state,
3789 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003790 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003791 curr_sess->si[0].exp ?
3792 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3793 TICKS_TO_MS(1000)) : "");
3794
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003795 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003796 " s1=[%d,%1xh,fd=%d,ex=%s]",
3797 curr_sess->si[1].state,
3798 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003799 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003800 curr_sess->si[1].exp ?
3801 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3802 TICKS_TO_MS(1000)) : "");
3803
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003804 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003805 " exp=%s",
3806 curr_sess->task->expire ?
3807 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3808 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003809 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003810 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003811
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003812 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003813
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003814 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003815 /* let's try again later from this session. We add ourselves into
3816 * this session's users so that it can remove us upon termination.
3817 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003818 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003819 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003820 }
3821
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003822 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003823 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003824 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003825
Willy Tarreau295a8372011-03-10 11:25:07 +01003826 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003827 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003828 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003829 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003830 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003831 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003832
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003833 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003834 return 0;
3835
Willy Tarreau295a8372011-03-10 11:25:07 +01003836 si->applet.ctx.sess.target = NULL;
3837 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003838 return 1;
3839 }
3840
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003841 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003842 /* fall through */
3843
3844 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003845 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003846 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003847 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003848}
3849
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003850/* This function dumps all tables' states onto the stream interface's
3851 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003852 * properly set. It returns 0 if the output buffer is full and it needs
3853 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003854 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003855static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003856{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003857 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003858 struct ebmb_node *eb;
3859 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003860 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003861
3862 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003863 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003864 * - STAT_ST_INIT : the first call
3865 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003866 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003867 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003868 * and the entry pointer points to the next entry to be dumped,
3869 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003870 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003871 * data though.
3872 */
3873
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003874 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003875 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003876 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003877 si->applet.ctx.table.entry->ref_cnt--;
3878 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003879 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003880 return 1;
3881 }
3882
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003883 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003884
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003885 while (si->conn->xprt_st != STAT_ST_FIN) {
3886 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003887 case STAT_ST_INIT:
3888 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3889 if (!si->applet.ctx.table.proxy)
3890 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003891
Willy Tarreau295a8372011-03-10 11:25:07 +01003892 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003893 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003894 break;
3895
Willy Tarreau295a8372011-03-10 11:25:07 +01003896 case STAT_ST_INFO:
3897 if (!si->applet.ctx.table.proxy ||
3898 (si->applet.ctx.table.target &&
3899 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003900 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003901 break;
3902 }
3903
Willy Tarreau295a8372011-03-10 11:25:07 +01003904 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003905 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09003906 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003907 return 0;
3908
Willy Tarreau295a8372011-03-10 11:25:07 +01003909 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02003910 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003911 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003912 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003913 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003914 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3915 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003916 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003917 break;
3918 }
3919 }
3920 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003921 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003922 break;
3923
Willy Tarreau295a8372011-03-10 11:25:07 +01003924 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003925 skip_entry = false;
3926
Willy Tarreau295a8372011-03-10 11:25:07 +01003927 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003928 /* we're filtering on some data contents */
3929 void *ptr;
3930 long long data;
3931
Willy Tarreau295a8372011-03-10 11:25:07 +01003932 dt = si->applet.ctx.table.data_type;
3933 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3934 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003935 dt);
3936
3937 data = 0;
3938 switch (stktable_data_types[dt].std_type) {
3939 case STD_T_SINT:
3940 data = stktable_data_cast(ptr, std_t_sint);
3941 break;
3942 case STD_T_UINT:
3943 data = stktable_data_cast(ptr, std_t_uint);
3944 break;
3945 case STD_T_ULL:
3946 data = stktable_data_cast(ptr, std_t_ull);
3947 break;
3948 case STD_T_FRQP:
3949 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003950 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003951 break;
3952 }
3953
3954 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003955 if ((data < si->applet.ctx.table.value &&
3956 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3957 si->applet.ctx.table.data_op == STD_OP_GT ||
3958 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3959 (data == si->applet.ctx.table.value &&
3960 (si->applet.ctx.table.data_op == STD_OP_NE ||
3961 si->applet.ctx.table.data_op == STD_OP_GT ||
3962 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3963 (data > si->applet.ctx.table.value &&
3964 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3965 si->applet.ctx.table.data_op == STD_OP_LT ||
3966 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003967 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003968 }
3969
Simon Hormanc88b8872011-06-15 15:18:49 +09003970 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003971 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003972 si->applet.ctx.table.entry))
3973 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003974
Willy Tarreau295a8372011-03-10 11:25:07 +01003975 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003976
Willy Tarreau295a8372011-03-10 11:25:07 +01003977 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003978 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003979 struct stksess *old = si->applet.ctx.table.entry;
3980 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003981 if (show)
3982 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3983 else
3984 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003985 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003986 break;
3987 }
3988
Simon Hormanc88b8872011-06-15 15:18:49 +09003989
3990 if (show)
3991 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3992 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3993 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3994
Willy Tarreau295a8372011-03-10 11:25:07 +01003995 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003996 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003997 break;
3998
Willy Tarreau295a8372011-03-10 11:25:07 +01003999 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004000 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004001 break;
4002 }
4003 }
4004 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004005}
4006
Willy Tarreaud426a182010-03-05 14:58:26 +01004007/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004008 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4009 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4010 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4011 * lines are respected within the limit of 70 output chars. Lines that are
4012 * continuation of a previous truncated line begin with "+" instead of " "
4013 * after the offset. The new pointer is returned.
4014 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004015static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4016 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004017{
4018 int end;
4019 unsigned char c;
4020
4021 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004022 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004023 return ptr;
4024
Willy Tarreau77804732012-10-29 16:14:26 +01004025 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004026
Willy Tarreaud426a182010-03-05 14:58:26 +01004027 while (ptr < len && ptr < bsize) {
4028 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004029 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004030 if (out->len > end - 2)
4031 break;
4032 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004033 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004034 if (out->len > end - 3)
4035 break;
4036 out->str[out->len++] = '\\';
4037 switch (c) {
4038 case '\t': c = 't'; break;
4039 case '\n': c = 'n'; break;
4040 case '\r': c = 'r'; break;
4041 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004042 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004043 }
4044 out->str[out->len++] = c;
4045 } else {
4046 if (out->len > end - 5)
4047 break;
4048 out->str[out->len++] = '\\';
4049 out->str[out->len++] = 'x';
4050 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4051 out->str[out->len++] = hextab[c & 0xF];
4052 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004053 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004054 /* we had a line break, let's return now */
4055 out->str[out->len++] = '\n';
4056 *line = ptr;
4057 return ptr;
4058 }
4059 }
4060 /* we have an incomplete line, we return it as-is */
4061 out->str[out->len++] = '\n';
4062 return ptr;
4063}
4064
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004065/* This function dumps all captured errors onto the stream interface's
4066 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004067 * properly set. It returns 0 if the output buffer is full and it needs
4068 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004069 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004070static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004071{
4072 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004073
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004074 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004075 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004076
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004077 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004078
Willy Tarreau295a8372011-03-10 11:25:07 +01004079 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004080 /* the function had not been called yet, let's prepare the
4081 * buffer for a response.
4082 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004083 struct tm tm;
4084
4085 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004086 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004087 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4088 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4089 error_snapshot_id);
4090
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004091 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004092 /* Socket buffer full. Let's try again later from the same point */
4093 return 0;
4094 }
4095
Willy Tarreau295a8372011-03-10 11:25:07 +01004096 si->applet.ctx.errors.px = proxy;
4097 si->applet.ctx.errors.buf = 0;
4098 si->applet.ctx.errors.bol = 0;
4099 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004100 }
4101
4102 /* we have two inner loops here, one for the proxy, the other one for
4103 * the buffer.
4104 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004105 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004106 struct error_snapshot *es;
4107
Willy Tarreau295a8372011-03-10 11:25:07 +01004108 if (si->applet.ctx.errors.buf == 0)
4109 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004110 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004111 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004112
4113 if (!es->when.tv_sec)
4114 goto next;
4115
Willy Tarreau295a8372011-03-10 11:25:07 +01004116 if (si->applet.ctx.errors.iid >= 0 &&
4117 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4118 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004119 goto next;
4120
Willy Tarreau295a8372011-03-10 11:25:07 +01004121 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004122 /* just print headers now */
4123
4124 char pn[INET6_ADDRSTRLEN];
4125 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004126 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004127
4128 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004129 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004130 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004131 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004132
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004133 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4134 case AF_INET:
4135 case AF_INET6:
4136 port = get_host_port(&es->src);
4137 break;
4138 default:
4139 port = 0;
4140 }
4141
Willy Tarreau295a8372011-03-10 11:25:07 +01004142 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004143 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004144 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004145 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004146 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004147 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004148 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4149 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004150 break;
4151 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004152 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004153 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004154 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004155 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004156 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004157 break;
4158 }
4159
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004160 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004161 ", server %s (#%d), event #%u\n"
4162 " src %s:%d, session #%d, session flags 0x%08x\n"
4163 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4164 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4165 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4166 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4167 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4168 es->ev_id,
4169 pn, port, es->sid, es->s_flags,
4170 es->state, es->m_flags, es->t_flags,
4171 es->m_clen, es->m_blen,
4172 es->b_flags, es->b_out, es->b_tot,
4173 es->len, es->b_wrap, es->pos);
4174
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004175 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004176 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004177 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004178 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004179 si->applet.ctx.errors.ptr = 0;
4180 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004181 }
4182
Willy Tarreau295a8372011-03-10 11:25:07 +01004183 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004184 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004185 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004186 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004187 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004188 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004189 goto next;
4190 }
4191
4192 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004193 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004194 int newptr;
4195 int newline;
4196
Willy Tarreau295a8372011-03-10 11:25:07 +01004197 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004198 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 +01004199 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004200 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004201
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004202 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004203 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004204 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004205 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004206 si->applet.ctx.errors.ptr = newptr;
4207 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004208 };
4209 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004210 si->applet.ctx.errors.bol = 0;
4211 si->applet.ctx.errors.ptr = -1;
4212 si->applet.ctx.errors.buf++;
4213 if (si->applet.ctx.errors.buf > 1) {
4214 si->applet.ctx.errors.buf = 0;
4215 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004216 }
4217 }
4218
4219 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004220 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004221}
4222
Willy Tarreaud5781202012-09-22 19:32:35 +02004223/* parse the "level" argument on the bind lines */
4224static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4225{
4226 if (!*args[cur_arg + 1]) {
4227 memprintf(err, "'%s' : missing level", args[cur_arg]);
4228 return ERR_ALERT | ERR_FATAL;
4229 }
4230
4231 if (!strcmp(args[cur_arg+1], "user"))
4232 conf->level = ACCESS_LVL_USER;
4233 else if (!strcmp(args[cur_arg+1], "operator"))
4234 conf->level = ACCESS_LVL_OPER;
4235 else if (!strcmp(args[cur_arg+1], "admin"))
4236 conf->level = ACCESS_LVL_ADMIN;
4237 else {
4238 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4239 args[cur_arg], args[cur_arg+1]);
4240 return ERR_ALERT | ERR_FATAL;
4241 }
4242
4243 return 0;
4244}
4245
Willy Tarreaub24281b2011-02-13 13:16:36 +01004246struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004247 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004248 .name = "<STATS>", /* used for logging */
4249 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004250 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004251};
4252
Simon Horman9bd2c732011-06-15 15:18:44 +09004253static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004254 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004255 .name = "<CLI>", /* used for logging */
4256 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004257 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004258};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004259
Willy Tarreau10522fd2008-07-09 20:12:41 +02004260static struct cfg_kw_list cfg_kws = {{ },{
4261 { CFG_GLOBAL, "stats", stats_parse_global },
4262 { 0, NULL, NULL },
4263}};
4264
Willy Tarreaud5781202012-09-22 19:32:35 +02004265static struct bind_kw_list bind_kws = { "STAT", { }, {
4266 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4267 { NULL, NULL, 0 },
4268}};
4269
Willy Tarreau10522fd2008-07-09 20:12:41 +02004270__attribute__((constructor))
4271static void __dumpstats_module_init(void)
4272{
4273 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004274 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004275}
4276
Willy Tarreau91861262007-10-17 17:06:05 +02004277/*
4278 * Local variables:
4279 * c-indent-level: 8
4280 * c-basic-offset: 8
4281 * End:
4282 */