blob: da84f2a3de8edbd6abd58e17234e3352c984c9a4 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020055#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +020083 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020085 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020086 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020087 " disable : put a server or frontend in maintenance mode\n"
88 " enable : re-enable a server or frontend which is in maintenance mode\n"
89 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020090 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020091
Simon Horman9bd2c732011-06-15 15:18:44 +090092static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020093 "Permission denied\n"
94 "";
95
Willy Tarreau295a8372011-03-10 11:25:07 +010096/* data transmission states for the stats responses */
97enum {
98 STAT_ST_INIT = 0,
99 STAT_ST_HEAD,
100 STAT_ST_INFO,
101 STAT_ST_LIST,
102 STAT_ST_END,
103 STAT_ST_FIN,
104};
105
106/* data transmission states for the stats responses inside a proxy */
107enum {
108 STAT_PX_ST_INIT = 0,
109 STAT_PX_ST_TH,
110 STAT_PX_ST_FE,
111 STAT_PX_ST_LI,
112 STAT_PX_ST_SV,
113 STAT_PX_ST_BE,
114 STAT_PX_ST_END,
115 STAT_PX_ST_FIN,
116};
117
Cyril Bonté19979e12012-04-04 12:57:21 +0200118extern const char *stat_status_codes[];
119
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200120/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200121 * a new stats socket. It returns a positive value upon success, 0 if the connection
122 * needs to be closed and ignored, or a negative value upon critical failure.
123 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900124static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200125{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200126 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100127 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200128 copy_target(&s->target, &s->si[1].conn->target); // for logging only
129 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100130 s->si[1].applet.st1 = 0;
131 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200132
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200133 tv_zero(&s->logs.tv_request);
134 s->logs.t_queue = 0;
135 s->logs.t_connect = 0;
136 s->logs.t_data = 0;
137 s->logs.t_close = 0;
138 s->logs.bytes_in = s->logs.bytes_out = 0;
139 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
140 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200141
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200142 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200143
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200144 if (s->listener->timeout) {
145 s->req->rto = *s->listener->timeout;
146 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200148 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200149}
150
Willy Tarreau07e9e642010-08-17 21:48:17 +0200151/* allocate a new stats frontend named <name>, and return it
152 * (or NULL in case of lack of memory).
153 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200154static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200155{
156 struct proxy *fe;
157
158 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
159 if (!fe)
160 return NULL;
161
Willy Tarreau237250c2011-07-29 01:49:03 +0200162 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200163 fe->next = proxy;
164 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200165 fe->last_change = now.tv_sec;
166 fe->id = strdup("GLOBAL");
167 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200168 fe->maxconn = 10; /* default to 10 concurrent connections */
169 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200170 fe->conf.file = strdup(file);
171 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200172 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200173
174 /* the stats frontend is the only one able to assign ID #0 */
175 fe->conf.id.key = fe->uuid = 0;
176 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200177 return fe;
178}
179
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200181 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
182 * error message into the <err> buffer which will be preallocated. The trailing
183 * '\n' must not be written. The function must be called with <args> pointing to
184 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200185 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200186static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200187 struct proxy *defpx, const char *file, int line,
188 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200189{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200190 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200191 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200192
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200193 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194 int cur_arg;
195
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200196 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200197 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 +0200198 return -1;
199 }
200
Willy Tarreau89a63132009-08-16 17:41:45 +0200201 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200202 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200203 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200204 return -1;
205 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 }
207
Willy Tarreau4348fad2012-09-20 16:48:07 +0200208 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200209 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200210
Willy Tarreauc53d4222012-09-20 20:19:28 +0200211 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
212 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
213 file, line, args[0], args[1], err && *err ? *err : "error");
214 return -1;
215 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200216
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200217 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200218 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200219 static int bind_dumped;
220 struct bind_kw *kw;
221
222 kw = bind_find_kw(args[cur_arg]);
223 if (kw) {
224 if (!kw->parse) {
225 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
226 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200227 return -1;
228 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200229
230 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
231 if (err && *err)
232 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
233 else
234 memprintf(err, "'%s %s' : error encountered while processing '%s'",
235 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200236 return -1;
237 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200238
239 cur_arg += 1 + kw->skip;
240 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200241 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200242
243 if (!bind_dumped) {
244 bind_dump_kws(err);
245 indent_msg(err, 4);
246 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200247 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200248
249 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
250 args[0], args[1], args[cur_arg],
251 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
252 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100254
Willy Tarreauc53d4222012-09-20 20:19:28 +0200255 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
256 l->maxconn = global.stats_fe->maxconn;
257 l->backlog = global.stats_fe->backlog;
258 l->timeout = &global.stats_fe->timeout.client;
259 l->accept = session_accept;
260 l->handler = process_session;
261 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
262 l->nice = -64; /* we want to boost priority for local stats */
263 global.maxsock += l->maxconn;
264 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200265 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100267 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200268 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100269
270 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200271 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100272 return -1;
273 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100275 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 return -1;
278 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200279 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200280 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200282 return -1;
283 }
284 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200285 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 else if (!strcmp(args[1], "maxconn")) {
288 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200289
290 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200291 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292 return -1;
293 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200294
295 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200296 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200298 return -1;
299 }
300 }
301 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200303 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
304 int cur_arg = 2;
305 unsigned int set = 0;
306
307 while (*args[cur_arg]) {
308 int u;
309 if (strcmp(args[cur_arg], "all") == 0) {
310 set = 0;
311 break;
312 }
313 else if (strcmp(args[cur_arg], "odd") == 0) {
314 set |= 0x55555555;
315 }
316 else if (strcmp(args[cur_arg], "even") == 0) {
317 set |= 0xAAAAAAAA;
318 }
319 else {
320 u = str2uic(args[cur_arg]);
321 if (u < 1 || u > 32) {
322 memprintf(err,
323 "'%s %s' expects 'all', 'odd', 'even', or process numbers from 1 to 32.\n",
324 args[0], args[1]);
325 return -1;
326 }
327 set |= 1 << (u - 1);
328 }
329 cur_arg++;
330 }
331 global.stats_fe->bind_proc = set;
332 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200333 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200334 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200335 return -1;
336 }
337 return 0;
338}
339
Simon Horman9bd2c732011-06-15 15:18:44 +0900340static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100341{
Willy Tarreau77804732012-10-29 16:14:26 +0100342 return chunk_appendf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100343 "# pxname,svname,"
344 "qcur,qmax,"
345 "scur,smax,slim,stot,"
346 "bin,bout,"
347 "dreq,dresp,"
348 "ereq,econ,eresp,"
349 "wretr,wredis,"
350 "status,weight,act,bck,"
351 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200352 "pid,iid,sid,throttle,lbtot,tracked,type,"
353 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200354 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100355 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100356 "req_rate,req_rate_max,req_tot,"
357 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100358 "\n");
359}
360
Simon Hormand9366582011-06-15 15:18:45 +0900361/* print a string of text buffer to <out>. The format is :
362 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
363 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
364 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
365 */
366static int dump_text(struct chunk *out, const char *buf, int bsize)
367{
368 unsigned char c;
369 int ptr = 0;
370
371 while (buf[ptr] && ptr < bsize) {
372 c = buf[ptr];
373 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
374 if (out->len > out->size - 1)
375 break;
376 out->str[out->len++] = c;
377 }
378 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
379 if (out->len > out->size - 2)
380 break;
381 out->str[out->len++] = '\\';
382 switch (c) {
383 case ' ': c = ' '; break;
384 case '\t': c = 't'; break;
385 case '\n': c = 'n'; break;
386 case '\r': c = 'r'; break;
387 case '\e': c = 'e'; break;
388 case '\\': c = '\\'; break;
389 }
390 out->str[out->len++] = c;
391 }
392 else {
393 if (out->len > out->size - 4)
394 break;
395 out->str[out->len++] = '\\';
396 out->str[out->len++] = 'x';
397 out->str[out->len++] = hextab[(c >> 4) & 0xF];
398 out->str[out->len++] = hextab[c & 0xF];
399 }
400 ptr++;
401 }
402
403 return ptr;
404}
405
406/* print a buffer in hexa.
407 * Print stopped if <bsize> is reached, or if no more place in the chunk.
408 */
409static int dump_binary(struct chunk *out, const char *buf, int bsize)
410{
411 unsigned char c;
412 int ptr = 0;
413
414 while (ptr < bsize) {
415 c = buf[ptr];
416
417 if (out->len > out->size - 2)
418 break;
419 out->str[out->len++] = hextab[(c >> 4) & 0xF];
420 out->str[out->len++] = hextab[c & 0xF];
421
422 ptr++;
423 }
424 return ptr;
425}
426
427/* Dump the status of a table to a stream interface's
428 * read buffer. It returns 0 if the output buffer is full
429 * and needs to be called again, otherwise non-zero.
430 */
431static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
432 struct proxy *proxy, struct proxy *target)
433{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200434 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900435
Willy Tarreau77804732012-10-29 16:14:26 +0100436 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900437 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
438
439 /* any other information should be dumped here */
440
Willy Tarreau290e63a2012-09-20 18:07:14 +0200441 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100442 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900443
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200444 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900445 return 0;
446
447 return 1;
448}
449
450/* Dump the a table entry to a stream interface's
451 * read buffer. It returns 0 if the output buffer is full
452 * and needs to be called again, otherwise non-zero.
453 */
454static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
455 struct proxy *proxy, struct stksess *entry)
456{
457 int dt;
458
Willy Tarreau77804732012-10-29 16:14:26 +0100459 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900460
461 if (proxy->table.type == STKTABLE_TYPE_IP) {
462 char addr[INET_ADDRSTRLEN];
463 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100464 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900465 }
466 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
467 char addr[INET6_ADDRSTRLEN];
468 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100469 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900470 }
471 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100472 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900473 }
474 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100475 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900476 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
477 }
478 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100479 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900480 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
481 }
482
Willy Tarreau77804732012-10-29 16:14:26 +0100483 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900484
485 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
486 void *ptr;
487
488 if (proxy->table.data_ofs[dt] == 0)
489 continue;
490 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100491 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900492 else
Willy Tarreau77804732012-10-29 16:14:26 +0100493 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900494
495 ptr = stktable_data_ptr(&proxy->table, entry, dt);
496 switch (stktable_data_types[dt].std_type) {
497 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100498 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900499 break;
500 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100501 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900502 break;
503 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100504 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900505 break;
506 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100507 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900508 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
509 proxy->table.data_arg[dt].u));
510 break;
511 }
512 }
Willy Tarreau77804732012-10-29 16:14:26 +0100513 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900514
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200515 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900516 return 0;
517
518 return 1;
519}
520
Willy Tarreaudec98142012-06-06 23:37:08 +0200521static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900522{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200523 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900524 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900525 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900526 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900527 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200528 long long value;
529 int data_type;
530 void *ptr;
531 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900532
Simon Hormand5b9fd92011-06-15 15:18:48 +0900533 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900534
535 if (!*args[4]) {
536 si->applet.ctx.cli.msg = "Key value expected\n";
537 si->applet.st0 = STAT_CLI_PRINT;
538 return;
539 }
540
Simon Hormanc5b89f62011-06-15 15:18:50 +0900541 switch (px->table.type) {
542 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900543 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100544 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900545 break;
546 case STKTABLE_TYPE_IPV6:
547 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100548 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900549 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900550 case STKTABLE_TYPE_INTEGER:
551 {
552 char *endptr;
553 unsigned long val;
554 errno = 0;
555 val = strtoul(args[4], &endptr, 10);
556 if ((errno == ERANGE && val == ULONG_MAX) ||
557 (errno != 0 && val == 0) || endptr == args[4] ||
558 val > 0xffffffff) {
559 si->applet.ctx.cli.msg = "Invalid key\n";
560 si->applet.st0 = STAT_CLI_PRINT;
561 return;
562 }
563 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100564 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900565 break;
566 }
567 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900568 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100569 static_table_key->key = args[4];
570 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900571 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900572 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200573 switch (action) {
574 case STAT_CLI_O_TAB:
575 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
576 break;
577 case STAT_CLI_O_CLR:
578 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
579 break;
580 default:
581 si->applet.ctx.cli.msg = "Unknown action\n";
582 break;
583 }
Simon Horman121f3052011-06-15 15:18:46 +0900584 si->applet.st0 = STAT_CLI_PRINT;
585 return;
586 }
587
588 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200589 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900590 si->applet.ctx.cli.msg = stats_permission_denied_msg;
591 si->applet.st0 = STAT_CLI_PRINT;
592 return;
593 }
594
Willy Tarreau07115412012-10-29 21:56:59 +0100595 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900596
Willy Tarreaudec98142012-06-06 23:37:08 +0200597 switch (action) {
598 case STAT_CLI_O_TAB:
599 if (!ts)
600 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100601 chunk_reset(&trash);
602 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900603 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100604 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900605 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200606
607 case STAT_CLI_O_CLR:
608 if (!ts)
609 return;
610 if (ts->ref_cnt) {
611 /* don't delete an entry which is currently referenced */
612 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
613 si->applet.st0 = STAT_CLI_PRINT;
614 return;
615 }
616 stksess_kill(&px->table, ts);
617 break;
Simon Horman17bce342011-06-15 15:18:47 +0900618
Willy Tarreau654694e2012-06-07 01:03:16 +0200619 case STAT_CLI_O_SET:
620 if (strncmp(args[5], "data.", 5) != 0) {
621 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
622 si->applet.st0 = STAT_CLI_PRINT;
623 return;
624 }
625
626 data_type = stktable_get_data_type(args[5] + 5);
627 if (data_type < 0) {
628 si->applet.ctx.cli.msg = "Unknown data type\n";
629 si->applet.st0 = STAT_CLI_PRINT;
630 return;
631 }
632
633 if (!px->table.data_ofs[data_type]) {
634 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
635 si->applet.st0 = STAT_CLI_PRINT;
636 return;
637 }
638
639 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
640 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
641 si->applet.st0 = STAT_CLI_PRINT;
642 return;
643 }
644
645 if (ts)
646 stktable_touch(&px->table, ts, 1);
647 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100648 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200649 if (!ts) {
650 /* don't delete an entry which is currently referenced */
651 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
652 si->applet.st0 = STAT_CLI_PRINT;
653 return;
654 }
655 stktable_store(&px->table, ts, 1);
656 }
657
658 ptr = stktable_data_ptr(&px->table, ts, data_type);
659 switch (stktable_data_types[data_type].std_type) {
660 case STD_T_SINT:
661 stktable_data_cast(ptr, std_t_sint) = value;
662 break;
663 case STD_T_UINT:
664 stktable_data_cast(ptr, std_t_uint) = value;
665 break;
666 case STD_T_ULL:
667 stktable_data_cast(ptr, std_t_ull) = value;
668 break;
669 case STD_T_FRQP:
670 /* We only reset the previous value so that it slowly fades out */
671 frqp = &stktable_data_cast(ptr, std_t_frqp);
672 frqp->curr_tick = now_ms;
673 frqp->prev_ctr = value;
674 frqp->curr_ctr = 0;
675 break;
676 }
677 break;
678
Willy Tarreaudec98142012-06-06 23:37:08 +0200679 default:
680 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900681 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200682 break;
Simon Horman121f3052011-06-15 15:18:46 +0900683 }
Simon Horman121f3052011-06-15 15:18:46 +0900684}
685
Willy Tarreau654694e2012-06-07 01:03:16 +0200686static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900687{
Willy Tarreau654694e2012-06-07 01:03:16 +0200688 if (action != STAT_CLI_O_TAB) {
689 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
690 si->applet.st0 = STAT_CLI_PRINT;
691 return;
692 }
693
Simon Hormand5b9fd92011-06-15 15:18:48 +0900694 /* condition on stored data value */
695 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
696 if (si->applet.ctx.table.data_type < 0) {
697 si->applet.ctx.cli.msg = "Unknown data type\n";
698 si->applet.st0 = STAT_CLI_PRINT;
699 return;
700 }
701
702 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
703 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
704 si->applet.st0 = STAT_CLI_PRINT;
705 return;
706 }
707
708 si->applet.ctx.table.data_op = get_std_op(args[4]);
709 if (si->applet.ctx.table.data_op < 0) {
710 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
711 si->applet.st0 = STAT_CLI_PRINT;
712 return;
713 }
714
715 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
716 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
717 si->applet.st0 = STAT_CLI_PRINT;
718 return;
719 }
720}
721
Willy Tarreaudec98142012-06-06 23:37:08 +0200722static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900723{
724 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200725 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200726 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900727 si->applet.ctx.table.proxy = NULL;
728 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200729 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900730
731 if (*args[2]) {
732 si->applet.ctx.table.target = find_stktable(args[2]);
733 if (!si->applet.ctx.table.target) {
734 si->applet.ctx.cli.msg = "No such table\n";
735 si->applet.st0 = STAT_CLI_PRINT;
736 return;
737 }
738 }
739 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200740 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900741 goto err_args;
742 return;
743 }
744
745 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200746 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900747 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200748 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900749 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900750 goto err_args;
751
752 return;
753
754err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200755 switch (action) {
756 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900757 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 +0200758 break;
759 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900760 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 +0200761 break;
762 default:
763 si->applet.ctx.cli.msg = "Unknown action\n";
764 break;
765 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900766 si->applet.st0 = STAT_CLI_PRINT;
767}
768
Willy Tarreau532a4502011-09-07 22:37:44 +0200769/* Expects to find a frontend named <arg> and returns it, otherwise displays various
770 * adequate error messages and returns NULL. This function also expects the session
771 * level to be admin.
772 */
773static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
774{
775 struct proxy *px;
776
Willy Tarreau290e63a2012-09-20 18:07:14 +0200777 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200778 si->applet.ctx.cli.msg = stats_permission_denied_msg;
779 si->applet.st0 = STAT_CLI_PRINT;
780 return NULL;
781 }
782
783 if (!*arg) {
784 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
785 si->applet.st0 = STAT_CLI_PRINT;
786 return NULL;
787 }
788
789 px = findproxy(arg, PR_CAP_FE);
790 if (!px) {
791 si->applet.ctx.cli.msg = "No such frontend.\n";
792 si->applet.st0 = STAT_CLI_PRINT;
793 return NULL;
794 }
795 return px;
796}
797
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200798/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
799 * and returns the pointer to the server. Otherwise, display adequate error messages
800 * and returns NULL. This function also expects the session level to be admin. Note:
801 * the <arg> is modified to remove the '/'.
802 */
803static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
804{
805 struct proxy *px;
806 struct server *sv;
807 char *line;
808
Willy Tarreau290e63a2012-09-20 18:07:14 +0200809 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200810 si->applet.ctx.cli.msg = stats_permission_denied_msg;
811 si->applet.st0 = STAT_CLI_PRINT;
812 return NULL;
813 }
814
815 /* split "backend/server" and make <line> point to server */
816 for (line = arg; *line; line++)
817 if (*line == '/') {
818 *line++ = '\0';
819 break;
820 }
821
822 if (!*line || !*arg) {
823 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
824 si->applet.st0 = STAT_CLI_PRINT;
825 return NULL;
826 }
827
828 if (!get_backend_server(arg, line, &px, &sv)) {
829 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
830 si->applet.st0 = STAT_CLI_PRINT;
831 return NULL;
832 }
833
834 if (px->state == PR_STSTOPPED) {
835 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
836 si->applet.st0 = STAT_CLI_PRINT;
837 return NULL;
838 }
839
840 return sv;
841}
842
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200843/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200844 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100845 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200846 * designating the function which will have to process the request, which can
847 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200848 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900849static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200850{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200851 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200852 char *args[MAX_STATS_ARGS + 1];
853 int arg;
854
855 while (isspace((unsigned char)*line))
856 line++;
857
858 arg = 0;
859 args[arg] = line;
860
861 while (*line && arg < MAX_STATS_ARGS) {
862 if (isspace((unsigned char)*line)) {
863 *line++ = '\0';
864
865 while (isspace((unsigned char)*line))
866 line++;
867
868 args[++arg] = line;
869 continue;
870 }
871
872 line++;
873 }
874
875 while (++arg <= MAX_STATS_ARGS)
876 args[arg] = line;
877
Willy Tarreau295a8372011-03-10 11:25:07 +0100878 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200879 if (strcmp(args[0], "show") == 0) {
880 if (strcmp(args[1], "stat") == 0) {
881 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100882 si->applet.ctx.stats.flags |= STAT_BOUND;
883 si->applet.ctx.stats.iid = atoi(args[2]);
884 si->applet.ctx.stats.type = atoi(args[3]);
885 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200886 }
887
Willy Tarreau295a8372011-03-10 11:25:07 +0100888 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
889 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200890 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100891 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200892 }
893 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100894 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
895 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200896 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100897 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200898 }
899 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200900 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200901 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100902 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100903 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200904 return 1;
905 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100906 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100907 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100908 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100909 si->applet.ctx.sess.target = NULL;
910 si->applet.ctx.sess.section = 0; /* start with session status */
911 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100912 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200913 }
914 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200915 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100916 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100917 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200918 return 1;
919 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200920 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100921 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100923 si->applet.ctx.errors.iid = -1;
924 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200925 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100926 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200927 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200928 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200929 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200930 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700931 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200932 return 0;
933 }
934 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200935 else if (strcmp(args[0], "clear") == 0) {
936 if (strcmp(args[1], "counters") == 0) {
937 struct proxy *px;
938 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200939 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200940 int clrall = 0;
941
942 if (strcmp(args[2], "all") == 0)
943 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200944
Willy Tarreau6162db22009-10-10 17:13:00 +0200945 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200946 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
947 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100948 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100949 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200950 return 1;
951 }
952
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200953 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100954 if (clrall) {
955 memset(&px->be_counters, 0, sizeof(px->be_counters));
956 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
957 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200958 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100959 px->be_counters.conn_max = 0;
960 px->be_counters.p.http.rps_max = 0;
961 px->be_counters.sps_max = 0;
962 px->be_counters.cps_max = 0;
963 px->be_counters.nbpend_max = 0;
964
965 px->fe_counters.conn_max = 0;
966 px->fe_counters.p.http.rps_max = 0;
967 px->fe_counters.sps_max = 0;
968 px->fe_counters.cps_max = 0;
969 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200970 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200971
972 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200973 if (clrall)
974 memset(&sv->counters, 0, sizeof(sv->counters));
975 else {
976 sv->counters.cur_sess_max = 0;
977 sv->counters.nbpend_max = 0;
978 sv->counters.sps_max = 0;
979 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200980
Willy Tarreaua7944ad2012-09-26 21:03:11 +0200981 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200982 if (li->counters) {
983 if (clrall)
984 memset(li->counters, 0, sizeof(*li->counters));
985 else
986 li->counters->conn_max = 0;
987 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200988 }
989
Willy Tarreau81c25d02011-09-07 15:17:21 +0200990 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200991 return 1;
992 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200993 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200994 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200995 /* end of processing */
996 return 1;
997 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200998 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200999 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001000 return 0;
1001 }
1002 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001003 else if (strcmp(args[0], "get") == 0) {
1004 if (strcmp(args[1], "weight") == 0) {
1005 struct proxy *px;
1006 struct server *sv;
1007
1008 /* split "backend/server" and make <line> point to server */
1009 for (line = args[2]; *line; line++)
1010 if (*line == '/') {
1011 *line++ = '\0';
1012 break;
1013 }
1014
1015 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001016 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001017 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001018 return 1;
1019 }
1020
1021 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001022 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001023 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001024 return 1;
1025 }
1026
1027 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001028 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1029 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001030 return 1;
1031 }
1032 else { /* not "get weight" */
1033 return 0;
1034 }
1035 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001036 else if (strcmp(args[0], "set") == 0) {
1037 if (strcmp(args[1], "weight") == 0) {
1038 struct proxy *px;
1039 struct server *sv;
1040 int w;
1041
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001042 sv = expect_server_admin(s, si, args[2]);
1043 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001044 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001045 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001046
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001047 /* if the weight is terminated with '%', it is set relative to
1048 * the initial weight, otherwise it is absolute.
1049 */
1050 if (!*args[3]) {
1051 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001052 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001053 return 1;
1054 }
1055
Willy Tarreau4483d432009-10-10 19:30:08 +02001056 w = atoi(args[3]);
1057 if (strchr(args[3], '%') != NULL) {
1058 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001059 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001060 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001061 return 1;
1062 }
1063 w = sv->iweight * w / 100;
1064 }
1065 else {
1066 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001067 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001068 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001069 return 1;
1070 }
1071 }
1072
1073 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001074 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 +01001075 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001076 return 1;
1077 }
1078
1079 sv->uweight = w;
1080
1081 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1082 /* we must take care of not pushing the server to full throttle during slow starts */
1083 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1084 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1085 else
1086 sv->eweight = BE_WEIGHT_SCALE;
1087 sv->eweight *= sv->uweight;
1088 } else {
1089 sv->eweight = sv->uweight;
1090 }
1091
1092 /* static LB algorithms are a bit harder to update */
1093 if (px->lbprm.update_server_eweight)
1094 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001095 else if (sv->eweight) {
1096 if (px->lbprm.set_server_status_up)
1097 px->lbprm.set_server_status_up(sv);
1098 }
1099 else {
1100 if (px->lbprm.set_server_status_down)
1101 px->lbprm.set_server_status_down(sv);
1102 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001103
1104 return 1;
1105 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001106 else if (strcmp(args[1], "timeout") == 0) {
1107 if (strcmp(args[2], "cli") == 0) {
1108 unsigned timeout;
1109 const char *res;
1110
1111 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001112 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001113 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001114 return 1;
1115 }
1116
1117 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1118 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001119 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001120 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001121 return 1;
1122 }
1123
1124 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1125 return 1;
1126 }
1127 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001128 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001129 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001130 return 1;
1131 }
1132 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001133 else if (strcmp(args[1], "maxconn") == 0) {
1134 if (strcmp(args[2], "frontend") == 0) {
1135 struct proxy *px;
1136 struct listener *l;
1137 int v;
1138
Willy Tarreau532a4502011-09-07 22:37:44 +02001139 px = expect_frontend_admin(s, si, args[3]);
1140 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001141 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001142
1143 if (!*args[4]) {
1144 si->applet.ctx.cli.msg = "Integer value expected.\n";
1145 si->applet.st0 = STAT_CLI_PRINT;
1146 return 1;
1147 }
1148
1149 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001150 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001151 si->applet.ctx.cli.msg = "Value out of range.\n";
1152 si->applet.st0 = STAT_CLI_PRINT;
1153 return 1;
1154 }
1155
1156 /* OK, the value is fine, so we assign it to the proxy and to all of
1157 * its listeners. The blocked ones will be dequeued.
1158 */
1159 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001160 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001161 l->maxconn = v;
1162 if (l->state == LI_FULL)
1163 resume_listener(l);
1164 }
1165
1166 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1167 dequeue_all_listeners(&s->fe->listener_queue);
1168
1169 return 1;
1170 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001171 else if (strcmp(args[2], "global") == 0) {
1172 int v;
1173
Willy Tarreau290e63a2012-09-20 18:07:14 +02001174 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001175 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1176 si->applet.st0 = STAT_CLI_PRINT;
1177 return 1;
1178 }
1179
1180 if (!*args[3]) {
1181 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1182 si->applet.st0 = STAT_CLI_PRINT;
1183 return 1;
1184 }
1185
1186 v = atoi(args[3]);
1187 if (v > global.hardmaxconn) {
1188 si->applet.ctx.cli.msg = "Value out of range.\n";
1189 si->applet.st0 = STAT_CLI_PRINT;
1190 return 1;
1191 }
1192
1193 /* check for unlimited values */
1194 if (v <= 0)
1195 v = global.hardmaxconn;
1196
1197 global.maxconn = v;
1198
1199 /* Dequeues all of the listeners waiting for a resource */
1200 if (!LIST_ISEMPTY(&global_listener_queue))
1201 dequeue_all_listeners(&global_listener_queue);
1202
1203 return 1;
1204 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001205 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001206 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001207 si->applet.st0 = STAT_CLI_PRINT;
1208 return 1;
1209 }
1210 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001211 else if (strcmp(args[1], "rate-limit") == 0) {
1212 if (strcmp(args[2], "connections") == 0) {
1213 if (strcmp(args[3], "global") == 0) {
1214 int v;
1215
Willy Tarreau290e63a2012-09-20 18:07:14 +02001216 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001217 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1218 si->applet.st0 = STAT_CLI_PRINT;
1219 return 1;
1220 }
1221
1222 if (!*args[4]) {
1223 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1224 si->applet.st0 = STAT_CLI_PRINT;
1225 return 1;
1226 }
1227
1228 v = atoi(args[4]);
1229 if (v < 0) {
1230 si->applet.ctx.cli.msg = "Value out of range.\n";
1231 si->applet.st0 = STAT_CLI_PRINT;
1232 return 1;
1233 }
1234
1235 global.cps_lim = v;
1236
1237 /* Dequeues all of the listeners waiting for a resource */
1238 if (!LIST_ISEMPTY(&global_listener_queue))
1239 dequeue_all_listeners(&global_listener_queue);
1240
1241 return 1;
1242 }
1243 else {
1244 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1245 si->applet.st0 = STAT_CLI_PRINT;
1246 return 1;
1247 }
1248 }
1249 else {
1250 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1251 si->applet.st0 = STAT_CLI_PRINT;
1252 return 1;
1253 }
1254 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001255 else if (strcmp(args[1], "table") == 0) {
1256 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1257 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001258 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001259 return 0;
1260 }
1261 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001262 else if (strcmp(args[0], "enable") == 0) {
1263 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001264 struct server *sv;
1265
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001266 sv = expect_server_admin(s, si, args[2]);
1267 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001268 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001269
Cyril Bontécd19e512010-01-31 22:34:03 +01001270 if (sv->state & SRV_MAINTAIN) {
1271 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001272 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001273 /* If this server tracks the status of another one,
1274 * we must restore the good status.
1275 */
Willy Tarreau44267702011-10-28 15:35:33 +02001276 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001277 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001278 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001279 } else {
1280 sv->state &= ~SRV_MAINTAIN;
1281 set_server_down(sv);
1282 }
1283 } else {
1284 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001285 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001286 }
1287 }
1288
Willy Tarreau532a4502011-09-07 22:37:44 +02001289 return 1;
1290 }
1291 else if (strcmp(args[1], "frontend") == 0) {
1292 struct proxy *px;
1293
1294 px = expect_frontend_admin(s, si, args[2]);
1295 if (!px)
1296 return 1;
1297
1298 if (px->state == PR_STSTOPPED) {
1299 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1300 si->applet.st0 = STAT_CLI_PRINT;
1301 return 1;
1302 }
1303
1304 if (px->state != PR_STPAUSED) {
1305 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1306 si->applet.st0 = STAT_CLI_PRINT;
1307 return 1;
1308 }
1309
1310 if (!resume_proxy(px)) {
1311 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1312 si->applet.st0 = STAT_CLI_PRINT;
1313 return 1;
1314 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001315 return 1;
1316 }
1317 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001318 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1319 si->applet.st0 = STAT_CLI_PRINT;
1320 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001321 }
1322 }
1323 else if (strcmp(args[0], "disable") == 0) {
1324 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001325 struct server *sv;
1326
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001327 sv = expect_server_admin(s, si, args[2]);
1328 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001329 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001330
Cyril Bontécd19e512010-01-31 22:34:03 +01001331 if (! (sv->state & SRV_MAINTAIN)) {
1332 /* Not already in maintenance, we can change the server state */
1333 sv->state |= SRV_MAINTAIN;
1334 set_server_down(sv);
1335 }
1336
Willy Tarreau532a4502011-09-07 22:37:44 +02001337 return 1;
1338 }
1339 else if (strcmp(args[1], "frontend") == 0) {
1340 struct proxy *px;
1341
1342 px = expect_frontend_admin(s, si, args[2]);
1343 if (!px)
1344 return 1;
1345
1346 if (px->state == PR_STSTOPPED) {
1347 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1348 si->applet.st0 = STAT_CLI_PRINT;
1349 return 1;
1350 }
1351
1352 if (px->state == PR_STPAUSED) {
1353 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1354 si->applet.st0 = STAT_CLI_PRINT;
1355 return 1;
1356 }
1357
1358 if (!pause_proxy(px)) {
1359 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1360 si->applet.st0 = STAT_CLI_PRINT;
1361 return 1;
1362 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001363 return 1;
1364 }
1365 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001366 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1367 si->applet.st0 = STAT_CLI_PRINT;
1368 return 1;
1369 }
1370 }
1371 else if (strcmp(args[0], "shutdown") == 0) {
1372 if (strcmp(args[1], "frontend") == 0) {
1373 struct proxy *px;
1374
1375 px = expect_frontend_admin(s, si, args[2]);
1376 if (!px)
1377 return 1;
1378
1379 if (px->state == PR_STSTOPPED) {
1380 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1381 si->applet.st0 = STAT_CLI_PRINT;
1382 return 1;
1383 }
1384
1385 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1386 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1387 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1388 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1389 stop_proxy(px);
1390 return 1;
1391 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001392 else if (strcmp(args[1], "session") == 0) {
1393 struct session *sess, *ptr;
1394
Willy Tarreau290e63a2012-09-20 18:07:14 +02001395 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001396 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1397 si->applet.st0 = STAT_CLI_PRINT;
1398 return 1;
1399 }
1400
1401 if (!*args[2]) {
1402 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1403 si->applet.st0 = STAT_CLI_PRINT;
1404 return 1;
1405 }
1406
1407 ptr = (void *)strtoul(args[2], NULL, 0);
1408
1409 /* first, look for the requested session in the session table */
1410 list_for_each_entry(sess, &sessions, list) {
1411 if (sess == ptr)
1412 break;
1413 }
1414
1415 /* do we have the session ? */
1416 if (sess != ptr) {
1417 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1418 si->applet.st0 = STAT_CLI_PRINT;
1419 return 1;
1420 }
1421
1422 session_shutdown(sess, SN_ERR_KILLED);
1423 return 1;
1424 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001425 else if (strcmp(args[1], "sessions") == 0) {
1426 if (strcmp(args[2], "server") == 0) {
1427 struct server *sv;
1428 struct session *sess, *sess_bck;
1429
1430 sv = expect_server_admin(s, si, args[3]);
1431 if (!sv)
1432 return 1;
1433
1434 /* kill all the session that are on this server */
1435 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1436 if (sess->srv_conn == sv)
1437 session_shutdown(sess, SN_ERR_KILLED);
1438
1439 return 1;
1440 }
1441 else {
1442 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1443 si->applet.st0 = STAT_CLI_PRINT;
1444 return 1;
1445 }
1446 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001447 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001448 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001449 si->applet.st0 = STAT_CLI_PRINT;
1450 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001451 }
1452 }
1453 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001454 return 0;
1455 }
1456 return 1;
1457}
1458
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001459/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001460 * used to processes I/O from/to the stats unix socket. The system relies on a
1461 * state machine handling requests and various responses. We read a request,
1462 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001463 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1464 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001465 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001466 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001467static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001468{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001469 struct channel *req = si->ob;
1470 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001471 int reql;
1472 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001473
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001474 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1475 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001476
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001477 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001478 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001479 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001480 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001481 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001482 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001483 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001484 /* Let's close for real now. We just close the request
1485 * side, the conditions below will complete if needed.
1486 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001487 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001488 break;
1489 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001490 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001491 /* ensure we have some output room left in the event we
1492 * would want to return some info right after parsing.
1493 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001494 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001495 break;
1496
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001497 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001498 if (reql <= 0) { /* closed or EOL not found */
1499 if (reql == 0)
1500 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001501 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001502 continue;
1503 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001504
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001505 /* seek for a possible semi-colon. If we find one, we
1506 * replace it with an LF and skip only this part.
1507 */
1508 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001509 if (trash.str[len] == ';') {
1510 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001511 reql = len + 1;
1512 break;
1513 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001514
Willy Tarreau816fc222009-10-04 07:36:58 +02001515 /* now it is time to check that we have a full line,
1516 * remove the trailing \n and possibly \r, then cut the
1517 * line.
1518 */
1519 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001520 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001521 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001522 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001523 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001524
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001525 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001526 len--;
1527
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001528 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001529
Willy Tarreaubc4af052011-02-13 13:25:14 +01001530 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001531 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001532 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001533 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001534 continue;
1535 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001536 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001537 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001538 else if (strcmp(trash.str, "help") == 0 ||
1539 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001540 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001541 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001542 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001543 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001545 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001547 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001548 /* if prompt is disabled, print help on empty lines,
1549 * so that the user at least knows how to enable
1550 * prompt and find help.
1551 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001552 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001553 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001554 }
1555
1556 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001557 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001558 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001559 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001560 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001561 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001562 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001563 continue;
1564 }
1565
Willy Tarreaubc4af052011-02-13 13:25:14 +01001566 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001567 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001568 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001569 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001570 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001571 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001572 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001573 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001574 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001575 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001576 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001577 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001578 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001579 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001580 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001581 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001582 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001583 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001584 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001585 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001586 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001587 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001588 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001589 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001590 break;
1591 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001592
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001593 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001594 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001595 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001596 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597 }
1598
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001599 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001600 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001601 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001602
1603 /* Now we close the output if one of the writers did so,
1604 * or if we're not in interactive mode and the request
1605 * buffer is empty. This still allows pipelined requests
1606 * to be sent in non-interactive mode.
1607 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001608 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001609 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001610 continue;
1611 }
1612
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001613 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001614 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001615 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001616 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001617
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001618 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001619 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1620 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001621 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001622 * and nothing more to consume. This is comparable to a broken pipe, so
1623 * we forward the close to the request side so that it flows upstream to
1624 * the client.
1625 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001626 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001627 }
1628
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001629 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001630 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1631 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1632 /* We have no more processing to do, and nothing more to send, and
1633 * the client side has closed. So we'll forward this state downstream
1634 * on the response buffer.
1635 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001636 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001637 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001638 }
1639
1640 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001641 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001642
1643 /* we don't want to expire timeouts while we're processing requests */
1644 si->ib->rex = TICK_ETERNITY;
1645 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001646
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001647 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001648 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 +02001649 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001650 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 +02001651
1652 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1653 /* check that we have released everything then unregister */
1654 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001655 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001656}
1657
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001658/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001659 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001660 * It returns 0 as long as it does not complete, non-zero upon completion.
1661 * Some states are not used but it makes the code more similar to other
1662 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001663 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001664static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001665{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001666 struct proxy *px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001667 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001668
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001669 chunk_reset(&trash);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001670
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001671 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001672 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001673 /* the function had not been called yet */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001674 si->conn->xprt_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001675 /* fall through */
1676
Willy Tarreau295a8372011-03-10 11:25:07 +01001677 case STAT_ST_HEAD:
1678 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001679 print_csv_header(&trash);
1680 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001681 return 0;
1682 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001683
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001684 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001685 /* fall through */
1686
Willy Tarreau295a8372011-03-10 11:25:07 +01001687 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001688 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001689 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001690 chunk_appendf(&trash,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001691 "Name: " PRODUCT_NAME "\n"
1692 "Version: " HAPROXY_VERSION "\n"
1693 "Release_date: " HAPROXY_DATE "\n"
1694 "Nbproc: %d\n"
1695 "Process_num: %d\n"
1696 "Pid: %d\n"
1697 "Uptime: %dd %dh%02dm%02ds\n"
1698 "Uptime_sec: %d\n"
1699 "Memmax_MB: %d\n"
1700 "Ulimit-n: %d\n"
1701 "Maxsock: %d\n"
1702 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001703 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001704 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001705 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001706 "PipesUsed: %d\n"
1707 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001708 "ConnRate: %d\n"
1709 "ConnRateLimit: %d\n"
1710 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001711 "Tasks: %d\n"
1712 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001713 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001714 "node: %s\n"
1715 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001716 "",
1717 global.nbproc,
1718 relative_pid,
1719 pid,
1720 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1721 up,
1722 global.rlimit_memmax,
1723 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001724 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001725 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001726 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001727 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001728 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001729 );
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001730 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001731 return 0;
1732 }
1733
Willy Tarreau295a8372011-03-10 11:25:07 +01001734 si->applet.ctx.stats.px = proxy;
1735 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1736 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001737 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001738 /* fall through */
1739
Willy Tarreau295a8372011-03-10 11:25:07 +01001740 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001741 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001742 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1743 while (si->applet.ctx.stats.px) {
1744 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02001745 /* skip the disabled proxies, global frontend and non-networked ones */
1746 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001747 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001748 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001749 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001750 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001751
Willy Tarreau295a8372011-03-10 11:25:07 +01001752 si->applet.ctx.stats.px = px->next;
1753 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001754 }
1755 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001756 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001757
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001758 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001759 /* fall through */
1760
Willy Tarreau295a8372011-03-10 11:25:07 +01001761 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001762 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001763 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001764
Willy Tarreau295a8372011-03-10 11:25:07 +01001765 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001766 return 1;
1767
1768 default:
1769 /* unknown state ! */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001770 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001771 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001772 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001773}
1774
1775
Cyril Bonté70be45d2010-10-12 00:14:35 +02001776/* We don't want to land on the posted stats page because a refresh will
1777 * repost the data. We don't want this to happen on accident so we redirect
1778 * the browse to the stats page with a GET.
1779 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001780static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001781{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001782 struct session *s = si->conn->xprt_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001783
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001784 chunk_reset(&trash);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001785
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001786 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001787 case STAT_ST_INIT:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001788 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02001789 "HTTP/1.0 303 See Other\r\n"
1790 "Cache-Control: no-cache\r\n"
1791 "Content-Type: text/plain\r\n"
1792 "Connection: close\r\n"
1793 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001794 uri->uri_prefix,
1795 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1796 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1797 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1798 stat_status_codes[si->applet.ctx.stats.st_code] :
1799 stat_status_codes[STAT_STATUS_UNKN]);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001800 chunk_appendf(&trash, "\r\n\r\n");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001801
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001802 if (bi_putchk(si->ib, &trash) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001803 return 0;
1804
1805 s->txn.status = 303;
1806
1807 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1808 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1809 if (!(s->flags & SN_FINST_MASK))
1810 s->flags |= SN_FINST_R;
1811
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001812 si->conn->xprt_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001813 return 1;
1814 }
1815 return 1;
1816}
1817
1818
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001819/* This I/O handler runs as an applet embedded in a stream interface. It is
1820 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001821 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001822 * automatically unregisters itself once transfer is complete.
1823 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001824static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001825{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001826 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001827 struct channel *req = si->ob;
1828 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001829
1830 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1831 goto out;
1832
1833 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001834 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001835 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001836
Willy Tarreaubc4af052011-02-13 13:25:14 +01001837 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001838 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001839 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001840 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001841 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001842 }
1843 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001844 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001845 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001846 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001847 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001848 }
1849 }
1850
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001851 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001852 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001853
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001854 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001855 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001856 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001857 }
1858
1859 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001860 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001861
1862 /* we don't want to expire timeouts while we're processing requests */
1863 si->ib->rex = TICK_ETERNITY;
1864 si->ob->wex = TICK_ETERNITY;
1865
1866 out:
1867 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1868 /* check that we have released everything then unregister */
1869 stream_int_unregister_handler(si);
1870 }
1871}
1872
1873
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001874/* This function dumps statistics in HTTP format onto the stream interface's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001875 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001876 * properly set. It returns 0 if it had to stop writing data and an I/O is
1877 * needed, 1 if the dump is finished and the session must be closed, or -1
1878 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001879 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001880static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001881{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001882 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001883 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001884 struct proxy *px;
Willy Tarreau91861262007-10-17 17:06:05 +02001885 unsigned int up;
1886
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001887 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02001888
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001889 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001890 case STAT_ST_INIT:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001891 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02001892 "HTTP/1.0 200 OK\r\n"
1893 "Cache-Control: no-cache\r\n"
1894 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001895 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001896 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001897
Willy Tarreau295a8372011-03-10 11:25:07 +01001898 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001899 chunk_appendf(&trash, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001900 uri->refresh);
1901
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001902 chunk_appendf(&trash, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001903
1904 s->txn.status = 200;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001905 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001906 return 0;
1907
Willy Tarreau91861262007-10-17 17:06:05 +02001908 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1909 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1910 if (!(s->flags & SN_FINST_MASK))
1911 s->flags |= SN_FINST_R;
1912
1913 if (s->txn.meth == HTTP_METH_HEAD) {
1914 /* that's all we return in case of HEAD request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001915 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001916 return 1;
1917 }
1918
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001919 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001920 /* fall through */
1921
Willy Tarreau295a8372011-03-10 11:25:07 +01001922 case STAT_ST_HEAD:
1923 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001924 /* WARNING! This must fit in the first buffer !!! */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001925 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001926 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1927 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001928 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001929 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1930 "<style type=\"text/css\"><!--\n"
1931 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001932 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001933 " font-size: 12px;"
1934 " font-weight: normal;"
1935 " color: black;"
1936 " background: white;"
1937 "}\n"
1938 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001939 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001940 "}\n"
1941 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001942 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001943 " margin-bottom: 0.5em;"
1944 "}\n"
1945 "h2 {"
1946 " font-family: helvetica, arial;"
1947 " font-size: x-large;"
1948 " font-weight: bold;"
1949 " font-style: italic;"
1950 " color: #6020a0;"
1951 " margin-top: 0em;"
1952 " margin-bottom: 0em;"
1953 "}\n"
1954 "h3 {"
1955 " font-family: helvetica, arial;"
1956 " font-size: 16px;"
1957 " font-weight: bold;"
1958 " color: #b00040;"
1959 " background: #e8e8d0;"
1960 " margin-top: 0em;"
1961 " margin-bottom: 0em;"
1962 "}\n"
1963 "li {"
1964 " margin-top: 0.25em;"
1965 " margin-right: 2em;"
1966 "}\n"
1967 ".hr {margin-top: 0.25em;"
1968 " border-color: black;"
1969 " border-bottom-style: solid;"
1970 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001971 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001972 ".total {background: #20D0D0;color: #ffff80;}\n"
1973 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001974 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001975 ".backend {background: #e8e8d0;}\n"
1976 ".active0 {background: #ff9090;}\n"
1977 ".active1 {background: #ffd020;}\n"
1978 ".active2 {background: #ffffa0;}\n"
1979 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001980 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1981 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1982 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001983 ".backup0 {background: #ff9090;}\n"
1984 ".backup1 {background: #ff80ff;}\n"
1985 ".backup2 {background: #c060ff;}\n"
1986 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001987 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1988 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1989 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001990 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001991 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001992 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001993 "a.px:link {color: #ffff40; text-decoration: none;}"
1994 "a.px:visited {color: #ffff40; text-decoration: none;}"
1995 "a.px:hover {color: #ffffff; text-decoration: none;}"
1996 "a.lfsb:link {color: #000000; text-decoration: none;}"
1997 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1998 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1999 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002000 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002001 "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"
2002 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002003 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002004 "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 +02002005 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002006 "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 +02002007 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002008 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2009 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2010 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002011 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002012 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002013 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002014 (uri->flags&ST_SHNODE) ? " on " : "",
2015 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002016 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002017 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002018 print_csv_header(&trash);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002019 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002020 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002021 return 0;
2022
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002023 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002024 /* fall through */
2025
Willy Tarreau295a8372011-03-10 11:25:07 +01002026 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002027 up = (now.tv_sec - start_date.tv_sec);
2028
2029 /* WARNING! this has to fit the first packet too.
2030 * We are around 3.5 kB, add adding entries will
2031 * become tricky if we want to support 4kB buffers !
2032 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002033 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002034 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002035 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2036 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002037 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002038 "<hr width=\"100%%\" class=\"hr\">\n"
2039 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002040 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002041 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002042 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002043 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2044 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002045 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002046 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002047 "</td><td align=\"center\" nowrap>\n"
2048 "<table class=\"lgd\"><tr>\n"
2049 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2050 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2051 "</tr><tr>\n"
2052 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2053 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2054 "</tr><tr>\n"
2055 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2056 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2057 "</tr><tr>\n"
2058 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002059 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002060 "</tr><tr>\n"
2061 "<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 +02002062 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002063 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002064 "</td>"
2065 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2066 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2067 "",
2068 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002069 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2070 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2071 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002072 up / 86400, (up % 86400) / 3600,
2073 (up % 3600) / 60, (up % 60),
2074 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2075 global.rlimit_memmax ? " MB" : "",
2076 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002077 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002078 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002079 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002080 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002081
Willy Tarreau295a8372011-03-10 11:25:07 +01002082 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002083 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002084 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2085 uri->uri_prefix,
2086 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002087 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002088 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002089 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002090 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2091 uri->uri_prefix,
2092 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002093 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002094
Willy Tarreau55bb8452007-10-17 18:44:57 +02002095 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002096 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002097 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002098 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2099 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002101 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002102 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002103 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002104 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2105 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002106 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002107 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002108 }
Willy Tarreau91861262007-10-17 17:06:05 +02002109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002110 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002111 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2112 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002113 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2114 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002116 chunk_appendf(&trash,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002117 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2118 uri->uri_prefix,
2119 (uri->refresh > 0) ? ";norefresh" : "");
2120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002121 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002122 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002123 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002124 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002125 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2126 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2127 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2128 "</ul>"
2129 "</td>"
2130 "</tr></table>\n"
2131 ""
2132 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002133
Willy Tarreau295a8372011-03-10 11:25:07 +01002134 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002135 switch (si->applet.ctx.stats.st_code) {
2136 case STAT_STATUS_DONE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002137 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002138 "<p><div class=active3>"
2139 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2140 "Action processed successfully."
2141 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002142 break;
2143 case STAT_STATUS_NONE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002144 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002145 "<p><div class=active2>"
2146 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2147 "Nothing has changed."
2148 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002149 break;
2150 case STAT_STATUS_PART:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002151 chunk_appendf(&trash,
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002152 "<p><div class=active2>"
2153 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2154 "Action partially processed.<br>"
2155 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2156 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002157 break;
2158 case STAT_STATUS_ERRP:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002159 chunk_appendf(&trash,
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002160 "<p><div class=active0>"
2161 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2162 "Action not processed because of invalid parameters."
2163 "<ul>"
2164 "<li>The action is maybe unknown.</li>"
2165 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2166 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2167 "</ul>"
2168 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002169 break;
2170 case STAT_STATUS_EXCD:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002171 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002172 "<p><div class=active0>"
2173 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2174 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2175 "You should retry with less servers at a time.</b>"
2176 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002177 break;
2178 case STAT_STATUS_DENY:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002179 chunk_appendf(&trash,
Cyril Bonté474be412010-10-12 00:14:36 +02002180 "<p><div class=active0>"
2181 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2182 "<b>Action denied.</b>"
2183 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002184 break;
2185 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002186 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002187 "<p><div class=active6>"
2188 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2189 "Unexpected result."
2190 "</div>\n", uri->uri_prefix);
2191 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002192 chunk_appendf(&trash,"<p>\n");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002193 }
2194
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002195 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002196 return 0;
2197 }
Willy Tarreau91861262007-10-17 17:06:05 +02002198
Willy Tarreau295a8372011-03-10 11:25:07 +01002199 si->applet.ctx.stats.px = proxy;
2200 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002201 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002202 /* fall through */
2203
Willy Tarreau295a8372011-03-10 11:25:07 +01002204 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002205 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002206 while (si->applet.ctx.stats.px) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002207 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002208 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002209 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02002210 /* skip the disabled proxies, global frontend and non-networked ones */
2211 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002212 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002213 return 0;
2214
Willy Tarreau295a8372011-03-10 11:25:07 +01002215 si->applet.ctx.stats.px = px->next;
2216 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002217 }
2218 /* here, we just have reached the last proxy */
2219
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002220 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002221 /* fall through */
2222
Willy Tarreau295a8372011-03-10 11:25:07 +01002223 case STAT_ST_END:
2224 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002225 chunk_appendf(&trash, "</body></html>\n");
2226 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002227 return 0;
2228 }
Willy Tarreau91861262007-10-17 17:06:05 +02002229
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002230 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002231 /* fall through */
2232
Willy Tarreau295a8372011-03-10 11:25:07 +01002233 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002234 return 1;
2235
2236 default:
2237 /* unknown state ! */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002238 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002239 return -1;
2240 }
2241}
2242
2243
2244/*
2245 * Dumps statistics for a proxy.
2246 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2247 * ot non-zero if everything completed.
2248 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002249static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002250{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002251 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002252 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002253 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002254 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002255
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002256 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002257
Willy Tarreau295a8372011-03-10 11:25:07 +01002258 switch (si->applet.ctx.stats.px_st) {
2259 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002260 /* we are on a new proxy */
2261
2262 if (uri && uri->scope) {
2263 /* we have a limited scope, we have to check the proxy name */
2264 struct stat_scope *scope;
2265 int len;
2266
2267 len = strlen(px->id);
2268 scope = uri->scope;
2269
2270 while (scope) {
2271 /* match exact proxy name */
2272 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2273 break;
2274
2275 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002276 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002277 break;
2278 scope = scope->next;
2279 }
2280
2281 /* proxy name not found : don't dump anything */
2282 if (scope == NULL)
2283 return 1;
2284 }
2285
Willy Tarreau295a8372011-03-10 11:25:07 +01002286 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2287 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002288 return 1;
2289
Willy Tarreau295a8372011-03-10 11:25:07 +01002290 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002291 /* fall through */
2292
Willy Tarreau295a8372011-03-10 11:25:07 +01002293 case STAT_PX_ST_TH:
2294 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2295 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002296 /* A form to enable/disable this proxy servers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002297 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002298 "<form action=\"%s\" method=\"post\">",
2299 uri->uri_prefix);
2300 }
2301
Willy Tarreau55bb8452007-10-17 18:44:57 +02002302 /* print a new table */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002303 chunk_appendf(&trash,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002304 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002305 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002306 "<th class=\"pxname\" width=\"10%%\"");
2307
2308 if (uri->flags&ST_SHLGNDS) {
2309 /* cap, mode, id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002310 chunk_appendf(&trash, " title=\"cap: %s, mode: %s, id: %d",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002311 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2312 px->uuid);
2313
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002314 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002315 }
2316
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002317 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002318 ">%s<a name=\"%s\"></a>"
2319 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002320 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002321 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002322 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002323 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002324 "<tr class=\"titre\">",
2325 (uri->flags & ST_SHLGNDS)?"<u>":"",
2326 px->id, px->id, px->id,
2327 (uri->flags & ST_SHLGNDS)?"</u>":"",
2328 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2329
Willy Tarreau295a8372011-03-10 11:25:07 +01002330 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002331 /* Column heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002332 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002333 }
2334
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002335 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002336 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002337 "<th colspan=3>Queue</th>"
2338 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002339 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002340 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002341 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002342 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002343 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002344 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002345 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002346 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002347 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002348 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002349 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002350 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2351 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002352 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002353
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002354 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002355 return 0;
2356 }
Willy Tarreau91861262007-10-17 17:06:05 +02002357
Willy Tarreau295a8372011-03-10 11:25:07 +01002358 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002359 /* fall through */
2360
Willy Tarreau295a8372011-03-10 11:25:07 +01002361 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002362 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002363 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002364 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2365 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002366 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002367 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002368 "<tr class=\"frontend\">");
2369
Willy Tarreau295a8372011-03-10 11:25:07 +01002370 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002371 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002372 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002373 }
2374
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002375 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002376 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002377 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002378 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2379 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002380 "",
2381 px->id, px->id);
2382
2383 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002384 chunk_appendf(&trash,
Willy Tarreaub44939a2010-02-26 11:35:39 +01002385 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002386 "<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 +01002387 "",
2388 read_freq_ctr(&px->fe_req_per_sec),
2389 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002390 px->fe_counters.p.http.rps_max,
2391 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002392 LIM2A2(px->fe_sps_lim, "-"));
2393 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002394 chunk_appendf(&trash,
Willy Tarreaub44939a2010-02-26 11:35:39 +01002395 /* sessions rate : current, max, limit */
2396 "<td>%s</td><td>%s</td><td>%s</td>"
2397 "",
2398 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002399 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002400 }
2401
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002402 chunk_appendf(&trash,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002403 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002404 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002405 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002406 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002407 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002408
2409 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2410 if (px->mode == PR_MODE_HTTP) {
2411 int i;
2412
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002413 chunk_appendf(&trash, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002414
2415 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002416 chunk_appendf(&trash, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002417
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002418 chunk_appendf(&trash, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2419 chunk_appendf(&trash, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002420 }
2421
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002422 chunk_appendf(&trash,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002423 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002424 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002425 /* bytes : in, out */
2426 "<td>%s</td><td>%s</td>"
2427 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002428 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002429 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002430 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002431 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002432
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002433 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002434 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002435 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002436 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002437 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002438 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002439 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002440 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002441 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002442 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002443 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002444 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002445 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2446 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002447 px->state == PR_STREADY ? "OPEN" :
2448 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002449 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002450 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002451 /* pxid, name, queue cur, queue max, */
2452 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002453 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002454 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002455 /* bytes : in, out */
2456 "%lld,%lld,"
2457 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002458 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002459 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002460 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002461 /* warnings: retries, redispatches */
2462 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002463 /* server status : reflect frontend status */
2464 "%s,"
2465 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002466 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002467 /* pid, iid, sid, throttle, lbtot, tracked, type */
2468 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002469 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002470 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002471 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002472 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002473 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002474 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2475 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2476 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2477 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002478 px->state == PR_STREADY ? "OPEN" :
2479 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002480 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002481 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002482 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002483
2484 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2485 if (px->mode == PR_MODE_HTTP) {
2486 int i;
2487
2488 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002489 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002490
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002491 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002492 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002493 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002494 }
2495
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002496 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002497 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002498
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002499 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002500 chunk_appendf(&trash, "%u,%u,%lld,",
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002501 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002502 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002503
Willy Tarreauae526782010-03-04 20:34:23 +01002504 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002505 chunk_appendf(&trash, ",,");
Willy Tarreauae526782010-03-04 20:34:23 +01002506
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002507 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002508 chunk_appendf(&trash, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002509 }
Willy Tarreau91861262007-10-17 17:06:05 +02002510
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002511 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002512 return 0;
2513 }
2514
Willy Tarreau4348fad2012-09-20 16:48:07 +02002515 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002516 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002517 /* fall through */
2518
Willy Tarreau295a8372011-03-10 11:25:07 +01002519 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002520 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002521 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 +02002522 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002523 return 0;
2524
Willy Tarreau4348fad2012-09-20 16:48:07 +02002525 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002526 if (!l->counters)
2527 continue;
2528
Willy Tarreau295a8372011-03-10 11:25:07 +01002529 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2530 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002531 break;
2532
Willy Tarreau295a8372011-03-10 11:25:07 +01002533 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002534 continue;
2535 }
2536
Willy Tarreau295a8372011-03-10 11:25:07 +01002537 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002538 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002539 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002540 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002541 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002542 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002543 chunk_appendf(&trash, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002544
2545 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002546 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002547 int port;
2548
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002549 chunk_appendf(&trash, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002550
Willy Tarreau631f01c2011-09-05 00:36:48 +02002551 port = get_host_port(&l->addr);
2552 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2553 case AF_INET:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002554 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
Willy Tarreau631f01c2011-09-05 00:36:48 +02002555 break;
2556 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002557 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
Willy Tarreau631f01c2011-09-05 00:36:48 +02002558 break;
2559 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002560 chunk_appendf(&trash, "unix, ");
Willy Tarreau631f01c2011-09-05 00:36:48 +02002561 break;
2562 case -1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002563 chunk_appendf(&trash, "(%s), ", strerror(errno));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002564 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002565 }
2566
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002567 /* id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002568 chunk_appendf(&trash, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002569 }
2570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002571 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002572 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002573 ">%s<a name=\"%s/+%s\"></a>"
2574 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002575 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002576 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002577 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002578 "<td>%s</td><td>%s</td><td>%s</td>"
2579 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002580 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002581 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002582 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002583 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002584 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002585 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002586 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2587 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002589 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002590 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002591 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002592 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002593 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002594 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002595 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002596 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002597 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002598 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002599 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002600 "",
2601 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2602 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002603 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002604 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002605 chunk_appendf(&trash,
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002606 /* pxid, name, queue cur, queue max, */
2607 "%s,%s,,,"
2608 /* sessions: current, max, limit, total */
2609 "%d,%d,%d,%lld,"
2610 /* bytes: in, out */
2611 "%lld,%lld,"
2612 /* denied: req, resp */
2613 "%lld,%lld,"
2614 /* errors: request, connect, response */
2615 "%lld,,,"
2616 /* warnings: retries, redispatches */
2617 ",,"
2618 /* server status: reflect listener status */
2619 "%s,"
2620 /* rest of server: nothing */
2621 ",,,,,,,,"
2622 /* pid, iid, sid, throttle, lbtot, tracked, type */
2623 "%d,%d,%d,,,,%d,"
2624 /* rate, rate_lim, rate_max */
2625 ",,,"
2626 /* check_status, check_code, check_duration */
2627 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002628 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2629 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002630 /* failed health analyses */
2631 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002632 /* requests : req_rate, req_rate_max, req_tot, */
2633 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002634 /* errors: cli_aborts, srv_aborts */
2635 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002636 "\n",
2637 px->id, l->name,
2638 l->nbconn, l->counters->conn_max,
2639 l->maxconn, l->counters->cum_conn,
2640 l->counters->bytes_in, l->counters->bytes_out,
2641 l->counters->denied_req, l->counters->denied_resp,
2642 l->counters->failed_req,
2643 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2644 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2645 }
2646
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002647 if (bi_putchk(rep, &trash) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002648 return 0;
2649 }
2650
Willy Tarreau295a8372011-03-10 11:25:07 +01002651 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2652 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002653 /* fall through */
2654
Willy Tarreau295a8372011-03-10 11:25:07 +01002655 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002656 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002657 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002658 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 +02002659
Willy Tarreau9b28e032012-10-12 23:49:43 +02002660 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002661 return 0;
2662
Willy Tarreau295a8372011-03-10 11:25:07 +01002663 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002664
Willy Tarreau295a8372011-03-10 11:25:07 +01002665 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2666 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002667 break;
2668
Willy Tarreau295a8372011-03-10 11:25:07 +01002669 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002670 continue;
2671 }
2672
Willy Tarreau44267702011-10-28 15:35:33 +02002673 if (sv->track)
2674 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002675 else
2676 svs = sv;
2677
Willy Tarreau91861262007-10-17 17:06:05 +02002678 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002679 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002680 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002681 else if (svs->state & SRV_RUNNING) {
2682 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002683 sv_state = 3; /* UP */
2684 else
2685 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002686
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002687 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002688 sv_state += 2;
2689 }
Willy Tarreau91861262007-10-17 17:06:05 +02002690 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002691 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002692 sv_state = 1; /* going up */
2693 else
2694 sv_state = 0; /* DOWN */
2695
Willy Tarreau295a8372011-03-10 11:25:07 +01002696 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002697 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002698 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002699 continue;
2700 }
2701
Willy Tarreau295a8372011-03-10 11:25:07 +01002702 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002703 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2704 "UP %d/%d &darr;", "UP",
2705 "NOLB %d/%d &darr;", "NOLB",
2706 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002707 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002708 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002709 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002710 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002711 );
2712 }
2713 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002714 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002715 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002716 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002717 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2718 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002719
Willy Tarreau295a8372011-03-10 11:25:07 +01002720 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002721 chunk_appendf(&trash,
Cyril Bonté474be412010-10-12 00:14:36 +02002722 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2723 sv->id);
2724 }
2725
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002726 chunk_appendf(&trash, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002727
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002728 if (uri->flags&ST_SHLGNDS) {
2729 char str[INET6_ADDRSTRLEN];
2730
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002731 chunk_appendf(&trash, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002732
Willy Tarreau631f01c2011-09-05 00:36:48 +02002733 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002734 case AF_INET:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002735 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002736 break;
2737 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002738 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002739 break;
2740 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002741 chunk_appendf(&trash, "unix, ");
Willy Tarreau631f01c2011-09-05 00:36:48 +02002742 break;
2743 case -1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002744 chunk_appendf(&trash, "(%s), ", strerror(errno));
Willy Tarreau631f01c2011-09-05 00:36:48 +02002745 break;
2746 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002747 break;
2748 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002749
2750 /* id */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002751 chunk_appendf(&trash, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002752
2753 /* cookie */
2754 if (sv->cookie) {
2755 struct chunk src;
2756
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002757 chunk_appendf(&trash, ", cookie: '");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002758
2759 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002760 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002761
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002762 chunk_appendf(&trash, "'");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002763 }
2764
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002765 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002766 }
2767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002768 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002769 ">%s<a name=\"%s/%s\"></a>"
2770 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002771 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002772 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002773 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002774 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002775 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002776 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002777 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002778 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002779 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002780 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002781 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002782 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2783 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002784 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2785
2786 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2787 if (px->mode == PR_MODE_HTTP) {
2788 int i;
2789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002790 chunk_appendf(&trash, " title=\"rsp codes:");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002791
2792 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002793 chunk_appendf(&trash, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002794
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002795 chunk_appendf(&trash, " other=%lld\"", sv->counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002796 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002797
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002798 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002799 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002800 ">%s%s%s</td><td>%s</td>",
2801 (px->mode == PR_MODE_HTTP)?"<u>":"",
2802 U2H0(sv->counters.cum_sess),
2803 (px->mode == PR_MODE_HTTP)?"</u>":"",
2804 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002805
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002806 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002807 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002808 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002809 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002810 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002811 /* errors : request, connect */
2812 "<td></td><td>%s</td>"
2813 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002814 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002815 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002816 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002817 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002818 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2819 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002820 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002821 sv->counters.cli_aborts,
2822 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002823 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002824 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002825
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002826 /* status, lest check */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002827 chunk_appendf(&trash, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002828
Cyril Bontécd19e512010-01-31 22:34:03 +01002829 if (sv->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002830 chunk_appendf(&trash, "%s ",
Cyril Bontécd19e512010-01-31 22:34:03 +01002831 human_time(now.tv_sec - sv->last_change, 1));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002832 chunk_appendf(&trash, "MAINT");
Cyril Bontécd19e512010-01-31 22:34:03 +01002833 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002834 else if (svs != sv && svs->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002835 chunk_appendf(&trash, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002836 human_time(now.tv_sec - svs->last_change, 1));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002837 chunk_appendf(&trash, "MAINT(via)");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002838 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002839 else if (svs->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002840 chunk_appendf(&trash, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002841 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002842
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002843 chunk_appendf(&trash,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002844 srv_hlt_st[sv_state],
2845 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2846 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002847 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002848
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002849 if (sv->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002850 chunk_appendf(&trash, "</td><td class=ac title=\"%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002851 get_check_status_description(sv->check.status));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002852
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002853 if (*sv->check.desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002854 struct chunk src;
2855
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002856 chunk_appendf(&trash, ": ");
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002857
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002858 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002859 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002860 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002861
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002862 chunk_appendf(&trash, "\"><u> %s%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002863 tv_iszero(&sv->check.start)?"":"* ",
2864 get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002865
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002866 if (sv->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002867 chunk_appendf(&trash, "/%d", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002868
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002869 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002870 chunk_appendf(&trash, " in %lums</u>", sv->check.duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002871 } else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002872 chunk_appendf(&trash, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002873
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002874 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02002875 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002876 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002877 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002878 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002879 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002880 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002881 (sv->state & SRV_BACKUP) ? "-" : "Y",
2882 (sv->state & SRV_BACKUP) ? "Y" : "-");
2883
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002884 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002885 if (sv->state & SRV_CHECKED) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002886 chunk_appendf(&trash, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002887 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2888
2889 if (svs->observe)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002890 chunk_appendf(&trash, "/%lld", svs->counters.failed_hana);
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002891
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002892 chunk_appendf(&trash,
Willy Tarreaue0454092010-02-26 12:29:07 +01002893 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002894 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002895 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002896 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002897 } else if (sv != svs)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002898 chunk_appendf(&trash,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002899 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2900 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002901 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002902 chunk_appendf(&trash,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002903 "<td colspan=3></td>");
2904
2905 /* throttle */
2906 if ((sv->state & SRV_WARMINGUP) &&
2907 now.tv_sec < sv->last_change + sv->slowstart &&
2908 now.tv_sec >= sv->last_change) {
2909 unsigned int ratio;
2910 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002911 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002912 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002913 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002914 chunk_appendf(&trash,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002915 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002916 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002917 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002918 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2919 "UP %d/%d,", "UP,",
2920 "NOLB %d/%d,", "NOLB,",
2921 "no check," };
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002922 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002923 /* pxid, name */
2924 "%s,%s,"
2925 /* queue : current, max */
2926 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002927 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002928 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002929 /* bytes : in, out */
2930 "%lld,%lld,"
2931 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002932 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002933 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002934 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002935 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002936 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002937 "",
2938 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002939 sv->nbpend, sv->counters.nbpend_max,
2940 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002941 sv->counters.bytes_in, sv->counters.bytes_out,
2942 sv->counters.failed_secu,
2943 sv->counters.failed_conns, sv->counters.failed_resp,
2944 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002945
Willy Tarreau55bb8452007-10-17 18:44:57 +02002946 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002947 if (sv->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002948 chunk_appendf(&trash, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002949 }
2950 else if (svs != sv && svs->state & SRV_MAINTAIN) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002951 chunk_appendf(&trash, "MAINT(via),");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002952 }
2953 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002954 chunk_appendf(&trash,
Cyril Bontécd19e512010-01-31 22:34:03 +01002955 srv_hlt_st[sv_state],
2956 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2957 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2958 }
Willy Tarreau91861262007-10-17 17:06:05 +02002959
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002960 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002961 /* weight, active, backup */
2962 "%d,%d,%d,"
2963 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002964 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002965 (sv->state & SRV_BACKUP) ? 0 : 1,
2966 (sv->state & SRV_BACKUP) ? 1 : 0);
2967
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002968 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002969 if (sv->state & SRV_CHECKED)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002970 chunk_appendf(&trash,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002971 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002972 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002973 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002974 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002975 chunk_appendf(&trash,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002976 ",,,,");
2977
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002978 /* queue limit, pid, iid, sid, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002979 chunk_appendf(&trash,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002980 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002981 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002982 LIM2A0(sv->maxqueue, ""),
2983 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002984
2985 /* throttle */
2986 if ((sv->state & SRV_WARMINGUP) &&
2987 now.tv_sec < sv->last_change + sv->slowstart &&
2988 now.tv_sec >= sv->last_change) {
2989 unsigned int ratio;
2990 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002991 chunk_appendf(&trash, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002992 }
2993
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002994 /* sessions: lbtot */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002995 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002996
2997 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002998 if (sv->track)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002999 chunk_appendf(&trash, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02003000 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003001 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003002 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003003
Willy Tarreau7f062c42009-03-05 18:43:00 +01003004 /* type */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003005 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003006
3007 /* rate */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003008 chunk_appendf(&trash, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003009 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003010 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003011
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003012 if (sv->state & SRV_CHECKED) {
3013 /* check_status */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003014 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003015
3016 /* check_code */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003017 if (sv->check.status >= HCHK_STATUS_L57DATA)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003018 chunk_appendf(&trash, "%u,", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003019 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003020 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003021
3022 /* check_duration */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003023 if (sv->check.status >= HCHK_STATUS_CHECKED)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003024 chunk_appendf(&trash, "%lu,", sv->check.duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003025 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003026 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003027
3028 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003029 chunk_appendf(&trash, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003030 }
3031
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003032 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3033 if (px->mode == PR_MODE_HTTP) {
3034 int i;
3035
3036 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003037 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003038
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003039 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003040 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003041 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003042 }
3043
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003044 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003045 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003046
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003047 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003048 chunk_appendf(&trash, ",,,");
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003049
Willy Tarreauae526782010-03-04 20:34:23 +01003050 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003051 chunk_appendf(&trash, "%lld,%lld,",
Willy Tarreauae526782010-03-04 20:34:23 +01003052 sv->counters.cli_aborts, sv->counters.srv_aborts);
3053
Willy Tarreau7f062c42009-03-05 18:43:00 +01003054 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003055 chunk_appendf(&trash, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003056 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003057 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003058 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003059 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003060
Willy Tarreau295a8372011-03-10 11:25:07 +01003061 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003062 /* fall through */
3063
Willy Tarreau295a8372011-03-10 11:25:07 +01003064 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003065 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003066 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003067 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3068 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003069 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003070 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003071 /* Column sub-heading for Enable or Disable server */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003072 chunk_appendf(&trash, "<td></td>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003073 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003074 chunk_appendf(&trash, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003075
3076 if (uri->flags&ST_SHLGNDS) {
3077 /* balancing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003078 chunk_appendf(&trash, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003079 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003080
3081 /* cookie */
3082 if (px->cookie_name) {
3083 struct chunk src;
3084
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003085 chunk_appendf(&trash, ", cookie: '");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003086
3087 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003088 chunk_htmlencode(&trash, &src);
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003089
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003090 chunk_appendf(&trash, "'");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003091 }
3092
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003093 chunk_appendf(&trash, "\"");
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003094
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003095 }
3096
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003097 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003098 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003099 ">%s<a name=\"%s/Backend\"></a>"
3100 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003101 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003102 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003103 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003104 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003105 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003106 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003107 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003108 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003109 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3110 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003111
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003112 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003113 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003114 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003115 "<td"
3116 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003117 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003118
3119 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3120 if (px->mode == PR_MODE_HTTP) {
3121 int i;
3122
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003123 chunk_appendf(&trash, " title=\"rsp codes:");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003124
3125 for (i = 1; i < 6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003126 chunk_appendf(&trash, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003127
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003128 chunk_appendf(&trash, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003129 }
3130
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003131 chunk_appendf(&trash,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003132 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003133 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003134 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003135 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003136 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003137 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003138 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003139 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003140 U2H7(px->be_counters.cum_lbconn),
3141 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003142
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003143 chunk_appendf(&trash,
Willy Tarreau91861262007-10-17 17:06:05 +02003144 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003145 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003146 /* errors : request, connect */
3147 "<td></td><td>%s</td>"
3148 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003149 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003150 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003151 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003152 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003153 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003154 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003155 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003156 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3157 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003158 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003159 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3160 U2H2(px->be_counters.failed_conns),
3161 px->be_counters.cli_aborts,
3162 px->be_counters.srv_aborts,
3163 U2H5(px->be_counters.failed_resp),
3164 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003165 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003166 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3167 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003168 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003169 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003171 chunk_appendf(&trash,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003172 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003173 "<td class=ac>&nbsp;</td><td>%d</td>"
3174 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003175 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003176 "</tr>",
3177 px->down_trans,
3178 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003179 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003180 chunk_appendf(&trash,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003181 /* pxid, name */
3182 "%s,BACKEND,"
3183 /* queue : current, max */
3184 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003185 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003186 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003187 /* bytes : in, out */
3188 "%lld,%lld,"
3189 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003190 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003191 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003192 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003193 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003194 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003195 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003196 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003197 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003198 * active and backups. */
3199 "%s,"
3200 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003201 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003202 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003203 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003204 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003205 /* rate, rate_lim, rate_max, */
3206 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003207 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003208 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003209 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003210 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3211 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3212 px->be_counters.bytes_in, px->be_counters.bytes_out,
3213 px->be_counters.denied_req, px->be_counters.denied_resp,
3214 px->be_counters.failed_conns, px->be_counters.failed_resp,
3215 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003216 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003217 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003218 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003219 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003220 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003221 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003222 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003223 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003224 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003225
3226 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3227 if (px->mode == PR_MODE_HTTP) {
3228 int i;
3229
3230 for (i=1; i<6; i++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003231 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003232
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003233 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003234 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003235 chunk_appendf(&trash, ",,,,,,");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003236 }
3237
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003238 /* failed health analyses */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003239 chunk_appendf(&trash, ",");
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003240
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003241 /* requests : req_rate, req_rate_max, req_tot, */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003242 chunk_appendf(&trash, ",,,");
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003243
Willy Tarreauae526782010-03-04 20:34:23 +01003244 /* errors: cli_aborts, srv_aborts */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003245 chunk_appendf(&trash, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003246 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003247
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003248 /* finish with EOL */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003249 chunk_appendf(&trash, "\n");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003250
Willy Tarreau55bb8452007-10-17 18:44:57 +02003251 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003252 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003253 return 0;
3254 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003255
Willy Tarreau295a8372011-03-10 11:25:07 +01003256 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003257 /* fall through */
3258
Willy Tarreau295a8372011-03-10 11:25:07 +01003259 case STAT_PX_ST_END:
3260 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003261 chunk_appendf(&trash, "</table>");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003262
Willy Tarreau295a8372011-03-10 11:25:07 +01003263 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003264 /* close the form used to enable/disable this proxy servers */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003265 chunk_appendf(&trash,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003266 "Choose the action to perform on the checked servers : "
3267 "<select name=action>"
3268 "<option value=\"\"></option>"
3269 "<option value=\"disable\">Disable</option>"
3270 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003271 "<option value=\"stop\">Soft Stop</option>"
3272 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003273 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003274 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003275 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003276 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3277 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003278 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003279 }
3280
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003281 chunk_appendf(&trash, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003282
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003283 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003284 return 0;
3285 }
Willy Tarreau91861262007-10-17 17:06:05 +02003286
Willy Tarreau295a8372011-03-10 11:25:07 +01003287 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003288 /* fall through */
3289
Willy Tarreau295a8372011-03-10 11:25:07 +01003290 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003291 return 1;
3292
3293 default:
3294 /* unknown state, we should put an abort() here ! */
3295 return 1;
3296 }
3297}
3298
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003299/* This function dumps a complete session state onto the stream interface's
3300 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3301 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003302 * 0 if the output buffer is full and it needs to be called again, otherwise
3303 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003304 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003305static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003306{
3307 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003308 struct session *sess;
3309 extern const char *monthname[12];
3310 char pn[INET6_ADDRSTRLEN];
3311
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003312 chunk_reset(&trash);
Willy Tarreau295a8372011-03-10 11:25:07 +01003313 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003314
Willy Tarreau295a8372011-03-10 11:25:07 +01003315 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003316 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003317 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3318 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003319 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003320 si->applet.ctx.sess.target = NULL;
3321 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003322 return 1;
3323 }
3324
Willy Tarreau295a8372011-03-10 11:25:07 +01003325 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003326 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003327 si->applet.ctx.sess.uid = sess->uniq_id;
3328 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003329 /* fall through */
3330
3331 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003332 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003333 "%p: id=%u, proto=%s",
3334 sess,
3335 sess->uniq_id,
3336 sess->listener->proto->name);
3337
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003338 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003339 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003340 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003341 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003342 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003343 break;
3344 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003345 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003346 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003347 default:
3348 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003349 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003350 break;
3351 }
3352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003353 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003354 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003355 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003356
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003357 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003358 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003359 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3360 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3361 sess->listener ? sess->listener->luid : 0);
3362
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003363 conn_get_to_addr(sess->si[0].conn);
3364 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003365 case AF_INET:
3366 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003367 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003368 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003369 break;
3370 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003371 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003372 break;
3373 default:
3374 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003375 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003376 break;
3377 }
3378
Willy Tarreau50943332011-09-02 17:33:05 +02003379 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003380 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003381 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003382 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003383 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3384 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003385 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003386
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003387 conn_get_from_addr(sess->si[1].conn);
3388 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003389 case AF_INET:
3390 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003391 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003392 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003393 break;
3394 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003395 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003396 break;
3397 default:
3398 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003399 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003400 break;
3401 }
3402
3403 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003404 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003405 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003406 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3407 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3408 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003409 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003410
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003411 conn_get_to_addr(sess->si[1].conn);
3412 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003413 case AF_INET:
3414 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003415 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003416 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003417 break;
3418 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003419 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003420 break;
3421 default:
3422 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003423 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003424 break;
3425 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003426
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003427 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003428 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3429 sess->task,
3430 sess->task->state,
3431 sess->task->nice, sess->task->calls,
3432 sess->task->expire ?
3433 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3434 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3435 TICKS_TO_MS(1000)) : "<NEVER>",
3436 task_in_rq(sess->task) ? ", running" : "");
3437
3438 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003439 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003440 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3441 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3442 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3443 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3444
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003445 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003446 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3447 &sess->si[0],
3448 sess->si[0].state,
3449 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003450 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003451 sess->si[0].exp ?
3452 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3453 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3454 TICKS_TO_MS(1000)) : "<NEVER>",
3455 sess->si[0].err_type);
3456
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003457 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003458 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3459 &sess->si[1],
3460 sess->si[1].state,
3461 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003462 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003463 sess->si[1].exp ?
3464 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3465 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3466 TICKS_TO_MS(1000)) : "<NEVER>",
3467 sess->si[1].err_type);
3468
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003469 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003470 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3471 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3472 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3473
3474
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003475 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003476 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003477 " an_exp=%s",
3478 sess->req,
3479 sess->req->flags, sess->req->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003480 sess->req->buf->i, sess->req->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003481 sess->req->pipe ? sess->req->pipe->data : 0,
3482 sess->req->to_forward,
3483 sess->req->analyse_exp ?
3484 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3485 TICKS_TO_MS(1000)) : "<NEVER>");
3486
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003487 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003488 " rex=%s",
3489 sess->req->rex ?
3490 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3491 TICKS_TO_MS(1000)) : "<NEVER>");
3492
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003493 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003494 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003495 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003496 sess->req->wex ?
3497 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3498 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003499 sess->req->buf->data,
3500 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003501 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003502 sess->req->total);
3503
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003504 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003505 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003506 " an_exp=%s",
3507 sess->rep,
3508 sess->rep->flags, sess->rep->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003509 sess->rep->buf->i, sess->rep->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003510 sess->rep->pipe ? sess->rep->pipe->data : 0,
3511 sess->rep->to_forward,
3512 sess->rep->analyse_exp ?
3513 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3514 TICKS_TO_MS(1000)) : "<NEVER>");
3515
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003516 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003517 " rex=%s",
3518 sess->rep->rex ?
3519 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3520 TICKS_TO_MS(1000)) : "<NEVER>");
3521
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003522 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003523 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003524 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003525 sess->rep->wex ?
3526 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3527 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003528 sess->rep->buf->data,
3529 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003530 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003531 sess->rep->total);
3532
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003533 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003534 return 0;
3535
3536 /* use other states to dump the contents */
3537 }
3538 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003539 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003540 return 1;
3541}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003542
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003543/* This function dumps all sessions' states onto the stream interface's
3544 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003545 * properly set. It returns 0 if the output buffer is full and it needs
3546 * to be called again, otherwise non-zero. It is designed to be called
3547 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003548 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003549static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003550{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003551 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003552 /* If we're forced to shut down, we might have to remove our
3553 * reference to the last session being dumped.
3554 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003555 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3557 LIST_DEL(&si->applet.ctx.sess.bref.users);
3558 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003559 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003560 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003561 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003562 }
3563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003564 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003565
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003566 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003567 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003568 /* the function had not been called yet, let's prepare the
3569 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003570 * pointer to the first in the global list. When a target
3571 * session is being destroyed, it is responsible for updating
3572 * this pointer. We know we have reached the end when this
3573 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003574 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003575 LIST_INIT(&si->applet.ctx.sess.bref.users);
3576 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003577 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003578 /* fall through */
3579
Willy Tarreau295a8372011-03-10 11:25:07 +01003580 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003581 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003582 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3583 LIST_DEL(&si->applet.ctx.sess.bref.users);
3584 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003585 }
3586
3587 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003588 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003589 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003590 struct session *curr_sess;
3591
Willy Tarreau295a8372011-03-10 11:25:07 +01003592 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003593
Willy Tarreau295a8372011-03-10 11:25:07 +01003594 if (si->applet.ctx.sess.target) {
3595 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003596 goto next_sess;
3597
Willy Tarreau295a8372011-03-10 11:25:07 +01003598 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003599 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003600 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003601 return 0;
3602
3603 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003604 LIST_DEL(&si->applet.ctx.sess.bref.users);
3605 LIST_INIT(&si->applet.ctx.sess.bref.users);
3606 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003607 break;
3608 }
3609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003610 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003611 "%p: proto=%s",
3612 curr_sess,
3613 curr_sess->listener->proto->name);
3614
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003615
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003616 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003617 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003618 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003619 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003620 " src=%s:%d fe=%s be=%s srv=%s",
3621 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003622 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003623 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003624 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003625 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003626 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003627 break;
3628 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003629 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003630 " src=unix:%d fe=%s be=%s srv=%s",
3631 curr_sess->listener->luid,
3632 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003633 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003634 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003635 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003636 break;
3637 }
3638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003639 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003640 " ts=%02x age=%s calls=%d",
3641 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003642 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3643 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003645 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003646 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003647 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003648 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003649 curr_sess->req->analysers,
3650 curr_sess->req->rex ?
3651 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3652 TICKS_TO_MS(1000)) : "");
3653
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003654 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003655 ",wx=%s",
3656 curr_sess->req->wex ?
3657 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3658 TICKS_TO_MS(1000)) : "");
3659
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003660 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003661 ",ax=%s]",
3662 curr_sess->req->analyse_exp ?
3663 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3664 TICKS_TO_MS(1000)) : "");
3665
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003666 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003667 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003668 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003669 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003670 curr_sess->rep->analysers,
3671 curr_sess->rep->rex ?
3672 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3673 TICKS_TO_MS(1000)) : "");
3674
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003675 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003676 ",wx=%s",
3677 curr_sess->rep->wex ?
3678 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3679 TICKS_TO_MS(1000)) : "");
3680
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003681 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003682 ",ax=%s]",
3683 curr_sess->rep->analyse_exp ?
3684 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3685 TICKS_TO_MS(1000)) : "");
3686
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003687 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003688 " s0=[%d,%1xh,fd=%d,ex=%s]",
3689 curr_sess->si[0].state,
3690 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003691 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003692 curr_sess->si[0].exp ?
3693 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3694 TICKS_TO_MS(1000)) : "");
3695
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003696 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003697 " s1=[%d,%1xh,fd=%d,ex=%s]",
3698 curr_sess->si[1].state,
3699 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003700 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003701 curr_sess->si[1].exp ?
3702 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3703 TICKS_TO_MS(1000)) : "");
3704
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003705 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003706 " exp=%s",
3707 curr_sess->task->expire ?
3708 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3709 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003710 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003711 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003712
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003713 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003714
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003715 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003716 /* let's try again later from this session. We add ourselves into
3717 * this session's users so that it can remove us upon termination.
3718 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003719 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003720 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003721 }
3722
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003723 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003724 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003725 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003726
Willy Tarreau295a8372011-03-10 11:25:07 +01003727 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003728 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003729 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003730 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003731 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003732 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003733
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003734 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003735 return 0;
3736
Willy Tarreau295a8372011-03-10 11:25:07 +01003737 si->applet.ctx.sess.target = NULL;
3738 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003739 return 1;
3740 }
3741
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003742 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003743 /* fall through */
3744
3745 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003746 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003747 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003748 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003749}
3750
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003751/* This function dumps all tables' states onto the stream interface's
3752 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003753 * properly set. It returns 0 if the output buffer is full and it needs
3754 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003755 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003756static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003757{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003758 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003759 struct ebmb_node *eb;
3760 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003761 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003762
3763 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003764 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003765 * - STAT_ST_INIT : the first call
3766 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003767 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003768 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003769 * and the entry pointer points to the next entry to be dumped,
3770 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003771 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003772 * data though.
3773 */
3774
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003775 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003776 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003777 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003778 si->applet.ctx.table.entry->ref_cnt--;
3779 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003780 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003781 return 1;
3782 }
3783
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003784 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003785
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003786 while (si->conn->xprt_st != STAT_ST_FIN) {
3787 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003788 case STAT_ST_INIT:
3789 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3790 if (!si->applet.ctx.table.proxy)
3791 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003792
Willy Tarreau295a8372011-03-10 11:25:07 +01003793 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003794 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003795 break;
3796
Willy Tarreau295a8372011-03-10 11:25:07 +01003797 case STAT_ST_INFO:
3798 if (!si->applet.ctx.table.proxy ||
3799 (si->applet.ctx.table.target &&
3800 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003801 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003802 break;
3803 }
3804
Willy Tarreau295a8372011-03-10 11:25:07 +01003805 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003806 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09003807 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003808 return 0;
3809
Willy Tarreau295a8372011-03-10 11:25:07 +01003810 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02003811 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003812 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003813 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003814 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003815 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3816 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003817 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003818 break;
3819 }
3820 }
3821 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003822 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003823 break;
3824
Willy Tarreau295a8372011-03-10 11:25:07 +01003825 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003826 skip_entry = false;
3827
Willy Tarreau295a8372011-03-10 11:25:07 +01003828 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003829 /* we're filtering on some data contents */
3830 void *ptr;
3831 long long data;
3832
Willy Tarreau295a8372011-03-10 11:25:07 +01003833 dt = si->applet.ctx.table.data_type;
3834 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3835 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003836 dt);
3837
3838 data = 0;
3839 switch (stktable_data_types[dt].std_type) {
3840 case STD_T_SINT:
3841 data = stktable_data_cast(ptr, std_t_sint);
3842 break;
3843 case STD_T_UINT:
3844 data = stktable_data_cast(ptr, std_t_uint);
3845 break;
3846 case STD_T_ULL:
3847 data = stktable_data_cast(ptr, std_t_ull);
3848 break;
3849 case STD_T_FRQP:
3850 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003851 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003852 break;
3853 }
3854
3855 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003856 if ((data < si->applet.ctx.table.value &&
3857 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3858 si->applet.ctx.table.data_op == STD_OP_GT ||
3859 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3860 (data == si->applet.ctx.table.value &&
3861 (si->applet.ctx.table.data_op == STD_OP_NE ||
3862 si->applet.ctx.table.data_op == STD_OP_GT ||
3863 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3864 (data > si->applet.ctx.table.value &&
3865 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3866 si->applet.ctx.table.data_op == STD_OP_LT ||
3867 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003868 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003869 }
3870
Simon Hormanc88b8872011-06-15 15:18:49 +09003871 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003872 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003873 si->applet.ctx.table.entry))
3874 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003875
Willy Tarreau295a8372011-03-10 11:25:07 +01003876 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003877
Willy Tarreau295a8372011-03-10 11:25:07 +01003878 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003879 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003880 struct stksess *old = si->applet.ctx.table.entry;
3881 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003882 if (show)
3883 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3884 else
3885 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003886 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003887 break;
3888 }
3889
Simon Hormanc88b8872011-06-15 15:18:49 +09003890
3891 if (show)
3892 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3893 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3894 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3895
Willy Tarreau295a8372011-03-10 11:25:07 +01003896 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003897 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003898 break;
3899
Willy Tarreau295a8372011-03-10 11:25:07 +01003900 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003901 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003902 break;
3903 }
3904 }
3905 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003906}
3907
Willy Tarreaud426a182010-03-05 14:58:26 +01003908/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003909 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3910 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3911 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3912 * lines are respected within the limit of 70 output chars. Lines that are
3913 * continuation of a previous truncated line begin with "+" instead of " "
3914 * after the offset. The new pointer is returned.
3915 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003916static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3917 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003918{
3919 int end;
3920 unsigned char c;
3921
3922 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003923 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003924 return ptr;
3925
Willy Tarreau77804732012-10-29 16:14:26 +01003926 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003927
Willy Tarreaud426a182010-03-05 14:58:26 +01003928 while (ptr < len && ptr < bsize) {
3929 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003930 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003931 if (out->len > end - 2)
3932 break;
3933 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003934 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003935 if (out->len > end - 3)
3936 break;
3937 out->str[out->len++] = '\\';
3938 switch (c) {
3939 case '\t': c = 't'; break;
3940 case '\n': c = 'n'; break;
3941 case '\r': c = 'r'; break;
3942 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003943 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003944 }
3945 out->str[out->len++] = c;
3946 } else {
3947 if (out->len > end - 5)
3948 break;
3949 out->str[out->len++] = '\\';
3950 out->str[out->len++] = 'x';
3951 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3952 out->str[out->len++] = hextab[c & 0xF];
3953 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003954 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003955 /* we had a line break, let's return now */
3956 out->str[out->len++] = '\n';
3957 *line = ptr;
3958 return ptr;
3959 }
3960 }
3961 /* we have an incomplete line, we return it as-is */
3962 out->str[out->len++] = '\n';
3963 return ptr;
3964}
3965
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003966/* This function dumps all captured errors onto the stream interface's
3967 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003968 * properly set. It returns 0 if the output buffer is full and it needs
3969 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003970 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003971static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003972{
3973 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01003974
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003975 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003976 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003977
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003978 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003979
Willy Tarreau295a8372011-03-10 11:25:07 +01003980 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003981 /* the function had not been called yet, let's prepare the
3982 * buffer for a response.
3983 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003984 struct tm tm;
3985
3986 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003987 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01003988 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3989 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3990 error_snapshot_id);
3991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003992 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003993 /* Socket buffer full. Let's try again later from the same point */
3994 return 0;
3995 }
3996
Willy Tarreau295a8372011-03-10 11:25:07 +01003997 si->applet.ctx.errors.px = proxy;
3998 si->applet.ctx.errors.buf = 0;
3999 si->applet.ctx.errors.bol = 0;
4000 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004001 }
4002
4003 /* we have two inner loops here, one for the proxy, the other one for
4004 * the buffer.
4005 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004006 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004007 struct error_snapshot *es;
4008
Willy Tarreau295a8372011-03-10 11:25:07 +01004009 if (si->applet.ctx.errors.buf == 0)
4010 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004011 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004012 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004013
4014 if (!es->when.tv_sec)
4015 goto next;
4016
Willy Tarreau295a8372011-03-10 11:25:07 +01004017 if (si->applet.ctx.errors.iid >= 0 &&
4018 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4019 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004020 goto next;
4021
Willy Tarreau295a8372011-03-10 11:25:07 +01004022 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004023 /* just print headers now */
4024
4025 char pn[INET6_ADDRSTRLEN];
4026 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004027 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004028
4029 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004030 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004031 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004032 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004033
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004034 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4035 case AF_INET:
4036 case AF_INET6:
4037 port = get_host_port(&es->src);
4038 break;
4039 default:
4040 port = 0;
4041 }
4042
Willy Tarreau295a8372011-03-10 11:25:07 +01004043 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004044 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004045 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004046 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004047 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004048 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004049 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4050 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004051 break;
4052 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004053 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004054 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004055 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004056 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004057 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004058 break;
4059 }
4060
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004062 ", server %s (#%d), event #%u\n"
4063 " src %s:%d, session #%d, session flags 0x%08x\n"
4064 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4065 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4066 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4067 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4068 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4069 es->ev_id,
4070 pn, port, es->sid, es->s_flags,
4071 es->state, es->m_flags, es->t_flags,
4072 es->m_clen, es->m_blen,
4073 es->b_flags, es->b_out, es->b_tot,
4074 es->len, es->b_wrap, es->pos);
4075
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004076 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004077 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004078 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004079 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004080 si->applet.ctx.errors.ptr = 0;
4081 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004082 }
4083
Willy Tarreau295a8372011-03-10 11:25:07 +01004084 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004085 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004086 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004087 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004088 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004089 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004090 goto next;
4091 }
4092
4093 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004094 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004095 int newptr;
4096 int newline;
4097
Willy Tarreau295a8372011-03-10 11:25:07 +01004098 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004099 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 +01004100 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004101 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004102
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004103 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004104 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004105 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004106 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004107 si->applet.ctx.errors.ptr = newptr;
4108 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004109 };
4110 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004111 si->applet.ctx.errors.bol = 0;
4112 si->applet.ctx.errors.ptr = -1;
4113 si->applet.ctx.errors.buf++;
4114 if (si->applet.ctx.errors.buf > 1) {
4115 si->applet.ctx.errors.buf = 0;
4116 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004117 }
4118 }
4119
4120 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004121 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004122}
4123
Willy Tarreaud5781202012-09-22 19:32:35 +02004124/* parse the "level" argument on the bind lines */
4125static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4126{
4127 if (!*args[cur_arg + 1]) {
4128 memprintf(err, "'%s' : missing level", args[cur_arg]);
4129 return ERR_ALERT | ERR_FATAL;
4130 }
4131
4132 if (!strcmp(args[cur_arg+1], "user"))
4133 conf->level = ACCESS_LVL_USER;
4134 else if (!strcmp(args[cur_arg+1], "operator"))
4135 conf->level = ACCESS_LVL_OPER;
4136 else if (!strcmp(args[cur_arg+1], "admin"))
4137 conf->level = ACCESS_LVL_ADMIN;
4138 else {
4139 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4140 args[cur_arg], args[cur_arg+1]);
4141 return ERR_ALERT | ERR_FATAL;
4142 }
4143
4144 return 0;
4145}
4146
Willy Tarreaub24281b2011-02-13 13:16:36 +01004147struct si_applet http_stats_applet = {
4148 .name = "<STATS>", /* used for logging */
4149 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004150 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004151};
4152
Simon Horman9bd2c732011-06-15 15:18:44 +09004153static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004154 .name = "<CLI>", /* used for logging */
4155 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004156 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004157};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004158
Willy Tarreau10522fd2008-07-09 20:12:41 +02004159static struct cfg_kw_list cfg_kws = {{ },{
4160 { CFG_GLOBAL, "stats", stats_parse_global },
4161 { 0, NULL, NULL },
4162}};
4163
Willy Tarreaud5781202012-09-22 19:32:35 +02004164static struct bind_kw_list bind_kws = { "STAT", { }, {
4165 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4166 { NULL, NULL, 0 },
4167}};
4168
Willy Tarreau10522fd2008-07-09 20:12:41 +02004169__attribute__((constructor))
4170static void __dumpstats_module_init(void)
4171{
4172 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004173 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004174}
4175
Willy Tarreau91861262007-10-17 17:06:05 +02004176/*
4177 * Local variables:
4178 * c-indent-level: 8
4179 * c-basic-offset: 8
4180 * End:
4181 */