blob: d0f0345db5ff9c5f7165a68e250dcf2154e956ae [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 Tarreau3cefd522012-08-30 15:49:18 +0200128 copy_target(&s->target, &s->si[1].conn.target); // for logging only
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200129 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{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200342 return chunk_printf(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 Tarreauf7bc57c2012-10-03 00:19:48 +0200434 struct session *s = si->conn.xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900435
436 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
437 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)
Simon Hormand9366582011-06-15 15:18:45 +0900442 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
443
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
459 chunk_printf(msg, "%p:", entry);
460
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));
464 chunk_printf(msg, " key=%s", addr);
465 }
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));
469 chunk_printf(msg, " key=%s", addr);
470 }
471 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
472 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
473 }
474 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
475 chunk_printf(msg, " key=");
476 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
477 }
478 else {
479 chunk_printf(msg, " key=");
480 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
481 }
482
483 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
484
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)
491 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
492 else
493 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
494
495 ptr = stktable_data_ptr(&proxy->table, entry, dt);
496 switch (stktable_data_types[dt].std_type) {
497 case STD_T_SINT:
498 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
499 break;
500 case STD_T_UINT:
501 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
502 break;
503 case STD_T_ULL:
504 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
505 break;
506 case STD_T_FRQP:
507 chunk_printf(msg, "%d",
508 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
509 proxy->table.data_arg[dt].u));
510 break;
511 }
512 }
513 chunk_printf(msg, "\n");
514
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 Tarreauf7bc57c2012-10-03 00:19:48 +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 Tarreaudec98142012-06-06 23:37:08 +0200528 struct chunk msg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200529 long long value;
530 int data_type;
531 void *ptr;
532 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900533
Simon Hormand5b9fd92011-06-15 15:18:48 +0900534 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900535
536 if (!*args[4]) {
537 si->applet.ctx.cli.msg = "Key value expected\n";
538 si->applet.st0 = STAT_CLI_PRINT;
539 return;
540 }
541
Simon Hormanc5b89f62011-06-15 15:18:50 +0900542 switch (px->table.type) {
543 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900544 uint32_key = htonl(inetaddr_host(args[4]));
545 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900546 break;
547 case STKTABLE_TYPE_IPV6:
548 inet_pton(AF_INET6, args[4], ip6_key);
549 static_table_key.key = &ip6_key;
550 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900551 case STKTABLE_TYPE_INTEGER:
552 {
553 char *endptr;
554 unsigned long val;
555 errno = 0;
556 val = strtoul(args[4], &endptr, 10);
557 if ((errno == ERANGE && val == ULONG_MAX) ||
558 (errno != 0 && val == 0) || endptr == args[4] ||
559 val > 0xffffffff) {
560 si->applet.ctx.cli.msg = "Invalid key\n";
561 si->applet.st0 = STAT_CLI_PRINT;
562 return;
563 }
564 uint32_key = (uint32_t) val;
565 static_table_key.key = &uint32_key;
566 break;
567 }
568 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900569 case STKTABLE_TYPE_STRING:
570 static_table_key.key = args[4];
571 static_table_key.key_len = strlen(args[4]);
572 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900573 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200574 switch (action) {
575 case STAT_CLI_O_TAB:
576 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
577 break;
578 case STAT_CLI_O_CLR:
579 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
580 break;
581 default:
582 si->applet.ctx.cli.msg = "Unknown action\n";
583 break;
584 }
Simon Horman121f3052011-06-15 15:18:46 +0900585 si->applet.st0 = STAT_CLI_PRINT;
586 return;
587 }
588
589 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200590 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900591 si->applet.ctx.cli.msg = stats_permission_denied_msg;
592 si->applet.st0 = STAT_CLI_PRINT;
593 return;
594 }
595
596 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900597
Willy Tarreaudec98142012-06-06 23:37:08 +0200598 switch (action) {
599 case STAT_CLI_O_TAB:
600 if (!ts)
601 return;
David du Colombier7af46052012-05-16 14:16:48 +0200602 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900603 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
604 return;
605 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900606 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200607
608 case STAT_CLI_O_CLR:
609 if (!ts)
610 return;
611 if (ts->ref_cnt) {
612 /* don't delete an entry which is currently referenced */
613 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
614 si->applet.st0 = STAT_CLI_PRINT;
615 return;
616 }
617 stksess_kill(&px->table, ts);
618 break;
Simon Horman17bce342011-06-15 15:18:47 +0900619
Willy Tarreau654694e2012-06-07 01:03:16 +0200620 case STAT_CLI_O_SET:
621 if (strncmp(args[5], "data.", 5) != 0) {
622 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
623 si->applet.st0 = STAT_CLI_PRINT;
624 return;
625 }
626
627 data_type = stktable_get_data_type(args[5] + 5);
628 if (data_type < 0) {
629 si->applet.ctx.cli.msg = "Unknown data type\n";
630 si->applet.st0 = STAT_CLI_PRINT;
631 return;
632 }
633
634 if (!px->table.data_ofs[data_type]) {
635 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
636 si->applet.st0 = STAT_CLI_PRINT;
637 return;
638 }
639
640 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
641 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
642 si->applet.st0 = STAT_CLI_PRINT;
643 return;
644 }
645
646 if (ts)
647 stktable_touch(&px->table, ts, 1);
648 else {
649 ts = stksess_new(&px->table, &static_table_key);
650 if (!ts) {
651 /* don't delete an entry which is currently referenced */
652 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
653 si->applet.st0 = STAT_CLI_PRINT;
654 return;
655 }
656 stktable_store(&px->table, ts, 1);
657 }
658
659 ptr = stktable_data_ptr(&px->table, ts, data_type);
660 switch (stktable_data_types[data_type].std_type) {
661 case STD_T_SINT:
662 stktable_data_cast(ptr, std_t_sint) = value;
663 break;
664 case STD_T_UINT:
665 stktable_data_cast(ptr, std_t_uint) = value;
666 break;
667 case STD_T_ULL:
668 stktable_data_cast(ptr, std_t_ull) = value;
669 break;
670 case STD_T_FRQP:
671 /* We only reset the previous value so that it slowly fades out */
672 frqp = &stktable_data_cast(ptr, std_t_frqp);
673 frqp->curr_tick = now_ms;
674 frqp->prev_ctr = value;
675 frqp->curr_ctr = 0;
676 break;
677 }
678 break;
679
Willy Tarreaudec98142012-06-06 23:37:08 +0200680 default:
681 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900682 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200683 break;
Simon Horman121f3052011-06-15 15:18:46 +0900684 }
Simon Horman121f3052011-06-15 15:18:46 +0900685}
686
Willy Tarreau654694e2012-06-07 01:03:16 +0200687static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900688{
Willy Tarreau654694e2012-06-07 01:03:16 +0200689 if (action != STAT_CLI_O_TAB) {
690 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
691 si->applet.st0 = STAT_CLI_PRINT;
692 return;
693 }
694
Simon Hormand5b9fd92011-06-15 15:18:48 +0900695 /* condition on stored data value */
696 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
697 if (si->applet.ctx.table.data_type < 0) {
698 si->applet.ctx.cli.msg = "Unknown data type\n";
699 si->applet.st0 = STAT_CLI_PRINT;
700 return;
701 }
702
703 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
704 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
705 si->applet.st0 = STAT_CLI_PRINT;
706 return;
707 }
708
709 si->applet.ctx.table.data_op = get_std_op(args[4]);
710 if (si->applet.ctx.table.data_op < 0) {
711 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
712 si->applet.st0 = STAT_CLI_PRINT;
713 return;
714 }
715
716 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
717 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
718 si->applet.st0 = STAT_CLI_PRINT;
719 return;
720 }
721}
722
Willy Tarreaudec98142012-06-06 23:37:08 +0200723static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900724{
725 si->applet.ctx.table.data_type = -1;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200726 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200727 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900728 si->applet.ctx.table.proxy = NULL;
729 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200730 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900731
732 if (*args[2]) {
733 si->applet.ctx.table.target = find_stktable(args[2]);
734 if (!si->applet.ctx.table.target) {
735 si->applet.ctx.cli.msg = "No such table\n";
736 si->applet.st0 = STAT_CLI_PRINT;
737 return;
738 }
739 }
740 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200741 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900742 goto err_args;
743 return;
744 }
745
746 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200747 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900748 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200749 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900750 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900751 goto err_args;
752
753 return;
754
755err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200756 switch (action) {
757 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900758 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 +0200759 break;
760 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900761 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 +0200762 break;
763 default:
764 si->applet.ctx.cli.msg = "Unknown action\n";
765 break;
766 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900767 si->applet.st0 = STAT_CLI_PRINT;
768}
769
Willy Tarreau532a4502011-09-07 22:37:44 +0200770/* Expects to find a frontend named <arg> and returns it, otherwise displays various
771 * adequate error messages and returns NULL. This function also expects the session
772 * level to be admin.
773 */
774static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
775{
776 struct proxy *px;
777
Willy Tarreau290e63a2012-09-20 18:07:14 +0200778 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200779 si->applet.ctx.cli.msg = stats_permission_denied_msg;
780 si->applet.st0 = STAT_CLI_PRINT;
781 return NULL;
782 }
783
784 if (!*arg) {
785 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
786 si->applet.st0 = STAT_CLI_PRINT;
787 return NULL;
788 }
789
790 px = findproxy(arg, PR_CAP_FE);
791 if (!px) {
792 si->applet.ctx.cli.msg = "No such frontend.\n";
793 si->applet.st0 = STAT_CLI_PRINT;
794 return NULL;
795 }
796 return px;
797}
798
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200799/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
800 * and returns the pointer to the server. Otherwise, display adequate error messages
801 * and returns NULL. This function also expects the session level to be admin. Note:
802 * the <arg> is modified to remove the '/'.
803 */
804static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
805{
806 struct proxy *px;
807 struct server *sv;
808 char *line;
809
Willy Tarreau290e63a2012-09-20 18:07:14 +0200810 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200811 si->applet.ctx.cli.msg = stats_permission_denied_msg;
812 si->applet.st0 = STAT_CLI_PRINT;
813 return NULL;
814 }
815
816 /* split "backend/server" and make <line> point to server */
817 for (line = arg; *line; line++)
818 if (*line == '/') {
819 *line++ = '\0';
820 break;
821 }
822
823 if (!*line || !*arg) {
824 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
825 si->applet.st0 = STAT_CLI_PRINT;
826 return NULL;
827 }
828
829 if (!get_backend_server(arg, line, &px, &sv)) {
830 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
831 si->applet.st0 = STAT_CLI_PRINT;
832 return NULL;
833 }
834
835 if (px->state == PR_STSTOPPED) {
836 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
837 si->applet.st0 = STAT_CLI_PRINT;
838 return NULL;
839 }
840
841 return sv;
842}
843
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200844/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200845 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100846 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200847 * designating the function which will have to process the request, which can
848 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200849 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900850static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200851{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200852 struct session *s = si->conn.xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200853 char *args[MAX_STATS_ARGS + 1];
854 int arg;
855
856 while (isspace((unsigned char)*line))
857 line++;
858
859 arg = 0;
860 args[arg] = line;
861
862 while (*line && arg < MAX_STATS_ARGS) {
863 if (isspace((unsigned char)*line)) {
864 *line++ = '\0';
865
866 while (isspace((unsigned char)*line))
867 line++;
868
869 args[++arg] = line;
870 continue;
871 }
872
873 line++;
874 }
875
876 while (++arg <= MAX_STATS_ARGS)
877 args[arg] = line;
878
Willy Tarreau295a8372011-03-10 11:25:07 +0100879 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200880 if (strcmp(args[0], "show") == 0) {
881 if (strcmp(args[1], "stat") == 0) {
882 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100883 si->applet.ctx.stats.flags |= STAT_BOUND;
884 si->applet.ctx.stats.iid = atoi(args[2]);
885 si->applet.ctx.stats.type = atoi(args[3]);
886 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200887 }
888
Willy Tarreau295a8372011-03-10 11:25:07 +0100889 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
890 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200891 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100892 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200893 }
894 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100895 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
896 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200897 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100898 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200899 }
900 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200901 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200902 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100903 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100904 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200905 return 1;
906 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100907 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100908 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100909 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100910 si->applet.ctx.sess.target = NULL;
911 si->applet.ctx.sess.section = 0; /* start with session status */
912 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100913 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200914 }
915 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200916 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100917 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100918 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200919 return 1;
920 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200921 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100922 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100924 si->applet.ctx.errors.iid = -1;
925 si->applet.ctx.errors.px = NULL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200926 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100927 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200928 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200929 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200930 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200931 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700932 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200933 return 0;
934 }
935 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200936 else if (strcmp(args[0], "clear") == 0) {
937 if (strcmp(args[1], "counters") == 0) {
938 struct proxy *px;
939 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200940 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200941 int clrall = 0;
942
943 if (strcmp(args[2], "all") == 0)
944 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200945
Willy Tarreau6162db22009-10-10 17:13:00 +0200946 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200947 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
948 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200951 return 1;
952 }
953
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200954 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100955 if (clrall) {
956 memset(&px->be_counters, 0, sizeof(px->be_counters));
957 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
958 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200959 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100960 px->be_counters.conn_max = 0;
961 px->be_counters.p.http.rps_max = 0;
962 px->be_counters.sps_max = 0;
963 px->be_counters.cps_max = 0;
964 px->be_counters.nbpend_max = 0;
965
966 px->fe_counters.conn_max = 0;
967 px->fe_counters.p.http.rps_max = 0;
968 px->fe_counters.sps_max = 0;
969 px->fe_counters.cps_max = 0;
970 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200971 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200972
973 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200974 if (clrall)
975 memset(&sv->counters, 0, sizeof(sv->counters));
976 else {
977 sv->counters.cur_sess_max = 0;
978 sv->counters.nbpend_max = 0;
979 sv->counters.sps_max = 0;
980 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200981
Willy Tarreaua7944ad2012-09-26 21:03:11 +0200982 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200983 if (li->counters) {
984 if (clrall)
985 memset(li->counters, 0, sizeof(*li->counters));
986 else
987 li->counters->conn_max = 0;
988 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200989 }
990
Willy Tarreau81c25d02011-09-07 15:17:21 +0200991 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200992 return 1;
993 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200994 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200995 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200996 /* end of processing */
997 return 1;
998 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200999 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001000 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001001 return 0;
1002 }
1003 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001004 else if (strcmp(args[0], "get") == 0) {
1005 if (strcmp(args[1], "weight") == 0) {
1006 struct proxy *px;
1007 struct server *sv;
1008
1009 /* split "backend/server" and make <line> point to server */
1010 for (line = args[2]; *line; line++)
1011 if (*line == '/') {
1012 *line++ = '\0';
1013 break;
1014 }
1015
1016 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001017 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001018 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001019 return 1;
1020 }
1021
1022 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001023 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001024 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001025 return 1;
1026 }
1027
1028 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +02001029 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001030 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001031 return 1;
1032 }
1033 else { /* not "get weight" */
1034 return 0;
1035 }
1036 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001037 else if (strcmp(args[0], "set") == 0) {
1038 if (strcmp(args[1], "weight") == 0) {
1039 struct proxy *px;
1040 struct server *sv;
1041 int w;
1042
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001043 sv = expect_server_admin(s, si, args[2]);
1044 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001045 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001046 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001047
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001048 /* if the weight is terminated with '%', it is set relative to
1049 * the initial weight, otherwise it is absolute.
1050 */
1051 if (!*args[3]) {
1052 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001053 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001054 return 1;
1055 }
1056
Willy Tarreau4483d432009-10-10 19:30:08 +02001057 w = atoi(args[3]);
1058 if (strchr(args[3], '%') != NULL) {
1059 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001060 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001061 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001062 return 1;
1063 }
1064 w = sv->iweight * w / 100;
1065 }
1066 else {
1067 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001068 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001069 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001070 return 1;
1071 }
1072 }
1073
1074 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001075 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 +01001076 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001077 return 1;
1078 }
1079
1080 sv->uweight = w;
1081
1082 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1083 /* we must take care of not pushing the server to full throttle during slow starts */
1084 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1085 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1086 else
1087 sv->eweight = BE_WEIGHT_SCALE;
1088 sv->eweight *= sv->uweight;
1089 } else {
1090 sv->eweight = sv->uweight;
1091 }
1092
1093 /* static LB algorithms are a bit harder to update */
1094 if (px->lbprm.update_server_eweight)
1095 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001096 else if (sv->eweight) {
1097 if (px->lbprm.set_server_status_up)
1098 px->lbprm.set_server_status_up(sv);
1099 }
1100 else {
1101 if (px->lbprm.set_server_status_down)
1102 px->lbprm.set_server_status_down(sv);
1103 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001104
1105 return 1;
1106 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001107 else if (strcmp(args[1], "timeout") == 0) {
1108 if (strcmp(args[2], "cli") == 0) {
1109 unsigned timeout;
1110 const char *res;
1111
1112 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001113 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001114 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001115 return 1;
1116 }
1117
1118 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1119 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001120 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001121 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001122 return 1;
1123 }
1124
1125 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1126 return 1;
1127 }
1128 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001129 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001130 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001131 return 1;
1132 }
1133 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001134 else if (strcmp(args[1], "maxconn") == 0) {
1135 if (strcmp(args[2], "frontend") == 0) {
1136 struct proxy *px;
1137 struct listener *l;
1138 int v;
1139
Willy Tarreau532a4502011-09-07 22:37:44 +02001140 px = expect_frontend_admin(s, si, args[3]);
1141 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001142 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001143
1144 if (!*args[4]) {
1145 si->applet.ctx.cli.msg = "Integer value expected.\n";
1146 si->applet.st0 = STAT_CLI_PRINT;
1147 return 1;
1148 }
1149
1150 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001151 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001152 si->applet.ctx.cli.msg = "Value out of range.\n";
1153 si->applet.st0 = STAT_CLI_PRINT;
1154 return 1;
1155 }
1156
1157 /* OK, the value is fine, so we assign it to the proxy and to all of
1158 * its listeners. The blocked ones will be dequeued.
1159 */
1160 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001161 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001162 l->maxconn = v;
1163 if (l->state == LI_FULL)
1164 resume_listener(l);
1165 }
1166
1167 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1168 dequeue_all_listeners(&s->fe->listener_queue);
1169
1170 return 1;
1171 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001172 else if (strcmp(args[2], "global") == 0) {
1173 int v;
1174
Willy Tarreau290e63a2012-09-20 18:07:14 +02001175 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001176 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1177 si->applet.st0 = STAT_CLI_PRINT;
1178 return 1;
1179 }
1180
1181 if (!*args[3]) {
1182 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1183 si->applet.st0 = STAT_CLI_PRINT;
1184 return 1;
1185 }
1186
1187 v = atoi(args[3]);
1188 if (v > global.hardmaxconn) {
1189 si->applet.ctx.cli.msg = "Value out of range.\n";
1190 si->applet.st0 = STAT_CLI_PRINT;
1191 return 1;
1192 }
1193
1194 /* check for unlimited values */
1195 if (v <= 0)
1196 v = global.hardmaxconn;
1197
1198 global.maxconn = v;
1199
1200 /* Dequeues all of the listeners waiting for a resource */
1201 if (!LIST_ISEMPTY(&global_listener_queue))
1202 dequeue_all_listeners(&global_listener_queue);
1203
1204 return 1;
1205 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001206 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001207 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001208 si->applet.st0 = STAT_CLI_PRINT;
1209 return 1;
1210 }
1211 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001212 else if (strcmp(args[1], "rate-limit") == 0) {
1213 if (strcmp(args[2], "connections") == 0) {
1214 if (strcmp(args[3], "global") == 0) {
1215 int v;
1216
Willy Tarreau290e63a2012-09-20 18:07:14 +02001217 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001218 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1219 si->applet.st0 = STAT_CLI_PRINT;
1220 return 1;
1221 }
1222
1223 if (!*args[4]) {
1224 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1225 si->applet.st0 = STAT_CLI_PRINT;
1226 return 1;
1227 }
1228
1229 v = atoi(args[4]);
1230 if (v < 0) {
1231 si->applet.ctx.cli.msg = "Value out of range.\n";
1232 si->applet.st0 = STAT_CLI_PRINT;
1233 return 1;
1234 }
1235
1236 global.cps_lim = v;
1237
1238 /* Dequeues all of the listeners waiting for a resource */
1239 if (!LIST_ISEMPTY(&global_listener_queue))
1240 dequeue_all_listeners(&global_listener_queue);
1241
1242 return 1;
1243 }
1244 else {
1245 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1246 si->applet.st0 = STAT_CLI_PRINT;
1247 return 1;
1248 }
1249 }
1250 else {
1251 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1252 si->applet.st0 = STAT_CLI_PRINT;
1253 return 1;
1254 }
1255 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001256 else if (strcmp(args[1], "table") == 0) {
1257 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1258 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001259 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001260 return 0;
1261 }
1262 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001263 else if (strcmp(args[0], "enable") == 0) {
1264 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001265 struct server *sv;
1266
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001267 sv = expect_server_admin(s, si, args[2]);
1268 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001269 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001270
Cyril Bontécd19e512010-01-31 22:34:03 +01001271 if (sv->state & SRV_MAINTAIN) {
1272 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001273 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001274 /* If this server tracks the status of another one,
1275 * we must restore the good status.
1276 */
Willy Tarreau44267702011-10-28 15:35:33 +02001277 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001278 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001279 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001280 } else {
1281 sv->state &= ~SRV_MAINTAIN;
1282 set_server_down(sv);
1283 }
1284 } else {
1285 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001286 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001287 }
1288 }
1289
Willy Tarreau532a4502011-09-07 22:37:44 +02001290 return 1;
1291 }
1292 else if (strcmp(args[1], "frontend") == 0) {
1293 struct proxy *px;
1294
1295 px = expect_frontend_admin(s, si, args[2]);
1296 if (!px)
1297 return 1;
1298
1299 if (px->state == PR_STSTOPPED) {
1300 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1301 si->applet.st0 = STAT_CLI_PRINT;
1302 return 1;
1303 }
1304
1305 if (px->state != PR_STPAUSED) {
1306 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1307 si->applet.st0 = STAT_CLI_PRINT;
1308 return 1;
1309 }
1310
1311 if (!resume_proxy(px)) {
1312 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1313 si->applet.st0 = STAT_CLI_PRINT;
1314 return 1;
1315 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001316 return 1;
1317 }
1318 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001319 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1320 si->applet.st0 = STAT_CLI_PRINT;
1321 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001322 }
1323 }
1324 else if (strcmp(args[0], "disable") == 0) {
1325 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001326 struct server *sv;
1327
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001328 sv = expect_server_admin(s, si, args[2]);
1329 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001330 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001331
Cyril Bontécd19e512010-01-31 22:34:03 +01001332 if (! (sv->state & SRV_MAINTAIN)) {
1333 /* Not already in maintenance, we can change the server state */
1334 sv->state |= SRV_MAINTAIN;
1335 set_server_down(sv);
1336 }
1337
Willy Tarreau532a4502011-09-07 22:37:44 +02001338 return 1;
1339 }
1340 else if (strcmp(args[1], "frontend") == 0) {
1341 struct proxy *px;
1342
1343 px = expect_frontend_admin(s, si, args[2]);
1344 if (!px)
1345 return 1;
1346
1347 if (px->state == PR_STSTOPPED) {
1348 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1349 si->applet.st0 = STAT_CLI_PRINT;
1350 return 1;
1351 }
1352
1353 if (px->state == PR_STPAUSED) {
1354 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1355 si->applet.st0 = STAT_CLI_PRINT;
1356 return 1;
1357 }
1358
1359 if (!pause_proxy(px)) {
1360 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1361 si->applet.st0 = STAT_CLI_PRINT;
1362 return 1;
1363 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001364 return 1;
1365 }
1366 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001367 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1368 si->applet.st0 = STAT_CLI_PRINT;
1369 return 1;
1370 }
1371 }
1372 else if (strcmp(args[0], "shutdown") == 0) {
1373 if (strcmp(args[1], "frontend") == 0) {
1374 struct proxy *px;
1375
1376 px = expect_frontend_admin(s, si, args[2]);
1377 if (!px)
1378 return 1;
1379
1380 if (px->state == PR_STSTOPPED) {
1381 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1382 si->applet.st0 = STAT_CLI_PRINT;
1383 return 1;
1384 }
1385
1386 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1387 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1388 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1389 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1390 stop_proxy(px);
1391 return 1;
1392 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001393 else if (strcmp(args[1], "session") == 0) {
1394 struct session *sess, *ptr;
1395
Willy Tarreau290e63a2012-09-20 18:07:14 +02001396 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001397 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1398 si->applet.st0 = STAT_CLI_PRINT;
1399 return 1;
1400 }
1401
1402 if (!*args[2]) {
1403 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1404 si->applet.st0 = STAT_CLI_PRINT;
1405 return 1;
1406 }
1407
1408 ptr = (void *)strtoul(args[2], NULL, 0);
1409
1410 /* first, look for the requested session in the session table */
1411 list_for_each_entry(sess, &sessions, list) {
1412 if (sess == ptr)
1413 break;
1414 }
1415
1416 /* do we have the session ? */
1417 if (sess != ptr) {
1418 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1419 si->applet.st0 = STAT_CLI_PRINT;
1420 return 1;
1421 }
1422
1423 session_shutdown(sess, SN_ERR_KILLED);
1424 return 1;
1425 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001426 else if (strcmp(args[1], "sessions") == 0) {
1427 if (strcmp(args[2], "server") == 0) {
1428 struct server *sv;
1429 struct session *sess, *sess_bck;
1430
1431 sv = expect_server_admin(s, si, args[3]);
1432 if (!sv)
1433 return 1;
1434
1435 /* kill all the session that are on this server */
1436 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1437 if (sess->srv_conn == sv)
1438 session_shutdown(sess, SN_ERR_KILLED);
1439
1440 return 1;
1441 }
1442 else {
1443 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1444 si->applet.st0 = STAT_CLI_PRINT;
1445 return 1;
1446 }
1447 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001448 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001449 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001450 si->applet.st0 = STAT_CLI_PRINT;
1451 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001452 }
1453 }
1454 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001455 return 0;
1456 }
1457 return 1;
1458}
1459
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001460/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001461 * used to processes I/O from/to the stats unix socket. The system relies on a
1462 * state machine handling requests and various responses. We read a request,
1463 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001464 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1465 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001466 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001467 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001468static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001469{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001470 struct channel *req = si->ob;
1471 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001472 int reql;
1473 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001474
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001475 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1476 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001477
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001478 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001479 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001480 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001481 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001482 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001483 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001484 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001485 /* Let's close for real now. We just close the request
1486 * side, the conditions below will complete if needed.
1487 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001488 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001489 break;
1490 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001491 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001492 /* ensure we have some output room left in the event we
1493 * would want to return some info right after parsing.
1494 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001495 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001496 break;
1497
David du Colombier7af46052012-05-16 14:16:48 +02001498 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001499 if (reql <= 0) { /* closed or EOL not found */
1500 if (reql == 0)
1501 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001502 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001503 continue;
1504 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001505
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001506 /* seek for a possible semi-colon. If we find one, we
1507 * replace it with an LF and skip only this part.
1508 */
1509 for (len = 0; len < reql; len++)
1510 if (trash[len] == ';') {
1511 trash[len] = '\n';
1512 reql = len + 1;
1513 break;
1514 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001515
Willy Tarreau816fc222009-10-04 07:36:58 +02001516 /* now it is time to check that we have a full line,
1517 * remove the trailing \n and possibly \r, then cut the
1518 * line.
1519 */
1520 len = reql - 1;
1521 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001522 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001523 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001524 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525
Willy Tarreau816fc222009-10-04 07:36:58 +02001526 if (len && trash[len-1] == '\r')
1527 len--;
1528
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001529 trash[len] = '\0';
1530
Willy Tarreaubc4af052011-02-13 13:25:14 +01001531 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532 if (len) {
1533 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001534 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001535 continue;
1536 }
1537 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001538 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001539 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001540 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001541 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001542 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001543 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001544 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001545 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001546 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001547 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001548 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001549 /* if prompt is disabled, print help on empty lines,
1550 * so that the user at least knows how to enable
1551 * prompt and find help.
1552 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001553 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001554 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001555 }
1556
1557 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001558 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001559 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001560 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001561 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001562 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001564 continue;
1565 }
1566
Willy Tarreaubc4af052011-02-13 13:25:14 +01001567 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001568 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001569 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001570 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001571 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001572 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001573 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001574 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001575 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001576 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001577 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001578 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001579 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001580 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001581 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001582 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001583 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001584 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001585 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001586 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001587 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001588 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001589 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001590 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001591 break;
1592 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001593
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001594 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001595 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001596 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001597 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001598 }
1599
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001600 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001601 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001602 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001603
1604 /* Now we close the output if one of the writers did so,
1605 * or if we're not in interactive mode and the request
1606 * buffer is empty. This still allows pipelined requests
1607 * to be sent in non-interactive mode.
1608 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001609 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001610 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001611 continue;
1612 }
1613
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001614 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001615 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001616 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001617 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001618
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001619 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001620 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1621 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001622 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001623 * and nothing more to consume. This is comparable to a broken pipe, so
1624 * we forward the close to the request side so that it flows upstream to
1625 * the client.
1626 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001627 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001628 }
1629
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001630 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001631 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1632 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1633 /* We have no more processing to do, and nothing more to send, and
1634 * the client side has closed. So we'll forward this state downstream
1635 * on the response buffer.
1636 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001637 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001638 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001639 }
1640
1641 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001642 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001643
1644 /* we don't want to expire timeouts while we're processing requests */
1645 si->ib->rex = TICK_ETERNITY;
1646 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001647
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001648 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001649 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 +02001650 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001651 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 +02001652
1653 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1654 /* check that we have released everything then unregister */
1655 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001656 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001657}
1658
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001659/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001660 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001661 * It returns 0 as long as it does not complete, non-zero upon completion.
1662 * Some states are not used but it makes the code more similar to other
1663 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001664 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001665static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001666{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001667 struct proxy *px;
1668 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001669 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001670
David du Colombier7af46052012-05-16 14:16:48 +02001671 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001672
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001673 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001674 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001675 /* the function had not been called yet */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001676 si->conn.xprt_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001677 /* fall through */
1678
Willy Tarreau295a8372011-03-10 11:25:07 +01001679 case STAT_ST_HEAD:
1680 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001681 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001682 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001683 return 0;
1684 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001685
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001686 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001687 /* fall through */
1688
Willy Tarreau295a8372011-03-10 11:25:07 +01001689 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001690 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001691 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001692 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001693 "Name: " PRODUCT_NAME "\n"
1694 "Version: " HAPROXY_VERSION "\n"
1695 "Release_date: " HAPROXY_DATE "\n"
1696 "Nbproc: %d\n"
1697 "Process_num: %d\n"
1698 "Pid: %d\n"
1699 "Uptime: %dd %dh%02dm%02ds\n"
1700 "Uptime_sec: %d\n"
1701 "Memmax_MB: %d\n"
1702 "Ulimit-n: %d\n"
1703 "Maxsock: %d\n"
1704 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001705 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001706 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001707 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001708 "PipesUsed: %d\n"
1709 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001710 "ConnRate: %d\n"
1711 "ConnRateLimit: %d\n"
1712 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001713 "Tasks: %d\n"
1714 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001715 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001716 "node: %s\n"
1717 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001718 "",
1719 global.nbproc,
1720 relative_pid,
1721 pid,
1722 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1723 up,
1724 global.rlimit_memmax,
1725 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001726 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001727 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001728 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001729 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001730 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001731 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001732 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001733 return 0;
1734 }
1735
Willy Tarreau295a8372011-03-10 11:25:07 +01001736 si->applet.ctx.stats.px = proxy;
1737 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1738 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001739 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001740 /* fall through */
1741
Willy Tarreau295a8372011-03-10 11:25:07 +01001742 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001743 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001744 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1745 while (si->applet.ctx.stats.px) {
1746 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02001747 /* skip the disabled proxies, global frontend and non-networked ones */
1748 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001749 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001750 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001751 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001752 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001753
Willy Tarreau295a8372011-03-10 11:25:07 +01001754 si->applet.ctx.stats.px = px->next;
1755 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001756 }
1757 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001758 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001759
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001760 si->conn.xprt_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001761 /* fall through */
1762
Willy Tarreau295a8372011-03-10 11:25:07 +01001763 case STAT_ST_END:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001764 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001765 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001766
Willy Tarreau295a8372011-03-10 11:25:07 +01001767 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001768 return 1;
1769
1770 default:
1771 /* unknown state ! */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001772 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001773 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001774 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001775}
1776
1777
Cyril Bonté70be45d2010-10-12 00:14:35 +02001778/* We don't want to land on the posted stats page because a refresh will
1779 * repost the data. We don't want this to happen on accident so we redirect
1780 * the browse to the stats page with a GET.
1781 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001782static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001783{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001784 struct session *s = si->conn.xprt_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001785 struct chunk msg;
1786
David du Colombier7af46052012-05-16 14:16:48 +02001787 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001788
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001789 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001790 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001791 chunk_printf(&msg,
1792 "HTTP/1.0 303 See Other\r\n"
1793 "Cache-Control: no-cache\r\n"
1794 "Content-Type: text/plain\r\n"
1795 "Connection: close\r\n"
1796 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001797 uri->uri_prefix,
1798 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1799 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1800 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1801 stat_status_codes[si->applet.ctx.stats.st_code] :
1802 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001803 chunk_printf(&msg, "\r\n\r\n");
1804
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001805 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001806 return 0;
1807
1808 s->txn.status = 303;
1809
1810 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1811 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1812 if (!(s->flags & SN_FINST_MASK))
1813 s->flags |= SN_FINST_R;
1814
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001815 si->conn.xprt_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001816 return 1;
1817 }
1818 return 1;
1819}
1820
1821
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001822/* This I/O handler runs as an applet embedded in a stream interface. It is
1823 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001824 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001825 * automatically unregisters itself once transfer is complete.
1826 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001827static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001828{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001829 struct session *s = si->conn.xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001830 struct channel *req = si->ob;
1831 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001832
1833 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1834 goto out;
1835
1836 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001837 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001838 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001839
Willy Tarreaubc4af052011-02-13 13:25:14 +01001840 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001841 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001842 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001843 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001844 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001845 }
1846 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001847 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001848 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001849 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001850 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001851 }
1852 }
1853
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001854 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001855 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001856
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001857 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001858 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001859 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001860 }
1861
1862 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001863 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001864
1865 /* we don't want to expire timeouts while we're processing requests */
1866 si->ib->rex = TICK_ETERNITY;
1867 si->ob->wex = TICK_ETERNITY;
1868
1869 out:
1870 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1871 /* check that we have released everything then unregister */
1872 stream_int_unregister_handler(si);
1873 }
1874}
1875
1876
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001877/* This function dumps statistics in HTTP format onto the stream interface's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001878 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001879 * properly set. It returns 0 if it had to stop writing data and an I/O is
1880 * needed, 1 if the dump is finished and the session must be closed, or -1
1881 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001882 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001883static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001884{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001885 struct session *s = si->conn.xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001886 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001887 struct proxy *px;
1888 struct chunk msg;
1889 unsigned int up;
1890
David du Colombier7af46052012-05-16 14:16:48 +02001891 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001892
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001893 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001894 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001895 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001896 "HTTP/1.0 200 OK\r\n"
1897 "Cache-Control: no-cache\r\n"
1898 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001899 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001900 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001901
Willy Tarreau295a8372011-03-10 11:25:07 +01001902 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001903 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001904 uri->refresh);
1905
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001906 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001907
1908 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001909 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001910 return 0;
1911
Willy Tarreau91861262007-10-17 17:06:05 +02001912 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1913 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1914 if (!(s->flags & SN_FINST_MASK))
1915 s->flags |= SN_FINST_R;
1916
1917 if (s->txn.meth == HTTP_METH_HEAD) {
1918 /* that's all we return in case of HEAD request */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001919 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001920 return 1;
1921 }
1922
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001923 si->conn.xprt_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001924 /* fall through */
1925
Willy Tarreau295a8372011-03-10 11:25:07 +01001926 case STAT_ST_HEAD:
1927 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001928 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001929 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001930 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1931 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001932 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001933 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1934 "<style type=\"text/css\"><!--\n"
1935 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001936 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001937 " font-size: 12px;"
1938 " font-weight: normal;"
1939 " color: black;"
1940 " background: white;"
1941 "}\n"
1942 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001943 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001944 "}\n"
1945 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001946 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001947 " margin-bottom: 0.5em;"
1948 "}\n"
1949 "h2 {"
1950 " font-family: helvetica, arial;"
1951 " font-size: x-large;"
1952 " font-weight: bold;"
1953 " font-style: italic;"
1954 " color: #6020a0;"
1955 " margin-top: 0em;"
1956 " margin-bottom: 0em;"
1957 "}\n"
1958 "h3 {"
1959 " font-family: helvetica, arial;"
1960 " font-size: 16px;"
1961 " font-weight: bold;"
1962 " color: #b00040;"
1963 " background: #e8e8d0;"
1964 " margin-top: 0em;"
1965 " margin-bottom: 0em;"
1966 "}\n"
1967 "li {"
1968 " margin-top: 0.25em;"
1969 " margin-right: 2em;"
1970 "}\n"
1971 ".hr {margin-top: 0.25em;"
1972 " border-color: black;"
1973 " border-bottom-style: solid;"
1974 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001975 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001976 ".total {background: #20D0D0;color: #ffff80;}\n"
1977 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001978 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001979 ".backend {background: #e8e8d0;}\n"
1980 ".active0 {background: #ff9090;}\n"
1981 ".active1 {background: #ffd020;}\n"
1982 ".active2 {background: #ffffa0;}\n"
1983 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001984 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1985 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1986 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001987 ".backup0 {background: #ff9090;}\n"
1988 ".backup1 {background: #ff80ff;}\n"
1989 ".backup2 {background: #c060ff;}\n"
1990 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001991 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1992 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1993 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001994 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001995 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001996 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001997 "a.px:link {color: #ffff40; text-decoration: none;}"
1998 "a.px:visited {color: #ffff40; text-decoration: none;}"
1999 "a.px:hover {color: #ffffff; text-decoration: none;}"
2000 "a.lfsb:link {color: #000000; text-decoration: none;}"
2001 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2002 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2003 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002004 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002005 "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"
2006 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002007 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002008 "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 +02002009 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002010 "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 +02002011 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002012 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2013 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2014 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01002015 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002016 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002017 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002018 (uri->flags&ST_SHNODE) ? " on " : "",
2019 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02002020 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02002021 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002022 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002023 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002024 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002025 return 0;
2026
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002027 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02002028 /* fall through */
2029
Willy Tarreau295a8372011-03-10 11:25:07 +01002030 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002031 up = (now.tv_sec - start_date.tv_sec);
2032
2033 /* WARNING! this has to fit the first packet too.
2034 * We are around 3.5 kB, add adding entries will
2035 * become tricky if we want to support 4kB buffers !
2036 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002037 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002038 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002039 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2040 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002041 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002042 "<hr width=\"100%%\" class=\"hr\">\n"
2043 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002044 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002045 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002046 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002047 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2048 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002049 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002050 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002051 "</td><td align=\"center\" nowrap>\n"
2052 "<table class=\"lgd\"><tr>\n"
2053 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2054 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2055 "</tr><tr>\n"
2056 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2057 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2058 "</tr><tr>\n"
2059 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2060 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2061 "</tr><tr>\n"
2062 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002063 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002064 "</tr><tr>\n"
2065 "<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 +02002066 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002067 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002068 "</td>"
2069 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2070 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2071 "",
2072 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002073 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2074 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2075 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002076 up / 86400, (up % 86400) / 3600,
2077 (up % 3600) / 60, (up % 60),
2078 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2079 global.rlimit_memmax ? " MB" : "",
2080 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002081 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002082 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002083 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002084 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002085
Willy Tarreau295a8372011-03-10 11:25:07 +01002086 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002087 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002088 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2089 uri->uri_prefix,
2090 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002091 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002092 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002093 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002094 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2095 uri->uri_prefix,
2096 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002097 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002098
Willy Tarreau55bb8452007-10-17 18:44:57 +02002099 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002101 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002102 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2103 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002104 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002105 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002106 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002107 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002108 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2109 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002110 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002111 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002112 }
Willy Tarreau91861262007-10-17 17:06:05 +02002113
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002114 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002115 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2116 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002117 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2118 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002119
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002120 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002121 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2122 uri->uri_prefix,
2123 (uri->refresh > 0) ? ";norefresh" : "");
2124
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002125 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002126 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002127 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002128 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002129 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2130 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2131 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2132 "</ul>"
2133 "</td>"
2134 "</tr></table>\n"
2135 ""
2136 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002137
Willy Tarreau295a8372011-03-10 11:25:07 +01002138 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002139 switch (si->applet.ctx.stats.st_code) {
2140 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002141 chunk_printf(&msg,
2142 "<p><div class=active3>"
2143 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2144 "Action processed successfully."
2145 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002146 break;
2147 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002148 chunk_printf(&msg,
2149 "<p><div class=active2>"
2150 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2151 "Nothing has changed."
2152 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002153 break;
2154 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002155 chunk_printf(&msg,
2156 "<p><div class=active2>"
2157 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2158 "Action partially processed.<br>"
2159 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2160 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002161 break;
2162 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002163 chunk_printf(&msg,
2164 "<p><div class=active0>"
2165 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2166 "Action not processed because of invalid parameters."
2167 "<ul>"
2168 "<li>The action is maybe unknown.</li>"
2169 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2170 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2171 "</ul>"
2172 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002173 break;
2174 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002175 chunk_printf(&msg,
2176 "<p><div class=active0>"
2177 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2178 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2179 "You should retry with less servers at a time.</b>"
2180 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002181 break;
2182 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002183 chunk_printf(&msg,
2184 "<p><div class=active0>"
2185 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2186 "<b>Action denied.</b>"
2187 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002188 break;
2189 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002190 chunk_printf(&msg,
2191 "<p><div class=active6>"
2192 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2193 "Unexpected result."
2194 "</div>\n", uri->uri_prefix);
2195 }
2196 chunk_printf(&msg,"<p>\n");
2197 }
2198
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002199 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002200 return 0;
2201 }
Willy Tarreau91861262007-10-17 17:06:05 +02002202
Willy Tarreau295a8372011-03-10 11:25:07 +01002203 si->applet.ctx.stats.px = proxy;
2204 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002205 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002206 /* fall through */
2207
Willy Tarreau295a8372011-03-10 11:25:07 +01002208 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002209 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002210 while (si->applet.ctx.stats.px) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002211 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002212 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002213 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02002214 /* skip the disabled proxies, global frontend and non-networked ones */
2215 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002216 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002217 return 0;
2218
Willy Tarreau295a8372011-03-10 11:25:07 +01002219 si->applet.ctx.stats.px = px->next;
2220 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002221 }
2222 /* here, we just have reached the last proxy */
2223
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002224 si->conn.xprt_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002225 /* fall through */
2226
Willy Tarreau295a8372011-03-10 11:25:07 +01002227 case STAT_ST_END:
2228 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002229 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002230 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002231 return 0;
2232 }
Willy Tarreau91861262007-10-17 17:06:05 +02002233
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002234 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002235 /* fall through */
2236
Willy Tarreau295a8372011-03-10 11:25:07 +01002237 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002238 return 1;
2239
2240 default:
2241 /* unknown state ! */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002242 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002243 return -1;
2244 }
2245}
2246
2247
2248/*
2249 * Dumps statistics for a proxy.
2250 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2251 * ot non-zero if everything completed.
2252 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002253static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002254{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002255 struct session *s = si->conn.xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002256 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002257 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002258 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002259 struct chunk msg;
2260
David du Colombier7af46052012-05-16 14:16:48 +02002261 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002262
Willy Tarreau295a8372011-03-10 11:25:07 +01002263 switch (si->applet.ctx.stats.px_st) {
2264 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002265 /* we are on a new proxy */
2266
2267 if (uri && uri->scope) {
2268 /* we have a limited scope, we have to check the proxy name */
2269 struct stat_scope *scope;
2270 int len;
2271
2272 len = strlen(px->id);
2273 scope = uri->scope;
2274
2275 while (scope) {
2276 /* match exact proxy name */
2277 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2278 break;
2279
2280 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002281 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002282 break;
2283 scope = scope->next;
2284 }
2285
2286 /* proxy name not found : don't dump anything */
2287 if (scope == NULL)
2288 return 1;
2289 }
2290
Willy Tarreau295a8372011-03-10 11:25:07 +01002291 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2292 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002293 return 1;
2294
Willy Tarreau295a8372011-03-10 11:25:07 +01002295 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002296 /* fall through */
2297
Willy Tarreau295a8372011-03-10 11:25:07 +01002298 case STAT_PX_ST_TH:
2299 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2300 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002301 /* A form to enable/disable this proxy servers */
2302 chunk_printf(&msg,
2303 "<form action=\"%s\" method=\"post\">",
2304 uri->uri_prefix);
2305 }
2306
Willy Tarreau55bb8452007-10-17 18:44:57 +02002307 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002308 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002309 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002310 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002311 "<th class=\"pxname\" width=\"10%%\"");
2312
2313 if (uri->flags&ST_SHLGNDS) {
2314 /* cap, mode, id */
2315 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2316 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2317 px->uuid);
2318
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002319 chunk_printf(&msg, "\"");
2320 }
2321
2322 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002323 ">%s<a name=\"%s\"></a>"
2324 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002325 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002326 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002327 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002328 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002329 "<tr class=\"titre\">",
2330 (uri->flags & ST_SHLGNDS)?"<u>":"",
2331 px->id, px->id, px->id,
2332 (uri->flags & ST_SHLGNDS)?"</u>":"",
2333 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2334
Willy Tarreau295a8372011-03-10 11:25:07 +01002335 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002336 /* Column heading for Enable or Disable server */
2337 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2338 }
2339
2340 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002341 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002342 "<th colspan=3>Queue</th>"
2343 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002344 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002345 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002346 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002347 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002348 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002349 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002350 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002351 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002352 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002353 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002354 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002355 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2356 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002357 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002358
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002359 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002360 return 0;
2361 }
Willy Tarreau91861262007-10-17 17:06:05 +02002362
Willy Tarreau295a8372011-03-10 11:25:07 +01002363 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002364 /* fall through */
2365
Willy Tarreau295a8372011-03-10 11:25:07 +01002366 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002367 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002368 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002369 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2370 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002371 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002372 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002373 "<tr class=\"frontend\">");
2374
Willy Tarreau295a8372011-03-10 11:25:07 +01002375 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002376 /* Column sub-heading for Enable or Disable server */
2377 chunk_printf(&msg, "<td></td>");
2378 }
2379
2380 chunk_printf(&msg,
2381 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002382 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002383 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2384 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002385 "",
2386 px->id, px->id);
2387
2388 if (px->mode == PR_MODE_HTTP) {
2389 chunk_printf(&msg,
2390 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002391 "<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 +01002392 "",
2393 read_freq_ctr(&px->fe_req_per_sec),
2394 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002395 px->fe_counters.p.http.rps_max,
2396 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002397 LIM2A2(px->fe_sps_lim, "-"));
2398 } else {
2399 chunk_printf(&msg,
2400 /* sessions rate : current, max, limit */
2401 "<td>%s</td><td>%s</td><td>%s</td>"
2402 "",
2403 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002404 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002405 }
2406
2407 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002408 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002409 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002410 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002411 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002412 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002413
2414 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2415 if (px->mode == PR_MODE_HTTP) {
2416 int i;
2417
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002418 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002419
2420 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002421 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002422
Willy Tarreaueabea072011-09-10 23:29:44 +02002423 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2424 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002425 }
2426
2427 chunk_printf(&msg,
2428 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002429 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002430 /* bytes : in, out */
2431 "<td>%s</td><td>%s</td>"
2432 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002433 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002434 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002435 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002436 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002437
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002438 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002439 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002440 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002441 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002442 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002443 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002444 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002445 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002446 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002447 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002448 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002449 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002450 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2451 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002452 px->state == PR_STREADY ? "OPEN" :
2453 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002454 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002455 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002456 /* pxid, name, queue cur, queue max, */
2457 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002458 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002459 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002460 /* bytes : in, out */
2461 "%lld,%lld,"
2462 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002463 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002464 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002465 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002466 /* warnings: retries, redispatches */
2467 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002468 /* server status : reflect frontend status */
2469 "%s,"
2470 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002471 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002472 /* pid, iid, sid, throttle, lbtot, tracked, type */
2473 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002474 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002475 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002476 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002477 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002478 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002479 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2480 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2481 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2482 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002483 px->state == PR_STREADY ? "OPEN" :
2484 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002485 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002486 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002487 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002488
2489 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2490 if (px->mode == PR_MODE_HTTP) {
2491 int i;
2492
2493 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002494 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002495
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002496 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002497 } else {
2498 chunk_printf(&msg, ",,,,,,");
2499 }
2500
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002501 /* failed health analyses */
2502 chunk_printf(&msg, ",");
2503
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002504 /* requests : req_rate, req_rate_max, req_tot, */
2505 chunk_printf(&msg, "%u,%u,%lld,",
2506 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002507 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002508
Willy Tarreauae526782010-03-04 20:34:23 +01002509 /* errors: cli_aborts, srv_aborts */
2510 chunk_printf(&msg, ",,");
2511
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002512 /* finish with EOL */
2513 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002514 }
Willy Tarreau91861262007-10-17 17:06:05 +02002515
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002516 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002517 return 0;
2518 }
2519
Willy Tarreau4348fad2012-09-20 16:48:07 +02002520 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002521 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002522 /* fall through */
2523
Willy Tarreau295a8372011-03-10 11:25:07 +01002524 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002525 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002526 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 +02002527 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002528 return 0;
2529
Willy Tarreau4348fad2012-09-20 16:48:07 +02002530 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002531 if (!l->counters)
2532 continue;
2533
Willy Tarreau295a8372011-03-10 11:25:07 +01002534 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2535 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002536 break;
2537
Willy Tarreau295a8372011-03-10 11:25:07 +01002538 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002539 continue;
2540 }
2541
Willy Tarreau295a8372011-03-10 11:25:07 +01002542 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002543 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002544 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002545 /* Column sub-heading for Enable or Disable server */
2546 chunk_printf(&msg, "<td></td>");
2547 }
2548 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002549
2550 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002551 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002552 int port;
2553
Willy Tarreau631f01c2011-09-05 00:36:48 +02002554 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002555
Willy Tarreau631f01c2011-09-05 00:36:48 +02002556 port = get_host_port(&l->addr);
2557 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2558 case AF_INET:
2559 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2560 break;
2561 case AF_INET6:
2562 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2563 break;
2564 case AF_UNIX:
2565 chunk_printf(&msg, "unix, ");
2566 break;
2567 case -1:
2568 chunk_printf(&msg, "(%s), ", strerror(errno));
2569 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002570 }
2571
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002572 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002573 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002574 }
2575
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002576 chunk_printf(&msg,
2577 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002578 ">%s<a name=\"%s/+%s\"></a>"
2579 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002580 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002581 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002582 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002583 "<td>%s</td><td>%s</td><td>%s</td>"
2584 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002586 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002587 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002588 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002589 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002590 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002591 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2592 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2593
2594 chunk_printf(&msg,
2595 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002596 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002597 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002598 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002599 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002600 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002601 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002602 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002603 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002604 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002605 "",
2606 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2607 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002608 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002609 } else {
2610 chunk_printf(&msg,
2611 /* pxid, name, queue cur, queue max, */
2612 "%s,%s,,,"
2613 /* sessions: current, max, limit, total */
2614 "%d,%d,%d,%lld,"
2615 /* bytes: in, out */
2616 "%lld,%lld,"
2617 /* denied: req, resp */
2618 "%lld,%lld,"
2619 /* errors: request, connect, response */
2620 "%lld,,,"
2621 /* warnings: retries, redispatches */
2622 ",,"
2623 /* server status: reflect listener status */
2624 "%s,"
2625 /* rest of server: nothing */
2626 ",,,,,,,,"
2627 /* pid, iid, sid, throttle, lbtot, tracked, type */
2628 "%d,%d,%d,,,,%d,"
2629 /* rate, rate_lim, rate_max */
2630 ",,,"
2631 /* check_status, check_code, check_duration */
2632 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002633 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2634 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002635 /* failed health analyses */
2636 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002637 /* requests : req_rate, req_rate_max, req_tot, */
2638 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002639 /* errors: cli_aborts, srv_aborts */
2640 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 "\n",
2642 px->id, l->name,
2643 l->nbconn, l->counters->conn_max,
2644 l->maxconn, l->counters->cum_conn,
2645 l->counters->bytes_in, l->counters->bytes_out,
2646 l->counters->denied_req, l->counters->denied_resp,
2647 l->counters->failed_req,
2648 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2649 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2650 }
2651
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002652 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002653 return 0;
2654 }
2655
Willy Tarreau295a8372011-03-10 11:25:07 +01002656 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2657 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002658 /* fall through */
2659
Willy Tarreau295a8372011-03-10 11:25:07 +01002660 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002661 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002662 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002663 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 +02002664
Willy Tarreau9b28e032012-10-12 23:49:43 +02002665 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002666 return 0;
2667
Willy Tarreau295a8372011-03-10 11:25:07 +01002668 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002669
Willy Tarreau295a8372011-03-10 11:25:07 +01002670 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2671 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002672 break;
2673
Willy Tarreau295a8372011-03-10 11:25:07 +01002674 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002675 continue;
2676 }
2677
Willy Tarreau44267702011-10-28 15:35:33 +02002678 if (sv->track)
2679 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002680 else
2681 svs = sv;
2682
Willy Tarreau91861262007-10-17 17:06:05 +02002683 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002684 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002685 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002686 else if (svs->state & SRV_RUNNING) {
2687 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002688 sv_state = 3; /* UP */
2689 else
2690 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002691
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002692 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002693 sv_state += 2;
2694 }
Willy Tarreau91861262007-10-17 17:06:05 +02002695 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002696 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002697 sv_state = 1; /* going up */
2698 else
2699 sv_state = 0; /* DOWN */
2700
Willy Tarreau295a8372011-03-10 11:25:07 +01002701 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002702 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002703 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002704 continue;
2705 }
2706
Willy Tarreau295a8372011-03-10 11:25:07 +01002707 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002708 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2709 "UP %d/%d &darr;", "UP",
2710 "NOLB %d/%d &darr;", "NOLB",
2711 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002712 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002713 chunk_printf(&msg,
2714 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002715 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002716 );
2717 }
2718 else {
2719 chunk_printf(&msg,
2720 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002722 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2723 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002724
Willy Tarreau295a8372011-03-10 11:25:07 +01002725 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002726 chunk_printf(&msg,
2727 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2728 sv->id);
2729 }
2730
2731 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002733 if (uri->flags&ST_SHLGNDS) {
2734 char str[INET6_ADDRSTRLEN];
2735
Willy Tarreau631f01c2011-09-05 00:36:48 +02002736 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002737
Willy Tarreau631f01c2011-09-05 00:36:48 +02002738 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002739 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002740 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002741 break;
2742 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002743 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2744 break;
2745 case AF_UNIX:
2746 chunk_printf(&msg, "unix, ");
2747 break;
2748 case -1:
2749 chunk_printf(&msg, "(%s), ", strerror(errno));
2750 break;
2751 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002752 break;
2753 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002754
2755 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002756 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002757
2758 /* cookie */
2759 if (sv->cookie) {
2760 struct chunk src;
2761
2762 chunk_printf(&msg, ", cookie: '");
2763
2764 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2765 chunk_htmlencode(&msg, &src);
2766
2767 chunk_printf(&msg, "'");
2768 }
2769
2770 chunk_printf(&msg, "\"");
2771 }
2772
2773 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002774 ">%s<a name=\"%s/%s\"></a>"
2775 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002776 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002777 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002778 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002779 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002780 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002781 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002782 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002783 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002784 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002785 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002786 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002787 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2788 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002789 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2790
2791 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2792 if (px->mode == PR_MODE_HTTP) {
2793 int i;
2794
2795 chunk_printf(&msg, " title=\"rsp codes:");
2796
2797 for (i = 1; i < 6; i++)
2798 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2799
2800 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2801 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002802
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002803 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002804 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002805 ">%s%s%s</td><td>%s</td>",
2806 (px->mode == PR_MODE_HTTP)?"<u>":"",
2807 U2H0(sv->counters.cum_sess),
2808 (px->mode == PR_MODE_HTTP)?"</u>":"",
2809 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002810
2811 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002812 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002813 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002814 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002815 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002816 /* errors : request, connect */
2817 "<td></td><td>%s</td>"
2818 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002819 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002820 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002821 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002822 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002823 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2824 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002825 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002826 sv->counters.cli_aborts,
2827 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002828 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002829 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002830
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002831 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002832 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002833
Cyril Bontécd19e512010-01-31 22:34:03 +01002834 if (sv->state & SRV_MAINTAIN) {
2835 chunk_printf(&msg, "%s ",
2836 human_time(now.tv_sec - sv->last_change, 1));
2837 chunk_printf(&msg, "MAINT");
2838 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002839 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2840 chunk_printf(&msg, "%s ",
2841 human_time(now.tv_sec - svs->last_change, 1));
2842 chunk_printf(&msg, "MAINT(via)");
2843 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002844 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002845 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002846 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002847
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002848 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002849 srv_hlt_st[sv_state],
2850 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2851 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002852 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002853
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002854 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002855 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002856 get_check_status_description(sv->check.status));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002857
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002858 if (*sv->check.desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002859 struct chunk src;
2860
2861 chunk_printf(&msg, ": ");
2862
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002863 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002864 chunk_htmlencode(&msg, &src);
2865 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002866
Willy Tarreaue0454092010-02-26 12:29:07 +01002867 chunk_printf(&msg, "\"><u> %s%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002868 tv_iszero(&sv->check.start)?"":"* ",
2869 get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002870
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002871 if (sv->check.status >= HCHK_STATUS_L57DATA)
2872 chunk_printf(&msg, "/%d", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002873
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002874 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
2875 chunk_printf(&msg, " in %lums</u>", sv->check.duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002876 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002877 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002878
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002879 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002880 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002881 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002882 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002883 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002884 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002885 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002886 (sv->state & SRV_BACKUP) ? "-" : "Y",
2887 (sv->state & SRV_BACKUP) ? "Y" : "-");
2888
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002889 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002890 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002891 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002892 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2893
2894 if (svs->observe)
2895 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2896
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002897 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002898 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002899 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002900 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002901 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002902 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002903 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002904 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2905 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002906 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002907 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002908 "<td colspan=3></td>");
2909
2910 /* throttle */
2911 if ((sv->state & SRV_WARMINGUP) &&
2912 now.tv_sec < sv->last_change + sv->slowstart &&
2913 now.tv_sec >= sv->last_change) {
2914 unsigned int ratio;
2915 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002916 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002917 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002918 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002919 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002920 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002921 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002922 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002923 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2924 "UP %d/%d,", "UP,",
2925 "NOLB %d/%d,", "NOLB,",
2926 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002927 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002928 /* pxid, name */
2929 "%s,%s,"
2930 /* queue : current, max */
2931 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002932 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002933 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002934 /* bytes : in, out */
2935 "%lld,%lld,"
2936 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002937 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002938 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002939 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002940 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002941 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002942 "",
2943 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002944 sv->nbpend, sv->counters.nbpend_max,
2945 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002946 sv->counters.bytes_in, sv->counters.bytes_out,
2947 sv->counters.failed_secu,
2948 sv->counters.failed_conns, sv->counters.failed_resp,
2949 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002950
Willy Tarreau55bb8452007-10-17 18:44:57 +02002951 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002952 if (sv->state & SRV_MAINTAIN) {
2953 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002954 }
2955 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2956 chunk_printf(&msg, "MAINT(via),");
2957 }
2958 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002959 chunk_printf(&msg,
2960 srv_hlt_st[sv_state],
2961 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2962 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2963 }
Willy Tarreau91861262007-10-17 17:06:05 +02002964
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002965 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002966 /* weight, active, backup */
2967 "%d,%d,%d,"
2968 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002969 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002970 (sv->state & SRV_BACKUP) ? 0 : 1,
2971 (sv->state & SRV_BACKUP) ? 1 : 0);
2972
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002973 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002974 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002975 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002976 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002977 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002978 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002979 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002980 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002981 ",,,,");
2982
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002983 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002984 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002985 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002986 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002987 LIM2A0(sv->maxqueue, ""),
2988 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002989
2990 /* throttle */
2991 if ((sv->state & SRV_WARMINGUP) &&
2992 now.tv_sec < sv->last_change + sv->slowstart &&
2993 now.tv_sec >= sv->last_change) {
2994 unsigned int ratio;
2995 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002996 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002997 }
2998
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002999 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02003000 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003001
3002 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02003003 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003004 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02003005 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003006 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003007 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003008
Willy Tarreau7f062c42009-03-05 18:43:00 +01003009 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003010 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003011
3012 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003013 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003014 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02003015 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01003016
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003017 if (sv->state & SRV_CHECKED) {
3018 /* check_status */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003019 chunk_printf(&msg, "%s,", get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003020
3021 /* check_code */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003022 if (sv->check.status >= HCHK_STATUS_L57DATA)
3023 chunk_printf(&msg, "%u,", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003024 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003025 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003026
3027 /* check_duration */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02003028 if (sv->check.status >= HCHK_STATUS_CHECKED)
3029 chunk_printf(&msg, "%lu,", sv->check.duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003030 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003031 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003032
3033 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003034 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003035 }
3036
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003037 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3038 if (px->mode == PR_MODE_HTTP) {
3039 int i;
3040
3041 for (i=1; i<6; i++)
3042 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
3043
3044 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
3045 } else {
3046 chunk_printf(&msg, ",,,,,,");
3047 }
3048
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003049 /* failed health analyses */
3050 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
3051
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003052 /* requests : req_rate, req_rate_max, req_tot, */
3053 chunk_printf(&msg, ",,,");
3054
Willy Tarreauae526782010-03-04 20:34:23 +01003055 /* errors: cli_aborts, srv_aborts */
3056 chunk_printf(&msg, "%lld,%lld,",
3057 sv->counters.cli_aborts, sv->counters.srv_aborts);
3058
Willy Tarreau7f062c42009-03-05 18:43:00 +01003059 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003060 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003061 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003062 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003063 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003064 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003065
Willy Tarreau295a8372011-03-10 11:25:07 +01003066 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003067 /* fall through */
3068
Willy Tarreau295a8372011-03-10 11:25:07 +01003069 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003070 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003071 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003072 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3073 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003074 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003075 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003076 /* Column sub-heading for Enable or Disable server */
3077 chunk_printf(&msg, "<td></td>");
3078 }
3079 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003080
3081 if (uri->flags&ST_SHLGNDS) {
3082 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003083 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003084 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003085
3086 /* cookie */
3087 if (px->cookie_name) {
3088 struct chunk src;
3089
3090 chunk_printf(&msg, ", cookie: '");
3091
3092 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3093 chunk_htmlencode(&msg, &src);
3094
3095 chunk_printf(&msg, "'");
3096 }
3097
3098 chunk_printf(&msg, "\"");
3099
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003100 }
3101
3102 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003103 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003104 ">%s<a name=\"%s/Backend\"></a>"
3105 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003106 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003107 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003108 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003109 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003110 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003111 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003112 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003113 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003114 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3115 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003116
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003117 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003118 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003119 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003120 "<td"
3121 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003122 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003123
3124 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3125 if (px->mode == PR_MODE_HTTP) {
3126 int i;
3127
3128 chunk_printf(&msg, " title=\"rsp codes:");
3129
3130 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003131 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003132
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003133 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003134 }
3135
3136 chunk_printf(&msg,
3137 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003138 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003139 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003140 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003141 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003142 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003143 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003144 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003145 U2H7(px->be_counters.cum_lbconn),
3146 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003147
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003148 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003149 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003150 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003151 /* errors : request, connect */
3152 "<td></td><td>%s</td>"
3153 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003154 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003155 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003156 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003157 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003158 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003159 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003160 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003161 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3162 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003163 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003164 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3165 U2H2(px->be_counters.failed_conns),
3166 px->be_counters.cli_aborts,
3167 px->be_counters.srv_aborts,
3168 U2H5(px->be_counters.failed_resp),
3169 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003170 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003171 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3172 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003173 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003174 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003175
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003176 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003177 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003178 "<td class=ac>&nbsp;</td><td>%d</td>"
3179 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003180 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003181 "</tr>",
3182 px->down_trans,
3183 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003184 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003185 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003186 /* pxid, name */
3187 "%s,BACKEND,"
3188 /* queue : current, max */
3189 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003190 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003191 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003192 /* bytes : in, out */
3193 "%lld,%lld,"
3194 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003195 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003196 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003197 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003198 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003199 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003200 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003201 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003202 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003203 * active and backups. */
3204 "%s,"
3205 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003206 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003207 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003208 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003209 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003210 /* rate, rate_lim, rate_max, */
3211 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003212 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003213 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003214 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003215 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3216 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3217 px->be_counters.bytes_in, px->be_counters.bytes_out,
3218 px->be_counters.denied_req, px->be_counters.denied_resp,
3219 px->be_counters.failed_conns, px->be_counters.failed_resp,
3220 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003221 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003222 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003223 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003224 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003225 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003226 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003227 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003228 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003229 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003230
3231 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3232 if (px->mode == PR_MODE_HTTP) {
3233 int i;
3234
3235 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003236 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003237
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003238 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003239 } else {
3240 chunk_printf(&msg, ",,,,,,");
3241 }
3242
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003243 /* failed health analyses */
3244 chunk_printf(&msg, ",");
3245
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003246 /* requests : req_rate, req_rate_max, req_tot, */
3247 chunk_printf(&msg, ",,,");
3248
Willy Tarreauae526782010-03-04 20:34:23 +01003249 /* errors: cli_aborts, srv_aborts */
3250 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003251 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003252
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003253 /* finish with EOL */
3254 chunk_printf(&msg, "\n");
3255
Willy Tarreau55bb8452007-10-17 18:44:57 +02003256 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003257 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003258 return 0;
3259 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003260
Willy Tarreau295a8372011-03-10 11:25:07 +01003261 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003262 /* fall through */
3263
Willy Tarreau295a8372011-03-10 11:25:07 +01003264 case STAT_PX_ST_END:
3265 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003266 chunk_printf(&msg, "</table>");
3267
Willy Tarreau295a8372011-03-10 11:25:07 +01003268 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003269 /* close the form used to enable/disable this proxy servers */
3270 chunk_printf(&msg,
3271 "Choose the action to perform on the checked servers : "
3272 "<select name=action>"
3273 "<option value=\"\"></option>"
3274 "<option value=\"disable\">Disable</option>"
3275 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003276 "<option value=\"stop\">Soft Stop</option>"
3277 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003278 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003279 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003280 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003281 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3282 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003283 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003284 }
3285
3286 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003287
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003288 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003289 return 0;
3290 }
Willy Tarreau91861262007-10-17 17:06:05 +02003291
Willy Tarreau295a8372011-03-10 11:25:07 +01003292 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003293 /* fall through */
3294
Willy Tarreau295a8372011-03-10 11:25:07 +01003295 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003296 return 1;
3297
3298 default:
3299 /* unknown state, we should put an abort() here ! */
3300 return 1;
3301 }
3302}
3303
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003304/* This function dumps a complete session state onto the stream interface's
3305 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3306 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003307 * 0 if the output buffer is full and it needs to be called again, otherwise
3308 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003309 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003310static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003311{
3312 struct tm tm;
3313 struct chunk msg;
3314 struct session *sess;
3315 extern const char *monthname[12];
3316 char pn[INET6_ADDRSTRLEN];
3317
David du Colombier7af46052012-05-16 14:16:48 +02003318 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003319 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003320
Willy Tarreau295a8372011-03-10 11:25:07 +01003321 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003322 /* session changed, no need to go any further */
3323 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003324 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003325 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003326 si->applet.ctx.sess.target = NULL;
3327 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003328 return 1;
3329 }
3330
Willy Tarreau295a8372011-03-10 11:25:07 +01003331 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003332 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003333 si->applet.ctx.sess.uid = sess->uniq_id;
3334 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003335 /* fall through */
3336
3337 case 1:
3338 chunk_printf(&msg,
3339 "%p: id=%u, proto=%s",
3340 sess,
3341 sess->uniq_id,
3342 sess->listener->proto->name);
3343
Willy Tarreau986a9d22012-08-30 21:11:38 +02003344 switch (addr_to_str(&sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003345 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003346 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003347 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003348 pn, get_host_port(&sess->si[0].conn.addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003349 break;
3350 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003351 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003352 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003353 default:
3354 /* no more information to print right now */
3355 chunk_printf(&msg, "\n");
3356 break;
3357 }
3358
3359 chunk_printf(&msg,
3360 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003361 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003362
3363 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003364 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003365 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3366 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3367 sess->listener ? sess->listener->luid : 0);
3368
Willy Tarreau986a9d22012-08-30 21:11:38 +02003369 conn_get_to_addr(&sess->si[0].conn);
3370 switch (addr_to_str(&sess->si[0].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003371 case AF_INET:
3372 case AF_INET6:
3373 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003374 pn, get_host_port(&sess->si[0].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003375 break;
3376 case AF_UNIX:
3377 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3378 break;
3379 default:
3380 /* no more information to print right now */
3381 chunk_printf(&msg, "\n");
3382 break;
3383 }
3384
Willy Tarreau50943332011-09-02 17:33:05 +02003385 if (sess->be->cap & PR_CAP_BE)
3386 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003387 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003388 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003389 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3390 else
3391 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3392
Willy Tarreau986a9d22012-08-30 21:11:38 +02003393 conn_get_from_addr(&sess->si[1].conn);
3394 switch (addr_to_str(&sess->si[1].conn.addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003395 case AF_INET:
3396 case AF_INET6:
3397 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003398 pn, get_host_port(&sess->si[1].conn.addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003399 break;
3400 case AF_UNIX:
3401 chunk_printf(&msg, " addr=unix\n");
3402 break;
3403 default:
3404 /* no more information to print right now */
3405 chunk_printf(&msg, "\n");
3406 break;
3407 }
3408
3409 if (sess->be->cap & PR_CAP_BE)
3410 chunk_printf(&msg,
3411 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003412 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3413 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3414 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003415 chunk_printf(&msg, " server=<NONE> (id=-1)");
3416
Willy Tarreau986a9d22012-08-30 21:11:38 +02003417 conn_get_to_addr(&sess->si[1].conn);
3418 switch (addr_to_str(&sess->si[1].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003419 case AF_INET:
3420 case AF_INET6:
3421 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003422 pn, get_host_port(&sess->si[1].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003423 break;
3424 case AF_UNIX:
3425 chunk_printf(&msg, " addr=unix\n");
3426 break;
3427 default:
3428 /* no more information to print right now */
3429 chunk_printf(&msg, "\n");
3430 break;
3431 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003432
3433 chunk_printf(&msg,
3434 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3435 sess->task,
3436 sess->task->state,
3437 sess->task->nice, sess->task->calls,
3438 sess->task->expire ?
3439 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3440 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3441 TICKS_TO_MS(1000)) : "<NEVER>",
3442 task_in_rq(sess->task) ? ", running" : "");
3443
3444 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3445 chunk_printf(&msg,
3446 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3447 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3448 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3449 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3450
3451 chunk_printf(&msg,
3452 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3453 &sess->si[0],
3454 sess->si[0].state,
3455 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003456 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003457 sess->si[0].exp ?
3458 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3459 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3460 TICKS_TO_MS(1000)) : "<NEVER>",
3461 sess->si[0].err_type);
3462
3463 chunk_printf(&msg,
3464 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3465 &sess->si[1],
3466 sess->si[1].state,
3467 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003468 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003469 sess->si[1].exp ?
3470 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3471 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3472 TICKS_TO_MS(1000)) : "<NEVER>",
3473 sess->si[1].err_type);
3474
3475 chunk_printf(&msg,
3476 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3477 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3478 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3479
3480
3481 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003482 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003483 " an_exp=%s",
3484 sess->req,
3485 sess->req->flags, sess->req->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003486 sess->req->buf->i, sess->req->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003487 sess->req->pipe ? sess->req->pipe->data : 0,
3488 sess->req->to_forward,
3489 sess->req->analyse_exp ?
3490 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3491 TICKS_TO_MS(1000)) : "<NEVER>");
3492
3493 chunk_printf(&msg,
3494 " rex=%s",
3495 sess->req->rex ?
3496 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3497 TICKS_TO_MS(1000)) : "<NEVER>");
3498
3499 chunk_printf(&msg,
3500 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003501 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003502 sess->req->wex ?
3503 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3504 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003505 sess->req->buf->data,
3506 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003507 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003508 sess->req->total);
3509
3510 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003511 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003512 " an_exp=%s",
3513 sess->rep,
3514 sess->rep->flags, sess->rep->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003515 sess->rep->buf->i, sess->rep->buf->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003516 sess->rep->pipe ? sess->rep->pipe->data : 0,
3517 sess->rep->to_forward,
3518 sess->rep->analyse_exp ?
3519 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3520 TICKS_TO_MS(1000)) : "<NEVER>");
3521
3522 chunk_printf(&msg,
3523 " rex=%s",
3524 sess->rep->rex ?
3525 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3526 TICKS_TO_MS(1000)) : "<NEVER>");
3527
3528 chunk_printf(&msg,
3529 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003530 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003531 sess->rep->wex ?
3532 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3533 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau9b28e032012-10-12 23:49:43 +02003534 sess->rep->buf->data,
3535 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003536 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003537 sess->rep->total);
3538
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003539 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003540 return 0;
3541
3542 /* use other states to dump the contents */
3543 }
3544 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003545 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003546 return 1;
3547}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003548
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003549/* This function dumps all sessions' states onto the stream interface's
3550 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003551 * properly set. It returns 0 if the output buffer is full and it needs
3552 * to be called again, otherwise non-zero. It is designed to be called
3553 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003554 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003555static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003556{
3557 struct chunk msg;
3558
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003559 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003560 /* If we're forced to shut down, we might have to remove our
3561 * reference to the last session being dumped.
3562 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003563 if (si->conn.xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003564 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3565 LIST_DEL(&si->applet.ctx.sess.bref.users);
3566 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003567 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003568 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003569 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003570 }
3571
David du Colombier7af46052012-05-16 14:16:48 +02003572 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003573
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003574 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003575 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003576 /* the function had not been called yet, let's prepare the
3577 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003578 * pointer to the first in the global list. When a target
3579 * session is being destroyed, it is responsible for updating
3580 * this pointer. We know we have reached the end when this
3581 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003582 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003583 LIST_INIT(&si->applet.ctx.sess.bref.users);
3584 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003585 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003586 /* fall through */
3587
Willy Tarreau295a8372011-03-10 11:25:07 +01003588 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003589 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003590 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3591 LIST_DEL(&si->applet.ctx.sess.bref.users);
3592 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003593 }
3594
3595 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003596 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003597 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003598 struct session *curr_sess;
3599
Willy Tarreau295a8372011-03-10 11:25:07 +01003600 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003601
Willy Tarreau295a8372011-03-10 11:25:07 +01003602 if (si->applet.ctx.sess.target) {
3603 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003604 goto next_sess;
3605
Willy Tarreau295a8372011-03-10 11:25:07 +01003606 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003607 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003608 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003609 return 0;
3610
3611 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003612 LIST_DEL(&si->applet.ctx.sess.bref.users);
3613 LIST_INIT(&si->applet.ctx.sess.bref.users);
3614 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003615 break;
3616 }
3617
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003618 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003619 "%p: proto=%s",
3620 curr_sess,
3621 curr_sess->listener->proto->name);
3622
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003623
Willy Tarreau986a9d22012-08-30 21:11:38 +02003624 switch (addr_to_str(&curr_sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003625 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003626 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003627 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003628 " src=%s:%d fe=%s be=%s srv=%s",
3629 pn,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003630 get_host_port(&curr_sess->si[0].conn.addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003631 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003632 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003633 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003634 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003635 break;
3636 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003637 chunk_printf(&msg,
3638 " src=unix:%d fe=%s be=%s srv=%s",
3639 curr_sess->listener->luid,
3640 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003641 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003642 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003643 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003644 break;
3645 }
3646
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003647 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003648 " ts=%02x age=%s calls=%d",
3649 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003650 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3651 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003652
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003653 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003654 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003655 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003656 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003657 curr_sess->req->analysers,
3658 curr_sess->req->rex ?
3659 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3660 TICKS_TO_MS(1000)) : "");
3661
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003662 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003663 ",wx=%s",
3664 curr_sess->req->wex ?
3665 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3666 TICKS_TO_MS(1000)) : "");
3667
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003668 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003669 ",ax=%s]",
3670 curr_sess->req->analyse_exp ?
3671 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3672 TICKS_TO_MS(1000)) : "");
3673
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003674 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003675 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003676 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003677 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003678 curr_sess->rep->analysers,
3679 curr_sess->rep->rex ?
3680 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3681 TICKS_TO_MS(1000)) : "");
3682
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003683 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003684 ",wx=%s",
3685 curr_sess->rep->wex ?
3686 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3687 TICKS_TO_MS(1000)) : "");
3688
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003689 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003690 ",ax=%s]",
3691 curr_sess->rep->analyse_exp ?
3692 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3693 TICKS_TO_MS(1000)) : "");
3694
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003695 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003696 " s0=[%d,%1xh,fd=%d,ex=%s]",
3697 curr_sess->si[0].state,
3698 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003699 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003700 curr_sess->si[0].exp ?
3701 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3702 TICKS_TO_MS(1000)) : "");
3703
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003704 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003705 " s1=[%d,%1xh,fd=%d,ex=%s]",
3706 curr_sess->si[1].state,
3707 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003708 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003709 curr_sess->si[1].exp ?
3710 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3711 TICKS_TO_MS(1000)) : "");
3712
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003713 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003714 " exp=%s",
3715 curr_sess->task->expire ?
3716 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3717 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003718 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003719 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003720
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003721 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003722
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003723 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003724 /* let's try again later from this session. We add ourselves into
3725 * this session's users so that it can remove us upon termination.
3726 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003727 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003728 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003729 }
3730
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003731 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003732 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003733 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003734
Willy Tarreau295a8372011-03-10 11:25:07 +01003735 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003736 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003737 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003738 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3739 else
3740 chunk_printf(&msg, "Session not found.\n");
3741
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003742 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003743 return 0;
3744
Willy Tarreau295a8372011-03-10 11:25:07 +01003745 si->applet.ctx.sess.target = NULL;
3746 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003747 return 1;
3748 }
3749
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003750 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003751 /* fall through */
3752
3753 default:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003754 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003755 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003756 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003757}
3758
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003759/* This function dumps all tables' states onto the stream interface's
3760 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003761 * properly set. It returns 0 if the output buffer is full and it needs
3762 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003763 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003764static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003765{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003766 struct session *s = si->conn.xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003767 struct chunk msg;
3768 struct ebmb_node *eb;
3769 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003770 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003771
3772 /*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003773 * We have 3 possible states in si->conn.xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003774 * - STAT_ST_INIT : the first call
3775 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003776 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003777 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003778 * and the entry pointer points to the next entry to be dumped,
3779 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003780 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003781 * data though.
3782 */
3783
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003784 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003785 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003786 if (si->conn.xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003787 si->applet.ctx.table.entry->ref_cnt--;
3788 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003789 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003790 return 1;
3791 }
3792
David du Colombier7af46052012-05-16 14:16:48 +02003793 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003794
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003795 while (si->conn.xprt_st != STAT_ST_FIN) {
3796 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003797 case STAT_ST_INIT:
3798 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3799 if (!si->applet.ctx.table.proxy)
3800 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003801
Willy Tarreau295a8372011-03-10 11:25:07 +01003802 si->applet.ctx.table.entry = NULL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003803 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003804 break;
3805
Willy Tarreau295a8372011-03-10 11:25:07 +01003806 case STAT_ST_INFO:
3807 if (!si->applet.ctx.table.proxy ||
3808 (si->applet.ctx.table.target &&
3809 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003810 si->conn.xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003811 break;
3812 }
3813
Willy Tarreau295a8372011-03-10 11:25:07 +01003814 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003815 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3816 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003817 return 0;
3818
Willy Tarreau295a8372011-03-10 11:25:07 +01003819 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02003820 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003821 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003822 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003823 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003824 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3825 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003826 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003827 break;
3828 }
3829 }
3830 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003831 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003832 break;
3833
Willy Tarreau295a8372011-03-10 11:25:07 +01003834 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003835 skip_entry = false;
3836
Willy Tarreau295a8372011-03-10 11:25:07 +01003837 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003838 /* we're filtering on some data contents */
3839 void *ptr;
3840 long long data;
3841
Willy Tarreau295a8372011-03-10 11:25:07 +01003842 dt = si->applet.ctx.table.data_type;
3843 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3844 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003845 dt);
3846
3847 data = 0;
3848 switch (stktable_data_types[dt].std_type) {
3849 case STD_T_SINT:
3850 data = stktable_data_cast(ptr, std_t_sint);
3851 break;
3852 case STD_T_UINT:
3853 data = stktable_data_cast(ptr, std_t_uint);
3854 break;
3855 case STD_T_ULL:
3856 data = stktable_data_cast(ptr, std_t_ull);
3857 break;
3858 case STD_T_FRQP:
3859 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003860 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003861 break;
3862 }
3863
3864 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003865 if ((data < si->applet.ctx.table.value &&
3866 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3867 si->applet.ctx.table.data_op == STD_OP_GT ||
3868 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3869 (data == si->applet.ctx.table.value &&
3870 (si->applet.ctx.table.data_op == STD_OP_NE ||
3871 si->applet.ctx.table.data_op == STD_OP_GT ||
3872 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3873 (data > si->applet.ctx.table.value &&
3874 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3875 si->applet.ctx.table.data_op == STD_OP_LT ||
3876 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003877 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003878 }
3879
Simon Hormanc88b8872011-06-15 15:18:49 +09003880 if (show && !skip_entry &&
3881 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003882 si->applet.ctx.table.entry))
3883 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003884
Willy Tarreau295a8372011-03-10 11:25:07 +01003885 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003886
Willy Tarreau295a8372011-03-10 11:25:07 +01003887 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003888 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003889 struct stksess *old = si->applet.ctx.table.entry;
3890 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003891 if (show)
3892 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3893 else
3894 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003895 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003896 break;
3897 }
3898
Simon Hormanc88b8872011-06-15 15:18:49 +09003899
3900 if (show)
3901 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3902 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3903 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3904
Willy Tarreau295a8372011-03-10 11:25:07 +01003905 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003906 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003907 break;
3908
Willy Tarreau295a8372011-03-10 11:25:07 +01003909 case STAT_ST_END:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003910 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003911 break;
3912 }
3913 }
3914 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003915}
3916
Willy Tarreaud426a182010-03-05 14:58:26 +01003917/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003918 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3919 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3920 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3921 * lines are respected within the limit of 70 output chars. Lines that are
3922 * continuation of a previous truncated line begin with "+" instead of " "
3923 * after the offset. The new pointer is returned.
3924 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003925static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3926 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003927{
3928 int end;
3929 unsigned char c;
3930
3931 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003932 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003933 return ptr;
3934
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003935 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003936
Willy Tarreaud426a182010-03-05 14:58:26 +01003937 while (ptr < len && ptr < bsize) {
3938 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003939 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003940 if (out->len > end - 2)
3941 break;
3942 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003943 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003944 if (out->len > end - 3)
3945 break;
3946 out->str[out->len++] = '\\';
3947 switch (c) {
3948 case '\t': c = 't'; break;
3949 case '\n': c = 'n'; break;
3950 case '\r': c = 'r'; break;
3951 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003952 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003953 }
3954 out->str[out->len++] = c;
3955 } else {
3956 if (out->len > end - 5)
3957 break;
3958 out->str[out->len++] = '\\';
3959 out->str[out->len++] = 'x';
3960 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3961 out->str[out->len++] = hextab[c & 0xF];
3962 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003963 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003964 /* we had a line break, let's return now */
3965 out->str[out->len++] = '\n';
3966 *line = ptr;
3967 return ptr;
3968 }
3969 }
3970 /* we have an incomplete line, we return it as-is */
3971 out->str[out->len++] = '\n';
3972 return ptr;
3973}
3974
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003975/* This function dumps all captured errors onto the stream interface's
3976 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003977 * properly set. It returns 0 if the output buffer is full and it needs
3978 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003979 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003980static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003981{
3982 extern const char *monthname[12];
3983 struct chunk msg;
3984
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003985 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003986 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003987
David du Colombier7af46052012-05-16 14:16:48 +02003988 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003989
Willy Tarreau295a8372011-03-10 11:25:07 +01003990 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003991 /* the function had not been called yet, let's prepare the
3992 * buffer for a response.
3993 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003994 struct tm tm;
3995
3996 get_localtime(date.tv_sec, &tm);
3997 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3998 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3999 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4000 error_snapshot_id);
4001
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004002 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004003 /* Socket buffer full. Let's try again later from the same point */
4004 return 0;
4005 }
4006
Willy Tarreau295a8372011-03-10 11:25:07 +01004007 si->applet.ctx.errors.px = proxy;
4008 si->applet.ctx.errors.buf = 0;
4009 si->applet.ctx.errors.bol = 0;
4010 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004011 }
4012
4013 /* we have two inner loops here, one for the proxy, the other one for
4014 * the buffer.
4015 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004016 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004017 struct error_snapshot *es;
4018
Willy Tarreau295a8372011-03-10 11:25:07 +01004019 if (si->applet.ctx.errors.buf == 0)
4020 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004021 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004022 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004023
4024 if (!es->when.tv_sec)
4025 goto next;
4026
Willy Tarreau295a8372011-03-10 11:25:07 +01004027 if (si->applet.ctx.errors.iid >= 0 &&
4028 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4029 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004030 goto next;
4031
Willy Tarreau295a8372011-03-10 11:25:07 +01004032 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004033 /* just print headers now */
4034
4035 char pn[INET6_ADDRSTRLEN];
4036 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004037 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004038
4039 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01004040 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004041 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004042 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004043
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004044 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4045 case AF_INET:
4046 case AF_INET6:
4047 port = get_host_port(&es->src);
4048 break;
4049 default:
4050 port = 0;
4051 }
4052
Willy Tarreau295a8372011-03-10 11:25:07 +01004053 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004054 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004055 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004056 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004057 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004058 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004059 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4060 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004061 break;
4062 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004063 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004064 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004065 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004066 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004067 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004068 break;
4069 }
4070
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004071 chunk_printf(&msg,
4072 ", server %s (#%d), event #%u\n"
4073 " src %s:%d, session #%d, session flags 0x%08x\n"
4074 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4075 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4076 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4077 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4078 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4079 es->ev_id,
4080 pn, port, es->sid, es->s_flags,
4081 es->state, es->m_flags, es->t_flags,
4082 es->m_clen, es->m_blen,
4083 es->b_flags, es->b_out, es->b_tot,
4084 es->len, es->b_wrap, es->pos);
4085
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004086 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004087 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004088 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004089 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004090 si->applet.ctx.errors.ptr = 0;
4091 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004092 }
4093
Willy Tarreau295a8372011-03-10 11:25:07 +01004094 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004095 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004096 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004097 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004098 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004099 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004100 goto next;
4101 }
4102
4103 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004104 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004105 int newptr;
4106 int newline;
4107
Willy Tarreau295a8372011-03-10 11:25:07 +01004108 newline = si->applet.ctx.errors.bol;
4109 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4110 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004111 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004112
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004113 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004114 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004115 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004116 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004117 si->applet.ctx.errors.ptr = newptr;
4118 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004119 };
4120 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004121 si->applet.ctx.errors.bol = 0;
4122 si->applet.ctx.errors.ptr = -1;
4123 si->applet.ctx.errors.buf++;
4124 if (si->applet.ctx.errors.buf > 1) {
4125 si->applet.ctx.errors.buf = 0;
4126 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004127 }
4128 }
4129
4130 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004131 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004132}
4133
Willy Tarreaud5781202012-09-22 19:32:35 +02004134/* parse the "level" argument on the bind lines */
4135static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4136{
4137 if (!*args[cur_arg + 1]) {
4138 memprintf(err, "'%s' : missing level", args[cur_arg]);
4139 return ERR_ALERT | ERR_FATAL;
4140 }
4141
4142 if (!strcmp(args[cur_arg+1], "user"))
4143 conf->level = ACCESS_LVL_USER;
4144 else if (!strcmp(args[cur_arg+1], "operator"))
4145 conf->level = ACCESS_LVL_OPER;
4146 else if (!strcmp(args[cur_arg+1], "admin"))
4147 conf->level = ACCESS_LVL_ADMIN;
4148 else {
4149 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4150 args[cur_arg], args[cur_arg+1]);
4151 return ERR_ALERT | ERR_FATAL;
4152 }
4153
4154 return 0;
4155}
4156
Willy Tarreaub24281b2011-02-13 13:16:36 +01004157struct si_applet http_stats_applet = {
4158 .name = "<STATS>", /* used for logging */
4159 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004160 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004161};
4162
Simon Horman9bd2c732011-06-15 15:18:44 +09004163static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004164 .name = "<CLI>", /* used for logging */
4165 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004166 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004167};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004168
Willy Tarreau10522fd2008-07-09 20:12:41 +02004169static struct cfg_kw_list cfg_kws = {{ },{
4170 { CFG_GLOBAL, "stats", stats_parse_global },
4171 { 0, NULL, NULL },
4172}};
4173
Willy Tarreaud5781202012-09-22 19:32:35 +02004174static struct bind_kw_list bind_kws = { "STAT", { }, {
4175 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4176 { NULL, NULL, 0 },
4177}};
4178
Willy Tarreau10522fd2008-07-09 20:12:41 +02004179__attribute__((constructor))
4180static void __dumpstats_module_init(void)
4181{
4182 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004183 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004184}
4185
Willy Tarreau91861262007-10-17 17:06:05 +02004186/*
4187 * Local variables:
4188 * c-indent-level: 8
4189 * c-basic-offset: 8
4190 * End:
4191 */