blob: 94e94f2d4ea5300f27fb2692febb118061e4d931 [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 Tarreauf2943dc2012-10-26 20:10:28 +0200944 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100945 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200946 }
947 else if (strcmp(args[1], "info") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200948 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100949 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200950 }
951 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200952 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200953 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100954 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100955 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200956 return 1;
957 }
Willy Tarreau76153662012-11-26 01:16:39 +0100958 if (*args[2] && strcmp(args[2], "all") == 0)
959 si->applet.ctx.sess.target = (void *)-1;
960 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100961 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100962 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100963 si->applet.ctx.sess.target = NULL;
964 si->applet.ctx.sess.section = 0; /* start with session status */
965 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100966 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200967 }
968 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200969 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100970 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100971 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200972 return 1;
973 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200974 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100975 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200976 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 si->applet.ctx.errors.iid = -1;
978 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200979 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100980 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200981 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200982 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200983 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200984 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700985 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200986 return 0;
987 }
988 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200989 else if (strcmp(args[0], "clear") == 0) {
990 if (strcmp(args[1], "counters") == 0) {
991 struct proxy *px;
992 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200993 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200994 int clrall = 0;
995
996 if (strcmp(args[2], "all") == 0)
997 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200998
Willy Tarreau6162db22009-10-10 17:13:00 +0200999 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001000 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1001 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001002 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001003 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001004 return 1;
1005 }
1006
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001007 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001008 if (clrall) {
1009 memset(&px->be_counters, 0, sizeof(px->be_counters));
1010 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1011 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001012 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001013 px->be_counters.conn_max = 0;
1014 px->be_counters.p.http.rps_max = 0;
1015 px->be_counters.sps_max = 0;
1016 px->be_counters.cps_max = 0;
1017 px->be_counters.nbpend_max = 0;
1018
1019 px->fe_counters.conn_max = 0;
1020 px->fe_counters.p.http.rps_max = 0;
1021 px->fe_counters.sps_max = 0;
1022 px->fe_counters.cps_max = 0;
1023 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001024 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001025
1026 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001027 if (clrall)
1028 memset(&sv->counters, 0, sizeof(sv->counters));
1029 else {
1030 sv->counters.cur_sess_max = 0;
1031 sv->counters.nbpend_max = 0;
1032 sv->counters.sps_max = 0;
1033 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001034
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001035 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001036 if (li->counters) {
1037 if (clrall)
1038 memset(li->counters, 0, sizeof(*li->counters));
1039 else
1040 li->counters->conn_max = 0;
1041 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001042 }
1043
Willy Tarreau81c25d02011-09-07 15:17:21 +02001044 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001045 return 1;
1046 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001047 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001048 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001049 /* end of processing */
1050 return 1;
1051 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001052 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001053 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001054 return 0;
1055 }
1056 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001057 else if (strcmp(args[0], "get") == 0) {
1058 if (strcmp(args[1], "weight") == 0) {
1059 struct proxy *px;
1060 struct server *sv;
1061
1062 /* split "backend/server" and make <line> point to server */
1063 for (line = args[2]; *line; line++)
1064 if (*line == '/') {
1065 *line++ = '\0';
1066 break;
1067 }
1068
1069 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001070 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001071 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001072 return 1;
1073 }
1074
1075 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001076 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001077 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001078 return 1;
1079 }
1080
1081 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001082 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1083 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001084 return 1;
1085 }
1086 else { /* not "get weight" */
1087 return 0;
1088 }
1089 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001090 else if (strcmp(args[0], "set") == 0) {
1091 if (strcmp(args[1], "weight") == 0) {
1092 struct proxy *px;
1093 struct server *sv;
1094 int w;
1095
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001096 sv = expect_server_admin(s, si, args[2]);
1097 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001098 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001099 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001100
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001101 /* if the weight is terminated with '%', it is set relative to
1102 * the initial weight, otherwise it is absolute.
1103 */
1104 if (!*args[3]) {
1105 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001106 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001107 return 1;
1108 }
1109
Willy Tarreau4483d432009-10-10 19:30:08 +02001110 w = atoi(args[3]);
1111 if (strchr(args[3], '%') != NULL) {
1112 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001113 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001114 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001115 return 1;
1116 }
1117 w = sv->iweight * w / 100;
1118 }
1119 else {
1120 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001121 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001122 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001123 return 1;
1124 }
1125 }
1126
1127 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001128 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 +01001129 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001130 return 1;
1131 }
1132
1133 sv->uweight = w;
1134
1135 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1136 /* we must take care of not pushing the server to full throttle during slow starts */
1137 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1138 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1139 else
1140 sv->eweight = BE_WEIGHT_SCALE;
1141 sv->eweight *= sv->uweight;
1142 } else {
1143 sv->eweight = sv->uweight;
1144 }
1145
1146 /* static LB algorithms are a bit harder to update */
1147 if (px->lbprm.update_server_eweight)
1148 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001149 else if (sv->eweight) {
1150 if (px->lbprm.set_server_status_up)
1151 px->lbprm.set_server_status_up(sv);
1152 }
1153 else {
1154 if (px->lbprm.set_server_status_down)
1155 px->lbprm.set_server_status_down(sv);
1156 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001157
1158 return 1;
1159 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001160 else if (strcmp(args[1], "timeout") == 0) {
1161 if (strcmp(args[2], "cli") == 0) {
1162 unsigned timeout;
1163 const char *res;
1164
1165 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001166 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001167 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001168 return 1;
1169 }
1170
1171 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1172 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001173 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001174 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001175 return 1;
1176 }
1177
1178 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1179 return 1;
1180 }
1181 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001182 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001183 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001184 return 1;
1185 }
1186 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001187 else if (strcmp(args[1], "maxconn") == 0) {
1188 if (strcmp(args[2], "frontend") == 0) {
1189 struct proxy *px;
1190 struct listener *l;
1191 int v;
1192
Willy Tarreau532a4502011-09-07 22:37:44 +02001193 px = expect_frontend_admin(s, si, args[3]);
1194 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001195 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001196
1197 if (!*args[4]) {
1198 si->applet.ctx.cli.msg = "Integer value expected.\n";
1199 si->applet.st0 = STAT_CLI_PRINT;
1200 return 1;
1201 }
1202
1203 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001204 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001205 si->applet.ctx.cli.msg = "Value out of range.\n";
1206 si->applet.st0 = STAT_CLI_PRINT;
1207 return 1;
1208 }
1209
1210 /* OK, the value is fine, so we assign it to the proxy and to all of
1211 * its listeners. The blocked ones will be dequeued.
1212 */
1213 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001214 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001215 l->maxconn = v;
1216 if (l->state == LI_FULL)
1217 resume_listener(l);
1218 }
1219
1220 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1221 dequeue_all_listeners(&s->fe->listener_queue);
1222
1223 return 1;
1224 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001225 else if (strcmp(args[2], "global") == 0) {
1226 int v;
1227
Willy Tarreau290e63a2012-09-20 18:07:14 +02001228 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001229 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1230 si->applet.st0 = STAT_CLI_PRINT;
1231 return 1;
1232 }
1233
1234 if (!*args[3]) {
1235 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1236 si->applet.st0 = STAT_CLI_PRINT;
1237 return 1;
1238 }
1239
1240 v = atoi(args[3]);
1241 if (v > global.hardmaxconn) {
1242 si->applet.ctx.cli.msg = "Value out of range.\n";
1243 si->applet.st0 = STAT_CLI_PRINT;
1244 return 1;
1245 }
1246
1247 /* check for unlimited values */
1248 if (v <= 0)
1249 v = global.hardmaxconn;
1250
1251 global.maxconn = v;
1252
1253 /* Dequeues all of the listeners waiting for a resource */
1254 if (!LIST_ISEMPTY(&global_listener_queue))
1255 dequeue_all_listeners(&global_listener_queue);
1256
1257 return 1;
1258 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001259 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001260 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001261 si->applet.st0 = STAT_CLI_PRINT;
1262 return 1;
1263 }
1264 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001265 else if (strcmp(args[1], "rate-limit") == 0) {
1266 if (strcmp(args[2], "connections") == 0) {
1267 if (strcmp(args[3], "global") == 0) {
1268 int v;
1269
Willy Tarreau290e63a2012-09-20 18:07:14 +02001270 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001271 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1272 si->applet.st0 = STAT_CLI_PRINT;
1273 return 1;
1274 }
1275
1276 if (!*args[4]) {
1277 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1278 si->applet.st0 = STAT_CLI_PRINT;
1279 return 1;
1280 }
1281
1282 v = atoi(args[4]);
1283 if (v < 0) {
1284 si->applet.ctx.cli.msg = "Value out of range.\n";
1285 si->applet.st0 = STAT_CLI_PRINT;
1286 return 1;
1287 }
1288
1289 global.cps_lim = v;
1290
1291 /* Dequeues all of the listeners waiting for a resource */
1292 if (!LIST_ISEMPTY(&global_listener_queue))
1293 dequeue_all_listeners(&global_listener_queue);
1294
1295 return 1;
1296 }
1297 else {
1298 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1299 si->applet.st0 = STAT_CLI_PRINT;
1300 return 1;
1301 }
1302 }
William Lallemandd85f9172012-11-09 17:05:39 +01001303 else if (strcmp(args[2], "http-compression") == 0) {
1304 if (strcmp(args[3], "global") == 0) {
1305 int v;
1306
Willy Tarreau85d47f92012-11-21 00:29:50 +01001307 if (!*args[4]) {
1308 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1309 si->applet.st0 = STAT_CLI_PRINT;
1310 return 1;
1311 }
1312
William Lallemandd85f9172012-11-09 17:05:39 +01001313 v = atoi(args[4]);
1314 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1315 }
1316 else {
1317 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1318 si->applet.st0 = STAT_CLI_PRINT;
1319 return 1;
1320 }
1321 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001322 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001323 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001324 si->applet.st0 = STAT_CLI_PRINT;
1325 return 1;
1326 }
1327 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001328 else if (strcmp(args[1], "table") == 0) {
1329 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1330 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001331 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001332 return 0;
1333 }
1334 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001335 else if (strcmp(args[0], "enable") == 0) {
1336 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001337 struct server *sv;
1338
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001339 sv = expect_server_admin(s, si, args[2]);
1340 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001341 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001342
Cyril Bontécd19e512010-01-31 22:34:03 +01001343 if (sv->state & SRV_MAINTAIN) {
1344 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001345 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001346 /* If this server tracks the status of another one,
1347 * we must restore the good status.
1348 */
Willy Tarreau44267702011-10-28 15:35:33 +02001349 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001350 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001351 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001352 } else {
1353 sv->state &= ~SRV_MAINTAIN;
1354 set_server_down(sv);
1355 }
1356 } else {
1357 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001358 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001359 }
1360 }
1361
Willy Tarreau532a4502011-09-07 22:37:44 +02001362 return 1;
1363 }
1364 else if (strcmp(args[1], "frontend") == 0) {
1365 struct proxy *px;
1366
1367 px = expect_frontend_admin(s, si, args[2]);
1368 if (!px)
1369 return 1;
1370
1371 if (px->state == PR_STSTOPPED) {
1372 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1373 si->applet.st0 = STAT_CLI_PRINT;
1374 return 1;
1375 }
1376
1377 if (px->state != PR_STPAUSED) {
1378 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1379 si->applet.st0 = STAT_CLI_PRINT;
1380 return 1;
1381 }
1382
1383 if (!resume_proxy(px)) {
1384 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1385 si->applet.st0 = STAT_CLI_PRINT;
1386 return 1;
1387 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001388 return 1;
1389 }
1390 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001391 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1392 si->applet.st0 = STAT_CLI_PRINT;
1393 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001394 }
1395 }
1396 else if (strcmp(args[0], "disable") == 0) {
1397 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001398 struct server *sv;
1399
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001400 sv = expect_server_admin(s, si, args[2]);
1401 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001402 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001403
Cyril Bontécd19e512010-01-31 22:34:03 +01001404 if (! (sv->state & SRV_MAINTAIN)) {
1405 /* Not already in maintenance, we can change the server state */
1406 sv->state |= SRV_MAINTAIN;
1407 set_server_down(sv);
1408 }
1409
Willy Tarreau532a4502011-09-07 22:37:44 +02001410 return 1;
1411 }
1412 else if (strcmp(args[1], "frontend") == 0) {
1413 struct proxy *px;
1414
1415 px = expect_frontend_admin(s, si, args[2]);
1416 if (!px)
1417 return 1;
1418
1419 if (px->state == PR_STSTOPPED) {
1420 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1421 si->applet.st0 = STAT_CLI_PRINT;
1422 return 1;
1423 }
1424
1425 if (px->state == PR_STPAUSED) {
1426 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1427 si->applet.st0 = STAT_CLI_PRINT;
1428 return 1;
1429 }
1430
1431 if (!pause_proxy(px)) {
1432 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1433 si->applet.st0 = STAT_CLI_PRINT;
1434 return 1;
1435 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001436 return 1;
1437 }
1438 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001439 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1440 si->applet.st0 = STAT_CLI_PRINT;
1441 return 1;
1442 }
1443 }
1444 else if (strcmp(args[0], "shutdown") == 0) {
1445 if (strcmp(args[1], "frontend") == 0) {
1446 struct proxy *px;
1447
1448 px = expect_frontend_admin(s, si, args[2]);
1449 if (!px)
1450 return 1;
1451
1452 if (px->state == PR_STSTOPPED) {
1453 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1454 si->applet.st0 = STAT_CLI_PRINT;
1455 return 1;
1456 }
1457
1458 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1459 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1460 send_log(px, LOG_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 stop_proxy(px);
1463 return 1;
1464 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001465 else if (strcmp(args[1], "session") == 0) {
1466 struct session *sess, *ptr;
1467
Willy Tarreau290e63a2012-09-20 18:07:14 +02001468 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001469 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1470 si->applet.st0 = STAT_CLI_PRINT;
1471 return 1;
1472 }
1473
1474 if (!*args[2]) {
1475 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1476 si->applet.st0 = STAT_CLI_PRINT;
1477 return 1;
1478 }
1479
1480 ptr = (void *)strtoul(args[2], NULL, 0);
1481
1482 /* first, look for the requested session in the session table */
1483 list_for_each_entry(sess, &sessions, list) {
1484 if (sess == ptr)
1485 break;
1486 }
1487
1488 /* do we have the session ? */
1489 if (sess != ptr) {
1490 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1491 si->applet.st0 = STAT_CLI_PRINT;
1492 return 1;
1493 }
1494
1495 session_shutdown(sess, SN_ERR_KILLED);
1496 return 1;
1497 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001498 else if (strcmp(args[1], "sessions") == 0) {
1499 if (strcmp(args[2], "server") == 0) {
1500 struct server *sv;
1501 struct session *sess, *sess_bck;
1502
1503 sv = expect_server_admin(s, si, args[3]);
1504 if (!sv)
1505 return 1;
1506
1507 /* kill all the session that are on this server */
1508 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1509 if (sess->srv_conn == sv)
1510 session_shutdown(sess, SN_ERR_KILLED);
1511
1512 return 1;
1513 }
1514 else {
1515 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1516 si->applet.st0 = STAT_CLI_PRINT;
1517 return 1;
1518 }
1519 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001520 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001521 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001522 si->applet.st0 = STAT_CLI_PRINT;
1523 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001524 }
1525 }
1526 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001527 return 0;
1528 }
1529 return 1;
1530}
1531
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001533 * used to processes I/O from/to the stats unix socket. The system relies on a
1534 * state machine handling requests and various responses. We read a request,
1535 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001536 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1537 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001538 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001539 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001540static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001541{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001542 struct channel *req = si->ob;
1543 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001544 int reql;
1545 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001546
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001547 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1548 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001550 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001551 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001552 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001553 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001554 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001555 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001557 /* Let's close for real now. We just close the request
1558 * side, the conditions below will complete if needed.
1559 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001560 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001561 break;
1562 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001564 /* ensure we have some output room left in the event we
1565 * would want to return some info right after parsing.
1566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001567 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001568 break;
1569
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001570 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001571 if (reql <= 0) { /* closed or EOL not found */
1572 if (reql == 0)
1573 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001574 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001575 continue;
1576 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001577
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001578 /* seek for a possible semi-colon. If we find one, we
1579 * replace it with an LF and skip only this part.
1580 */
1581 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001582 if (trash.str[len] == ';') {
1583 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001584 reql = len + 1;
1585 break;
1586 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001587
Willy Tarreau816fc222009-10-04 07:36:58 +02001588 /* now it is time to check that we have a full line,
1589 * remove the trailing \n and possibly \r, then cut the
1590 * line.
1591 */
1592 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001593 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001594 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001595 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001596 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001598 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001599 len--;
1600
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001601 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001602
Willy Tarreaubc4af052011-02-13 13:25:14 +01001603 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001604 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001605 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001606 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001607 continue;
1608 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001609 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001610 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001611 else if (strcmp(trash.str, "help") == 0 ||
1612 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001613 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001614 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001615 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001616 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001617 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001618 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001619 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001620 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 /* if prompt is disabled, print help on empty lines,
1622 * so that the user at least knows how to enable
1623 * prompt and find help.
1624 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001625 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001626 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001627 }
1628
1629 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001630 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001631 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001632 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001633 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001634 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001635 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001636 continue;
1637 }
1638
Willy Tarreaubc4af052011-02-13 13:25:14 +01001639 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001640 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001641 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001642 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001643 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001644 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001645 if (stats_dump_info_to_buffer(si))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001646 si->applet.st0 = STAT_CLI_PROMPT;
1647 break;
1648 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001649 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001650 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001651 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001652 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001653 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001654 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001655 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001656 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001657 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001658 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001659 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001660 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001661 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001662 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001663 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001664 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001665 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001666 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667 break;
1668 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001669
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001670 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001671 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001672 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001673 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001674 }
1675
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001676 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001677 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001678 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001679
1680 /* Now we close the output if one of the writers did so,
1681 * or if we're not in interactive mode and the request
1682 * buffer is empty. This still allows pipelined requests
1683 * to be sent in non-interactive mode.
1684 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001685 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001686 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001687 continue;
1688 }
1689
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001690 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001691 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001692 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001693 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001694
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001695 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001696 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1697 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001698 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001699 * and nothing more to consume. This is comparable to a broken pipe, so
1700 * we forward the close to the request side so that it flows upstream to
1701 * the client.
1702 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001703 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001704 }
1705
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001706 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001707 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1708 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1709 /* We have no more processing to do, and nothing more to send, and
1710 * the client side has closed. So we'll forward this state downstream
1711 * on the response buffer.
1712 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001713 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001714 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001715 }
1716
1717 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001718 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001719
1720 /* we don't want to expire timeouts while we're processing requests */
1721 si->ib->rex = TICK_ETERNITY;
1722 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001723
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001724 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001725 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 +02001726 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001727 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 +02001728
1729 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1730 /* check that we have released everything then unregister */
1731 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001732 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001733}
1734
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001735/* This function dumps information onto the stream interface's read buffer.
1736 * It returns 0 as long as it does not complete, non-zero upon completion.
1737 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001738 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001739static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001740{
1741 unsigned int up = (now.tv_sec - start_date.tv_sec);
1742
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001743 chunk_printf(&trash,
1744 "Name: " PRODUCT_NAME "\n"
1745 "Version: " HAPROXY_VERSION "\n"
1746 "Release_date: " HAPROXY_DATE "\n"
1747 "Nbproc: %d\n"
1748 "Process_num: %d\n"
1749 "Pid: %d\n"
1750 "Uptime: %dd %dh%02dm%02ds\n"
1751 "Uptime_sec: %d\n"
1752 "Memmax_MB: %d\n"
1753 "Ulimit-n: %d\n"
1754 "Maxsock: %d\n"
1755 "Maxconn: %d\n"
1756 "Hard_maxconn: %d\n"
1757 "Maxpipes: %d\n"
1758 "CurrConns: %d\n"
1759 "PipesUsed: %d\n"
1760 "PipesFree: %d\n"
1761 "ConnRate: %d\n"
1762 "ConnRateLimit: %d\n"
1763 "MaxConnRate: %d\n"
1764 "CompressBpsIn: %u\n"
1765 "CompressBpsOut: %u\n"
1766 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001767#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001768 "ZlibMemUsage: %ld\n"
1769 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001770#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001771 "Tasks: %d\n"
1772 "Run_queue: %d\n"
1773 "Idle_pct: %d\n"
1774 "node: %s\n"
1775 "description: %s\n"
1776 "",
1777 global.nbproc,
1778 relative_pid,
1779 pid,
1780 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1781 up,
1782 global.rlimit_memmax,
1783 global.rlimit_nofile,
1784 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1785 actconn, pipes_used, pipes_free,
1786 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1787 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1788 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001789#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001790 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001791#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001792 nb_tasks_cur, run_queue_cur, idle_pct,
1793 global.node, global.desc ? global.desc : ""
1794 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001795
1796 if (bi_putchk(si->ib, &trash) == -1)
1797 return 0;
1798
1799 return 1;
1800}
1801
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001802/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1803 * the state from stream interface <si>. The caller is responsible for clearing
1804 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001805 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001806static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001807{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001808 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001809
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001810 if (!(px->cap & PR_CAP_FE))
1811 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001812
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001813 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1814 return 0;
1815
Willy Tarreau354898b2012-12-23 18:15:23 +01001816 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001817 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001818 /* name, queue */
1819 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001820
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001821 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1822 /* Column sub-heading for Enable or Disable server */
1823 chunk_appendf(&trash, "<td></td>");
1824 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001825
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001826 chunk_appendf(&trash,
1827 "<td class=ac>"
1828 "<a name=\"%s/Frontend\"></a>"
1829 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1830 "<td colspan=3></td>"
1831 "",
1832 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001833
Willy Tarreau466c9b52012-12-23 02:25:03 +01001834 chunk_appendf(&trash,
1835 /* sessions rate : current */
1836 "<td><u>%s<div><table class=det>"
1837 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1838 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1839 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001840 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1841 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1842 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001843
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001844 if (px->mode == PR_MODE_HTTP)
1845 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001846 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001847 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001848
1849 chunk_appendf(&trash,
1850 "</table></div></u></td>"
1851 /* sessions rate : max */
1852 "<td><u>%s<div><table class=det>"
1853 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1854 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1855 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001856 U2H(px->fe_counters.sps_max),
1857 U2H(px->fe_counters.cps_max),
1858 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001859
1860 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001861 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001862 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001863 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001864
1865 chunk_appendf(&trash,
1866 "</table></div></u></td>"
1867 /* sessions rate : limit */
1868 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001869 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001870
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001871 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001872 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001873 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001874 "<td><u>%s<div><table class=det>"
1875 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1876 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001877 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001878 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1879 U2H(px->fe_counters.cum_sess),
1880 U2H(px->fe_counters.cum_conn),
1881 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001882
Willy Tarreau466c9b52012-12-23 02:25:03 +01001883 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001884 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001885 chunk_appendf(&trash,
1886 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1887 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1888 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1889 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1890 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1891 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1892 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1893 "<tr><th>- other responses:</th><td>%s</td></tr>"
1894 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1895 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001896 U2H(px->fe_counters.p.http.cum_req),
1897 U2H(px->fe_counters.p.http.rsp[1]),
1898 U2H(px->fe_counters.p.http.rsp[2]),
1899 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001900 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001901 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001902 U2H(px->fe_counters.p.http.rsp[3]),
1903 U2H(px->fe_counters.p.http.rsp[4]),
1904 U2H(px->fe_counters.p.http.rsp[5]),
1905 U2H(px->fe_counters.p.http.rsp[0]),
1906 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001907 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001908
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001909 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001910 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001911 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001912 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001913 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001914 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001915 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001916 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001917
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001918 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001919 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1920 "<td>%s%s<div>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001921 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001922 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001923 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1924 px->fe_counters.comp_in ?
1925 (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 +01001926 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001927
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001928 chunk_appendf(&trash,
1929 /* denied: req, resp */
1930 "<td>%s</td><td>%s</td>"
1931 /* errors : request, connect, response */
1932 "<td>%s</td><td></td><td></td>"
1933 /* warnings: retries, redispatches */
1934 "<td></td><td></td>"
1935 /* server status : reflect frontend status */
1936 "<td class=ac>%s</td>"
1937 /* rest of server: nothing */
1938 "<td class=ac colspan=8></td></tr>"
1939 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001940 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1941 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001942 px->state == PR_STREADY ? "OPEN" :
1943 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001944 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001945 else { /* CSV mode */
1946 chunk_appendf(&trash,
1947 /* pxid, name, queue cur, queue max, */
1948 "%s,FRONTEND,,,"
1949 /* sessions : current, max, limit, total */
1950 "%d,%d,%d,%lld,"
1951 /* bytes : in, out */
1952 "%lld,%lld,"
1953 /* denied: req, resp */
1954 "%lld,%lld,"
1955 /* errors : request, connect, response */
1956 "%lld,,,"
1957 /* warnings: retries, redispatches */
1958 ",,"
1959 /* server status : reflect frontend status */
1960 "%s,"
1961 /* rest of server: nothing */
1962 ",,,,,,,,"
1963 /* pid, iid, sid, throttle, lbtot, tracked, type */
1964 "%d,%d,0,,,,%d,"
1965 /* rate, rate_lim, rate_max */
1966 "%u,%u,%u,"
1967 /* check_status, check_code, check_duration */
1968 ",,,",
1969 px->id,
1970 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1971 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1972 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1973 px->fe_counters.failed_req,
1974 px->state == PR_STREADY ? "OPEN" :
1975 px->state == PR_STFULL ? "FULL" : "STOP",
1976 relative_pid, px->uuid, STATS_TYPE_FE,
1977 read_freq_ctr(&px->fe_sess_per_sec),
1978 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001979
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001980 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1981 if (px->mode == PR_MODE_HTTP) {
1982 for (i=1; i<6; i++)
1983 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1984 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1985 }
1986 else
1987 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001989 /* failed health analyses */
1990 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001991
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001992 /* requests : req_rate, req_rate_max, req_tot, */
1993 chunk_appendf(&trash, "%u,%u,%lld,",
1994 read_freq_ctr(&px->fe_req_per_sec),
1995 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1996
1997 /* errors: cli_aborts, srv_aborts */
1998 chunk_appendf(&trash, ",,");
1999
2000 /* compression: in, out, bypassed */
2001 chunk_appendf(&trash, "%lld,%lld,%lld,",
2002 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2003
2004 /* compression: comp_rsp */
2005 chunk_appendf(&trash, "%lld,",
2006 px->fe_counters.p.http.comp_rsp);
2007
2008 /* finish with EOL */
2009 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002010 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002011 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002012}
2013
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002014/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2015 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2016 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2017 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002018 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002019static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002020{
Willy Tarreau354898b2012-12-23 18:15:23 +01002021 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002022 chunk_appendf(&trash, "<tr class=socket>");
2023 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2024 /* Column sub-heading for Enable or Disable server */
2025 chunk_appendf(&trash, "<td></td>");
2026 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002027 chunk_appendf(&trash,
2028 /* frontend name, listener name */
2029 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2030 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2031 "",
2032 px->id, l->name,
2033 (flags & ST_SHLGNDS)?"<u>":"",
2034 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002035
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002036 if (flags & ST_SHLGNDS) {
2037 char str[INET6_ADDRSTRLEN];
2038 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002039
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002040 chunk_appendf(&trash, "<div>");
Willy Tarreau91861262007-10-17 17:06:05 +02002041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002042 port = get_host_port(&l->addr);
2043 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2044 case AF_INET:
2045 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2046 break;
2047 case AF_INET6:
2048 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2049 break;
2050 case AF_UNIX:
2051 chunk_appendf(&trash, "unix, ");
2052 break;
2053 case -1:
2054 chunk_appendf(&trash, "(%s), ", strerror(errno));
2055 break;
2056 }
Willy Tarreau91861262007-10-17 17:06:05 +02002057
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002058 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002059 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002060 }
Willy Tarreau91861262007-10-17 17:06:05 +02002061
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002062 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002063 /* queue */
2064 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002065 /* sessions rate: current, max, limit */
2066 "<td colspan=3>&nbsp;</td>"
2067 /* sessions: current, max, limit, total, lbtot */
2068 "<td>%s</td><td>%s</td><td>%s</td>"
2069 "<td>%s</td><td>&nbsp;</td>"
2070 /* bytes: in, out */
2071 "<td>%s</td><td>%s</td>"
2072 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002073 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002074 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2075 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002076
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002077 chunk_appendf(&trash,
2078 /* denied: req, resp */
2079 "<td>%s</td><td>%s</td>"
2080 /* errors: request, connect, response */
2081 "<td>%s</td><td></td><td></td>"
2082 /* warnings: retries, redispatches */
2083 "<td></td><td></td>"
2084 /* server status: reflect listener status */
2085 "<td class=ac>%s</td>"
2086 /* rest of server: nothing */
2087 "<td class=ac colspan=8></td></tr>"
2088 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002089 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2090 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002091 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2092 }
2093 else { /* CSV mode */
2094 chunk_appendf(&trash,
2095 /* pxid, name, queue cur, queue max, */
2096 "%s,%s,,,"
2097 /* sessions: current, max, limit, total */
2098 "%d,%d,%d,%lld,"
2099 /* bytes: in, out */
2100 "%lld,%lld,"
2101 /* denied: req, resp */
2102 "%lld,%lld,"
2103 /* errors: request, connect, response */
2104 "%lld,,,"
2105 /* warnings: retries, redispatches */
2106 ",,"
2107 /* server status: reflect listener status */
2108 "%s,"
2109 /* rest of server: nothing */
2110 ",,,,,,,,"
2111 /* pid, iid, sid, throttle, lbtot, tracked, type */
2112 "%d,%d,%d,,,,%d,"
2113 /* rate, rate_lim, rate_max */
2114 ",,,"
2115 /* check_status, check_code, check_duration */
2116 ",,,"
2117 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2118 ",,,,,,"
2119 /* failed health analyses */
2120 ","
2121 /* requests : req_rate, req_rate_max, req_tot, */
2122 ",,,"
2123 /* errors: cli_aborts, srv_aborts */
2124 ",,"
2125 /* compression: in, out, bypassed, comp_rsp */
2126 ",,,,"
2127 "\n",
2128 px->id, l->name,
2129 l->nbconn, l->counters->conn_max,
2130 l->maxconn, l->counters->cum_conn,
2131 l->counters->bytes_in, l->counters->bytes_out,
2132 l->counters->denied_req, l->counters->denied_resp,
2133 l->counters->failed_req,
2134 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2135 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2136 }
2137 return 1;
2138}
Willy Tarreau91861262007-10-17 17:06:05 +02002139
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002140/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2141 * from stream interface <si>, stats flags <flags>, and server state <state>.
2142 * The caller is responsible for clearing the trash if needed. Returns non-zero
2143 * if it emits anything, zero otherwise. The <state> parameter can take the
2144 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked.
2145 */
2146static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2147{
2148 struct server *ref = sv->track ? sv->track : sv;
2149 char str[INET6_ADDRSTRLEN];
2150 struct chunk src;
2151 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002152
Willy Tarreau354898b2012-12-23 18:15:23 +01002153 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002154 static char *srv_hlt_st[7] = {
2155 "DOWN",
2156 "DN %d/%d &uarr;",
2157 "UP %d/%d &darr;",
2158 "UP",
2159 "NOLB %d/%d &darr;",
2160 "NOLB",
2161 "<i>no check</i>"
2162 };
Willy Tarreau91861262007-10-17 17:06:05 +02002163
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002164 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2165 chunk_appendf(&trash, "<tr class=\"maintain\">");
2166 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002167 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002168 "<tr class=\"%s%d\">",
2169 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002171 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002172 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002173 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2174 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002175
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002176 chunk_appendf(&trash,
2177 "<td class=ac><a name=\"%s/%s\"></a>%s"
2178 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2179 "",
2180 px->id, sv->id,
2181 (flags & ST_SHLGNDS) ? "<u>" : "",
2182 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002183
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002184 if (flags & ST_SHLGNDS) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002185 chunk_appendf(&trash, "<div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002186
2187 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2188 case AF_INET:
2189 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2190 break;
2191 case AF_INET6:
2192 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2193 break;
2194 case AF_UNIX:
2195 chunk_appendf(&trash, "unix, ");
2196 break;
2197 case -1:
2198 chunk_appendf(&trash, "(%s), ", strerror(errno));
2199 break;
2200 default: /* address family not supported */
2201 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002202 }
Willy Tarreau91861262007-10-17 17:06:05 +02002203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002204 /* id */
2205 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002206
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002207 /* cookie */
2208 if (sv->cookie) {
2209 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002211 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2212 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002213
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002214 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002215 }
2216
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002217 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002218 }
Willy Tarreau91861262007-10-17 17:06:05 +02002219
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002220 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002221 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002222 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002223 /* sessions rate : current, max, limit */
2224 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002225 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002226 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002227 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2228 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002229
Willy Tarreau466c9b52012-12-23 02:25:03 +01002230
2231 chunk_appendf(&trash,
2232 /* sessions: current, max, limit, total */
2233 "<td>%s</td><td>%s</td><td>%s</td>"
2234 "<td><u>%s<div><table class=det>"
2235 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2236 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002237 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2238 U2H(sv->counters.cum_sess),
2239 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002240
Willy Tarreau466c9b52012-12-23 02:25:03 +01002241 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2242 if (px->mode == PR_MODE_HTTP) {
2243 unsigned long long tot;
2244 for (tot = i = 0; i < 6; i++)
2245 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002246
Willy Tarreau466c9b52012-12-23 02:25:03 +01002247 chunk_appendf(&trash,
2248 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2249 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2250 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2251 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2252 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2253 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2254 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2255 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002256 U2H(tot),
2257 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2258 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2259 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2260 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2261 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2262 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 +02002263 }
Willy Tarreau91861262007-10-17 17:06:05 +02002264
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002265 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002266 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002267 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002268 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002269 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002270
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002271 chunk_appendf(&trash,
2272 /* bytes : in, out */
2273 "<td>%s</td><td>%s</td>"
2274 /* denied: req, resp */
2275 "<td></td><td>%s</td>"
2276 /* errors : request, connect */
2277 "<td></td><td>%s</td>"
2278 /* errors : response */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002279 "<td><u>%s<div>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002280 /* warnings: retries, redispatches */
2281 "<td>%lld</td><td>%lld</td>"
2282 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002283 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2284 U2H(sv->counters.failed_secu),
2285 U2H(sv->counters.failed_conns),
2286 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002287 sv->counters.cli_aborts,
2288 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002289 sv->counters.retries, sv->counters.redispatches);
2290
2291 /* status, lest check */
2292 chunk_appendf(&trash, "<td class=ac>");
2293
2294 if (sv->state & SRV_MAINTAIN) {
2295 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2296 chunk_appendf(&trash, "MAINT");
2297 }
2298 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2299 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2300 chunk_appendf(&trash, "MAINT(via)");
2301 }
2302 else if (ref->state & SRV_CHECKED) {
2303 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2304 chunk_appendf(&trash,
2305 srv_hlt_st[state],
2306 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2307 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002308 }
Willy Tarreau91861262007-10-17 17:06:05 +02002309
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002310 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002311 chunk_appendf(&trash,
2312 "</td><td class=ac><u> %s%s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002313 (sv->state & SRV_CHK_RUNNING) ? "* " : "",
2314 get_check_status_info(sv->check.status));
2315
2316 if (sv->check.status >= HCHK_STATUS_L57DATA)
2317 chunk_appendf(&trash, "/%d", sv->check.code);
2318
2319 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002320 chunk_appendf(&trash, " in %lums", sv->check.duration);
2321
2322 chunk_appendf(&trash, "<div>%s",
2323 get_check_status_description(sv->check.status));
2324 if (*sv->check.desc) {
2325 chunk_appendf(&trash, ": ");
2326 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2327 chunk_htmlencode(&trash, &src);
2328 }
2329 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002330 }
2331 else
2332 chunk_appendf(&trash, "</td><td>");
2333
2334 chunk_appendf(&trash,
2335 /* weight */
2336 "</td><td class=ac>%d</td>"
2337 /* act, bck */
2338 "<td class=ac>%s</td><td class=ac>%s</td>"
2339 "",
2340 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2341 (sv->state & SRV_BACKUP) ? "-" : "Y",
2342 (sv->state & SRV_BACKUP) ? "Y" : "-");
2343
2344 /* check failures: unique, fatal, down time */
2345 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002346 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002347
2348 if (ref->observe)
2349 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2350
2351 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002352 "<div>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002353 "<td>%lld</td><td>%s</td>"
2354 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002355 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002356 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2357 }
2358 else if (sv != ref)
2359 chunk_appendf(&trash,
2360 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2361 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2362 else
2363 chunk_appendf(&trash, "<td colspan=3></td>");
2364
2365 /* throttle */
2366 if ((sv->state & SRV_WARMINGUP) &&
2367 now.tv_sec < sv->last_change + sv->slowstart &&
2368 now.tv_sec >= sv->last_change) {
2369 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n",
2370 (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2371 }
2372 else
2373 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2374 }
2375 else { /* CSV mode */
2376 static char *srv_hlt_st[7] = {
2377 "DOWN,",
2378 "DOWN %d/%d,",
2379 "UP %d/%d,",
2380 "UP,",
2381 "NOLB %d/%d,",
2382 "NOLB,",
2383 "no check,"
2384 };
2385
2386 chunk_appendf(&trash,
2387 /* pxid, name */
2388 "%s,%s,"
2389 /* queue : current, max */
2390 "%d,%d,"
2391 /* sessions : current, max, limit, total */
2392 "%d,%d,%s,%lld,"
2393 /* bytes : in, out */
2394 "%lld,%lld,"
2395 /* denied: req, resp */
2396 ",%lld,"
2397 /* errors : request, connect, response */
2398 ",%lld,%lld,"
2399 /* warnings: retries, redispatches */
2400 "%lld,%lld,"
2401 "",
2402 px->id, sv->id,
2403 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002404 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002405 sv->counters.bytes_in, sv->counters.bytes_out,
2406 sv->counters.failed_secu,
2407 sv->counters.failed_conns, sv->counters.failed_resp,
2408 sv->counters.retries, sv->counters.redispatches);
2409
2410 /* status */
2411 if (sv->state & SRV_MAINTAIN)
2412 chunk_appendf(&trash, "MAINT,");
2413 else if (ref != sv && ref->state & SRV_MAINTAIN)
2414 chunk_appendf(&trash, "MAINT(via),");
2415 else
2416 chunk_appendf(&trash,
2417 srv_hlt_st[state],
2418 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2419 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
2420
2421 chunk_appendf(&trash,
2422 /* weight, active, backup */
2423 "%d,%d,%d,"
2424 "",
2425 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2426 (sv->state & SRV_BACKUP) ? 0 : 1,
2427 (sv->state & SRV_BACKUP) ? 1 : 0);
2428
2429 /* check failures: unique, fatal; last change, total downtime */
2430 if (sv->state & SRV_CHECKED)
2431 chunk_appendf(&trash,
2432 "%lld,%lld,%d,%d,",
2433 sv->counters.failed_checks, sv->counters.down_trans,
2434 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2435 else
2436 chunk_appendf(&trash, ",,,,");
2437
2438 /* queue limit, pid, iid, sid, */
2439 chunk_appendf(&trash,
2440 "%s,"
2441 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002442 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002443 relative_pid, px->uuid, sv->puid);
2444
2445 /* throttle */
2446 if ((sv->state & SRV_WARMINGUP) &&
2447 now.tv_sec < sv->last_change + sv->slowstart &&
2448 now.tv_sec >= sv->last_change)
2449 chunk_appendf(&trash, "%d", (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2450
2451 /* sessions: lbtot */
2452 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2453
2454 /* tracked */
2455 if (sv->track)
2456 chunk_appendf(&trash, "%s/%s,",
2457 sv->track->proxy->id, sv->track->id);
2458 else
2459 chunk_appendf(&trash, ",");
2460
2461 /* type */
2462 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2463
2464 /* rate */
2465 chunk_appendf(&trash, "%u,,%u,",
2466 read_freq_ctr(&sv->sess_per_sec),
2467 sv->counters.sps_max);
2468
2469 if (sv->state & SRV_CHECKED) {
2470 /* check_status */
2471 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2472
2473 /* check_code */
2474 if (sv->check.status >= HCHK_STATUS_L57DATA)
2475 chunk_appendf(&trash, "%u,", sv->check.code);
2476 else
2477 chunk_appendf(&trash, ",");
2478
2479 /* check_duration */
2480 if (sv->check.status >= HCHK_STATUS_CHECKED)
2481 chunk_appendf(&trash, "%lu,", sv->check.duration);
2482 else
2483 chunk_appendf(&trash, ",");
2484
2485 }
2486 else
2487 chunk_appendf(&trash, ",,,");
2488
2489 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2490 if (px->mode == PR_MODE_HTTP) {
2491 for (i=1; i<6; i++)
2492 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2493
2494 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2495 }
2496 else
2497 chunk_appendf(&trash, ",,,,,,");
2498
2499 /* failed health analyses */
2500 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2501
2502 /* requests : req_rate, req_rate_max, req_tot, */
2503 chunk_appendf(&trash, ",,,");
2504
2505 /* errors: cli_aborts, srv_aborts */
2506 chunk_appendf(&trash, "%lld,%lld,",
2507 sv->counters.cli_aborts, sv->counters.srv_aborts);
2508
2509 /* compression: in, out, bypassed, comp_rsp */
2510 chunk_appendf(&trash, ",,,,");
2511
2512 /* finish with EOL */
2513 chunk_appendf(&trash, "\n");
2514 }
2515 return 1;
2516}
2517
2518/* Dumps a line for backend <px> to the trash for and uses the state from stream
2519 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2520 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2521 */
2522static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2523{
2524 struct chunk src;
2525 int i;
2526
2527 if (!(px->cap & PR_CAP_BE))
2528 return 0;
2529
2530 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2531 return 0;
2532
Willy Tarreau354898b2012-12-23 18:15:23 +01002533 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 chunk_appendf(&trash, "<tr class=\"backend\">");
2535 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2536 /* Column sub-heading for Enable or Disable server */
2537 chunk_appendf(&trash, "<td></td>");
2538 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002539 chunk_appendf(&trash,
2540 "<td class=ac>"
2541 /* name */
2542 "%s<a name=\"%s/Backend\"></a>"
2543 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2544 "",
2545 (flags & ST_SHLGNDS)?"<u>":"",
2546 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547
2548 if (flags & ST_SHLGNDS) {
2549 /* balancing */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002550 chunk_appendf(&trash, "<div>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002551 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2552
2553 /* cookie */
2554 if (px->cookie_name) {
2555 chunk_appendf(&trash, ", cookie: '");
2556 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2557 chunk_htmlencode(&trash, &src);
2558 chunk_appendf(&trash, "'");
2559 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002560 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002561 }
2562
2563 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002564 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002565 /* queue : current, max */
2566 "<td>%s</td><td>%s</td><td></td>"
2567 /* sessions rate : current, max, limit */
2568 "<td>%s</td><td>%s</td><td></td>"
2569 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002570 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002571 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2572 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002573
2574 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002575 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002576 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002577 "<td><u>%s<div><table class=det>"
2578 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002580 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2581 U2H(px->be_counters.cum_conn),
2582 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583
Willy Tarreau466c9b52012-12-23 02:25:03 +01002584 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002585 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002586 chunk_appendf(&trash,
2587 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2588 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2589 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2590 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2591 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2592 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2593 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2594 "<tr><th>- other responses:</th><td>%s</td></tr>"
2595 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2596 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002597 U2H(px->be_counters.p.http.cum_req),
2598 U2H(px->be_counters.p.http.rsp[1]),
2599 U2H(px->be_counters.p.http.rsp[2]),
2600 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002601 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002602 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002603 U2H(px->be_counters.p.http.rsp[3]),
2604 U2H(px->be_counters.p.http.rsp[4]),
2605 U2H(px->be_counters.p.http.rsp[5]),
2606 U2H(px->be_counters.p.http.rsp[0]),
2607 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002608 }
2609
2610 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002611 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002612 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002613 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002615 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002617 U2H(px->be_counters.cum_lbconn),
2618 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002619
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002620 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002621 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
2622 "<td>%s%s<div>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002623 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002624 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002625 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2626 px->be_counters.comp_in ?
2627 (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 +01002628 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2629
2630 chunk_appendf(&trash,
2631 /* denied: req, resp */
2632 "<td>%s</td><td>%s</td>"
2633 /* errors : request, connect */
2634 "<td></td><td>%s</td>"
2635 /* errors : response */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002636 "<td><u>%s<div>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637 /* warnings: retries, redispatches */
2638 "<td>%lld</td><td>%lld</td>"
2639 /* backend status: reflect backend status (up/down): we display UP
2640 * if the backend has known working servers or if it has no server at
2641 * all (eg: for stats). Then we display the total weight, number of
2642 * active and backups. */
2643 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2644 "<td class=ac>%d</td><td class=ac>%d</td>"
2645 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002646 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2647 U2H(px->be_counters.failed_conns),
2648 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002649 px->be_counters.cli_aborts,
2650 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 px->be_counters.retries, px->be_counters.redispatches,
2652 human_time(now.tv_sec - px->last_change, 1),
2653 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2654 "<font color=\"red\"><b>DOWN</b></font>",
2655 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2656 px->srv_act, px->srv_bck);
2657
2658 chunk_appendf(&trash,
2659 /* rest of backend: nothing, down transitions, total downtime, throttle */
2660 "<td class=ac>&nbsp;</td><td>%d</td>"
2661 "<td>%s</td>"
2662 "<td></td>"
2663 "</tr>",
2664 px->down_trans,
2665 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2666 }
2667 else { /* CSV mode */
2668 chunk_appendf(&trash,
2669 /* pxid, name */
2670 "%s,BACKEND,"
2671 /* queue : current, max */
2672 "%d,%d,"
2673 /* sessions : current, max, limit, total */
2674 "%d,%d,%d,%lld,"
2675 /* bytes : in, out */
2676 "%lld,%lld,"
2677 /* denied: req, resp */
2678 "%lld,%lld,"
2679 /* errors : request, connect, response */
2680 ",%lld,%lld,"
2681 /* warnings: retries, redispatches */
2682 "%lld,%lld,"
2683 /* backend status: reflect backend status (up/down): we display UP
2684 * if the backend has known working servers or if it has no server at
2685 * all (eg: for stats). Then we display the total weight, number of
2686 * active and backups. */
2687 "%s,"
2688 "%d,%d,%d,"
2689 /* rest of backend: nothing, down transitions, last change, total downtime */
2690 ",%d,%d,%d,,"
2691 /* pid, iid, sid, throttle, lbtot, tracked, type */
2692 "%d,%d,0,,%lld,,%d,"
2693 /* rate, rate_lim, rate_max, */
2694 "%u,,%u,"
2695 /* check_status, check_code, check_duration */
2696 ",,,",
2697 px->id,
2698 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2699 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2700 px->be_counters.bytes_in, px->be_counters.bytes_out,
2701 px->be_counters.denied_req, px->be_counters.denied_resp,
2702 px->be_counters.failed_conns, px->be_counters.failed_resp,
2703 px->be_counters.retries, px->be_counters.redispatches,
2704 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2705 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2706 px->srv_act, px->srv_bck,
2707 px->down_trans, (int)(now.tv_sec - px->last_change),
2708 px->srv?be_downtime(px):0,
2709 relative_pid, px->uuid,
2710 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2711 read_freq_ctr(&px->be_sess_per_sec),
2712 px->be_counters.sps_max);
2713
2714 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2715 if (px->mode == PR_MODE_HTTP) {
2716 for (i=1; i<6; i++)
2717 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2718 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2719 }
2720 else
2721 chunk_appendf(&trash, ",,,,,,");
2722
2723 /* failed health analyses */
2724 chunk_appendf(&trash, ",");
2725
2726 /* requests : req_rate, req_rate_max, req_tot, */
2727 chunk_appendf(&trash, ",,,");
2728
2729 /* errors: cli_aborts, srv_aborts */
2730 chunk_appendf(&trash, "%lld,%lld,",
2731 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2732
2733 /* compression: in, out, bypassed */
2734 chunk_appendf(&trash, "%lld,%lld,%lld,",
2735 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2736
2737 /* compression: comp_rsp */
2738 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2739
2740 /* finish with EOL */
2741 chunk_appendf(&trash, "\n");
2742 }
2743 return 1;
2744}
2745
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002746/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002748 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002750static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002751{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002752 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2753 /* A form to enable/disable this proxy servers */
2754 chunk_appendf(&trash,
2755 "<form action=\"%s\" method=\"post\">",
2756 uri->uri_prefix);
2757 }
2758
2759 /* print a new table */
2760 chunk_appendf(&trash,
2761 "<table class=\"tbl\" width=\"100%%\">\n"
2762 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002763 "<th class=\"pxname\" width=\"10%%\">");
2764
2765 chunk_appendf(&trash,
2766 "<a name=\"%s\"></a>%s"
2767 "<a class=px href=\"#%s\">%s</a>",
2768 px->id,
2769 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2770 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002771
2772 if (uri->flags & ST_SHLGNDS) {
2773 /* cap, mode, id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002774 chunk_appendf(&trash, "<div>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2776 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002777 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 }
2779
2780 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002781 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 "<th class=\"%s\" width=\"90%%\">%s</th>"
2783 "</tr>\n"
2784 "</table>\n"
2785 "<table class=\"tbl\" width=\"100%%\">\n"
2786 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2788 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2789
2790 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2791 /* Column heading for Enable or Disable server */
2792 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002793 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794
2795 chunk_appendf(&trash,
2796 "<th rowspan=2></th>"
2797 "<th colspan=3>Queue</th>"
2798 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2799 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2800 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2801 "<th colspan=9>Server</th>"
2802 "</tr>\n"
2803 "<tr class=\"titre\">"
2804 "<th>Cur</th><th>Max</th><th>Limit</th>"
2805 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2806 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2807 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2808 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2809 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2810 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2811 "<th>Thrtle</th>\n"
2812 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002813}
2814
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002815/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002818static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002819{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002820 chunk_appendf(&trash, "</table>");
2821
2822 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2823 /* close the form used to enable/disable this proxy servers */
2824 chunk_appendf(&trash,
2825 "Choose the action to perform on the checked servers : "
2826 "<select name=action>"
2827 "<option value=\"\"></option>"
2828 "<option value=\"disable\">Disable</option>"
2829 "<option value=\"enable\">Enable</option>"
2830 "<option value=\"stop\">Soft Stop</option>"
2831 "<option value=\"start\">Soft Start</option>"
2832 "<option value=\"shutdown\">Kill Sessions</option>"
2833 "</select>"
2834 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2835 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2836 "</form>",
2837 px->uuid);
2838 }
2839
2840 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841}
Willy Tarreau91861262007-10-17 17:06:05 +02002842
2843/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002844 * Dumps statistics for a proxy. The output is sent to the stream interface's
2845 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2846 * buffer space, or non-zero if everything completed. This function is used
2847 * both by the CLI and the HTTP entry points, and is able to dump the output
2848 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002849 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002850static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002851{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002852 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002853 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002854 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002855 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002856
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002857 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002858
Willy Tarreau295a8372011-03-10 11:25:07 +01002859 switch (si->applet.ctx.stats.px_st) {
2860 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002861 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002862 if (uri && uri->scope) {
2863 /* we have a limited scope, we have to check the proxy name */
2864 struct stat_scope *scope;
2865 int len;
2866
2867 len = strlen(px->id);
2868 scope = uri->scope;
2869
2870 while (scope) {
2871 /* match exact proxy name */
2872 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2873 break;
2874
2875 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002876 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002877 break;
2878 scope = scope->next;
2879 }
2880
2881 /* proxy name not found : don't dump anything */
2882 if (scope == NULL)
2883 return 1;
2884 }
2885
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002886 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2887 (si->applet.ctx.stats.iid != -1) &&
2888 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002889 return 1;
2890
Willy Tarreau295a8372011-03-10 11:25:07 +01002891 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002892 /* fall through */
2893
Willy Tarreau295a8372011-03-10 11:25:07 +01002894 case STAT_PX_ST_TH:
Willy Tarreau354898b2012-12-23 18:15:23 +01002895 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002896 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002897 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002898 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002899 }
Willy Tarreau91861262007-10-17 17:06:05 +02002900
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002902 /* fall through */
2903
Willy Tarreau295a8372011-03-10 11:25:07 +01002904 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002905 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002906 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002907 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002908 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002909
Willy Tarreau4348fad2012-09-20 16:48:07 +02002910 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002911 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002912 /* fall through */
2913
Willy Tarreau295a8372011-03-10 11:25:07 +01002914 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002915 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002916 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 +02002917 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002918 return 0;
2919
Willy Tarreau4348fad2012-09-20 16:48:07 +02002920 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002921 if (!l->counters)
2922 continue;
2923
Willy Tarreau295a8372011-03-10 11:25:07 +01002924 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2925 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002926 break;
2927
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002929 continue;
2930 }
2931
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932 /* print the frontend */
2933 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2934 if (bi_putchk(rep, &trash) == -1)
2935 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002936 }
2937
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2939 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002940 /* fall through */
2941
Willy Tarreau295a8372011-03-10 11:25:07 +01002942 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002943 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002944 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002945 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 +02002946
Willy Tarreau9b28e032012-10-12 23:49:43 +02002947 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002948 return 0;
2949
Willy Tarreau295a8372011-03-10 11:25:07 +01002950 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002951
Willy Tarreau295a8372011-03-10 11:25:07 +01002952 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2953 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002954 break;
2955
Willy Tarreau295a8372011-03-10 11:25:07 +01002956 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002957 continue;
2958 }
2959
Willy Tarreau44267702011-10-28 15:35:33 +02002960 if (sv->track)
2961 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002962 else
2963 svs = sv;
2964
Willy Tarreau91861262007-10-17 17:06:05 +02002965 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002966 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002967 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002968 else if (svs->state & SRV_RUNNING) {
2969 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002970 sv_state = 3; /* UP */
2971 else
2972 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002973
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002974 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002975 sv_state += 2;
2976 }
Willy Tarreau91861262007-10-17 17:06:05 +02002977 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002978 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002979 sv_state = 1; /* going up */
2980 else
2981 sv_state = 0; /* DOWN */
2982
Willy Tarreau295a8372011-03-10 11:25:07 +01002983 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002984 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002985 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002986 continue;
2987 }
2988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002989 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
2990 if (bi_putchk(rep, &trash) == -1)
2991 return 0;
2992 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02002993
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002994 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
2995 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002996
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002997 case STAT_PX_ST_BE:
2998 /* print the backend */
2999 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3000 if (bi_putchk(rep, &trash) == -1)
3001 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
3004 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003005
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 case STAT_PX_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003007 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003008 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003009 if (bi_putchk(rep, &trash) == -1)
3010 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003011 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
3014 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003015
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003016 case STAT_PX_ST_FIN:
3017 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003018
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003019 default:
3020 /* unknown state, we should put an abort() here ! */
3021 return 1;
3022 }
3023}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003024
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003025/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3026 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003028static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029{
3030 /* WARNING! This must fit in the first buffer !!! */
3031 chunk_appendf(&trash,
3032 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3033 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3034 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3035 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3036 "<style type=\"text/css\"><!--\n"
3037 "body {"
3038 " font-family: arial, helvetica, sans-serif;"
3039 " font-size: 12px;"
3040 " font-weight: normal;"
3041 " color: black;"
3042 " background: white;"
3043 "}\n"
3044 "th,td {"
3045 " font-size: 10px;"
3046 "}\n"
3047 "h1 {"
3048 " font-size: x-large;"
3049 " margin-bottom: 0.5em;"
3050 "}\n"
3051 "h2 {"
3052 " font-family: helvetica, arial;"
3053 " font-size: x-large;"
3054 " font-weight: bold;"
3055 " font-style: italic;"
3056 " color: #6020a0;"
3057 " margin-top: 0em;"
3058 " margin-bottom: 0em;"
3059 "}\n"
3060 "h3 {"
3061 " font-family: helvetica, arial;"
3062 " font-size: 16px;"
3063 " font-weight: bold;"
3064 " color: #b00040;"
3065 " background: #e8e8d0;"
3066 " margin-top: 0em;"
3067 " margin-bottom: 0em;"
3068 "}\n"
3069 "li {"
3070 " margin-top: 0.25em;"
3071 " margin-right: 2em;"
3072 "}\n"
3073 ".hr {margin-top: 0.25em;"
3074 " border-color: black;"
3075 " border-bottom-style: solid;"
3076 "}\n"
3077 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3078 ".total {background: #20D0D0;color: #ffff80;}\n"
3079 ".frontend {background: #e8e8d0;}\n"
3080 ".socket {background: #d0d0d0;}\n"
3081 ".backend {background: #e8e8d0;}\n"
3082 ".active0 {background: #ff9090;}\n"
3083 ".active1 {background: #ffd020;}\n"
3084 ".active2 {background: #ffffa0;}\n"
3085 ".active3 {background: #c0ffc0;}\n"
3086 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3087 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
3088 ".active6 {background: #e0e0e0;}\n"
3089 ".backup0 {background: #ff9090;}\n"
3090 ".backup1 {background: #ff80ff;}\n"
3091 ".backup2 {background: #c060ff;}\n"
3092 ".backup3 {background: #b0d0ff;}\n"
3093 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3094 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3095 ".backup6 {background: #e0e0e0;}\n"
3096 ".maintain {background: #c07820;}\n"
3097 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3098 "\n"
3099 "a.px:link {color: #ffff40; text-decoration: none;}"
3100 "a.px:visited {color: #ffff40; text-decoration: none;}"
3101 "a.px:hover {color: #ffffff; text-decoration: none;}"
3102 "a.lfsb:link {color: #000000; text-decoration: none;}"
3103 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3104 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3105 "\n"
3106 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3107 "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"
3108 "table.tbl td.ac { text-align: center;}\n"
3109 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3110 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3111 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3112 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3113 "\n"
3114 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3115 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3116 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003117 "table.det { border-collapse: collapse; border-style: none; }\n"
3118 "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"
3119 "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 +01003120 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003121 "div {\n"
3122 " display:block;\n"
3123 " visibility:hidden;\n"
3124 " z-index:2147483647;\n"
3125 " position:absolute;\n"
3126 " padding:2px 4px 3px;\n"
3127 " background:#f0f060; color:#000000;\n"
3128 " border:1px solid #7040c0;\n"
3129 " white-space:nowrap;\n"
3130 " font-style:normal;font-size:11px;font-weight:normal;\n"
3131 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3132 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3133 "}\n"
3134 "u:hover div {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003135 "-->\n"
3136 "</style></head>\n",
3137 (uri->flags & ST_SHNODE) ? " on " : "",
3138 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3139 );
3140}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003141
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003142/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003144 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003146static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003147{
3148 unsigned int up = (now.tv_sec - start_date.tv_sec);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003149
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003150 /* WARNING! this has to fit the first packet too.
3151 * We are around 3.5 kB, add adding entries will
3152 * become tricky if we want to support 4kB buffers !
3153 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003154 chunk_appendf(&trash,
3155 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3156 PRODUCT_NAME "%s</a></h1>\n"
3157 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3158 "<hr width=\"100%%\" class=\"hr\">\n"
3159 "<h3>&gt; General process information</h3>\n"
3160 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3161 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3162 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3163 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3164 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3165 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3166 "Running tasks: %d/%d; idle = %d %%<br>\n"
3167 "</td><td align=\"center\" nowrap>\n"
3168 "<table class=\"lgd\"><tr>\n"
3169 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3170 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3171 "</tr><tr>\n"
3172 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3173 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3174 "</tr><tr>\n"
3175 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3176 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3177 "</tr><tr>\n"
3178 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3179 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
3180 "</tr><tr>\n"
3181 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3182 "</tr></table>\n"
3183 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3184 "</td>"
3185 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3186 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3187 "",
3188 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3189 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3190 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3191 (uri->flags & ST_SHDESC) ? ": " : "",
3192 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3193 pid, relative_pid, global.nbproc,
3194 up / 86400, (up % 86400) / 3600,
3195 (up % 3600) / 60, (up % 60),
3196 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3197 global.rlimit_memmax ? " MB" : "",
3198 global.rlimit_nofile,
3199 global.maxsock, global.maxconn, global.maxpipes,
3200 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3201 run_queue_cur, nb_tasks_cur, idle_pct
3202 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003204 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3205 chunk_appendf(&trash,
3206 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
3207 uri->uri_prefix,
3208 "",
3209 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
3210 else
3211 chunk_appendf(&trash,
3212 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3213 uri->uri_prefix,
3214 ";up",
3215 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02003216
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 if (uri->refresh > 0) {
3218 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3219 chunk_appendf(&trash,
3220 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
3221 uri->uri_prefix,
3222 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3223 "");
3224 else
3225 chunk_appendf(&trash,
3226 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
3227 uri->uri_prefix,
3228 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3229 ";norefresh");
3230 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003231
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003232 chunk_appendf(&trash,
3233 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
3234 uri->uri_prefix,
3235 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3236 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003237
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003238 chunk_appendf(&trash,
3239 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
3240 uri->uri_prefix,
3241 (uri->refresh > 0) ? ";norefresh" : "");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003242
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003243 chunk_appendf(&trash,
3244 "</ul></td>"
3245 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3246 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3247 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3248 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3249 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3250 "</ul>"
3251 "</td>"
3252 "</tr></table>\n"
3253 ""
3254 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003255
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 if (si->applet.ctx.stats.st_code) {
3257 switch (si->applet.ctx.stats.st_code) {
3258 case STAT_STATUS_DONE:
3259 chunk_appendf(&trash,
3260 "<p><div class=active3>"
3261 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3262 "Action processed successfully."
3263 "</div>\n", uri->uri_prefix);
3264 break;
3265 case STAT_STATUS_NONE:
3266 chunk_appendf(&trash,
3267 "<p><div class=active2>"
3268 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3269 "Nothing has changed."
3270 "</div>\n", uri->uri_prefix);
3271 break;
3272 case STAT_STATUS_PART:
3273 chunk_appendf(&trash,
3274 "<p><div class=active2>"
3275 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3276 "Action partially processed.<br>"
3277 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3278 "</div>\n", uri->uri_prefix);
3279 break;
3280 case STAT_STATUS_ERRP:
3281 chunk_appendf(&trash,
3282 "<p><div class=active0>"
3283 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3284 "Action not processed because of invalid parameters."
3285 "<ul>"
3286 "<li>The action is maybe unknown.</li>"
3287 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3288 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3289 "</ul>"
3290 "</div>\n", uri->uri_prefix);
3291 break;
3292 case STAT_STATUS_EXCD:
3293 chunk_appendf(&trash,
3294 "<p><div class=active0>"
3295 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3296 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3297 "You should retry with less servers at a time.</b>"
3298 "</div>\n", uri->uri_prefix);
3299 break;
3300 case STAT_STATUS_DENY:
3301 chunk_appendf(&trash,
3302 "<p><div class=active0>"
3303 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3304 "<b>Action denied.</b>"
3305 "</div>\n", uri->uri_prefix);
3306 break;
3307 default:
3308 chunk_appendf(&trash,
3309 "<p><div class=active6>"
3310 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3311 "Unexpected result."
3312 "</div>\n", uri->uri_prefix);
3313 }
3314 chunk_appendf(&trash, "<p>\n");
3315 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003317
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003318/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3319 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003321static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003324}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003325
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003326/* This function dumps statistics onto the stream interface's read buffer in
3327 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3328 * are ignored for CSV format (hence <uri> may be NULL there). The xprt_ctx must
3329 * have been zeroed first, and the flags properly set. It returns 0 if it had to
3330 * stop writing data and an I/O is needed, 1 if the dump is finished and the
3331 * session must be closed, or -1 in case of any error. This function is used by
3332 * both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003334static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003336 struct channel *rep = si->ib;
3337 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003338
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003340
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 switch (si->conn->xprt_st) {
3342 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3344 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003345
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003346 case STAT_ST_HEAD:
Willy Tarreau354898b2012-12-23 18:15:23 +01003347 if (si->applet.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003348 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003349 else
3350 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003351
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 if (bi_putchk(rep, &trash) == -1)
3353 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003354
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 si->conn->xprt_st = STAT_ST_INFO;
3356 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003357
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 case STAT_ST_INFO:
Willy Tarreau354898b2012-12-23 18:15:23 +01003359 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003360 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003361 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003362 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 }
Willy Tarreau91861262007-10-17 17:06:05 +02003364
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 si->applet.ctx.stats.px = proxy;
3366 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3367 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003368 /* fall through */
3369
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 case STAT_ST_LIST:
3371 /* dump proxies */
3372 while (si->applet.ctx.stats.px) {
3373 if (buffer_almost_full(rep->buf))
3374 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003375
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 px = si->applet.ctx.stats.px;
3377 /* skip the disabled proxies, global frontend and non-networked ones */
3378 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003379 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003380 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003381
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382 si->applet.ctx.stats.px = px->next;
3383 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3384 }
3385 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003386
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003387 si->conn->xprt_st = STAT_ST_END;
3388 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003389
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003390 case STAT_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003391 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003392 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 if (bi_putchk(rep, &trash) == -1)
3394 return 0;
3395 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003396
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397 si->conn->xprt_st = STAT_ST_FIN;
3398 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003399
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 case STAT_ST_FIN:
3401 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 default:
3404 /* unknown state ! */
3405 si->conn->xprt_st = STAT_ST_FIN;
3406 return -1;
3407 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408}
Willy Tarreauae526782010-03-04 20:34:23 +01003409
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410/* This I/O handler runs as an applet embedded in a stream interface. It is
3411 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3412 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3413 * automatically unregisters itself once transfer is complete.
3414 */
3415static void http_stats_io_handler(struct stream_interface *si)
3416{
3417 struct session *s = si->conn->xprt_ctx;
3418 struct channel *req = si->ob;
3419 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003420
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3422 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003423
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 /* check that the output is not closed */
3425 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3426 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 if (!si->applet.st0) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003429 if (stats_dump_stat_to_buffer(si, s->be->uri_auth)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003430 si->applet.st0 = 1;
3431 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432 }
3433 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3436 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3439 si_shutr(si);
3440 res->flags |= CF_READ_NULL;
3441 }
Willy Tarreau91861262007-10-17 17:06:05 +02003442
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 /* update all other flags and resync with the other side */
3444 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003445
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 /* we don't want to expire timeouts while we're processing requests */
3447 si->ib->rex = TICK_ETERNITY;
3448 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003449
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 out:
3451 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3452 /* check that we have released everything then unregister */
3453 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003454 }
3455}
3456
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003457
Willy Tarreau909d5172012-11-26 03:04:41 +01003458static inline const char *get_conn_ctrl_name(const struct connection *conn)
3459{
3460 if (!conn->ctrl)
3461 return "NONE";
3462 return conn->ctrl->name;
3463}
3464
3465static inline const char *get_conn_xprt_name(const struct connection *conn)
3466{
3467 static char ptr[17];
3468
3469 if (!conn->xprt)
3470 return "NONE";
3471
3472 if (conn->xprt == &raw_sock)
3473 return "RAW";
3474
3475#ifdef USE_OPENSSL
3476 if (conn->xprt == &ssl_sock)
3477 return "SSL";
3478#endif
3479 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3480 return ptr;
3481}
3482
3483static inline const char *get_conn_data_name(const struct connection *conn)
3484{
3485 static char ptr[17];
3486
3487 if (!conn->data)
3488 return "NONE";
3489
3490 if (conn->data == &sess_conn_cb)
3491 return "SESS";
3492
3493 if (conn->data == &si_conn_cb)
3494 return "STRM";
3495
3496 if (conn->data == &check_conn_cb)
3497 return "CHCK";
3498
3499 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3500 return ptr;
3501}
3502
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003503/* This function dumps a complete session state onto the stream interface's
3504 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3505 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003506 * 0 if the output buffer is full and it needs to be called again, otherwise
3507 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003508 */
Willy Tarreau76153662012-11-26 01:16:39 +01003509static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003510{
3511 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003512 extern const char *monthname[12];
3513 char pn[INET6_ADDRSTRLEN];
3514
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003515 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003516
Willy Tarreau295a8372011-03-10 11:25:07 +01003517 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003518 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003519 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3520 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003521 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003522 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003523 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003524 return 1;
3525 }
3526
Willy Tarreau295a8372011-03-10 11:25:07 +01003527 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003528 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003529 si->applet.ctx.sess.uid = sess->uniq_id;
3530 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003531 /* fall through */
3532
3533 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003534 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003535 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003536 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003537 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003538 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3539 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003540 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003541 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003542
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003543 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003544 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003545 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003546 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003547 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003548 break;
3549 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003550 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003551 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003552 default:
3553 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003554 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003555 break;
3556 }
3557
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003558 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003559 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003560 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003562 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003563 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003564 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3565 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3566 sess->listener ? sess->listener->luid : 0);
3567
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003568 conn_get_to_addr(sess->si[0].conn);
3569 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003570 case AF_INET:
3571 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003572 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003573 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003574 break;
3575 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003576 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003577 break;
3578 default:
3579 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003580 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003581 break;
3582 }
3583
Willy Tarreau50943332011-09-02 17:33:05 +02003584 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003585 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003586 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003587 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003588 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3589 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003590 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003591
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003592 conn_get_from_addr(sess->si[1].conn);
3593 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003594 case AF_INET:
3595 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003596 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003597 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003598 break;
3599 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003600 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003601 break;
3602 default:
3603 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003604 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003605 break;
3606 }
3607
3608 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003609 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003610 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003611 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3612 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003613 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003614 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003615
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003616 conn_get_to_addr(sess->si[1].conn);
3617 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003618 case AF_INET:
3619 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003620 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003621 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003622 break;
3623 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003624 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003625 break;
3626 default:
3627 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003628 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003629 break;
3630 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003632 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003633 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003634 sess->task,
3635 sess->task->state,
3636 sess->task->nice, sess->task->calls,
3637 sess->task->expire ?
3638 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3639 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3640 TICKS_TO_MS(1000)) : "<NEVER>",
3641 task_in_rq(sess->task) ? ", running" : "");
3642
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003643 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003644 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003645 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3646
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003647 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003648 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3649 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3650 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3651
3652 chunk_appendf(&trash,
3653 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003654 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003655 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003656 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003657 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003658 sess->si[0].exp ?
3659 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3660 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3661 TICKS_TO_MS(1000)) : "<NEVER>",
3662 sess->si[0].err_type);
3663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003664 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003665 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003666 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003667 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003668 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003669 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003670 sess->si[1].exp ?
3671 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3672 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3673 TICKS_TO_MS(1000)) : "<NEVER>",
3674 sess->si[1].err_type);
3675
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003676 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003677 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003678 sess->si[0].conn,
3679 get_conn_ctrl_name(sess->si[0].conn),
3680 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003681 get_conn_data_name(sess->si[0].conn),
3682 obj_type_name(sess->si[0].conn->target),
3683 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003684
3685 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003686 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003687 sess->si[0].conn->flags,
3688 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003689 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3690 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3691 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 +01003692
3693 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003694 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003695 sess->si[1].conn,
3696 get_conn_ctrl_name(sess->si[1].conn),
3697 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003698 get_conn_data_name(sess->si[1].conn),
3699 obj_type_name(sess->si[1].conn->target),
3700 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003701
3702 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003703 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003704 sess->si[1].conn->flags,
3705 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003706 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3707 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3708 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 +01003709
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003710 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003711 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003712 " an_exp=%s",
3713 sess->req,
3714 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003715 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003716 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003717 sess->req->analyse_exp ?
3718 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3719 TICKS_TO_MS(1000)) : "<NEVER>");
3720
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003721 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003722 " rex=%s",
3723 sess->req->rex ?
3724 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3725 TICKS_TO_MS(1000)) : "<NEVER>");
3726
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003727 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003728 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003729 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003730 sess->req->wex ?
3731 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3732 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003733 sess->req->buf,
3734 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003735 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003736 sess->txn.req.next, sess->req->buf->i,
3737 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003738
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003739 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003740 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003741 " an_exp=%s",
3742 sess->rep,
3743 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003744 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003745 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003746 sess->rep->analyse_exp ?
3747 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3748 TICKS_TO_MS(1000)) : "<NEVER>");
3749
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003750 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003751 " rex=%s",
3752 sess->rep->rex ?
3753 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3754 TICKS_TO_MS(1000)) : "<NEVER>");
3755
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003756 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003757 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003758 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003759 sess->rep->wex ?
3760 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3761 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003762 sess->rep->buf,
3763 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003764 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003765 sess->txn.rsp.next, sess->rep->buf->i,
3766 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003768 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003769 return 0;
3770
3771 /* use other states to dump the contents */
3772 }
3773 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003774 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003775 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003776 return 1;
3777}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003778
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003779/* This function dumps all sessions' states onto the stream interface's
3780 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003781 * properly set. It returns 0 if the output buffer is full and it needs
3782 * to be called again, otherwise non-zero. It is designed to be called
3783 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003784 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003785static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003786{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003787 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003788 /* If we're forced to shut down, we might have to remove our
3789 * reference to the last session being dumped.
3790 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003791 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003792 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3793 LIST_DEL(&si->applet.ctx.sess.bref.users);
3794 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003795 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003796 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003797 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003798 }
3799
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003800 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003801
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003802 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003803 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003804 /* the function had not been called yet, let's prepare the
3805 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003806 * pointer to the first in the global list. When a target
3807 * session is being destroyed, it is responsible for updating
3808 * this pointer. We know we have reached the end when this
3809 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003810 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003811 LIST_INIT(&si->applet.ctx.sess.bref.users);
3812 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003813 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003814 /* fall through */
3815
Willy Tarreau295a8372011-03-10 11:25:07 +01003816 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003817 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003818 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3819 LIST_DEL(&si->applet.ctx.sess.bref.users);
3820 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003821 }
3822
3823 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003824 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003825 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003826 struct session *curr_sess;
3827
Willy Tarreau295a8372011-03-10 11:25:07 +01003828 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003829
Willy Tarreau295a8372011-03-10 11:25:07 +01003830 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003831 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003832 goto next_sess;
3833
Willy Tarreau295a8372011-03-10 11:25:07 +01003834 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003835 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003836 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003837 return 0;
3838
3839 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003840 LIST_DEL(&si->applet.ctx.sess.bref.users);
3841 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003842 if (si->applet.ctx.sess.target != (void *)-1) {
3843 si->applet.ctx.sess.target = NULL;
3844 break;
3845 }
3846 else
3847 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003848 }
3849
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003850 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003851 "%p: proto=%s",
3852 curr_sess,
3853 curr_sess->listener->proto->name);
3854
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003855
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003856 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003857 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003858 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003859 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003860 " src=%s:%d fe=%s be=%s srv=%s",
3861 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003862 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003863 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003864 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003865 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003866 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003867 break;
3868 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003869 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003870 " src=unix:%d fe=%s be=%s srv=%s",
3871 curr_sess->listener->luid,
3872 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003873 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003874 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003875 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003876 break;
3877 }
3878
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003879 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003880 " ts=%02x age=%s calls=%d",
3881 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003882 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3883 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003884
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003885 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003886 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003887 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003888 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003889 curr_sess->req->analysers,
3890 curr_sess->req->rex ?
3891 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3892 TICKS_TO_MS(1000)) : "");
3893
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003894 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003895 ",wx=%s",
3896 curr_sess->req->wex ?
3897 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3898 TICKS_TO_MS(1000)) : "");
3899
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003900 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003901 ",ax=%s]",
3902 curr_sess->req->analyse_exp ?
3903 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3904 TICKS_TO_MS(1000)) : "");
3905
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003906 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003907 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003908 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003909 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003910 curr_sess->rep->analysers,
3911 curr_sess->rep->rex ?
3912 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3913 TICKS_TO_MS(1000)) : "");
3914
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003915 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003916 ",wx=%s",
3917 curr_sess->rep->wex ?
3918 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3919 TICKS_TO_MS(1000)) : "");
3920
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003921 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003922 ",ax=%s]",
3923 curr_sess->rep->analyse_exp ?
3924 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3925 TICKS_TO_MS(1000)) : "");
3926
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003927 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003928 " s0=[%d,%1xh,fd=%d,ex=%s]",
3929 curr_sess->si[0].state,
3930 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003931 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003932 curr_sess->si[0].exp ?
3933 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3934 TICKS_TO_MS(1000)) : "");
3935
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003936 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003937 " s1=[%d,%1xh,fd=%d,ex=%s]",
3938 curr_sess->si[1].state,
3939 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003940 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003941 curr_sess->si[1].exp ?
3942 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3943 TICKS_TO_MS(1000)) : "");
3944
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003945 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003946 " exp=%s",
3947 curr_sess->task->expire ?
3948 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3949 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003950 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003951 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003952
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003955 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003956 /* let's try again later from this session. We add ourselves into
3957 * this session's users so that it can remove us upon termination.
3958 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003959 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003960 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003961 }
3962
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003963 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003964 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003965 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003966
Willy Tarreau76153662012-11-26 01:16:39 +01003967 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003968 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003969 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003970 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003971 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003972 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003973
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003974 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003975 return 0;
3976
Willy Tarreau295a8372011-03-10 11:25:07 +01003977 si->applet.ctx.sess.target = NULL;
3978 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003979 return 1;
3980 }
3981
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003982 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003983 /* fall through */
3984
3985 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003986 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003987 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003988 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003989}
3990
Willy Tarreau5f9a8772012-11-26 02:22:40 +01003991/* This is called when the stream interface is closed. For instance, upon an
3992 * external abort, we won't call the i/o handler anymore so we may need to
3993 * remove back references to the session currently being dumped.
3994 */
3995void cli_release_handler(struct stream_interface *si)
3996{
3997 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
3998 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
3999 LIST_DEL(&si->applet.ctx.sess.bref.users);
4000 }
4001}
4002
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004003/* This function dumps all tables' states onto the stream interface's
4004 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004005 * properly set. It returns 0 if the output buffer is full and it needs
4006 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004007 */
Willy Tarreau44455022012-12-05 23:01:12 +01004008static int stats_table_request(struct stream_interface *si, int show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004009{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004010 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004011 struct ebmb_node *eb;
4012 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004013 int skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004014
4015 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004016 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01004017 * - STAT_ST_INIT : the first call
4018 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004019 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004020 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004021 * and the entry pointer points to the next entry to be dumped,
4022 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004023 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004024 * data though.
4025 */
4026
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004027 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004028 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004029 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004030 si->applet.ctx.table.entry->ref_cnt--;
4031 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004032 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004033 return 1;
4034 }
4035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004036 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004037
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004038 while (si->conn->xprt_st != STAT_ST_FIN) {
4039 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004040 case STAT_ST_INIT:
4041 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4042 if (!si->applet.ctx.table.proxy)
4043 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004044
Willy Tarreau295a8372011-03-10 11:25:07 +01004045 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004046 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004047 break;
4048
Willy Tarreau295a8372011-03-10 11:25:07 +01004049 case STAT_ST_INFO:
4050 if (!si->applet.ctx.table.proxy ||
4051 (si->applet.ctx.table.target &&
4052 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004053 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004054 break;
4055 }
4056
Willy Tarreau295a8372011-03-10 11:25:07 +01004057 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004058 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004059 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004060 return 0;
4061
Willy Tarreau295a8372011-03-10 11:25:07 +01004062 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004063 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004064 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004065 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004066 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004067 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4068 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004069 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004070 break;
4071 }
4072 }
4073 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004074 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004075 break;
4076
Willy Tarreau295a8372011-03-10 11:25:07 +01004077 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004078 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004079
Willy Tarreau295a8372011-03-10 11:25:07 +01004080 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004081 /* we're filtering on some data contents */
4082 void *ptr;
4083 long long data;
4084
Willy Tarreau295a8372011-03-10 11:25:07 +01004085 dt = si->applet.ctx.table.data_type;
4086 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4087 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004088 dt);
4089
4090 data = 0;
4091 switch (stktable_data_types[dt].std_type) {
4092 case STD_T_SINT:
4093 data = stktable_data_cast(ptr, std_t_sint);
4094 break;
4095 case STD_T_UINT:
4096 data = stktable_data_cast(ptr, std_t_uint);
4097 break;
4098 case STD_T_ULL:
4099 data = stktable_data_cast(ptr, std_t_ull);
4100 break;
4101 case STD_T_FRQP:
4102 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004103 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004104 break;
4105 }
4106
4107 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004108 if ((data < si->applet.ctx.table.value &&
4109 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4110 si->applet.ctx.table.data_op == STD_OP_GT ||
4111 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4112 (data == si->applet.ctx.table.value &&
4113 (si->applet.ctx.table.data_op == STD_OP_NE ||
4114 si->applet.ctx.table.data_op == STD_OP_GT ||
4115 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4116 (data > si->applet.ctx.table.value &&
4117 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4118 si->applet.ctx.table.data_op == STD_OP_LT ||
4119 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004120 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004121 }
4122
Simon Hormanc88b8872011-06-15 15:18:49 +09004123 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004124 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004125 si->applet.ctx.table.entry))
4126 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004127
Willy Tarreau295a8372011-03-10 11:25:07 +01004128 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004129
Willy Tarreau295a8372011-03-10 11:25:07 +01004130 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004131 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004132 struct stksess *old = si->applet.ctx.table.entry;
4133 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004134 if (show)
4135 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
4136 else
4137 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004138 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004139 break;
4140 }
4141
Simon Hormanc88b8872011-06-15 15:18:49 +09004142
4143 if (show)
4144 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4145 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4146 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4147
Willy Tarreau295a8372011-03-10 11:25:07 +01004148 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004149 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004150 break;
4151
Willy Tarreau295a8372011-03-10 11:25:07 +01004152 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004153 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004154 break;
4155 }
4156 }
4157 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004158}
4159
Willy Tarreaud426a182010-03-05 14:58:26 +01004160/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004161 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4162 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4163 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4164 * lines are respected within the limit of 70 output chars. Lines that are
4165 * continuation of a previous truncated line begin with "+" instead of " "
4166 * after the offset. The new pointer is returned.
4167 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004168static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4169 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004170{
4171 int end;
4172 unsigned char c;
4173
4174 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004175 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004176 return ptr;
4177
Willy Tarreau77804732012-10-29 16:14:26 +01004178 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004179
Willy Tarreaud426a182010-03-05 14:58:26 +01004180 while (ptr < len && ptr < bsize) {
4181 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004182 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004183 if (out->len > end - 2)
4184 break;
4185 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004186 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004187 if (out->len > end - 3)
4188 break;
4189 out->str[out->len++] = '\\';
4190 switch (c) {
4191 case '\t': c = 't'; break;
4192 case '\n': c = 'n'; break;
4193 case '\r': c = 'r'; break;
4194 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004195 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004196 }
4197 out->str[out->len++] = c;
4198 } else {
4199 if (out->len > end - 5)
4200 break;
4201 out->str[out->len++] = '\\';
4202 out->str[out->len++] = 'x';
4203 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4204 out->str[out->len++] = hextab[c & 0xF];
4205 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004206 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004207 /* we had a line break, let's return now */
4208 out->str[out->len++] = '\n';
4209 *line = ptr;
4210 return ptr;
4211 }
4212 }
4213 /* we have an incomplete line, we return it as-is */
4214 out->str[out->len++] = '\n';
4215 return ptr;
4216}
4217
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004218/* This function dumps all captured errors onto the stream interface's
4219 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004220 * properly set. It returns 0 if the output buffer is full and it needs
4221 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004222 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004223static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004224{
4225 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004226
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004227 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004228 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004229
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004230 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004231
Willy Tarreau295a8372011-03-10 11:25:07 +01004232 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004233 /* the function had not been called yet, let's prepare the
4234 * buffer for a response.
4235 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004236 struct tm tm;
4237
4238 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004239 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004240 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4241 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4242 error_snapshot_id);
4243
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004244 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004245 /* Socket buffer full. Let's try again later from the same point */
4246 return 0;
4247 }
4248
Willy Tarreau295a8372011-03-10 11:25:07 +01004249 si->applet.ctx.errors.px = proxy;
4250 si->applet.ctx.errors.buf = 0;
4251 si->applet.ctx.errors.bol = 0;
4252 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004253 }
4254
4255 /* we have two inner loops here, one for the proxy, the other one for
4256 * the buffer.
4257 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004258 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004259 struct error_snapshot *es;
4260
Willy Tarreau295a8372011-03-10 11:25:07 +01004261 if (si->applet.ctx.errors.buf == 0)
4262 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004263 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004264 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004265
4266 if (!es->when.tv_sec)
4267 goto next;
4268
Willy Tarreau295a8372011-03-10 11:25:07 +01004269 if (si->applet.ctx.errors.iid >= 0 &&
4270 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4271 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004272 goto next;
4273
Willy Tarreau295a8372011-03-10 11:25:07 +01004274 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004275 /* just print headers now */
4276
4277 char pn[INET6_ADDRSTRLEN];
4278 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004279 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004280
4281 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004282 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004283 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004284 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004285
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004286 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4287 case AF_INET:
4288 case AF_INET6:
4289 port = get_host_port(&es->src);
4290 break;
4291 default:
4292 port = 0;
4293 }
4294
Willy Tarreau295a8372011-03-10 11:25:07 +01004295 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004296 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004297 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004298 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004299 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004300 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004301 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4302 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004303 break;
4304 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004305 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004306 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004307 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004308 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004309 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004310 break;
4311 }
4312
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004313 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004314 ", server %s (#%d), event #%u\n"
4315 " src %s:%d, session #%d, session flags 0x%08x\n"
4316 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4317 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4318 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4319 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4320 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4321 es->ev_id,
4322 pn, port, es->sid, es->s_flags,
4323 es->state, es->m_flags, es->t_flags,
4324 es->m_clen, es->m_blen,
4325 es->b_flags, es->b_out, es->b_tot,
4326 es->len, es->b_wrap, es->pos);
4327
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004328 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004329 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004330 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004331 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004332 si->applet.ctx.errors.ptr = 0;
4333 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004334 }
4335
Willy Tarreau295a8372011-03-10 11:25:07 +01004336 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004337 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004338 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004339 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004340 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004341 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004342 goto next;
4343 }
4344
4345 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004346 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004347 int newptr;
4348 int newline;
4349
Willy Tarreau295a8372011-03-10 11:25:07 +01004350 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004351 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 +01004352 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004353 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004354
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004355 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004356 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004357 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004358 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004359 si->applet.ctx.errors.ptr = newptr;
4360 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004361 };
4362 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004363 si->applet.ctx.errors.bol = 0;
4364 si->applet.ctx.errors.ptr = -1;
4365 si->applet.ctx.errors.buf++;
4366 if (si->applet.ctx.errors.buf > 1) {
4367 si->applet.ctx.errors.buf = 0;
4368 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004369 }
4370 }
4371
4372 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004373 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004374}
4375
Willy Tarreaud5781202012-09-22 19:32:35 +02004376/* parse the "level" argument on the bind lines */
4377static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4378{
4379 if (!*args[cur_arg + 1]) {
4380 memprintf(err, "'%s' : missing level", args[cur_arg]);
4381 return ERR_ALERT | ERR_FATAL;
4382 }
4383
4384 if (!strcmp(args[cur_arg+1], "user"))
4385 conf->level = ACCESS_LVL_USER;
4386 else if (!strcmp(args[cur_arg+1], "operator"))
4387 conf->level = ACCESS_LVL_OPER;
4388 else if (!strcmp(args[cur_arg+1], "admin"))
4389 conf->level = ACCESS_LVL_ADMIN;
4390 else {
4391 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4392 args[cur_arg], args[cur_arg+1]);
4393 return ERR_ALERT | ERR_FATAL;
4394 }
4395
4396 return 0;
4397}
4398
Willy Tarreaub24281b2011-02-13 13:16:36 +01004399struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004400 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004401 .name = "<STATS>", /* used for logging */
4402 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004403 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004404};
4405
Simon Horman9bd2c732011-06-15 15:18:44 +09004406static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004407 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004408 .name = "<CLI>", /* used for logging */
4409 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004410 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004411};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004412
Willy Tarreau10522fd2008-07-09 20:12:41 +02004413static struct cfg_kw_list cfg_kws = {{ },{
4414 { CFG_GLOBAL, "stats", stats_parse_global },
4415 { 0, NULL, NULL },
4416}};
4417
Willy Tarreaud5781202012-09-22 19:32:35 +02004418static struct bind_kw_list bind_kws = { "STAT", { }, {
4419 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4420 { NULL, NULL, 0 },
4421}};
4422
Willy Tarreau10522fd2008-07-09 20:12:41 +02004423__attribute__((constructor))
4424static void __dumpstats_module_init(void)
4425{
4426 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004427 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004428}
4429
Willy Tarreau91861262007-10-17 17:06:05 +02004430/*
4431 * Local variables:
4432 * c-indent-level: 8
4433 * c-basic-offset: 8
4434 * End:
4435 */