blob: 3a890f3c15225a0859af585f6902ec9b5ec97ed7 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010051#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010064static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010065static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090066static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010068static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010069static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Simon Horman9bd2c732011-06-15 15:18:44 +090071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010072/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010073 * cli_io_handler()
74 * -> stats_dump_sess_to_buffer() // "show sess"
75 * -> stats_dump_errors_to_buffer() // "show errors"
76 * -> stats_dump_info_to_buffer() // "show info"
77 * -> stats_dump_stat_to_buffer() // "show stat"
78 * -> stats_dump_csv_header()
79 * -> stats_dump_proxy_to_buffer()
80 * -> stats_dump_fe_stats()
81 * -> stats_dump_li_stats()
82 * -> stats_dump_sv_stats()
83 * -> stats_dump_be_stats()
84 *
85 * http_stats_io_handler()
86 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
87 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
88 * -> stats_dump_html_head() // emits the HTML headers
89 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
90 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
91 * -> stats_dump_html_px_hdr()
92 * -> stats_dump_fe_stats()
93 * -> stats_dump_li_stats()
94 * -> stats_dump_sv_stats()
95 * -> stats_dump_be_stats()
96 * -> stats_dump_html_px_end()
97 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010098 */
99
Simon Horman9bd2c732011-06-15 15:18:44 +0900100static struct si_applet cli_applet;
101
102static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200103 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200104 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200105 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200106 " help : this message\n"
107 " prompt : toggle interactive mode with prompt\n"
108 " quit : disconnect\n"
109 " show info : report information about the running process\n"
110 " show stat : report counters for each proxy and server\n"
111 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100112 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200113 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200114 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200115 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200116 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100117 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200118 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200119 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200120 " disable : put a server or frontend in maintenance mode\n"
121 " enable : re-enable a server or frontend which is in maintenance mode\n"
122 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200123 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200126 "Permission denied\n"
127 "";
128
Willy Tarreau295a8372011-03-10 11:25:07 +0100129/* data transmission states for the stats responses */
130enum {
131 STAT_ST_INIT = 0,
132 STAT_ST_HEAD,
133 STAT_ST_INFO,
134 STAT_ST_LIST,
135 STAT_ST_END,
136 STAT_ST_FIN,
137};
138
139/* data transmission states for the stats responses inside a proxy */
140enum {
141 STAT_PX_ST_INIT = 0,
142 STAT_PX_ST_TH,
143 STAT_PX_ST_FE,
144 STAT_PX_ST_LI,
145 STAT_PX_ST_SV,
146 STAT_PX_ST_BE,
147 STAT_PX_ST_END,
148 STAT_PX_ST_FIN,
149};
150
Cyril Bonté19979e12012-04-04 12:57:21 +0200151extern const char *stat_status_codes[];
152
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200153/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the connection
155 * needs to be closed and ignored, or a negative value upon critical failure.
156 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900157static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200158{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200159 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100160 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100161 s->target = s->si[1].conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200162 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100163 s->si[1].applet.st1 = 0;
164 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200165
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200166 tv_zero(&s->logs.tv_request);
167 s->logs.t_queue = 0;
168 s->logs.t_connect = 0;
169 s->logs.t_data = 0;
170 s->logs.t_close = 0;
171 s->logs.bytes_in = s->logs.bytes_out = 0;
172 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
173 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200174
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200175 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200176
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200177 if (s->listener->timeout) {
178 s->req->rto = *s->listener->timeout;
179 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200181 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200182}
183
Willy Tarreau07e9e642010-08-17 21:48:17 +0200184/* allocate a new stats frontend named <name>, and return it
185 * (or NULL in case of lack of memory).
186 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200187static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200188{
189 struct proxy *fe;
190
191 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
192 if (!fe)
193 return NULL;
194
Willy Tarreau237250c2011-07-29 01:49:03 +0200195 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200196 fe->next = proxy;
197 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200198 fe->last_change = now.tv_sec;
199 fe->id = strdup("GLOBAL");
200 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200201 fe->maxconn = 10; /* default to 10 concurrent connections */
202 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200203 fe->conf.file = strdup(file);
204 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200205 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200206
207 /* the stats frontend is the only one able to assign ID #0 */
208 fe->conf.id.key = fe->uuid = 0;
209 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200210 return fe;
211}
212
Willy Tarreaufbee7132007-10-18 13:53:22 +0200213/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200214 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
215 * error message into the <err> buffer which will be preallocated. The trailing
216 * '\n' must not be written. The function must be called with <args> pointing to
217 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200218 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200219static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200220 struct proxy *defpx, const char *file, int line,
221 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200222{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200223 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200224 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200225
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200226 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200227 int cur_arg;
228
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200229 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200230 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 +0200231 return -1;
232 }
233
Willy Tarreau89a63132009-08-16 17:41:45 +0200234 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200235 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200236 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200237 return -1;
238 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200239 }
240
Willy Tarreau4348fad2012-09-20 16:48:07 +0200241 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200242 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200243
Willy Tarreauc53d4222012-09-20 20:19:28 +0200244 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
245 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
246 file, line, args[0], args[1], err && *err ? *err : "error");
247 return -1;
248 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200249
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200250 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200251 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200252 static int bind_dumped;
253 struct bind_kw *kw;
254
255 kw = bind_find_kw(args[cur_arg]);
256 if (kw) {
257 if (!kw->parse) {
258 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
259 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200260 return -1;
261 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200262
263 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
264 if (err && *err)
265 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
266 else
267 memprintf(err, "'%s %s' : error encountered while processing '%s'",
268 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200269 return -1;
270 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200271
272 cur_arg += 1 + kw->skip;
273 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200274 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200275
276 if (!bind_dumped) {
277 bind_dump_kws(err);
278 indent_msg(err, 4);
279 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200281
282 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
283 args[0], args[1], args[cur_arg],
284 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
285 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100287
Willy Tarreauc53d4222012-09-20 20:19:28 +0200288 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
289 l->maxconn = global.stats_fe->maxconn;
290 l->backlog = global.stats_fe->backlog;
291 l->timeout = &global.stats_fe->timeout.client;
292 l->accept = session_accept;
293 l->handler = process_session;
294 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
295 l->nice = -64; /* we want to boost priority for local stats */
296 global.maxsock += l->maxconn;
297 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200298 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100300 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100302
303 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200304 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100305 return -1;
306 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100308 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200309 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200310 return -1;
311 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200312 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200313 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200314 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200315 return -1;
316 }
317 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200318 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200319 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 else if (!strcmp(args[1], "maxconn")) {
321 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322
323 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200324 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200325 return -1;
326 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200327
328 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200329 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200330 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200331 return -1;
332 }
333 }
334 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200335 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200336 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
337 int cur_arg = 2;
338 unsigned int set = 0;
339
340 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100341 unsigned int low, high;
342
Willy Tarreau35b7b162012-10-22 23:17:18 +0200343 if (strcmp(args[cur_arg], "all") == 0) {
344 set = 0;
345 break;
346 }
347 else if (strcmp(args[cur_arg], "odd") == 0) {
348 set |= 0x55555555;
349 }
350 else if (strcmp(args[cur_arg], "even") == 0) {
351 set |= 0xAAAAAAAA;
352 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100353 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100354 char *dash = strchr(args[cur_arg], '-');
355
356 low = high = str2uic(args[cur_arg]);
357 if (dash)
358 high = str2uic(dash + 1);
359
360 if (high < low) {
361 unsigned int swap = low;
362 low = high;
363 high = swap;
364 }
365
366 if (low < 1 || high > 32) {
367 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
368 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200369 return -1;
370 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100371
372 while (low <= high)
373 set |= 1 << (low++ - 1);
374 }
375 else {
376 memprintf(err,
377 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
378 args[0], args[1]);
379 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200380 }
381 cur_arg++;
382 }
383 global.stats_fe->bind_proc = set;
384 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200385 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200386 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200387 return -1;
388 }
389 return 0;
390}
391
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100392/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
393 * for clearing it if needed.
394 */
395static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100396{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100397 chunk_appendf(&trash,
398 "# pxname,svname,"
399 "qcur,qmax,"
400 "scur,smax,slim,stot,"
401 "bin,bout,"
402 "dreq,dresp,"
403 "ereq,econ,eresp,"
404 "wretr,wredis,"
405 "status,weight,act,bck,"
406 "chkfail,chkdown,lastchg,downtime,qlimit,"
407 "pid,iid,sid,throttle,lbtot,tracked,type,"
408 "rate,rate_lim,rate_max,"
409 "check_status,check_code,check_duration,"
410 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
411 "req_rate,req_rate_max,req_tot,"
412 "cli_abrt,srv_abrt,"
413 "comp_in,comp_out,comp_byp,comp_rsp,"
414 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100415}
416
Simon Hormand9366582011-06-15 15:18:45 +0900417/* print a string of text buffer to <out>. The format is :
418 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
419 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
420 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
421 */
422static int dump_text(struct chunk *out, const char *buf, int bsize)
423{
424 unsigned char c;
425 int ptr = 0;
426
427 while (buf[ptr] && ptr < bsize) {
428 c = buf[ptr];
429 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
430 if (out->len > out->size - 1)
431 break;
432 out->str[out->len++] = c;
433 }
434 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
435 if (out->len > out->size - 2)
436 break;
437 out->str[out->len++] = '\\';
438 switch (c) {
439 case ' ': c = ' '; break;
440 case '\t': c = 't'; break;
441 case '\n': c = 'n'; break;
442 case '\r': c = 'r'; break;
443 case '\e': c = 'e'; break;
444 case '\\': c = '\\'; break;
445 }
446 out->str[out->len++] = c;
447 }
448 else {
449 if (out->len > out->size - 4)
450 break;
451 out->str[out->len++] = '\\';
452 out->str[out->len++] = 'x';
453 out->str[out->len++] = hextab[(c >> 4) & 0xF];
454 out->str[out->len++] = hextab[c & 0xF];
455 }
456 ptr++;
457 }
458
459 return ptr;
460}
461
462/* print a buffer in hexa.
463 * Print stopped if <bsize> is reached, or if no more place in the chunk.
464 */
465static int dump_binary(struct chunk *out, const char *buf, int bsize)
466{
467 unsigned char c;
468 int ptr = 0;
469
470 while (ptr < bsize) {
471 c = buf[ptr];
472
473 if (out->len > out->size - 2)
474 break;
475 out->str[out->len++] = hextab[(c >> 4) & 0xF];
476 out->str[out->len++] = hextab[c & 0xF];
477
478 ptr++;
479 }
480 return ptr;
481}
482
483/* Dump the status of a table to a stream interface's
484 * read buffer. It returns 0 if the output buffer is full
485 * and needs to be called again, otherwise non-zero.
486 */
487static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
488 struct proxy *proxy, struct proxy *target)
489{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200490 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900491
Willy Tarreau77804732012-10-29 16:14:26 +0100492 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900493 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
494
495 /* any other information should be dumped here */
496
Willy Tarreau290e63a2012-09-20 18:07:14 +0200497 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100498 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900499
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200500 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900501 return 0;
502
503 return 1;
504}
505
506/* Dump the a table entry to a stream interface's
507 * read buffer. It returns 0 if the output buffer is full
508 * and needs to be called again, otherwise non-zero.
509 */
510static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
511 struct proxy *proxy, struct stksess *entry)
512{
513 int dt;
514
Willy Tarreau77804732012-10-29 16:14:26 +0100515 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900516
517 if (proxy->table.type == STKTABLE_TYPE_IP) {
518 char addr[INET_ADDRSTRLEN];
519 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100520 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900521 }
522 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
523 char addr[INET6_ADDRSTRLEN];
524 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100525 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900526 }
527 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100528 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900529 }
530 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900532 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
533 }
534 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100535 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900536 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
537 }
538
Willy Tarreau77804732012-10-29 16:14:26 +0100539 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900540
541 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
542 void *ptr;
543
544 if (proxy->table.data_ofs[dt] == 0)
545 continue;
546 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100547 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900548 else
Willy Tarreau77804732012-10-29 16:14:26 +0100549 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900550
551 ptr = stktable_data_ptr(&proxy->table, entry, dt);
552 switch (stktable_data_types[dt].std_type) {
553 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100554 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900555 break;
556 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100557 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900558 break;
559 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900561 break;
562 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900564 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
565 proxy->table.data_arg[dt].u));
566 break;
567 }
568 }
Willy Tarreau77804732012-10-29 16:14:26 +0100569 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900570
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200571 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900572 return 0;
573
574 return 1;
575}
576
Willy Tarreaudec98142012-06-06 23:37:08 +0200577static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900578{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200579 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900580 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900581 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900582 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900583 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200584 long long value;
585 int data_type;
586 void *ptr;
587 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900588
Simon Hormand5b9fd92011-06-15 15:18:48 +0900589 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900590
591 if (!*args[4]) {
592 si->applet.ctx.cli.msg = "Key value expected\n";
593 si->applet.st0 = STAT_CLI_PRINT;
594 return;
595 }
596
Simon Hormanc5b89f62011-06-15 15:18:50 +0900597 switch (px->table.type) {
598 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900599 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100600 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900601 break;
602 case STKTABLE_TYPE_IPV6:
603 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100604 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900605 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900606 case STKTABLE_TYPE_INTEGER:
607 {
608 char *endptr;
609 unsigned long val;
610 errno = 0;
611 val = strtoul(args[4], &endptr, 10);
612 if ((errno == ERANGE && val == ULONG_MAX) ||
613 (errno != 0 && val == 0) || endptr == args[4] ||
614 val > 0xffffffff) {
615 si->applet.ctx.cli.msg = "Invalid key\n";
616 si->applet.st0 = STAT_CLI_PRINT;
617 return;
618 }
619 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100620 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900621 break;
622 }
623 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900624 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100625 static_table_key->key = args[4];
626 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900627 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900628 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200629 switch (action) {
630 case STAT_CLI_O_TAB:
631 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
632 break;
633 case STAT_CLI_O_CLR:
634 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
635 break;
636 default:
637 si->applet.ctx.cli.msg = "Unknown action\n";
638 break;
639 }
Simon Horman121f3052011-06-15 15:18:46 +0900640 si->applet.st0 = STAT_CLI_PRINT;
641 return;
642 }
643
644 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200645 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900646 si->applet.ctx.cli.msg = stats_permission_denied_msg;
647 si->applet.st0 = STAT_CLI_PRINT;
648 return;
649 }
650
Willy Tarreau07115412012-10-29 21:56:59 +0100651 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900652
Willy Tarreaudec98142012-06-06 23:37:08 +0200653 switch (action) {
654 case STAT_CLI_O_TAB:
655 if (!ts)
656 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100657 chunk_reset(&trash);
658 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900659 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100660 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900661 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200662
663 case STAT_CLI_O_CLR:
664 if (!ts)
665 return;
666 if (ts->ref_cnt) {
667 /* don't delete an entry which is currently referenced */
668 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
669 si->applet.st0 = STAT_CLI_PRINT;
670 return;
671 }
672 stksess_kill(&px->table, ts);
673 break;
Simon Horman17bce342011-06-15 15:18:47 +0900674
Willy Tarreau654694e2012-06-07 01:03:16 +0200675 case STAT_CLI_O_SET:
676 if (strncmp(args[5], "data.", 5) != 0) {
677 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
678 si->applet.st0 = STAT_CLI_PRINT;
679 return;
680 }
681
682 data_type = stktable_get_data_type(args[5] + 5);
683 if (data_type < 0) {
684 si->applet.ctx.cli.msg = "Unknown data type\n";
685 si->applet.st0 = STAT_CLI_PRINT;
686 return;
687 }
688
689 if (!px->table.data_ofs[data_type]) {
690 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
691 si->applet.st0 = STAT_CLI_PRINT;
692 return;
693 }
694
695 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
696 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
697 si->applet.st0 = STAT_CLI_PRINT;
698 return;
699 }
700
701 if (ts)
702 stktable_touch(&px->table, ts, 1);
703 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100704 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200705 if (!ts) {
706 /* don't delete an entry which is currently referenced */
707 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
708 si->applet.st0 = STAT_CLI_PRINT;
709 return;
710 }
711 stktable_store(&px->table, ts, 1);
712 }
713
714 ptr = stktable_data_ptr(&px->table, ts, data_type);
715 switch (stktable_data_types[data_type].std_type) {
716 case STD_T_SINT:
717 stktable_data_cast(ptr, std_t_sint) = value;
718 break;
719 case STD_T_UINT:
720 stktable_data_cast(ptr, std_t_uint) = value;
721 break;
722 case STD_T_ULL:
723 stktable_data_cast(ptr, std_t_ull) = value;
724 break;
725 case STD_T_FRQP:
726 /* We only reset the previous value so that it slowly fades out */
727 frqp = &stktable_data_cast(ptr, std_t_frqp);
728 frqp->curr_tick = now_ms;
729 frqp->prev_ctr = value;
730 frqp->curr_ctr = 0;
731 break;
732 }
733 break;
734
Willy Tarreaudec98142012-06-06 23:37:08 +0200735 default:
736 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900737 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200738 break;
Simon Horman121f3052011-06-15 15:18:46 +0900739 }
Simon Horman121f3052011-06-15 15:18:46 +0900740}
741
Willy Tarreau654694e2012-06-07 01:03:16 +0200742static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900743{
Willy Tarreau654694e2012-06-07 01:03:16 +0200744 if (action != STAT_CLI_O_TAB) {
745 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
746 si->applet.st0 = STAT_CLI_PRINT;
747 return;
748 }
749
Simon Hormand5b9fd92011-06-15 15:18:48 +0900750 /* condition on stored data value */
751 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
752 if (si->applet.ctx.table.data_type < 0) {
753 si->applet.ctx.cli.msg = "Unknown data type\n";
754 si->applet.st0 = STAT_CLI_PRINT;
755 return;
756 }
757
758 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
759 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
760 si->applet.st0 = STAT_CLI_PRINT;
761 return;
762 }
763
764 si->applet.ctx.table.data_op = get_std_op(args[4]);
765 if (si->applet.ctx.table.data_op < 0) {
766 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
767 si->applet.st0 = STAT_CLI_PRINT;
768 return;
769 }
770
771 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
772 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
773 si->applet.st0 = STAT_CLI_PRINT;
774 return;
775 }
776}
777
Willy Tarreaudec98142012-06-06 23:37:08 +0200778static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900779{
780 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200781 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200782 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900783 si->applet.ctx.table.proxy = NULL;
784 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200785 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900786
787 if (*args[2]) {
788 si->applet.ctx.table.target = find_stktable(args[2]);
789 if (!si->applet.ctx.table.target) {
790 si->applet.ctx.cli.msg = "No such table\n";
791 si->applet.st0 = STAT_CLI_PRINT;
792 return;
793 }
794 }
795 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200796 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900797 goto err_args;
798 return;
799 }
800
801 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200802 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900803 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200804 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900805 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900806 goto err_args;
807
808 return;
809
810err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200811 switch (action) {
812 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900813 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 +0200814 break;
815 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900816 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 +0200817 break;
818 default:
819 si->applet.ctx.cli.msg = "Unknown action\n";
820 break;
821 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900822 si->applet.st0 = STAT_CLI_PRINT;
823}
824
Willy Tarreau532a4502011-09-07 22:37:44 +0200825/* Expects to find a frontend named <arg> and returns it, otherwise displays various
826 * adequate error messages and returns NULL. This function also expects the session
827 * level to be admin.
828 */
829static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
830{
831 struct proxy *px;
832
Willy Tarreau290e63a2012-09-20 18:07:14 +0200833 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200834 si->applet.ctx.cli.msg = stats_permission_denied_msg;
835 si->applet.st0 = STAT_CLI_PRINT;
836 return NULL;
837 }
838
839 if (!*arg) {
840 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
841 si->applet.st0 = STAT_CLI_PRINT;
842 return NULL;
843 }
844
845 px = findproxy(arg, PR_CAP_FE);
846 if (!px) {
847 si->applet.ctx.cli.msg = "No such frontend.\n";
848 si->applet.st0 = STAT_CLI_PRINT;
849 return NULL;
850 }
851 return px;
852}
853
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200854/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
855 * and returns the pointer to the server. Otherwise, display adequate error messages
856 * and returns NULL. This function also expects the session level to be admin. Note:
857 * the <arg> is modified to remove the '/'.
858 */
859static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
860{
861 struct proxy *px;
862 struct server *sv;
863 char *line;
864
Willy Tarreau290e63a2012-09-20 18:07:14 +0200865 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200866 si->applet.ctx.cli.msg = stats_permission_denied_msg;
867 si->applet.st0 = STAT_CLI_PRINT;
868 return NULL;
869 }
870
871 /* split "backend/server" and make <line> point to server */
872 for (line = arg; *line; line++)
873 if (*line == '/') {
874 *line++ = '\0';
875 break;
876 }
877
878 if (!*line || !*arg) {
879 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
880 si->applet.st0 = STAT_CLI_PRINT;
881 return NULL;
882 }
883
884 if (!get_backend_server(arg, line, &px, &sv)) {
885 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
886 si->applet.st0 = STAT_CLI_PRINT;
887 return NULL;
888 }
889
890 if (px->state == PR_STSTOPPED) {
891 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
892 si->applet.st0 = STAT_CLI_PRINT;
893 return NULL;
894 }
895
896 return sv;
897}
898
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200899/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200900 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100901 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200902 * designating the function which will have to process the request, which can
903 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200904 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900905static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200906{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200907 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200908 char *args[MAX_STATS_ARGS + 1];
909 int arg;
910
911 while (isspace((unsigned char)*line))
912 line++;
913
914 arg = 0;
915 args[arg] = line;
916
917 while (*line && arg < MAX_STATS_ARGS) {
918 if (isspace((unsigned char)*line)) {
919 *line++ = '\0';
920
921 while (isspace((unsigned char)*line))
922 line++;
923
924 args[++arg] = line;
925 continue;
926 }
927
928 line++;
929 }
930
931 while (++arg <= MAX_STATS_ARGS)
932 args[arg] = line;
933
Willy Tarreau295a8372011-03-10 11:25:07 +0100934 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200935 if (strcmp(args[0], "show") == 0) {
936 if (strcmp(args[1], "stat") == 0) {
937 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100938 si->applet.ctx.stats.flags |= STAT_BOUND;
939 si->applet.ctx.stats.iid = atoi(args[2]);
940 si->applet.ctx.stats.type = atoi(args[3]);
941 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200942 }
943
Willy Tarreau295a8372011-03-10 11:25:07 +0100944 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200945 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100946 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200947 }
948 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200950 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100951 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200952 }
953 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200954 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200955 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100956 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100957 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200958 return 1;
959 }
Willy Tarreau76153662012-11-26 01:16:39 +0100960 if (*args[2] && strcmp(args[2], "all") == 0)
961 si->applet.ctx.sess.target = (void *)-1;
962 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100963 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100964 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100965 si->applet.ctx.sess.target = NULL;
966 si->applet.ctx.sess.section = 0; /* start with session status */
967 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100968 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200969 }
970 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200971 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100972 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100973 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200974 return 1;
975 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200976 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200978 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100979 si->applet.ctx.errors.iid = -1;
980 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200981 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100982 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200983 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200984 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200985 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200986 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700987 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200988 return 0;
989 }
990 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200991 else if (strcmp(args[0], "clear") == 0) {
992 if (strcmp(args[1], "counters") == 0) {
993 struct proxy *px;
994 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200995 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200996 int clrall = 0;
997
998 if (strcmp(args[2], "all") == 0)
999 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001000
Willy Tarreau6162db22009-10-10 17:13:00 +02001001 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001002 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1003 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001004 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001005 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001006 return 1;
1007 }
1008
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001009 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001010 if (clrall) {
1011 memset(&px->be_counters, 0, sizeof(px->be_counters));
1012 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1013 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001014 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001015 px->be_counters.conn_max = 0;
1016 px->be_counters.p.http.rps_max = 0;
1017 px->be_counters.sps_max = 0;
1018 px->be_counters.cps_max = 0;
1019 px->be_counters.nbpend_max = 0;
1020
1021 px->fe_counters.conn_max = 0;
1022 px->fe_counters.p.http.rps_max = 0;
1023 px->fe_counters.sps_max = 0;
1024 px->fe_counters.cps_max = 0;
1025 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001026 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001027
1028 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001029 if (clrall)
1030 memset(&sv->counters, 0, sizeof(sv->counters));
1031 else {
1032 sv->counters.cur_sess_max = 0;
1033 sv->counters.nbpend_max = 0;
1034 sv->counters.sps_max = 0;
1035 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001036
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001037 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001038 if (li->counters) {
1039 if (clrall)
1040 memset(li->counters, 0, sizeof(*li->counters));
1041 else
1042 li->counters->conn_max = 0;
1043 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001044 }
1045
Willy Tarreau81c25d02011-09-07 15:17:21 +02001046 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001047 return 1;
1048 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001049 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001050 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001051 /* end of processing */
1052 return 1;
1053 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001054 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001055 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001056 return 0;
1057 }
1058 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001059 else if (strcmp(args[0], "get") == 0) {
1060 if (strcmp(args[1], "weight") == 0) {
1061 struct proxy *px;
1062 struct server *sv;
1063
1064 /* split "backend/server" and make <line> point to server */
1065 for (line = args[2]; *line; line++)
1066 if (*line == '/') {
1067 *line++ = '\0';
1068 break;
1069 }
1070
1071 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001072 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001073 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001074 return 1;
1075 }
1076
1077 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001078 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001079 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001080 return 1;
1081 }
1082
1083 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001084 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1085 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001086 return 1;
1087 }
1088 else { /* not "get weight" */
1089 return 0;
1090 }
1091 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001092 else if (strcmp(args[0], "set") == 0) {
1093 if (strcmp(args[1], "weight") == 0) {
1094 struct proxy *px;
1095 struct server *sv;
1096 int w;
1097
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001098 sv = expect_server_admin(s, si, args[2]);
1099 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001100 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001101 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001102
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001103 /* if the weight is terminated with '%', it is set relative to
1104 * the initial weight, otherwise it is absolute.
1105 */
1106 if (!*args[3]) {
1107 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001108 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001109 return 1;
1110 }
1111
Willy Tarreau4483d432009-10-10 19:30:08 +02001112 w = atoi(args[3]);
1113 if (strchr(args[3], '%') != NULL) {
1114 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001115 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001116 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001117 return 1;
1118 }
1119 w = sv->iweight * w / 100;
1120 }
1121 else {
1122 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001123 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001124 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001125 return 1;
1126 }
1127 }
1128
1129 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001130 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 +01001131 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001132 return 1;
1133 }
1134
1135 sv->uweight = w;
1136
1137 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1138 /* we must take care of not pushing the server to full throttle during slow starts */
1139 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1140 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1141 else
1142 sv->eweight = BE_WEIGHT_SCALE;
1143 sv->eweight *= sv->uweight;
1144 } else {
1145 sv->eweight = sv->uweight;
1146 }
1147
1148 /* static LB algorithms are a bit harder to update */
1149 if (px->lbprm.update_server_eweight)
1150 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001151 else if (sv->eweight) {
1152 if (px->lbprm.set_server_status_up)
1153 px->lbprm.set_server_status_up(sv);
1154 }
1155 else {
1156 if (px->lbprm.set_server_status_down)
1157 px->lbprm.set_server_status_down(sv);
1158 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001159
1160 return 1;
1161 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001162 else if (strcmp(args[1], "timeout") == 0) {
1163 if (strcmp(args[2], "cli") == 0) {
1164 unsigned timeout;
1165 const char *res;
1166
1167 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001168 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001169 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001170 return 1;
1171 }
1172
1173 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1174 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001175 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001176 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001177 return 1;
1178 }
1179
1180 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1181 return 1;
1182 }
1183 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001184 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001185 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001186 return 1;
1187 }
1188 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001189 else if (strcmp(args[1], "maxconn") == 0) {
1190 if (strcmp(args[2], "frontend") == 0) {
1191 struct proxy *px;
1192 struct listener *l;
1193 int v;
1194
Willy Tarreau532a4502011-09-07 22:37:44 +02001195 px = expect_frontend_admin(s, si, args[3]);
1196 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001197 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001198
1199 if (!*args[4]) {
1200 si->applet.ctx.cli.msg = "Integer value expected.\n";
1201 si->applet.st0 = STAT_CLI_PRINT;
1202 return 1;
1203 }
1204
1205 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001206 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001207 si->applet.ctx.cli.msg = "Value out of range.\n";
1208 si->applet.st0 = STAT_CLI_PRINT;
1209 return 1;
1210 }
1211
1212 /* OK, the value is fine, so we assign it to the proxy and to all of
1213 * its listeners. The blocked ones will be dequeued.
1214 */
1215 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001216 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001217 l->maxconn = v;
1218 if (l->state == LI_FULL)
1219 resume_listener(l);
1220 }
1221
1222 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1223 dequeue_all_listeners(&s->fe->listener_queue);
1224
1225 return 1;
1226 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001227 else if (strcmp(args[2], "global") == 0) {
1228 int v;
1229
Willy Tarreau290e63a2012-09-20 18:07:14 +02001230 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001231 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1232 si->applet.st0 = STAT_CLI_PRINT;
1233 return 1;
1234 }
1235
1236 if (!*args[3]) {
1237 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1238 si->applet.st0 = STAT_CLI_PRINT;
1239 return 1;
1240 }
1241
1242 v = atoi(args[3]);
1243 if (v > global.hardmaxconn) {
1244 si->applet.ctx.cli.msg = "Value out of range.\n";
1245 si->applet.st0 = STAT_CLI_PRINT;
1246 return 1;
1247 }
1248
1249 /* check for unlimited values */
1250 if (v <= 0)
1251 v = global.hardmaxconn;
1252
1253 global.maxconn = v;
1254
1255 /* Dequeues all of the listeners waiting for a resource */
1256 if (!LIST_ISEMPTY(&global_listener_queue))
1257 dequeue_all_listeners(&global_listener_queue);
1258
1259 return 1;
1260 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001261 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001262 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001263 si->applet.st0 = STAT_CLI_PRINT;
1264 return 1;
1265 }
1266 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001267 else if (strcmp(args[1], "rate-limit") == 0) {
1268 if (strcmp(args[2], "connections") == 0) {
1269 if (strcmp(args[3], "global") == 0) {
1270 int v;
1271
Willy Tarreau290e63a2012-09-20 18:07:14 +02001272 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001273 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1274 si->applet.st0 = STAT_CLI_PRINT;
1275 return 1;
1276 }
1277
1278 if (!*args[4]) {
1279 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1280 si->applet.st0 = STAT_CLI_PRINT;
1281 return 1;
1282 }
1283
1284 v = atoi(args[4]);
1285 if (v < 0) {
1286 si->applet.ctx.cli.msg = "Value out of range.\n";
1287 si->applet.st0 = STAT_CLI_PRINT;
1288 return 1;
1289 }
1290
1291 global.cps_lim = v;
1292
1293 /* Dequeues all of the listeners waiting for a resource */
1294 if (!LIST_ISEMPTY(&global_listener_queue))
1295 dequeue_all_listeners(&global_listener_queue);
1296
1297 return 1;
1298 }
1299 else {
1300 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1301 si->applet.st0 = STAT_CLI_PRINT;
1302 return 1;
1303 }
1304 }
William Lallemandd85f9172012-11-09 17:05:39 +01001305 else if (strcmp(args[2], "http-compression") == 0) {
1306 if (strcmp(args[3], "global") == 0) {
1307 int v;
1308
Willy Tarreau85d47f92012-11-21 00:29:50 +01001309 if (!*args[4]) {
1310 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1311 si->applet.st0 = STAT_CLI_PRINT;
1312 return 1;
1313 }
1314
William Lallemandd85f9172012-11-09 17:05:39 +01001315 v = atoi(args[4]);
1316 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1317 }
1318 else {
1319 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1320 si->applet.st0 = STAT_CLI_PRINT;
1321 return 1;
1322 }
1323 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001324 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001325 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001326 si->applet.st0 = STAT_CLI_PRINT;
1327 return 1;
1328 }
1329 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001330 else if (strcmp(args[1], "table") == 0) {
1331 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1332 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001333 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001334 return 0;
1335 }
1336 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001337 else if (strcmp(args[0], "enable") == 0) {
1338 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001339 struct server *sv;
1340
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001341 sv = expect_server_admin(s, si, args[2]);
1342 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001343 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001344
Cyril Bontécd19e512010-01-31 22:34:03 +01001345 if (sv->state & SRV_MAINTAIN) {
1346 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001347 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001348 /* If this server tracks the status of another one,
1349 * we must restore the good status.
1350 */
Willy Tarreau44267702011-10-28 15:35:33 +02001351 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001352 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001353 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001354 } else {
1355 sv->state &= ~SRV_MAINTAIN;
1356 set_server_down(sv);
1357 }
1358 } else {
1359 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001360 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001361 }
1362 }
1363
Willy Tarreau532a4502011-09-07 22:37:44 +02001364 return 1;
1365 }
1366 else if (strcmp(args[1], "frontend") == 0) {
1367 struct proxy *px;
1368
1369 px = expect_frontend_admin(s, si, args[2]);
1370 if (!px)
1371 return 1;
1372
1373 if (px->state == PR_STSTOPPED) {
1374 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1375 si->applet.st0 = STAT_CLI_PRINT;
1376 return 1;
1377 }
1378
1379 if (px->state != PR_STPAUSED) {
1380 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1381 si->applet.st0 = STAT_CLI_PRINT;
1382 return 1;
1383 }
1384
1385 if (!resume_proxy(px)) {
1386 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1387 si->applet.st0 = STAT_CLI_PRINT;
1388 return 1;
1389 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001390 return 1;
1391 }
1392 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001393 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1394 si->applet.st0 = STAT_CLI_PRINT;
1395 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001396 }
1397 }
1398 else if (strcmp(args[0], "disable") == 0) {
1399 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001400 struct server *sv;
1401
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001402 sv = expect_server_admin(s, si, args[2]);
1403 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001404 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001405
Cyril Bontécd19e512010-01-31 22:34:03 +01001406 if (! (sv->state & SRV_MAINTAIN)) {
1407 /* Not already in maintenance, we can change the server state */
1408 sv->state |= SRV_MAINTAIN;
1409 set_server_down(sv);
1410 }
1411
Willy Tarreau532a4502011-09-07 22:37:44 +02001412 return 1;
1413 }
1414 else if (strcmp(args[1], "frontend") == 0) {
1415 struct proxy *px;
1416
1417 px = expect_frontend_admin(s, si, args[2]);
1418 if (!px)
1419 return 1;
1420
1421 if (px->state == PR_STSTOPPED) {
1422 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1423 si->applet.st0 = STAT_CLI_PRINT;
1424 return 1;
1425 }
1426
1427 if (px->state == PR_STPAUSED) {
1428 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1429 si->applet.st0 = STAT_CLI_PRINT;
1430 return 1;
1431 }
1432
1433 if (!pause_proxy(px)) {
1434 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1435 si->applet.st0 = STAT_CLI_PRINT;
1436 return 1;
1437 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001438 return 1;
1439 }
1440 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001441 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1442 si->applet.st0 = STAT_CLI_PRINT;
1443 return 1;
1444 }
1445 }
1446 else if (strcmp(args[0], "shutdown") == 0) {
1447 if (strcmp(args[1], "frontend") == 0) {
1448 struct proxy *px;
1449
1450 px = expect_frontend_admin(s, si, args[2]);
1451 if (!px)
1452 return 1;
1453
1454 if (px->state == PR_STSTOPPED) {
1455 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1456 si->applet.st0 = STAT_CLI_PRINT;
1457 return 1;
1458 }
1459
1460 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1461 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1462 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1463 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1464 stop_proxy(px);
1465 return 1;
1466 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001467 else if (strcmp(args[1], "session") == 0) {
1468 struct session *sess, *ptr;
1469
Willy Tarreau290e63a2012-09-20 18:07:14 +02001470 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001471 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1472 si->applet.st0 = STAT_CLI_PRINT;
1473 return 1;
1474 }
1475
1476 if (!*args[2]) {
1477 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1478 si->applet.st0 = STAT_CLI_PRINT;
1479 return 1;
1480 }
1481
1482 ptr = (void *)strtoul(args[2], NULL, 0);
1483
1484 /* first, look for the requested session in the session table */
1485 list_for_each_entry(sess, &sessions, list) {
1486 if (sess == ptr)
1487 break;
1488 }
1489
1490 /* do we have the session ? */
1491 if (sess != ptr) {
1492 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1493 si->applet.st0 = STAT_CLI_PRINT;
1494 return 1;
1495 }
1496
1497 session_shutdown(sess, SN_ERR_KILLED);
1498 return 1;
1499 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001500 else if (strcmp(args[1], "sessions") == 0) {
1501 if (strcmp(args[2], "server") == 0) {
1502 struct server *sv;
1503 struct session *sess, *sess_bck;
1504
1505 sv = expect_server_admin(s, si, args[3]);
1506 if (!sv)
1507 return 1;
1508
1509 /* kill all the session that are on this server */
1510 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1511 if (sess->srv_conn == sv)
1512 session_shutdown(sess, SN_ERR_KILLED);
1513
1514 return 1;
1515 }
1516 else {
1517 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1518 si->applet.st0 = STAT_CLI_PRINT;
1519 return 1;
1520 }
1521 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001522 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001523 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001524 si->applet.st0 = STAT_CLI_PRINT;
1525 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001526 }
1527 }
1528 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001529 return 0;
1530 }
1531 return 1;
1532}
1533
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001534/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001535 * used to processes I/O from/to the stats unix socket. The system relies on a
1536 * state machine handling requests and various responses. We read a request,
1537 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001538 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1539 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001540 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001541 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001542static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001543{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001544 struct channel *req = si->ob;
1545 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546 int reql;
1547 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001548
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001549 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1550 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001551
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001552 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001553 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001554 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001555 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001557 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001558 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001559 /* Let's close for real now. We just close the request
1560 * side, the conditions below will complete if needed.
1561 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001562 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001563 break;
1564 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001565 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001566 /* ensure we have some output room left in the event we
1567 * would want to return some info right after parsing.
1568 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001569 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001570 break;
1571
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001572 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001573 if (reql <= 0) { /* closed or EOL not found */
1574 if (reql == 0)
1575 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001576 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001577 continue;
1578 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001579
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001580 /* seek for a possible semi-colon. If we find one, we
1581 * replace it with an LF and skip only this part.
1582 */
1583 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001584 if (trash.str[len] == ';') {
1585 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001586 reql = len + 1;
1587 break;
1588 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001589
Willy Tarreau816fc222009-10-04 07:36:58 +02001590 /* now it is time to check that we have a full line,
1591 * remove the trailing \n and possibly \r, then cut the
1592 * line.
1593 */
1594 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001595 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001596 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001598 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001599
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001600 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001601 len--;
1602
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001603 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001604
Willy Tarreaubc4af052011-02-13 13:25:14 +01001605 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001606 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001607 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001608 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609 continue;
1610 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001611 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001612 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001613 else if (strcmp(trash.str, "help") == 0 ||
1614 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001615 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001616 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001617 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001618 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001619 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001620 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001622 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001623 /* if prompt is disabled, print help on empty lines,
1624 * so that the user at least knows how to enable
1625 * prompt and find help.
1626 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001627 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001628 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001629 }
1630
1631 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001632 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001633 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001634 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001635 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001636 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001637 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001638 continue;
1639 }
1640
Willy Tarreaubc4af052011-02-13 13:25:14 +01001641 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001642 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001643 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001644 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001645 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001646 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001647 if (stats_dump_info_to_buffer(si))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001648 si->applet.st0 = STAT_CLI_PROMPT;
1649 break;
1650 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001651 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001652 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001653 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001654 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001655 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001656 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001657 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001658 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001659 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001660 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001661 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001662 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001663 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001664 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001665 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001666 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001667 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001668 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001669 break;
1670 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001671
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001672 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001673 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001674 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001675 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001676 }
1677
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001678 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001679 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001680 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001681
1682 /* Now we close the output if one of the writers did so,
1683 * or if we're not in interactive mode and the request
1684 * buffer is empty. This still allows pipelined requests
1685 * to be sent in non-interactive mode.
1686 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001687 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001688 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001689 continue;
1690 }
1691
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001692 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001693 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001694 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001695 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001696
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001697 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001698 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1699 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001700 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001701 * and nothing more to consume. This is comparable to a broken pipe, so
1702 * we forward the close to the request side so that it flows upstream to
1703 * the client.
1704 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001705 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001706 }
1707
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001708 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001709 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1710 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1711 /* We have no more processing to do, and nothing more to send, and
1712 * the client side has closed. So we'll forward this state downstream
1713 * on the response buffer.
1714 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001715 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001716 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001717 }
1718
1719 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001720 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001721
1722 /* we don't want to expire timeouts while we're processing requests */
1723 si->ib->rex = TICK_ETERNITY;
1724 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001725
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001726 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001727 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 +02001728 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001729 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 +02001730
1731 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1732 /* check that we have released everything then unregister */
1733 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001734 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001735}
1736
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001737/* This function dumps information onto the stream interface's read buffer.
1738 * It returns 0 as long as it does not complete, non-zero upon completion.
1739 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001740 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001741static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001742{
1743 unsigned int up = (now.tv_sec - start_date.tv_sec);
1744
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001745 chunk_printf(&trash,
1746 "Name: " PRODUCT_NAME "\n"
1747 "Version: " HAPROXY_VERSION "\n"
1748 "Release_date: " HAPROXY_DATE "\n"
1749 "Nbproc: %d\n"
1750 "Process_num: %d\n"
1751 "Pid: %d\n"
1752 "Uptime: %dd %dh%02dm%02ds\n"
1753 "Uptime_sec: %d\n"
1754 "Memmax_MB: %d\n"
1755 "Ulimit-n: %d\n"
1756 "Maxsock: %d\n"
1757 "Maxconn: %d\n"
1758 "Hard_maxconn: %d\n"
1759 "Maxpipes: %d\n"
1760 "CurrConns: %d\n"
1761 "PipesUsed: %d\n"
1762 "PipesFree: %d\n"
1763 "ConnRate: %d\n"
1764 "ConnRateLimit: %d\n"
1765 "MaxConnRate: %d\n"
1766 "CompressBpsIn: %u\n"
1767 "CompressBpsOut: %u\n"
1768 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001769#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001770 "ZlibMemUsage: %ld\n"
1771 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001772#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001773 "Tasks: %d\n"
1774 "Run_queue: %d\n"
1775 "Idle_pct: %d\n"
1776 "node: %s\n"
1777 "description: %s\n"
1778 "",
1779 global.nbproc,
1780 relative_pid,
1781 pid,
1782 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1783 up,
1784 global.rlimit_memmax,
1785 global.rlimit_nofile,
1786 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1787 actconn, pipes_used, pipes_free,
1788 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1789 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1790 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001791#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001792 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001793#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001794 nb_tasks_cur, run_queue_cur, idle_pct,
1795 global.node, global.desc ? global.desc : ""
1796 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001797
1798 if (bi_putchk(si->ib, &trash) == -1)
1799 return 0;
1800
1801 return 1;
1802}
1803
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001804/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1805 * the state from stream interface <si>. The caller is responsible for clearing
1806 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001807 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001808static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001809{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001810 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001811
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001812 if (!(px->cap & PR_CAP_FE))
1813 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001814
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001815 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1816 return 0;
1817
1818 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001819 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001820 /* name, queue */
1821 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001823 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1824 /* Column sub-heading for Enable or Disable server */
1825 chunk_appendf(&trash, "<td></td>");
1826 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001827
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001828 chunk_appendf(&trash,
1829 "<td class=ac>"
1830 "<a name=\"%s/Frontend\"></a>"
1831 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1832 "<td colspan=3></td>"
1833 "",
1834 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001835
Willy Tarreau466c9b52012-12-23 02:25:03 +01001836 chunk_appendf(&trash,
1837 /* sessions rate : current */
1838 "<td><u>%s<div><table class=det>"
1839 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1840 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1841 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001842 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1843 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1844 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001845
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001846 if (px->mode == PR_MODE_HTTP)
1847 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001848 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001849 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001850
1851 chunk_appendf(&trash,
1852 "</table></div></u></td>"
1853 /* sessions rate : max */
1854 "<td><u>%s<div><table class=det>"
1855 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1856 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1857 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001858 U2H(px->fe_counters.sps_max),
1859 U2H(px->fe_counters.cps_max),
1860 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001861
1862 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001863 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001864 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001865 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001866
1867 chunk_appendf(&trash,
1868 "</table></div></u></td>"
1869 /* sessions rate : limit */
1870 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001871 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001872
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001873 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001874 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001875 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001876 "<td><u>%s<div><table class=det>"
1877 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1878 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001879 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001880 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1881 U2H(px->fe_counters.cum_sess),
1882 U2H(px->fe_counters.cum_conn),
1883 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001884
Willy Tarreau466c9b52012-12-23 02:25:03 +01001885 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001886 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001887 chunk_appendf(&trash,
1888 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1889 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1890 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1891 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1892 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1893 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1894 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1895 "<tr><th>- other responses:</th><td>%s</td></tr>"
1896 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1897 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001898 U2H(px->fe_counters.p.http.cum_req),
1899 U2H(px->fe_counters.p.http.rsp[1]),
1900 U2H(px->fe_counters.p.http.rsp[2]),
1901 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001902 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001903 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001904 U2H(px->fe_counters.p.http.rsp[3]),
1905 U2H(px->fe_counters.p.http.rsp[4]),
1906 U2H(px->fe_counters.p.http.rsp[5]),
1907 U2H(px->fe_counters.p.http.rsp[0]),
1908 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001909 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001910
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001911 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001912 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001913 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001914 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001915 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001916 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001917 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001918 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001919
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001920 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001921 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1922 "<td>%s%s<div>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001923 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001924 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001925 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1926 px->fe_counters.comp_in ?
1927 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001928 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001929
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001930 chunk_appendf(&trash,
1931 /* denied: req, resp */
1932 "<td>%s</td><td>%s</td>"
1933 /* errors : request, connect, response */
1934 "<td>%s</td><td></td><td></td>"
1935 /* warnings: retries, redispatches */
1936 "<td></td><td></td>"
1937 /* server status : reflect frontend status */
1938 "<td class=ac>%s</td>"
1939 /* rest of server: nothing */
1940 "<td class=ac colspan=8></td></tr>"
1941 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001942 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1943 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001944 px->state == PR_STREADY ? "OPEN" :
1945 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001946 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001947 else { /* CSV mode */
1948 chunk_appendf(&trash,
1949 /* pxid, name, queue cur, queue max, */
1950 "%s,FRONTEND,,,"
1951 /* sessions : current, max, limit, total */
1952 "%d,%d,%d,%lld,"
1953 /* bytes : in, out */
1954 "%lld,%lld,"
1955 /* denied: req, resp */
1956 "%lld,%lld,"
1957 /* errors : request, connect, response */
1958 "%lld,,,"
1959 /* warnings: retries, redispatches */
1960 ",,"
1961 /* server status : reflect frontend status */
1962 "%s,"
1963 /* rest of server: nothing */
1964 ",,,,,,,,"
1965 /* pid, iid, sid, throttle, lbtot, tracked, type */
1966 "%d,%d,0,,,,%d,"
1967 /* rate, rate_lim, rate_max */
1968 "%u,%u,%u,"
1969 /* check_status, check_code, check_duration */
1970 ",,,",
1971 px->id,
1972 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1973 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1974 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1975 px->fe_counters.failed_req,
1976 px->state == PR_STREADY ? "OPEN" :
1977 px->state == PR_STFULL ? "FULL" : "STOP",
1978 relative_pid, px->uuid, STATS_TYPE_FE,
1979 read_freq_ctr(&px->fe_sess_per_sec),
1980 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001981
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001982 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1983 if (px->mode == PR_MODE_HTTP) {
1984 for (i=1; i<6; i++)
1985 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1986 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1987 }
1988 else
1989 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001990
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001991 /* failed health analyses */
1992 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001993
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001994 /* requests : req_rate, req_rate_max, req_tot, */
1995 chunk_appendf(&trash, "%u,%u,%lld,",
1996 read_freq_ctr(&px->fe_req_per_sec),
1997 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1998
1999 /* errors: cli_aborts, srv_aborts */
2000 chunk_appendf(&trash, ",,");
2001
2002 /* compression: in, out, bypassed */
2003 chunk_appendf(&trash, "%lld,%lld,%lld,",
2004 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2005
2006 /* compression: comp_rsp */
2007 chunk_appendf(&trash, "%lld,",
2008 px->fe_counters.p.http.comp_rsp);
2009
2010 /* finish with EOL */
2011 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002012 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002013 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002014}
2015
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002016/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2017 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2018 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2019 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002020 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002021static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002022{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002023 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2024 chunk_appendf(&trash, "<tr class=socket>");
2025 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2026 /* Column sub-heading for Enable or Disable server */
2027 chunk_appendf(&trash, "<td></td>");
2028 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002029 chunk_appendf(&trash,
2030 /* frontend name, listener name */
2031 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2032 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2033 "",
2034 px->id, l->name,
2035 (flags & ST_SHLGNDS)?"<u>":"",
2036 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002037
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002038 if (flags & ST_SHLGNDS) {
2039 char str[INET6_ADDRSTRLEN];
2040 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002041
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002042 chunk_appendf(&trash, "<div>");
Willy Tarreau91861262007-10-17 17:06:05 +02002043
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002044 port = get_host_port(&l->addr);
2045 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2046 case AF_INET:
2047 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2048 break;
2049 case AF_INET6:
2050 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2051 break;
2052 case AF_UNIX:
2053 chunk_appendf(&trash, "unix, ");
2054 break;
2055 case -1:
2056 chunk_appendf(&trash, "(%s), ", strerror(errno));
2057 break;
2058 }
Willy Tarreau91861262007-10-17 17:06:05 +02002059
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002060 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002061 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002062 }
Willy Tarreau91861262007-10-17 17:06:05 +02002063
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002064 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002065 /* queue */
2066 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002067 /* sessions rate: current, max, limit */
2068 "<td colspan=3>&nbsp;</td>"
2069 /* sessions: current, max, limit, total, lbtot */
2070 "<td>%s</td><td>%s</td><td>%s</td>"
2071 "<td>%s</td><td>&nbsp;</td>"
2072 /* bytes: in, out */
2073 "<td>%s</td><td>%s</td>"
2074 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002075 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002076 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2077 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002078
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002079 chunk_appendf(&trash,
2080 /* denied: req, resp */
2081 "<td>%s</td><td>%s</td>"
2082 /* errors: request, connect, response */
2083 "<td>%s</td><td></td><td></td>"
2084 /* warnings: retries, redispatches */
2085 "<td></td><td></td>"
2086 /* server status: reflect listener status */
2087 "<td class=ac>%s</td>"
2088 /* rest of server: nothing */
2089 "<td class=ac colspan=8></td></tr>"
2090 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002091 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2092 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002093 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2094 }
2095 else { /* CSV mode */
2096 chunk_appendf(&trash,
2097 /* pxid, name, queue cur, queue max, */
2098 "%s,%s,,,"
2099 /* sessions: current, max, limit, total */
2100 "%d,%d,%d,%lld,"
2101 /* bytes: in, out */
2102 "%lld,%lld,"
2103 /* denied: req, resp */
2104 "%lld,%lld,"
2105 /* errors: request, connect, response */
2106 "%lld,,,"
2107 /* warnings: retries, redispatches */
2108 ",,"
2109 /* server status: reflect listener status */
2110 "%s,"
2111 /* rest of server: nothing */
2112 ",,,,,,,,"
2113 /* pid, iid, sid, throttle, lbtot, tracked, type */
2114 "%d,%d,%d,,,,%d,"
2115 /* rate, rate_lim, rate_max */
2116 ",,,"
2117 /* check_status, check_code, check_duration */
2118 ",,,"
2119 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2120 ",,,,,,"
2121 /* failed health analyses */
2122 ","
2123 /* requests : req_rate, req_rate_max, req_tot, */
2124 ",,,"
2125 /* errors: cli_aborts, srv_aborts */
2126 ",,"
2127 /* compression: in, out, bypassed, comp_rsp */
2128 ",,,,"
2129 "\n",
2130 px->id, l->name,
2131 l->nbconn, l->counters->conn_max,
2132 l->maxconn, l->counters->cum_conn,
2133 l->counters->bytes_in, l->counters->bytes_out,
2134 l->counters->denied_req, l->counters->denied_resp,
2135 l->counters->failed_req,
2136 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2137 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2138 }
2139 return 1;
2140}
Willy Tarreau91861262007-10-17 17:06:05 +02002141
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002142/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2143 * from stream interface <si>, stats flags <flags>, and server state <state>.
2144 * The caller is responsible for clearing the trash if needed. Returns non-zero
2145 * if it emits anything, zero otherwise. The <state> parameter can take the
2146 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked.
2147 */
2148static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2149{
2150 struct server *ref = sv->track ? sv->track : sv;
2151 char str[INET6_ADDRSTRLEN];
2152 struct chunk src;
2153 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002154
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002155 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) { /* HTML mode */
2156 static char *srv_hlt_st[7] = {
2157 "DOWN",
2158 "DN %d/%d &uarr;",
2159 "UP %d/%d &darr;",
2160 "UP",
2161 "NOLB %d/%d &darr;",
2162 "NOLB",
2163 "<i>no check</i>"
2164 };
Willy Tarreau91861262007-10-17 17:06:05 +02002165
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002166 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2167 chunk_appendf(&trash, "<tr class=\"maintain\">");
2168 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002169 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002170 "<tr class=\"%s%d\">",
2171 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002172
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002173 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002174 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002175 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2176 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002177
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002178 chunk_appendf(&trash,
2179 "<td class=ac><a name=\"%s/%s\"></a>%s"
2180 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2181 "",
2182 px->id, sv->id,
2183 (flags & ST_SHLGNDS) ? "<u>" : "",
2184 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002185
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002186 if (flags & ST_SHLGNDS) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002187 chunk_appendf(&trash, "<div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002188
2189 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2190 case AF_INET:
2191 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2192 break;
2193 case AF_INET6:
2194 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2195 break;
2196 case AF_UNIX:
2197 chunk_appendf(&trash, "unix, ");
2198 break;
2199 case -1:
2200 chunk_appendf(&trash, "(%s), ", strerror(errno));
2201 break;
2202 default: /* address family not supported */
2203 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002204 }
Willy Tarreau91861262007-10-17 17:06:05 +02002205
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002206 /* id */
2207 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002208
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002209 /* cookie */
2210 if (sv->cookie) {
2211 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002212
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002213 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2214 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002215
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002216 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002217 }
2218
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002219 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002220 }
Willy Tarreau91861262007-10-17 17:06:05 +02002221
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002222 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002223 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002224 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002225 /* sessions rate : current, max, limit */
2226 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002227 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002228 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002229 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2230 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002231
Willy Tarreau466c9b52012-12-23 02:25:03 +01002232
2233 chunk_appendf(&trash,
2234 /* sessions: current, max, limit, total */
2235 "<td>%s</td><td>%s</td><td>%s</td>"
2236 "<td><u>%s<div><table class=det>"
2237 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2238 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002239 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2240 U2H(sv->counters.cum_sess),
2241 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002242
Willy Tarreau466c9b52012-12-23 02:25:03 +01002243 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2244 if (px->mode == PR_MODE_HTTP) {
2245 unsigned long long tot;
2246 for (tot = i = 0; i < 6; i++)
2247 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002248
Willy Tarreau466c9b52012-12-23 02:25:03 +01002249 chunk_appendf(&trash,
2250 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2251 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2252 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2253 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2254 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2255 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2256 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2257 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002258 U2H(tot),
2259 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2260 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2261 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2262 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2263 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2264 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02002265 }
Willy Tarreau91861262007-10-17 17:06:05 +02002266
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002267 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002268 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002269 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002270 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002271 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002272
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002273 chunk_appendf(&trash,
2274 /* bytes : in, out */
2275 "<td>%s</td><td>%s</td>"
2276 /* denied: req, resp */
2277 "<td></td><td>%s</td>"
2278 /* errors : request, connect */
2279 "<td></td><td>%s</td>"
2280 /* errors : response */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002281 "<td><u>%s<div>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002282 /* warnings: retries, redispatches */
2283 "<td>%lld</td><td>%lld</td>"
2284 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002285 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2286 U2H(sv->counters.failed_secu),
2287 U2H(sv->counters.failed_conns),
2288 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002289 sv->counters.cli_aborts,
2290 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002291 sv->counters.retries, sv->counters.redispatches);
2292
2293 /* status, lest check */
2294 chunk_appendf(&trash, "<td class=ac>");
2295
2296 if (sv->state & SRV_MAINTAIN) {
2297 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2298 chunk_appendf(&trash, "MAINT");
2299 }
2300 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2301 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2302 chunk_appendf(&trash, "MAINT(via)");
2303 }
2304 else if (ref->state & SRV_CHECKED) {
2305 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2306 chunk_appendf(&trash,
2307 srv_hlt_st[state],
2308 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2309 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002310 }
Willy Tarreau91861262007-10-17 17:06:05 +02002311
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002312 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002313 chunk_appendf(&trash,
2314 "</td><td class=ac><u> %s%s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002315 (sv->state & SRV_CHK_RUNNING) ? "* " : "",
2316 get_check_status_info(sv->check.status));
2317
2318 if (sv->check.status >= HCHK_STATUS_L57DATA)
2319 chunk_appendf(&trash, "/%d", sv->check.code);
2320
2321 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002322 chunk_appendf(&trash, " in %lums", sv->check.duration);
2323
2324 chunk_appendf(&trash, "<div>%s",
2325 get_check_status_description(sv->check.status));
2326 if (*sv->check.desc) {
2327 chunk_appendf(&trash, ": ");
2328 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2329 chunk_htmlencode(&trash, &src);
2330 }
2331 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002332 }
2333 else
2334 chunk_appendf(&trash, "</td><td>");
2335
2336 chunk_appendf(&trash,
2337 /* weight */
2338 "</td><td class=ac>%d</td>"
2339 /* act, bck */
2340 "<td class=ac>%s</td><td class=ac>%s</td>"
2341 "",
2342 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2343 (sv->state & SRV_BACKUP) ? "-" : "Y",
2344 (sv->state & SRV_BACKUP) ? "Y" : "-");
2345
2346 /* check failures: unique, fatal, down time */
2347 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002348 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002349
2350 if (ref->observe)
2351 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2352
2353 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002354 "<div>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002355 "<td>%lld</td><td>%s</td>"
2356 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002357 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002358 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2359 }
2360 else if (sv != ref)
2361 chunk_appendf(&trash,
2362 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2363 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2364 else
2365 chunk_appendf(&trash, "<td colspan=3></td>");
2366
2367 /* throttle */
2368 if ((sv->state & SRV_WARMINGUP) &&
2369 now.tv_sec < sv->last_change + sv->slowstart &&
2370 now.tv_sec >= sv->last_change) {
2371 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n",
2372 (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2373 }
2374 else
2375 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2376 }
2377 else { /* CSV mode */
2378 static char *srv_hlt_st[7] = {
2379 "DOWN,",
2380 "DOWN %d/%d,",
2381 "UP %d/%d,",
2382 "UP,",
2383 "NOLB %d/%d,",
2384 "NOLB,",
2385 "no check,"
2386 };
2387
2388 chunk_appendf(&trash,
2389 /* pxid, name */
2390 "%s,%s,"
2391 /* queue : current, max */
2392 "%d,%d,"
2393 /* sessions : current, max, limit, total */
2394 "%d,%d,%s,%lld,"
2395 /* bytes : in, out */
2396 "%lld,%lld,"
2397 /* denied: req, resp */
2398 ",%lld,"
2399 /* errors : request, connect, response */
2400 ",%lld,%lld,"
2401 /* warnings: retries, redispatches */
2402 "%lld,%lld,"
2403 "",
2404 px->id, sv->id,
2405 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002406 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002407 sv->counters.bytes_in, sv->counters.bytes_out,
2408 sv->counters.failed_secu,
2409 sv->counters.failed_conns, sv->counters.failed_resp,
2410 sv->counters.retries, sv->counters.redispatches);
2411
2412 /* status */
2413 if (sv->state & SRV_MAINTAIN)
2414 chunk_appendf(&trash, "MAINT,");
2415 else if (ref != sv && ref->state & SRV_MAINTAIN)
2416 chunk_appendf(&trash, "MAINT(via),");
2417 else
2418 chunk_appendf(&trash,
2419 srv_hlt_st[state],
2420 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2421 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
2422
2423 chunk_appendf(&trash,
2424 /* weight, active, backup */
2425 "%d,%d,%d,"
2426 "",
2427 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2428 (sv->state & SRV_BACKUP) ? 0 : 1,
2429 (sv->state & SRV_BACKUP) ? 1 : 0);
2430
2431 /* check failures: unique, fatal; last change, total downtime */
2432 if (sv->state & SRV_CHECKED)
2433 chunk_appendf(&trash,
2434 "%lld,%lld,%d,%d,",
2435 sv->counters.failed_checks, sv->counters.down_trans,
2436 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2437 else
2438 chunk_appendf(&trash, ",,,,");
2439
2440 /* queue limit, pid, iid, sid, */
2441 chunk_appendf(&trash,
2442 "%s,"
2443 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002444 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002445 relative_pid, px->uuid, sv->puid);
2446
2447 /* throttle */
2448 if ((sv->state & SRV_WARMINGUP) &&
2449 now.tv_sec < sv->last_change + sv->slowstart &&
2450 now.tv_sec >= sv->last_change)
2451 chunk_appendf(&trash, "%d", (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2452
2453 /* sessions: lbtot */
2454 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2455
2456 /* tracked */
2457 if (sv->track)
2458 chunk_appendf(&trash, "%s/%s,",
2459 sv->track->proxy->id, sv->track->id);
2460 else
2461 chunk_appendf(&trash, ",");
2462
2463 /* type */
2464 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2465
2466 /* rate */
2467 chunk_appendf(&trash, "%u,,%u,",
2468 read_freq_ctr(&sv->sess_per_sec),
2469 sv->counters.sps_max);
2470
2471 if (sv->state & SRV_CHECKED) {
2472 /* check_status */
2473 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2474
2475 /* check_code */
2476 if (sv->check.status >= HCHK_STATUS_L57DATA)
2477 chunk_appendf(&trash, "%u,", sv->check.code);
2478 else
2479 chunk_appendf(&trash, ",");
2480
2481 /* check_duration */
2482 if (sv->check.status >= HCHK_STATUS_CHECKED)
2483 chunk_appendf(&trash, "%lu,", sv->check.duration);
2484 else
2485 chunk_appendf(&trash, ",");
2486
2487 }
2488 else
2489 chunk_appendf(&trash, ",,,");
2490
2491 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2492 if (px->mode == PR_MODE_HTTP) {
2493 for (i=1; i<6; i++)
2494 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2495
2496 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2497 }
2498 else
2499 chunk_appendf(&trash, ",,,,,,");
2500
2501 /* failed health analyses */
2502 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2503
2504 /* requests : req_rate, req_rate_max, req_tot, */
2505 chunk_appendf(&trash, ",,,");
2506
2507 /* errors: cli_aborts, srv_aborts */
2508 chunk_appendf(&trash, "%lld,%lld,",
2509 sv->counters.cli_aborts, sv->counters.srv_aborts);
2510
2511 /* compression: in, out, bypassed, comp_rsp */
2512 chunk_appendf(&trash, ",,,,");
2513
2514 /* finish with EOL */
2515 chunk_appendf(&trash, "\n");
2516 }
2517 return 1;
2518}
2519
2520/* Dumps a line for backend <px> to the trash for and uses the state from stream
2521 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2522 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2523 */
2524static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2525{
2526 struct chunk src;
2527 int i;
2528
2529 if (!(px->cap & PR_CAP_BE))
2530 return 0;
2531
2532 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2533 return 0;
2534
2535 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) { /* HTML mode */
2536 chunk_appendf(&trash, "<tr class=\"backend\">");
2537 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2538 /* Column sub-heading for Enable or Disable server */
2539 chunk_appendf(&trash, "<td></td>");
2540 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002541 chunk_appendf(&trash,
2542 "<td class=ac>"
2543 /* name */
2544 "%s<a name=\"%s/Backend\"></a>"
2545 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2546 "",
2547 (flags & ST_SHLGNDS)?"<u>":"",
2548 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002549
2550 if (flags & ST_SHLGNDS) {
2551 /* balancing */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002552 chunk_appendf(&trash, "<div>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002553 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2554
2555 /* cookie */
2556 if (px->cookie_name) {
2557 chunk_appendf(&trash, ", cookie: '");
2558 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2559 chunk_htmlencode(&trash, &src);
2560 chunk_appendf(&trash, "'");
2561 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002562 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 }
2564
2565 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002566 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002567 /* queue : current, max */
2568 "<td>%s</td><td>%s</td><td></td>"
2569 /* sessions rate : current, max, limit */
2570 "<td>%s</td><td>%s</td><td></td>"
2571 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002573 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2574 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575
2576 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002577 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002579 "<td><u>%s<div><table class=det>"
2580 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002581 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002582 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2583 U2H(px->be_counters.cum_conn),
2584 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002585
Willy Tarreau466c9b52012-12-23 02:25:03 +01002586 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002587 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002588 chunk_appendf(&trash,
2589 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2590 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2591 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2592 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2593 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2594 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2595 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2596 "<tr><th>- other responses:</th><td>%s</td></tr>"
2597 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2598 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002599 U2H(px->be_counters.p.http.cum_req),
2600 U2H(px->be_counters.p.http.rsp[1]),
2601 U2H(px->be_counters.p.http.rsp[2]),
2602 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002603 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002604 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002605 U2H(px->be_counters.p.http.rsp[3]),
2606 U2H(px->be_counters.p.http.rsp[4]),
2607 U2H(px->be_counters.p.http.rsp[5]),
2608 U2H(px->be_counters.p.http.rsp[0]),
2609 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610 }
2611
2612 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002613 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002614 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002615 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002617 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002618 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002619 U2H(px->be_counters.cum_lbconn),
2620 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002621
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002622 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002623 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
2624 "<td>%s%s<div>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002625 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002626 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002627 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2628 px->be_counters.comp_in ?
2629 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002630 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2631
2632 chunk_appendf(&trash,
2633 /* denied: req, resp */
2634 "<td>%s</td><td>%s</td>"
2635 /* errors : request, connect */
2636 "<td></td><td>%s</td>"
2637 /* errors : response */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002638 "<td><u>%s<div>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002639 /* warnings: retries, redispatches */
2640 "<td>%lld</td><td>%lld</td>"
2641 /* backend status: reflect backend status (up/down): we display UP
2642 * if the backend has known working servers or if it has no server at
2643 * all (eg: for stats). Then we display the total weight, number of
2644 * active and backups. */
2645 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2646 "<td class=ac>%d</td><td class=ac>%d</td>"
2647 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002648 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2649 U2H(px->be_counters.failed_conns),
2650 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 px->be_counters.cli_aborts,
2652 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002653 px->be_counters.retries, px->be_counters.redispatches,
2654 human_time(now.tv_sec - px->last_change, 1),
2655 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2656 "<font color=\"red\"><b>DOWN</b></font>",
2657 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2658 px->srv_act, px->srv_bck);
2659
2660 chunk_appendf(&trash,
2661 /* rest of backend: nothing, down transitions, total downtime, throttle */
2662 "<td class=ac>&nbsp;</td><td>%d</td>"
2663 "<td>%s</td>"
2664 "<td></td>"
2665 "</tr>",
2666 px->down_trans,
2667 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2668 }
2669 else { /* CSV mode */
2670 chunk_appendf(&trash,
2671 /* pxid, name */
2672 "%s,BACKEND,"
2673 /* queue : current, max */
2674 "%d,%d,"
2675 /* sessions : current, max, limit, total */
2676 "%d,%d,%d,%lld,"
2677 /* bytes : in, out */
2678 "%lld,%lld,"
2679 /* denied: req, resp */
2680 "%lld,%lld,"
2681 /* errors : request, connect, response */
2682 ",%lld,%lld,"
2683 /* warnings: retries, redispatches */
2684 "%lld,%lld,"
2685 /* backend status: reflect backend status (up/down): we display UP
2686 * if the backend has known working servers or if it has no server at
2687 * all (eg: for stats). Then we display the total weight, number of
2688 * active and backups. */
2689 "%s,"
2690 "%d,%d,%d,"
2691 /* rest of backend: nothing, down transitions, last change, total downtime */
2692 ",%d,%d,%d,,"
2693 /* pid, iid, sid, throttle, lbtot, tracked, type */
2694 "%d,%d,0,,%lld,,%d,"
2695 /* rate, rate_lim, rate_max, */
2696 "%u,,%u,"
2697 /* check_status, check_code, check_duration */
2698 ",,,",
2699 px->id,
2700 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2701 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2702 px->be_counters.bytes_in, px->be_counters.bytes_out,
2703 px->be_counters.denied_req, px->be_counters.denied_resp,
2704 px->be_counters.failed_conns, px->be_counters.failed_resp,
2705 px->be_counters.retries, px->be_counters.redispatches,
2706 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2707 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2708 px->srv_act, px->srv_bck,
2709 px->down_trans, (int)(now.tv_sec - px->last_change),
2710 px->srv?be_downtime(px):0,
2711 relative_pid, px->uuid,
2712 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2713 read_freq_ctr(&px->be_sess_per_sec),
2714 px->be_counters.sps_max);
2715
2716 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2717 if (px->mode == PR_MODE_HTTP) {
2718 for (i=1; i<6; i++)
2719 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2720 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2721 }
2722 else
2723 chunk_appendf(&trash, ",,,,,,");
2724
2725 /* failed health analyses */
2726 chunk_appendf(&trash, ",");
2727
2728 /* requests : req_rate, req_rate_max, req_tot, */
2729 chunk_appendf(&trash, ",,,");
2730
2731 /* errors: cli_aborts, srv_aborts */
2732 chunk_appendf(&trash, "%lld,%lld,",
2733 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2734
2735 /* compression: in, out, bypassed */
2736 chunk_appendf(&trash, "%lld,%lld,%lld,",
2737 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2738
2739 /* compression: comp_rsp */
2740 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2741
2742 /* finish with EOL */
2743 chunk_appendf(&trash, "\n");
2744 }
2745 return 1;
2746}
2747
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002748/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002750 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002751 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002752static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002754 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2755 /* A form to enable/disable this proxy servers */
2756 chunk_appendf(&trash,
2757 "<form action=\"%s\" method=\"post\">",
2758 uri->uri_prefix);
2759 }
2760
2761 /* print a new table */
2762 chunk_appendf(&trash,
2763 "<table class=\"tbl\" width=\"100%%\">\n"
2764 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002765 "<th class=\"pxname\" width=\"10%%\">");
2766
2767 chunk_appendf(&trash,
2768 "<a name=\"%s\"></a>%s"
2769 "<a class=px href=\"#%s\">%s</a>",
2770 px->id,
2771 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2772 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002773
2774 if (uri->flags & ST_SHLGNDS) {
2775 /* cap, mode, id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002776 chunk_appendf(&trash, "<div>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002777 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2778 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002779 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002780 }
2781
2782 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002783 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002784 "<th class=\"%s\" width=\"90%%\">%s</th>"
2785 "</tr>\n"
2786 "</table>\n"
2787 "<table class=\"tbl\" width=\"100%%\">\n"
2788 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2790 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2791
2792 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2793 /* Column heading for Enable or Disable server */
2794 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002795 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796
2797 chunk_appendf(&trash,
2798 "<th rowspan=2></th>"
2799 "<th colspan=3>Queue</th>"
2800 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2801 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2802 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2803 "<th colspan=9>Server</th>"
2804 "</tr>\n"
2805 "<tr class=\"titre\">"
2806 "<th>Cur</th><th>Max</th><th>Limit</th>"
2807 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2808 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2809 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2810 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2811 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2812 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2813 "<th>Thrtle</th>\n"
2814 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002815}
2816
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002817/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002819 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002820static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002821{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002822 chunk_appendf(&trash, "</table>");
2823
2824 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2825 /* close the form used to enable/disable this proxy servers */
2826 chunk_appendf(&trash,
2827 "Choose the action to perform on the checked servers : "
2828 "<select name=action>"
2829 "<option value=\"\"></option>"
2830 "<option value=\"disable\">Disable</option>"
2831 "<option value=\"enable\">Enable</option>"
2832 "<option value=\"stop\">Soft Stop</option>"
2833 "<option value=\"start\">Soft Start</option>"
2834 "<option value=\"shutdown\">Kill Sessions</option>"
2835 "</select>"
2836 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2837 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2838 "</form>",
2839 px->uuid);
2840 }
2841
2842 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843}
Willy Tarreau91861262007-10-17 17:06:05 +02002844
2845/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002846 * Dumps statistics for a proxy. The output is sent to the stream interface's
2847 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2848 * buffer space, or non-zero if everything completed. This function is used
2849 * both by the CLI and the HTTP entry points, and is able to dump the output
2850 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002851 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002852static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002853{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002854 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002855 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002856 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002857 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002858
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002859 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002860
Willy Tarreau295a8372011-03-10 11:25:07 +01002861 switch (si->applet.ctx.stats.px_st) {
2862 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002863 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002864 if (uri && uri->scope) {
2865 /* we have a limited scope, we have to check the proxy name */
2866 struct stat_scope *scope;
2867 int len;
2868
2869 len = strlen(px->id);
2870 scope = uri->scope;
2871
2872 while (scope) {
2873 /* match exact proxy name */
2874 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2875 break;
2876
2877 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002878 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002879 break;
2880 scope = scope->next;
2881 }
2882
2883 /* proxy name not found : don't dump anything */
2884 if (scope == NULL)
2885 return 1;
2886 }
2887
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002888 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2889 (si->applet.ctx.stats.iid != -1) &&
2890 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002891 return 1;
2892
Willy Tarreau295a8372011-03-10 11:25:07 +01002893 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002894 /* fall through */
2895
Willy Tarreau295a8372011-03-10 11:25:07 +01002896 case STAT_PX_ST_TH:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002897 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2898 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002899 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002900 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002901 }
Willy Tarreau91861262007-10-17 17:06:05 +02002902
Willy Tarreau295a8372011-03-10 11:25:07 +01002903 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002904 /* fall through */
2905
Willy Tarreau295a8372011-03-10 11:25:07 +01002906 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002907 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002908 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002909 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002910 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002911
Willy Tarreau4348fad2012-09-20 16:48:07 +02002912 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002913 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002914 /* fall through */
2915
Willy Tarreau295a8372011-03-10 11:25:07 +01002916 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002917 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002918 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 +02002919 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002920 return 0;
2921
Willy Tarreau4348fad2012-09-20 16:48:07 +02002922 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002923 if (!l->counters)
2924 continue;
2925
Willy Tarreau295a8372011-03-10 11:25:07 +01002926 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2927 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002928 break;
2929
Willy Tarreau295a8372011-03-10 11:25:07 +01002930 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002931 continue;
2932 }
2933
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002934 /* print the frontend */
2935 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2936 if (bi_putchk(rep, &trash) == -1)
2937 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002938 }
2939
Willy Tarreau295a8372011-03-10 11:25:07 +01002940 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2941 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002942 /* fall through */
2943
Willy Tarreau295a8372011-03-10 11:25:07 +01002944 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002945 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002946 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002947 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 +02002948
Willy Tarreau9b28e032012-10-12 23:49:43 +02002949 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002950 return 0;
2951
Willy Tarreau295a8372011-03-10 11:25:07 +01002952 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002953
Willy Tarreau295a8372011-03-10 11:25:07 +01002954 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2955 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002956 break;
2957
Willy Tarreau295a8372011-03-10 11:25:07 +01002958 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002959 continue;
2960 }
2961
Willy Tarreau44267702011-10-28 15:35:33 +02002962 if (sv->track)
2963 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002964 else
2965 svs = sv;
2966
Willy Tarreau91861262007-10-17 17:06:05 +02002967 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002968 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002969 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002970 else if (svs->state & SRV_RUNNING) {
2971 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002972 sv_state = 3; /* UP */
2973 else
2974 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002975
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002976 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002977 sv_state += 2;
2978 }
Willy Tarreau91861262007-10-17 17:06:05 +02002979 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002980 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002981 sv_state = 1; /* going up */
2982 else
2983 sv_state = 0; /* DOWN */
2984
Willy Tarreau295a8372011-03-10 11:25:07 +01002985 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002986 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002987 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002988 continue;
2989 }
2990
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002991 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
2992 if (bi_putchk(rep, &trash) == -1)
2993 return 0;
2994 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02002995
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002996 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
2997 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002998
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002999 case STAT_PX_ST_BE:
3000 /* print the backend */
3001 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3002 if (bi_putchk(rep, &trash) == -1)
3003 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003004
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
3006 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003008 case STAT_PX_ST_END:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003009 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
3010 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 if (bi_putchk(rep, &trash) == -1)
3012 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003013 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003015 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
3016 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003017
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 case STAT_PX_ST_FIN:
3019 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003020
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003021 default:
3022 /* unknown state, we should put an abort() here ! */
3023 return 1;
3024 }
3025}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003026
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003027/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3028 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003030static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003031{
3032 /* WARNING! This must fit in the first buffer !!! */
3033 chunk_appendf(&trash,
3034 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3035 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3036 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3037 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3038 "<style type=\"text/css\"><!--\n"
3039 "body {"
3040 " font-family: arial, helvetica, sans-serif;"
3041 " font-size: 12px;"
3042 " font-weight: normal;"
3043 " color: black;"
3044 " background: white;"
3045 "}\n"
3046 "th,td {"
3047 " font-size: 10px;"
3048 "}\n"
3049 "h1 {"
3050 " font-size: x-large;"
3051 " margin-bottom: 0.5em;"
3052 "}\n"
3053 "h2 {"
3054 " font-family: helvetica, arial;"
3055 " font-size: x-large;"
3056 " font-weight: bold;"
3057 " font-style: italic;"
3058 " color: #6020a0;"
3059 " margin-top: 0em;"
3060 " margin-bottom: 0em;"
3061 "}\n"
3062 "h3 {"
3063 " font-family: helvetica, arial;"
3064 " font-size: 16px;"
3065 " font-weight: bold;"
3066 " color: #b00040;"
3067 " background: #e8e8d0;"
3068 " margin-top: 0em;"
3069 " margin-bottom: 0em;"
3070 "}\n"
3071 "li {"
3072 " margin-top: 0.25em;"
3073 " margin-right: 2em;"
3074 "}\n"
3075 ".hr {margin-top: 0.25em;"
3076 " border-color: black;"
3077 " border-bottom-style: solid;"
3078 "}\n"
3079 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3080 ".total {background: #20D0D0;color: #ffff80;}\n"
3081 ".frontend {background: #e8e8d0;}\n"
3082 ".socket {background: #d0d0d0;}\n"
3083 ".backend {background: #e8e8d0;}\n"
3084 ".active0 {background: #ff9090;}\n"
3085 ".active1 {background: #ffd020;}\n"
3086 ".active2 {background: #ffffa0;}\n"
3087 ".active3 {background: #c0ffc0;}\n"
3088 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3089 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
3090 ".active6 {background: #e0e0e0;}\n"
3091 ".backup0 {background: #ff9090;}\n"
3092 ".backup1 {background: #ff80ff;}\n"
3093 ".backup2 {background: #c060ff;}\n"
3094 ".backup3 {background: #b0d0ff;}\n"
3095 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3096 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3097 ".backup6 {background: #e0e0e0;}\n"
3098 ".maintain {background: #c07820;}\n"
3099 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3100 "\n"
3101 "a.px:link {color: #ffff40; text-decoration: none;}"
3102 "a.px:visited {color: #ffff40; text-decoration: none;}"
3103 "a.px:hover {color: #ffffff; text-decoration: none;}"
3104 "a.lfsb:link {color: #000000; text-decoration: none;}"
3105 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3106 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3107 "\n"
3108 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3109 "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"
3110 "table.tbl td.ac { text-align: center;}\n"
3111 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3112 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3113 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3114 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3115 "\n"
3116 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3117 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3118 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003119 "table.det { border-collapse: collapse; border-style: none; }\n"
3120 "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n"
3121 "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;font-family: monospace;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003122 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003123 "div {\n"
3124 " display:block;\n"
3125 " visibility:hidden;\n"
3126 " z-index:2147483647;\n"
3127 " position:absolute;\n"
3128 " padding:2px 4px 3px;\n"
3129 " background:#f0f060; color:#000000;\n"
3130 " border:1px solid #7040c0;\n"
3131 " white-space:nowrap;\n"
3132 " font-style:normal;font-size:11px;font-weight:normal;\n"
3133 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3134 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3135 "}\n"
3136 "u:hover div {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137 "-->\n"
3138 "</style></head>\n",
3139 (uri->flags & ST_SHNODE) ? " on " : "",
3140 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3141 );
3142}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003143
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003144/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003146 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003147 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003148static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003149{
3150 unsigned int up = (now.tv_sec - start_date.tv_sec);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003151
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 /* WARNING! this has to fit the first packet too.
3153 * We are around 3.5 kB, add adding entries will
3154 * become tricky if we want to support 4kB buffers !
3155 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003156 chunk_appendf(&trash,
3157 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3158 PRODUCT_NAME "%s</a></h1>\n"
3159 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3160 "<hr width=\"100%%\" class=\"hr\">\n"
3161 "<h3>&gt; General process information</h3>\n"
3162 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3163 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3164 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3165 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3166 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3167 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3168 "Running tasks: %d/%d; idle = %d %%<br>\n"
3169 "</td><td align=\"center\" nowrap>\n"
3170 "<table class=\"lgd\"><tr>\n"
3171 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3172 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3173 "</tr><tr>\n"
3174 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3175 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3176 "</tr><tr>\n"
3177 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3178 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3179 "</tr><tr>\n"
3180 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3181 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
3182 "</tr><tr>\n"
3183 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3184 "</tr></table>\n"
3185 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3186 "</td>"
3187 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3188 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3189 "",
3190 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3191 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3192 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3193 (uri->flags & ST_SHDESC) ? ": " : "",
3194 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3195 pid, relative_pid, global.nbproc,
3196 up / 86400, (up % 86400) / 3600,
3197 (up % 3600) / 60, (up % 60),
3198 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3199 global.rlimit_memmax ? " MB" : "",
3200 global.rlimit_nofile,
3201 global.maxsock, global.maxconn, global.maxpipes,
3202 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3203 run_queue_cur, nb_tasks_cur, idle_pct
3204 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003205
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3207 chunk_appendf(&trash,
3208 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
3209 uri->uri_prefix,
3210 "",
3211 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
3212 else
3213 chunk_appendf(&trash,
3214 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3215 uri->uri_prefix,
3216 ";up",
3217 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02003218
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 if (uri->refresh > 0) {
3220 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3221 chunk_appendf(&trash,
3222 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
3223 uri->uri_prefix,
3224 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3225 "");
3226 else
3227 chunk_appendf(&trash,
3228 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
3229 uri->uri_prefix,
3230 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3231 ";norefresh");
3232 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003233
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003234 chunk_appendf(&trash,
3235 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
3236 uri->uri_prefix,
3237 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3238 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003239
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003240 chunk_appendf(&trash,
3241 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
3242 uri->uri_prefix,
3243 (uri->refresh > 0) ? ";norefresh" : "");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003244
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003245 chunk_appendf(&trash,
3246 "</ul></td>"
3247 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3248 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3249 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3250 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3251 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3252 "</ul>"
3253 "</td>"
3254 "</tr></table>\n"
3255 ""
3256 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003257
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003258 if (si->applet.ctx.stats.st_code) {
3259 switch (si->applet.ctx.stats.st_code) {
3260 case STAT_STATUS_DONE:
3261 chunk_appendf(&trash,
3262 "<p><div class=active3>"
3263 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3264 "Action processed successfully."
3265 "</div>\n", uri->uri_prefix);
3266 break;
3267 case STAT_STATUS_NONE:
3268 chunk_appendf(&trash,
3269 "<p><div class=active2>"
3270 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3271 "Nothing has changed."
3272 "</div>\n", uri->uri_prefix);
3273 break;
3274 case STAT_STATUS_PART:
3275 chunk_appendf(&trash,
3276 "<p><div class=active2>"
3277 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3278 "Action partially processed.<br>"
3279 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3280 "</div>\n", uri->uri_prefix);
3281 break;
3282 case STAT_STATUS_ERRP:
3283 chunk_appendf(&trash,
3284 "<p><div class=active0>"
3285 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3286 "Action not processed because of invalid parameters."
3287 "<ul>"
3288 "<li>The action is maybe unknown.</li>"
3289 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3290 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3291 "</ul>"
3292 "</div>\n", uri->uri_prefix);
3293 break;
3294 case STAT_STATUS_EXCD:
3295 chunk_appendf(&trash,
3296 "<p><div class=active0>"
3297 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3298 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3299 "You should retry with less servers at a time.</b>"
3300 "</div>\n", uri->uri_prefix);
3301 break;
3302 case STAT_STATUS_DENY:
3303 chunk_appendf(&trash,
3304 "<p><div class=active0>"
3305 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3306 "<b>Action denied.</b>"
3307 "</div>\n", uri->uri_prefix);
3308 break;
3309 default:
3310 chunk_appendf(&trash,
3311 "<p><div class=active6>"
3312 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3313 "Unexpected result."
3314 "</div>\n", uri->uri_prefix);
3315 }
3316 chunk_appendf(&trash, "<p>\n");
3317 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003318}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003319
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003320/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3321 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003323static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003324{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003325 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003326}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003327
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003328/* This function dumps statistics onto the stream interface's read buffer in
3329 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3330 * are ignored for CSV format (hence <uri> may be NULL there). The xprt_ctx must
3331 * have been zeroed first, and the flags properly set. It returns 0 if it had to
3332 * stop writing data and an I/O is needed, 1 if the dump is finished and the
3333 * session must be closed, or -1 in case of any error. This function is used by
3334 * both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003336static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003338 struct channel *rep = si->ib;
3339 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003340
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003342
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 switch (si->conn->xprt_st) {
3344 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003345 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3346 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003347
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003348 case STAT_ST_HEAD:
3349 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
3350 stats_dump_csv_header();
3351 else
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003352 stats_dump_html_head(uri);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003353
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 if (bi_putchk(rep, &trash) == -1)
3355 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003356
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 si->conn->xprt_st = STAT_ST_INFO;
3358 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003359
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 case STAT_ST_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003361 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
3362 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003363 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003364 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 }
Willy Tarreau91861262007-10-17 17:06:05 +02003366
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003367 si->applet.ctx.stats.px = proxy;
3368 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3369 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003370 /* fall through */
3371
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003372 case STAT_ST_LIST:
3373 /* dump proxies */
3374 while (si->applet.ctx.stats.px) {
3375 if (buffer_almost_full(rep->buf))
3376 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003377
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 px = si->applet.ctx.stats.px;
3379 /* skip the disabled proxies, global frontend and non-networked ones */
3380 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003381 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003383
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 si->applet.ctx.stats.px = px->next;
3385 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3386 }
3387 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003388
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 si->conn->xprt_st = STAT_ST_END;
3390 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003391
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392 case STAT_ST_END:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003393 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
3394 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 if (bi_putchk(rep, &trash) == -1)
3396 return 0;
3397 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003398
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 si->conn->xprt_st = STAT_ST_FIN;
3400 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003401
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402 case STAT_ST_FIN:
3403 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 default:
3406 /* unknown state ! */
3407 si->conn->xprt_st = STAT_ST_FIN;
3408 return -1;
3409 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410}
Willy Tarreauae526782010-03-04 20:34:23 +01003411
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412/* This I/O handler runs as an applet embedded in a stream interface. It is
3413 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3414 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3415 * automatically unregisters itself once transfer is complete.
3416 */
3417static void http_stats_io_handler(struct stream_interface *si)
3418{
3419 struct session *s = si->conn->xprt_ctx;
3420 struct channel *req = si->ob;
3421 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003422
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003423 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3424 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003425
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 /* check that the output is not closed */
3427 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3428 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003429
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430 if (!si->applet.st0) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003431 if (stats_dump_stat_to_buffer(si, s->be->uri_auth)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003432 si->applet.st0 = 1;
3433 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003434 }
3435 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003436
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3438 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003439
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3441 si_shutr(si);
3442 res->flags |= CF_READ_NULL;
3443 }
Willy Tarreau91861262007-10-17 17:06:05 +02003444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 /* update all other flags and resync with the other side */
3446 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003447
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 /* we don't want to expire timeouts while we're processing requests */
3449 si->ib->rex = TICK_ETERNITY;
3450 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003451
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 out:
3453 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3454 /* check that we have released everything then unregister */
3455 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003456 }
3457}
3458
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459
Willy Tarreau909d5172012-11-26 03:04:41 +01003460static inline const char *get_conn_ctrl_name(const struct connection *conn)
3461{
3462 if (!conn->ctrl)
3463 return "NONE";
3464 return conn->ctrl->name;
3465}
3466
3467static inline const char *get_conn_xprt_name(const struct connection *conn)
3468{
3469 static char ptr[17];
3470
3471 if (!conn->xprt)
3472 return "NONE";
3473
3474 if (conn->xprt == &raw_sock)
3475 return "RAW";
3476
3477#ifdef USE_OPENSSL
3478 if (conn->xprt == &ssl_sock)
3479 return "SSL";
3480#endif
3481 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3482 return ptr;
3483}
3484
3485static inline const char *get_conn_data_name(const struct connection *conn)
3486{
3487 static char ptr[17];
3488
3489 if (!conn->data)
3490 return "NONE";
3491
3492 if (conn->data == &sess_conn_cb)
3493 return "SESS";
3494
3495 if (conn->data == &si_conn_cb)
3496 return "STRM";
3497
3498 if (conn->data == &check_conn_cb)
3499 return "CHCK";
3500
3501 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3502 return ptr;
3503}
3504
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003505/* This function dumps a complete session state onto the stream interface's
3506 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3507 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003508 * 0 if the output buffer is full and it needs to be called again, otherwise
3509 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003510 */
Willy Tarreau76153662012-11-26 01:16:39 +01003511static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003512{
3513 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003514 extern const char *monthname[12];
3515 char pn[INET6_ADDRSTRLEN];
3516
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003517 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003518
Willy Tarreau295a8372011-03-10 11:25:07 +01003519 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003520 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003521 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3522 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003523 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003524 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003525 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003526 return 1;
3527 }
3528
Willy Tarreau295a8372011-03-10 11:25:07 +01003529 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003530 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003531 si->applet.ctx.sess.uid = sess->uniq_id;
3532 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003533 /* fall through */
3534
3535 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003536 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003537 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003538 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003539 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003540 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3541 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003542 sess->uniq_id,
3543 sess->listener->proto->name);
3544
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003545 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003546 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003547 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003548 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003549 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003550 break;
3551 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003552 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003553 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003554 default:
3555 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003556 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003557 break;
3558 }
3559
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003560 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003561 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003562 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003564 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003565 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003566 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3567 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3568 sess->listener ? sess->listener->luid : 0);
3569
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003570 conn_get_to_addr(sess->si[0].conn);
3571 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003572 case AF_INET:
3573 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003574 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003575 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003576 break;
3577 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003578 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003579 break;
3580 default:
3581 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003582 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003583 break;
3584 }
3585
Willy Tarreau50943332011-09-02 17:33:05 +02003586 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003587 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003588 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003589 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003590 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3591 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003592 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003593
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003594 conn_get_from_addr(sess->si[1].conn);
3595 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003596 case AF_INET:
3597 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003598 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003599 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003600 break;
3601 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003602 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003603 break;
3604 default:
3605 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003606 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003607 break;
3608 }
3609
3610 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003611 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003612 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003613 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3614 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003615 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003616 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003617
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003618 conn_get_to_addr(sess->si[1].conn);
3619 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003620 case AF_INET:
3621 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003622 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003623 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003624 break;
3625 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003626 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003627 break;
3628 default:
3629 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003630 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003631 break;
3632 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003633
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003634 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003635 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003636 sess->task,
3637 sess->task->state,
3638 sess->task->nice, sess->task->calls,
3639 sess->task->expire ?
3640 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3641 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3642 TICKS_TO_MS(1000)) : "<NEVER>",
3643 task_in_rq(sess->task) ? ", running" : "");
3644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003645 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003646 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003647 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3648
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003649 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003650 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3651 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3652 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3653
3654 chunk_appendf(&trash,
3655 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003656 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003657 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003658 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003659 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003660 sess->si[0].exp ?
3661 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3662 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3663 TICKS_TO_MS(1000)) : "<NEVER>",
3664 sess->si[0].err_type);
3665
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003666 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003667 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003668 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003669 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003670 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003671 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003672 sess->si[1].exp ?
3673 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3674 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3675 TICKS_TO_MS(1000)) : "<NEVER>",
3676 sess->si[1].err_type);
3677
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003678 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003679 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003680 sess->si[0].conn,
3681 get_conn_ctrl_name(sess->si[0].conn),
3682 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003683 get_conn_data_name(sess->si[0].conn),
3684 obj_type_name(sess->si[0].conn->target),
3685 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003686
3687 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003688 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003689 sess->si[0].conn->flags,
3690 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003691 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3692 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3693 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].updated : 0);
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003694
3695 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003696 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003697 sess->si[1].conn,
3698 get_conn_ctrl_name(sess->si[1].conn),
3699 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003700 get_conn_data_name(sess->si[1].conn),
3701 obj_type_name(sess->si[1].conn->target),
3702 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003703
3704 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003705 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003706 sess->si[1].conn->flags,
3707 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003708 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3709 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3710 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].updated : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003712 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003713 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003714 " an_exp=%s",
3715 sess->req,
3716 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003717 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003718 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003719 sess->req->analyse_exp ?
3720 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3721 TICKS_TO_MS(1000)) : "<NEVER>");
3722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003723 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003724 " rex=%s",
3725 sess->req->rex ?
3726 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3727 TICKS_TO_MS(1000)) : "<NEVER>");
3728
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003729 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003730 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003731 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003732 sess->req->wex ?
3733 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3734 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003735 sess->req->buf,
3736 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003737 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003738 sess->txn.req.next, sess->req->buf->i,
3739 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003740
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003741 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003742 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003743 " an_exp=%s",
3744 sess->rep,
3745 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003746 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003747 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003748 sess->rep->analyse_exp ?
3749 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3750 TICKS_TO_MS(1000)) : "<NEVER>");
3751
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003752 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003753 " rex=%s",
3754 sess->rep->rex ?
3755 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3756 TICKS_TO_MS(1000)) : "<NEVER>");
3757
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003758 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003759 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003760 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003761 sess->rep->wex ?
3762 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3763 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003764 sess->rep->buf,
3765 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003766 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003767 sess->txn.rsp.next, sess->rep->buf->i,
3768 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003769
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003770 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003771 return 0;
3772
3773 /* use other states to dump the contents */
3774 }
3775 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003776 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003777 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003778 return 1;
3779}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003780
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003781/* This function dumps all sessions' states onto the stream interface's
3782 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003783 * properly set. It returns 0 if the output buffer is full and it needs
3784 * to be called again, otherwise non-zero. It is designed to be called
3785 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003786 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003787static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003788{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003789 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003790 /* If we're forced to shut down, we might have to remove our
3791 * reference to the last session being dumped.
3792 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003793 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003794 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3795 LIST_DEL(&si->applet.ctx.sess.bref.users);
3796 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003797 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003798 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003799 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003800 }
3801
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003802 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003803
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003804 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003805 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003806 /* the function had not been called yet, let's prepare the
3807 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003808 * pointer to the first in the global list. When a target
3809 * session is being destroyed, it is responsible for updating
3810 * this pointer. We know we have reached the end when this
3811 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003812 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003813 LIST_INIT(&si->applet.ctx.sess.bref.users);
3814 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003815 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003816 /* fall through */
3817
Willy Tarreau295a8372011-03-10 11:25:07 +01003818 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003819 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003820 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3821 LIST_DEL(&si->applet.ctx.sess.bref.users);
3822 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003823 }
3824
3825 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003826 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003827 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003828 struct session *curr_sess;
3829
Willy Tarreau295a8372011-03-10 11:25:07 +01003830 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003831
Willy Tarreau295a8372011-03-10 11:25:07 +01003832 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003833 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003834 goto next_sess;
3835
Willy Tarreau295a8372011-03-10 11:25:07 +01003836 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003837 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003838 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003839 return 0;
3840
3841 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003842 LIST_DEL(&si->applet.ctx.sess.bref.users);
3843 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003844 if (si->applet.ctx.sess.target != (void *)-1) {
3845 si->applet.ctx.sess.target = NULL;
3846 break;
3847 }
3848 else
3849 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003850 }
3851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003852 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003853 "%p: proto=%s",
3854 curr_sess,
3855 curr_sess->listener->proto->name);
3856
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003857
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003858 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003859 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003860 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003861 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003862 " src=%s:%d fe=%s be=%s srv=%s",
3863 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003864 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003865 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003866 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003867 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003868 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003869 break;
3870 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003871 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003872 " src=unix:%d fe=%s be=%s srv=%s",
3873 curr_sess->listener->luid,
3874 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003875 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003876 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003877 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003878 break;
3879 }
3880
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003881 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003882 " ts=%02x age=%s calls=%d",
3883 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003884 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3885 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003886
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003887 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003888 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003889 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003890 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003891 curr_sess->req->analysers,
3892 curr_sess->req->rex ?
3893 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3894 TICKS_TO_MS(1000)) : "");
3895
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003896 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003897 ",wx=%s",
3898 curr_sess->req->wex ?
3899 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3900 TICKS_TO_MS(1000)) : "");
3901
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003902 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003903 ",ax=%s]",
3904 curr_sess->req->analyse_exp ?
3905 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3906 TICKS_TO_MS(1000)) : "");
3907
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003908 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003909 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003910 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003911 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003912 curr_sess->rep->analysers,
3913 curr_sess->rep->rex ?
3914 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3915 TICKS_TO_MS(1000)) : "");
3916
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003917 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003918 ",wx=%s",
3919 curr_sess->rep->wex ?
3920 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3921 TICKS_TO_MS(1000)) : "");
3922
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003923 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003924 ",ax=%s]",
3925 curr_sess->rep->analyse_exp ?
3926 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3927 TICKS_TO_MS(1000)) : "");
3928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003929 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003930 " s0=[%d,%1xh,fd=%d,ex=%s]",
3931 curr_sess->si[0].state,
3932 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003933 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003934 curr_sess->si[0].exp ?
3935 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3936 TICKS_TO_MS(1000)) : "");
3937
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003938 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003939 " s1=[%d,%1xh,fd=%d,ex=%s]",
3940 curr_sess->si[1].state,
3941 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003942 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003943 curr_sess->si[1].exp ?
3944 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3945 TICKS_TO_MS(1000)) : "");
3946
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003947 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003948 " exp=%s",
3949 curr_sess->task->expire ?
3950 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3951 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003952 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003955 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003956
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003957 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003958 /* let's try again later from this session. We add ourselves into
3959 * this session's users so that it can remove us upon termination.
3960 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003961 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003962 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003963 }
3964
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003965 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003966 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003967 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003968
Willy Tarreau76153662012-11-26 01:16:39 +01003969 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003970 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003971 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003972 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003973 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003974 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003975
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003976 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003977 return 0;
3978
Willy Tarreau295a8372011-03-10 11:25:07 +01003979 si->applet.ctx.sess.target = NULL;
3980 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003981 return 1;
3982 }
3983
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003984 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003985 /* fall through */
3986
3987 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003988 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003989 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003990 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003991}
3992
Willy Tarreau5f9a8772012-11-26 02:22:40 +01003993/* This is called when the stream interface is closed. For instance, upon an
3994 * external abort, we won't call the i/o handler anymore so we may need to
3995 * remove back references to the session currently being dumped.
3996 */
3997void cli_release_handler(struct stream_interface *si)
3998{
3999 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
4000 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
4001 LIST_DEL(&si->applet.ctx.sess.bref.users);
4002 }
4003}
4004
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004005/* This function dumps all tables' states onto the stream interface's
4006 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004007 * properly set. It returns 0 if the output buffer is full and it needs
4008 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004009 */
Willy Tarreau44455022012-12-05 23:01:12 +01004010static int stats_table_request(struct stream_interface *si, int show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004011{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004012 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004013 struct ebmb_node *eb;
4014 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004015 int skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004016
4017 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004018 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01004019 * - STAT_ST_INIT : the first call
4020 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004021 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004022 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004023 * and the entry pointer points to the next entry to be dumped,
4024 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004025 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004026 * data though.
4027 */
4028
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004029 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004030 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004031 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004032 si->applet.ctx.table.entry->ref_cnt--;
4033 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004034 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004035 return 1;
4036 }
4037
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004038 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004039
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004040 while (si->conn->xprt_st != STAT_ST_FIN) {
4041 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004042 case STAT_ST_INIT:
4043 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4044 if (!si->applet.ctx.table.proxy)
4045 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004046
Willy Tarreau295a8372011-03-10 11:25:07 +01004047 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004048 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004049 break;
4050
Willy Tarreau295a8372011-03-10 11:25:07 +01004051 case STAT_ST_INFO:
4052 if (!si->applet.ctx.table.proxy ||
4053 (si->applet.ctx.table.target &&
4054 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004055 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004056 break;
4057 }
4058
Willy Tarreau295a8372011-03-10 11:25:07 +01004059 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004060 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004061 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004062 return 0;
4063
Willy Tarreau295a8372011-03-10 11:25:07 +01004064 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004065 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004066 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004067 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004068 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004069 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4070 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004071 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004072 break;
4073 }
4074 }
4075 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004076 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004077 break;
4078
Willy Tarreau295a8372011-03-10 11:25:07 +01004079 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004080 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004081
Willy Tarreau295a8372011-03-10 11:25:07 +01004082 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004083 /* we're filtering on some data contents */
4084 void *ptr;
4085 long long data;
4086
Willy Tarreau295a8372011-03-10 11:25:07 +01004087 dt = si->applet.ctx.table.data_type;
4088 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4089 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004090 dt);
4091
4092 data = 0;
4093 switch (stktable_data_types[dt].std_type) {
4094 case STD_T_SINT:
4095 data = stktable_data_cast(ptr, std_t_sint);
4096 break;
4097 case STD_T_UINT:
4098 data = stktable_data_cast(ptr, std_t_uint);
4099 break;
4100 case STD_T_ULL:
4101 data = stktable_data_cast(ptr, std_t_ull);
4102 break;
4103 case STD_T_FRQP:
4104 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004105 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004106 break;
4107 }
4108
4109 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004110 if ((data < si->applet.ctx.table.value &&
4111 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4112 si->applet.ctx.table.data_op == STD_OP_GT ||
4113 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4114 (data == si->applet.ctx.table.value &&
4115 (si->applet.ctx.table.data_op == STD_OP_NE ||
4116 si->applet.ctx.table.data_op == STD_OP_GT ||
4117 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4118 (data > si->applet.ctx.table.value &&
4119 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4120 si->applet.ctx.table.data_op == STD_OP_LT ||
4121 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004122 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004123 }
4124
Simon Hormanc88b8872011-06-15 15:18:49 +09004125 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004126 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004127 si->applet.ctx.table.entry))
4128 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004129
Willy Tarreau295a8372011-03-10 11:25:07 +01004130 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004131
Willy Tarreau295a8372011-03-10 11:25:07 +01004132 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004133 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004134 struct stksess *old = si->applet.ctx.table.entry;
4135 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004136 if (show)
4137 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
4138 else
4139 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004140 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004141 break;
4142 }
4143
Simon Hormanc88b8872011-06-15 15:18:49 +09004144
4145 if (show)
4146 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4147 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4148 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4149
Willy Tarreau295a8372011-03-10 11:25:07 +01004150 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004151 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004152 break;
4153
Willy Tarreau295a8372011-03-10 11:25:07 +01004154 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004155 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004156 break;
4157 }
4158 }
4159 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004160}
4161
Willy Tarreaud426a182010-03-05 14:58:26 +01004162/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004163 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4164 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4165 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4166 * lines are respected within the limit of 70 output chars. Lines that are
4167 * continuation of a previous truncated line begin with "+" instead of " "
4168 * after the offset. The new pointer is returned.
4169 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004170static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4171 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004172{
4173 int end;
4174 unsigned char c;
4175
4176 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004177 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004178 return ptr;
4179
Willy Tarreau77804732012-10-29 16:14:26 +01004180 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004181
Willy Tarreaud426a182010-03-05 14:58:26 +01004182 while (ptr < len && ptr < bsize) {
4183 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004184 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004185 if (out->len > end - 2)
4186 break;
4187 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004188 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004189 if (out->len > end - 3)
4190 break;
4191 out->str[out->len++] = '\\';
4192 switch (c) {
4193 case '\t': c = 't'; break;
4194 case '\n': c = 'n'; break;
4195 case '\r': c = 'r'; break;
4196 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004197 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004198 }
4199 out->str[out->len++] = c;
4200 } else {
4201 if (out->len > end - 5)
4202 break;
4203 out->str[out->len++] = '\\';
4204 out->str[out->len++] = 'x';
4205 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4206 out->str[out->len++] = hextab[c & 0xF];
4207 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004208 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004209 /* we had a line break, let's return now */
4210 out->str[out->len++] = '\n';
4211 *line = ptr;
4212 return ptr;
4213 }
4214 }
4215 /* we have an incomplete line, we return it as-is */
4216 out->str[out->len++] = '\n';
4217 return ptr;
4218}
4219
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004220/* This function dumps all captured errors onto the stream interface's
4221 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004222 * properly set. It returns 0 if the output buffer is full and it needs
4223 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004224 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004225static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004226{
4227 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004228
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004229 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004230 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004231
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004232 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004233
Willy Tarreau295a8372011-03-10 11:25:07 +01004234 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004235 /* the function had not been called yet, let's prepare the
4236 * buffer for a response.
4237 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004238 struct tm tm;
4239
4240 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004241 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004242 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4243 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4244 error_snapshot_id);
4245
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004246 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004247 /* Socket buffer full. Let's try again later from the same point */
4248 return 0;
4249 }
4250
Willy Tarreau295a8372011-03-10 11:25:07 +01004251 si->applet.ctx.errors.px = proxy;
4252 si->applet.ctx.errors.buf = 0;
4253 si->applet.ctx.errors.bol = 0;
4254 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004255 }
4256
4257 /* we have two inner loops here, one for the proxy, the other one for
4258 * the buffer.
4259 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004260 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004261 struct error_snapshot *es;
4262
Willy Tarreau295a8372011-03-10 11:25:07 +01004263 if (si->applet.ctx.errors.buf == 0)
4264 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004265 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004266 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004267
4268 if (!es->when.tv_sec)
4269 goto next;
4270
Willy Tarreau295a8372011-03-10 11:25:07 +01004271 if (si->applet.ctx.errors.iid >= 0 &&
4272 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4273 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004274 goto next;
4275
Willy Tarreau295a8372011-03-10 11:25:07 +01004276 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004277 /* just print headers now */
4278
4279 char pn[INET6_ADDRSTRLEN];
4280 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004281 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004282
4283 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004284 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004285 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004286 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004287
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004288 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4289 case AF_INET:
4290 case AF_INET6:
4291 port = get_host_port(&es->src);
4292 break;
4293 default:
4294 port = 0;
4295 }
4296
Willy Tarreau295a8372011-03-10 11:25:07 +01004297 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004298 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004299 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004300 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004301 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004302 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004303 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4304 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004305 break;
4306 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004307 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004308 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004309 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004310 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004311 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004312 break;
4313 }
4314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004315 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004316 ", server %s (#%d), event #%u\n"
4317 " src %s:%d, session #%d, session flags 0x%08x\n"
4318 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4319 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4320 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4321 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4322 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4323 es->ev_id,
4324 pn, port, es->sid, es->s_flags,
4325 es->state, es->m_flags, es->t_flags,
4326 es->m_clen, es->m_blen,
4327 es->b_flags, es->b_out, es->b_tot,
4328 es->len, es->b_wrap, es->pos);
4329
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004330 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004331 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004332 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004333 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004334 si->applet.ctx.errors.ptr = 0;
4335 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004336 }
4337
Willy Tarreau295a8372011-03-10 11:25:07 +01004338 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004339 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004340 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004341 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004342 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004343 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004344 goto next;
4345 }
4346
4347 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004348 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004349 int newptr;
4350 int newline;
4351
Willy Tarreau295a8372011-03-10 11:25:07 +01004352 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004353 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
Willy Tarreau295a8372011-03-10 11:25:07 +01004354 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004355 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004356
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004357 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004358 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004359 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004360 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004361 si->applet.ctx.errors.ptr = newptr;
4362 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004363 };
4364 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004365 si->applet.ctx.errors.bol = 0;
4366 si->applet.ctx.errors.ptr = -1;
4367 si->applet.ctx.errors.buf++;
4368 if (si->applet.ctx.errors.buf > 1) {
4369 si->applet.ctx.errors.buf = 0;
4370 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004371 }
4372 }
4373
4374 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004375 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004376}
4377
Willy Tarreaud5781202012-09-22 19:32:35 +02004378/* parse the "level" argument on the bind lines */
4379static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4380{
4381 if (!*args[cur_arg + 1]) {
4382 memprintf(err, "'%s' : missing level", args[cur_arg]);
4383 return ERR_ALERT | ERR_FATAL;
4384 }
4385
4386 if (!strcmp(args[cur_arg+1], "user"))
4387 conf->level = ACCESS_LVL_USER;
4388 else if (!strcmp(args[cur_arg+1], "operator"))
4389 conf->level = ACCESS_LVL_OPER;
4390 else if (!strcmp(args[cur_arg+1], "admin"))
4391 conf->level = ACCESS_LVL_ADMIN;
4392 else {
4393 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4394 args[cur_arg], args[cur_arg+1]);
4395 return ERR_ALERT | ERR_FATAL;
4396 }
4397
4398 return 0;
4399}
4400
Willy Tarreaub24281b2011-02-13 13:16:36 +01004401struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004402 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004403 .name = "<STATS>", /* used for logging */
4404 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004405 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004406};
4407
Simon Horman9bd2c732011-06-15 15:18:44 +09004408static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004409 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004410 .name = "<CLI>", /* used for logging */
4411 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004412 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004413};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004414
Willy Tarreau10522fd2008-07-09 20:12:41 +02004415static struct cfg_kw_list cfg_kws = {{ },{
4416 { CFG_GLOBAL, "stats", stats_parse_global },
4417 { 0, NULL, NULL },
4418}};
4419
Willy Tarreaud5781202012-09-22 19:32:35 +02004420static struct bind_kw_list bind_kws = { "STAT", { }, {
4421 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4422 { NULL, NULL, 0 },
4423}};
4424
Willy Tarreau10522fd2008-07-09 20:12:41 +02004425__attribute__((constructor))
4426static void __dumpstats_module_init(void)
4427{
4428 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004429 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004430}
4431
Willy Tarreau91861262007-10-17 17:06:05 +02004432/*
4433 * Local variables:
4434 * c-indent-level: 8
4435 * c-basic-offset: 8
4436 * End:
4437 */