blob: e23a766aab1e30192dfcb76476415cfbcd2ad49b [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
Willy Tarreau91319572013-04-20 09:48:50 +0200340 if (!global.stats_fe) {
341 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
342 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
343 return -1;
344 }
345 }
346
Willy Tarreau35b7b162012-10-22 23:17:18 +0200347 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100348 unsigned int low, high;
349
Willy Tarreau35b7b162012-10-22 23:17:18 +0200350 if (strcmp(args[cur_arg], "all") == 0) {
351 set = 0;
352 break;
353 }
354 else if (strcmp(args[cur_arg], "odd") == 0) {
355 set |= 0x55555555;
356 }
357 else if (strcmp(args[cur_arg], "even") == 0) {
358 set |= 0xAAAAAAAA;
359 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100360 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100361 char *dash = strchr(args[cur_arg], '-');
362
363 low = high = str2uic(args[cur_arg]);
364 if (dash)
365 high = str2uic(dash + 1);
366
367 if (high < low) {
368 unsigned int swap = low;
369 low = high;
370 high = swap;
371 }
372
373 if (low < 1 || high > 32) {
374 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
375 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200376 return -1;
377 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100378
379 while (low <= high)
380 set |= 1 << (low++ - 1);
381 }
382 else {
383 memprintf(err,
384 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
385 args[0], args[1]);
386 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200387 }
388 cur_arg++;
389 }
390 global.stats_fe->bind_proc = set;
391 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200392 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200393 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200394 return -1;
395 }
396 return 0;
397}
398
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100399/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
400 * for clearing it if needed.
401 */
402static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100403{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100404 chunk_appendf(&trash,
405 "# pxname,svname,"
406 "qcur,qmax,"
407 "scur,smax,slim,stot,"
408 "bin,bout,"
409 "dreq,dresp,"
410 "ereq,econ,eresp,"
411 "wretr,wredis,"
412 "status,weight,act,bck,"
413 "chkfail,chkdown,lastchg,downtime,qlimit,"
414 "pid,iid,sid,throttle,lbtot,tracked,type,"
415 "rate,rate_lim,rate_max,"
416 "check_status,check_code,check_duration,"
417 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
418 "req_rate,req_rate_max,req_tot,"
419 "cli_abrt,srv_abrt,"
420 "comp_in,comp_out,comp_byp,comp_rsp,"
421 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100422}
423
Simon Hormand9366582011-06-15 15:18:45 +0900424/* print a string of text buffer to <out>. The format is :
425 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
426 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
427 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
428 */
429static int dump_text(struct chunk *out, const char *buf, int bsize)
430{
431 unsigned char c;
432 int ptr = 0;
433
434 while (buf[ptr] && ptr < bsize) {
435 c = buf[ptr];
436 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
437 if (out->len > out->size - 1)
438 break;
439 out->str[out->len++] = c;
440 }
441 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
442 if (out->len > out->size - 2)
443 break;
444 out->str[out->len++] = '\\';
445 switch (c) {
446 case ' ': c = ' '; break;
447 case '\t': c = 't'; break;
448 case '\n': c = 'n'; break;
449 case '\r': c = 'r'; break;
450 case '\e': c = 'e'; break;
451 case '\\': c = '\\'; break;
452 }
453 out->str[out->len++] = c;
454 }
455 else {
456 if (out->len > out->size - 4)
457 break;
458 out->str[out->len++] = '\\';
459 out->str[out->len++] = 'x';
460 out->str[out->len++] = hextab[(c >> 4) & 0xF];
461 out->str[out->len++] = hextab[c & 0xF];
462 }
463 ptr++;
464 }
465
466 return ptr;
467}
468
469/* print a buffer in hexa.
470 * Print stopped if <bsize> is reached, or if no more place in the chunk.
471 */
472static int dump_binary(struct chunk *out, const char *buf, int bsize)
473{
474 unsigned char c;
475 int ptr = 0;
476
477 while (ptr < bsize) {
478 c = buf[ptr];
479
480 if (out->len > out->size - 2)
481 break;
482 out->str[out->len++] = hextab[(c >> 4) & 0xF];
483 out->str[out->len++] = hextab[c & 0xF];
484
485 ptr++;
486 }
487 return ptr;
488}
489
490/* Dump the status of a table to a stream interface's
491 * read buffer. It returns 0 if the output buffer is full
492 * and needs to be called again, otherwise non-zero.
493 */
494static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
495 struct proxy *proxy, struct proxy *target)
496{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200497 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900498
Willy Tarreau77804732012-10-29 16:14:26 +0100499 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900500 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
501
502 /* any other information should be dumped here */
503
Willy Tarreau290e63a2012-09-20 18:07:14 +0200504 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100505 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900506
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200507 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900508 return 0;
509
510 return 1;
511}
512
513/* Dump the a table entry to a stream interface's
514 * read buffer. It returns 0 if the output buffer is full
515 * and needs to be called again, otherwise non-zero.
516 */
517static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
518 struct proxy *proxy, struct stksess *entry)
519{
520 int dt;
521
Willy Tarreau77804732012-10-29 16:14:26 +0100522 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900523
524 if (proxy->table.type == STKTABLE_TYPE_IP) {
525 char addr[INET_ADDRSTRLEN];
526 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100527 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900528 }
529 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
530 char addr[INET6_ADDRSTRLEN];
531 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100532 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900533 }
534 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100535 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900536 }
537 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900539 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
540 }
541 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100542 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900543 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
544 }
545
Willy Tarreau77804732012-10-29 16:14:26 +0100546 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900547
548 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
549 void *ptr;
550
551 if (proxy->table.data_ofs[dt] == 0)
552 continue;
553 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100554 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900555 else
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900557
558 ptr = stktable_data_ptr(&proxy->table, entry, dt);
559 switch (stktable_data_types[dt].std_type) {
560 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100561 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900562 break;
563 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100564 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900565 break;
566 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100567 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900568 break;
569 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100570 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900571 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
572 proxy->table.data_arg[dt].u));
573 break;
574 }
575 }
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900577
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200578 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900579 return 0;
580
581 return 1;
582}
583
Willy Tarreaudec98142012-06-06 23:37:08 +0200584static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900585{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200586 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900587 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900588 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900589 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900590 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200591 long long value;
592 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200593 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200594 void *ptr;
595 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900596
Simon Hormand5b9fd92011-06-15 15:18:48 +0900597 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900598
599 if (!*args[4]) {
600 si->applet.ctx.cli.msg = "Key value expected\n";
601 si->applet.st0 = STAT_CLI_PRINT;
602 return;
603 }
604
Simon Hormanc5b89f62011-06-15 15:18:50 +0900605 switch (px->table.type) {
606 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900607 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100608 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900609 break;
610 case STKTABLE_TYPE_IPV6:
611 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100612 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900613 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900614 case STKTABLE_TYPE_INTEGER:
615 {
616 char *endptr;
617 unsigned long val;
618 errno = 0;
619 val = strtoul(args[4], &endptr, 10);
620 if ((errno == ERANGE && val == ULONG_MAX) ||
621 (errno != 0 && val == 0) || endptr == args[4] ||
622 val > 0xffffffff) {
623 si->applet.ctx.cli.msg = "Invalid key\n";
624 si->applet.st0 = STAT_CLI_PRINT;
625 return;
626 }
627 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100628 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900629 break;
630 }
631 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900632 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100633 static_table_key->key = args[4];
634 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900635 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900636 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200637 switch (action) {
638 case STAT_CLI_O_TAB:
639 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
640 break;
641 case STAT_CLI_O_CLR:
642 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
643 break;
644 default:
645 si->applet.ctx.cli.msg = "Unknown action\n";
646 break;
647 }
Simon Horman121f3052011-06-15 15:18:46 +0900648 si->applet.st0 = STAT_CLI_PRINT;
649 return;
650 }
651
652 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200653 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900654 si->applet.ctx.cli.msg = stats_permission_denied_msg;
655 si->applet.st0 = STAT_CLI_PRINT;
656 return;
657 }
658
Willy Tarreau07115412012-10-29 21:56:59 +0100659 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900660
Willy Tarreaudec98142012-06-06 23:37:08 +0200661 switch (action) {
662 case STAT_CLI_O_TAB:
663 if (!ts)
664 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 chunk_reset(&trash);
666 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900667 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100668 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900669 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200670
671 case STAT_CLI_O_CLR:
672 if (!ts)
673 return;
674 if (ts->ref_cnt) {
675 /* don't delete an entry which is currently referenced */
676 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
677 si->applet.st0 = STAT_CLI_PRINT;
678 return;
679 }
680 stksess_kill(&px->table, ts);
681 break;
Simon Horman17bce342011-06-15 15:18:47 +0900682
Willy Tarreau654694e2012-06-07 01:03:16 +0200683 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200684 if (ts)
685 stktable_touch(&px->table, ts, 1);
686 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100687 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200688 if (!ts) {
689 /* don't delete an entry which is currently referenced */
690 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
691 si->applet.st0 = STAT_CLI_PRINT;
692 return;
693 }
694 stktable_store(&px->table, ts, 1);
695 }
696
Willy Tarreau47060b62013-08-01 21:11:42 +0200697 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
698 if (strncmp(args[cur_arg], "data.", 5) != 0) {
699 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
700 si->applet.st0 = STAT_CLI_PRINT;
701 return;
702 }
703
704 data_type = stktable_get_data_type(args[cur_arg] + 5);
705 if (data_type < 0) {
706 si->applet.ctx.cli.msg = "Unknown data type\n";
707 si->applet.st0 = STAT_CLI_PRINT;
708 return;
709 }
710
711 if (!px->table.data_ofs[data_type]) {
712 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
713 si->applet.st0 = STAT_CLI_PRINT;
714 return;
715 }
716
717 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
718 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
719 si->applet.st0 = STAT_CLI_PRINT;
720 return;
721 }
722
723 ptr = stktable_data_ptr(&px->table, ts, data_type);
724
725 switch (stktable_data_types[data_type].std_type) {
726 case STD_T_SINT:
727 stktable_data_cast(ptr, std_t_sint) = value;
728 break;
729 case STD_T_UINT:
730 stktable_data_cast(ptr, std_t_uint) = value;
731 break;
732 case STD_T_ULL:
733 stktable_data_cast(ptr, std_t_ull) = value;
734 break;
735 case STD_T_FRQP:
736 /* We set both the current and previous values. That way
737 * the reported frequency is stable during all the period
738 * then slowly fades out. This allows external tools to
739 * push measures without having to update them too often.
740 */
741 frqp = &stktable_data_cast(ptr, std_t_frqp);
742 frqp->curr_tick = now_ms;
743 frqp->prev_ctr = 0;
744 frqp->curr_ctr = value;
745 break;
746 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200747 }
748 break;
749
Willy Tarreaudec98142012-06-06 23:37:08 +0200750 default:
751 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900752 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200753 break;
Simon Horman121f3052011-06-15 15:18:46 +0900754 }
Simon Horman121f3052011-06-15 15:18:46 +0900755}
756
Willy Tarreau654694e2012-06-07 01:03:16 +0200757static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900758{
Willy Tarreau04b3a192013-04-13 09:41:37 +0200759 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
760 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau654694e2012-06-07 01:03:16 +0200761 si->applet.st0 = STAT_CLI_PRINT;
762 return;
763 }
764
Simon Hormand5b9fd92011-06-15 15:18:48 +0900765 /* condition on stored data value */
766 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
767 if (si->applet.ctx.table.data_type < 0) {
768 si->applet.ctx.cli.msg = "Unknown data type\n";
769 si->applet.st0 = STAT_CLI_PRINT;
770 return;
771 }
772
773 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
774 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
775 si->applet.st0 = STAT_CLI_PRINT;
776 return;
777 }
778
779 si->applet.ctx.table.data_op = get_std_op(args[4]);
780 if (si->applet.ctx.table.data_op < 0) {
781 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
782 si->applet.st0 = STAT_CLI_PRINT;
783 return;
784 }
785
786 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
787 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
788 si->applet.st0 = STAT_CLI_PRINT;
789 return;
790 }
791}
792
Willy Tarreaudec98142012-06-06 23:37:08 +0200793static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900794{
795 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200796 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200797 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900798 si->applet.ctx.table.proxy = NULL;
799 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200800 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900801
802 if (*args[2]) {
803 si->applet.ctx.table.target = find_stktable(args[2]);
804 if (!si->applet.ctx.table.target) {
805 si->applet.ctx.cli.msg = "No such table\n";
806 si->applet.st0 = STAT_CLI_PRINT;
807 return;
808 }
809 }
810 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200811 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812 goto err_args;
813 return;
814 }
815
816 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200817 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900818 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200819 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900820 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900821 goto err_args;
822
823 return;
824
825err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200826 switch (action) {
827 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900828 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 +0200829 break;
830 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900831 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 +0200832 break;
833 default:
834 si->applet.ctx.cli.msg = "Unknown action\n";
835 break;
836 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900837 si->applet.st0 = STAT_CLI_PRINT;
838}
839
Willy Tarreau532a4502011-09-07 22:37:44 +0200840/* Expects to find a frontend named <arg> and returns it, otherwise displays various
841 * adequate error messages and returns NULL. This function also expects the session
842 * level to be admin.
843 */
844static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
845{
846 struct proxy *px;
847
Willy Tarreau290e63a2012-09-20 18:07:14 +0200848 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200849 si->applet.ctx.cli.msg = stats_permission_denied_msg;
850 si->applet.st0 = STAT_CLI_PRINT;
851 return NULL;
852 }
853
854 if (!*arg) {
855 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
856 si->applet.st0 = STAT_CLI_PRINT;
857 return NULL;
858 }
859
860 px = findproxy(arg, PR_CAP_FE);
861 if (!px) {
862 si->applet.ctx.cli.msg = "No such frontend.\n";
863 si->applet.st0 = STAT_CLI_PRINT;
864 return NULL;
865 }
866 return px;
867}
868
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200869/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
870 * and returns the pointer to the server. Otherwise, display adequate error messages
871 * and returns NULL. This function also expects the session level to be admin. Note:
872 * the <arg> is modified to remove the '/'.
873 */
874static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
875{
876 struct proxy *px;
877 struct server *sv;
878 char *line;
879
Willy Tarreau290e63a2012-09-20 18:07:14 +0200880 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200881 si->applet.ctx.cli.msg = stats_permission_denied_msg;
882 si->applet.st0 = STAT_CLI_PRINT;
883 return NULL;
884 }
885
886 /* split "backend/server" and make <line> point to server */
887 for (line = arg; *line; line++)
888 if (*line == '/') {
889 *line++ = '\0';
890 break;
891 }
892
893 if (!*line || !*arg) {
894 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
895 si->applet.st0 = STAT_CLI_PRINT;
896 return NULL;
897 }
898
899 if (!get_backend_server(arg, line, &px, &sv)) {
900 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
901 si->applet.st0 = STAT_CLI_PRINT;
902 return NULL;
903 }
904
905 if (px->state == PR_STSTOPPED) {
906 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
907 si->applet.st0 = STAT_CLI_PRINT;
908 return NULL;
909 }
910
911 return sv;
912}
913
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200914/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200915 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100916 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200917 * designating the function which will have to process the request, which can
918 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200919 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900920static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200921{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200922 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923 char *args[MAX_STATS_ARGS + 1];
924 int arg;
925
926 while (isspace((unsigned char)*line))
927 line++;
928
929 arg = 0;
930 args[arg] = line;
931
932 while (*line && arg < MAX_STATS_ARGS) {
933 if (isspace((unsigned char)*line)) {
934 *line++ = '\0';
935
936 while (isspace((unsigned char)*line))
937 line++;
938
939 args[++arg] = line;
940 continue;
941 }
942
943 line++;
944 }
945
946 while (++arg <= MAX_STATS_ARGS)
947 args[arg] = line;
948
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200950 if (strcmp(args[0], "show") == 0) {
951 if (strcmp(args[1], "stat") == 0) {
952 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100953 si->applet.ctx.stats.flags |= STAT_BOUND;
954 si->applet.ctx.stats.iid = atoi(args[2]);
955 si->applet.ctx.stats.type = atoi(args[3]);
956 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200957 }
958
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200959 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100960 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200961 }
962 else if (strcmp(args[1], "info") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200963 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100964 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200965 }
966 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200967 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200968 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100969 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100970 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200971 return 1;
972 }
Willy Tarreau76153662012-11-26 01:16:39 +0100973 if (*args[2] && strcmp(args[2], "all") == 0)
974 si->applet.ctx.sess.target = (void *)-1;
975 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100976 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100977 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100978 si->applet.ctx.sess.target = NULL;
979 si->applet.ctx.sess.section = 0; /* start with session status */
980 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100981 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200982 }
983 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200984 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100985 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100986 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200987 return 1;
988 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200989 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100990 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200991 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100992 si->applet.ctx.errors.iid = -1;
993 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200994 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100995 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200996 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200997 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200998 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200999 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001000 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001001 return 0;
1002 }
1003 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001004 else if (strcmp(args[0], "clear") == 0) {
1005 if (strcmp(args[1], "counters") == 0) {
1006 struct proxy *px;
1007 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001008 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001009 int clrall = 0;
1010
1011 if (strcmp(args[2], "all") == 0)
1012 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001013
Willy Tarreau6162db22009-10-10 17:13:00 +02001014 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001015 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1016 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001017 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001018 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001019 return 1;
1020 }
1021
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001022 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001023 if (clrall) {
1024 memset(&px->be_counters, 0, sizeof(px->be_counters));
1025 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1026 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001027 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001028 px->be_counters.conn_max = 0;
1029 px->be_counters.p.http.rps_max = 0;
1030 px->be_counters.sps_max = 0;
1031 px->be_counters.cps_max = 0;
1032 px->be_counters.nbpend_max = 0;
1033
1034 px->fe_counters.conn_max = 0;
1035 px->fe_counters.p.http.rps_max = 0;
1036 px->fe_counters.sps_max = 0;
1037 px->fe_counters.cps_max = 0;
1038 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001039 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001040
1041 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001042 if (clrall)
1043 memset(&sv->counters, 0, sizeof(sv->counters));
1044 else {
1045 sv->counters.cur_sess_max = 0;
1046 sv->counters.nbpend_max = 0;
1047 sv->counters.sps_max = 0;
1048 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001049
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001050 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001051 if (li->counters) {
1052 if (clrall)
1053 memset(li->counters, 0, sizeof(*li->counters));
1054 else
1055 li->counters->conn_max = 0;
1056 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001057 }
1058
Willy Tarreau81c25d02011-09-07 15:17:21 +02001059 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001060 return 1;
1061 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001062 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001063 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001064 /* end of processing */
1065 return 1;
1066 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001067 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001068 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001069 return 0;
1070 }
1071 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001072 else if (strcmp(args[0], "get") == 0) {
1073 if (strcmp(args[1], "weight") == 0) {
1074 struct proxy *px;
1075 struct server *sv;
1076
1077 /* split "backend/server" and make <line> point to server */
1078 for (line = args[2]; *line; line++)
1079 if (*line == '/') {
1080 *line++ = '\0';
1081 break;
1082 }
1083
1084 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001085 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001086 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001087 return 1;
1088 }
1089
1090 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001091 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001092 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001093 return 1;
1094 }
1095
1096 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001097 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1098 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001099 return 1;
1100 }
1101 else { /* not "get weight" */
1102 return 0;
1103 }
1104 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001105 else if (strcmp(args[0], "set") == 0) {
1106 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001107 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001108 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001109
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001110 sv = expect_server_admin(s, si, args[2]);
1111 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001112 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001113
Simon Horman7d09b9a2013-02-12 10:45:51 +09001114 warning = server_parse_weight_change_request(sv, args[3]);
1115 if (warning) {
1116 si->applet.ctx.cli.msg = warning;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001117 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001118 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001119 return 1;
1120 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001121 else if (strcmp(args[1], "timeout") == 0) {
1122 if (strcmp(args[2], "cli") == 0) {
1123 unsigned timeout;
1124 const char *res;
1125
1126 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001127 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001128 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001129 return 1;
1130 }
1131
1132 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1133 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001134 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001135 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001136 return 1;
1137 }
1138
1139 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1140 return 1;
1141 }
1142 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001143 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001144 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001145 return 1;
1146 }
1147 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001148 else if (strcmp(args[1], "maxconn") == 0) {
1149 if (strcmp(args[2], "frontend") == 0) {
1150 struct proxy *px;
1151 struct listener *l;
1152 int v;
1153
Willy Tarreau532a4502011-09-07 22:37:44 +02001154 px = expect_frontend_admin(s, si, args[3]);
1155 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001156 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001157
1158 if (!*args[4]) {
1159 si->applet.ctx.cli.msg = "Integer value expected.\n";
1160 si->applet.st0 = STAT_CLI_PRINT;
1161 return 1;
1162 }
1163
1164 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001165 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001166 si->applet.ctx.cli.msg = "Value out of range.\n";
1167 si->applet.st0 = STAT_CLI_PRINT;
1168 return 1;
1169 }
1170
1171 /* OK, the value is fine, so we assign it to the proxy and to all of
1172 * its listeners. The blocked ones will be dequeued.
1173 */
1174 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001175 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001176 l->maxconn = v;
1177 if (l->state == LI_FULL)
1178 resume_listener(l);
1179 }
1180
1181 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1182 dequeue_all_listeners(&s->fe->listener_queue);
1183
1184 return 1;
1185 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001186 else if (strcmp(args[2], "global") == 0) {
1187 int v;
1188
Willy Tarreau290e63a2012-09-20 18:07:14 +02001189 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001190 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1191 si->applet.st0 = STAT_CLI_PRINT;
1192 return 1;
1193 }
1194
1195 if (!*args[3]) {
1196 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1197 si->applet.st0 = STAT_CLI_PRINT;
1198 return 1;
1199 }
1200
1201 v = atoi(args[3]);
1202 if (v > global.hardmaxconn) {
1203 si->applet.ctx.cli.msg = "Value out of range.\n";
1204 si->applet.st0 = STAT_CLI_PRINT;
1205 return 1;
1206 }
1207
1208 /* check for unlimited values */
1209 if (v <= 0)
1210 v = global.hardmaxconn;
1211
1212 global.maxconn = v;
1213
1214 /* Dequeues all of the listeners waiting for a resource */
1215 if (!LIST_ISEMPTY(&global_listener_queue))
1216 dequeue_all_listeners(&global_listener_queue);
1217
1218 return 1;
1219 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001220 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001221 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001222 si->applet.st0 = STAT_CLI_PRINT;
1223 return 1;
1224 }
1225 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001226 else if (strcmp(args[1], "rate-limit") == 0) {
1227 if (strcmp(args[2], "connections") == 0) {
1228 if (strcmp(args[3], "global") == 0) {
1229 int v;
1230
Willy Tarreau290e63a2012-09-20 18:07:14 +02001231 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001232 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1233 si->applet.st0 = STAT_CLI_PRINT;
1234 return 1;
1235 }
1236
1237 if (!*args[4]) {
1238 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1239 si->applet.st0 = STAT_CLI_PRINT;
1240 return 1;
1241 }
1242
1243 v = atoi(args[4]);
1244 if (v < 0) {
1245 si->applet.ctx.cli.msg = "Value out of range.\n";
1246 si->applet.st0 = STAT_CLI_PRINT;
1247 return 1;
1248 }
1249
1250 global.cps_lim = v;
1251
1252 /* Dequeues all of the listeners waiting for a resource */
1253 if (!LIST_ISEMPTY(&global_listener_queue))
1254 dequeue_all_listeners(&global_listener_queue);
1255
1256 return 1;
1257 }
1258 else {
1259 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1260 si->applet.st0 = STAT_CLI_PRINT;
1261 return 1;
1262 }
1263 }
William Lallemandd85f9172012-11-09 17:05:39 +01001264 else if (strcmp(args[2], "http-compression") == 0) {
1265 if (strcmp(args[3], "global") == 0) {
1266 int v;
1267
Willy Tarreau85d47f92012-11-21 00:29:50 +01001268 if (!*args[4]) {
1269 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1270 si->applet.st0 = STAT_CLI_PRINT;
1271 return 1;
1272 }
1273
William Lallemandd85f9172012-11-09 17:05:39 +01001274 v = atoi(args[4]);
1275 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1276 }
1277 else {
1278 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1279 si->applet.st0 = STAT_CLI_PRINT;
1280 return 1;
1281 }
1282 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001283 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001284 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001285 si->applet.st0 = STAT_CLI_PRINT;
1286 return 1;
1287 }
1288 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001289 else if (strcmp(args[1], "table") == 0) {
1290 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1291 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001292 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001293 return 0;
1294 }
1295 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001296 else if (strcmp(args[0], "enable") == 0) {
1297 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001298 struct server *sv;
1299
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001300 sv = expect_server_admin(s, si, args[2]);
1301 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001302 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001303
Cyril Bontécd19e512010-01-31 22:34:03 +01001304 if (sv->state & SRV_MAINTAIN) {
1305 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001306 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001307 /* If this server tracks the status of another one,
1308 * we must restore the good status.
1309 */
Willy Tarreau44267702011-10-28 15:35:33 +02001310 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001311 set_server_up(&sv->check);
Simon Horman125d0992013-02-24 17:23:38 +09001312 sv->check.health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001313 } else {
1314 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001315 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001316 }
1317 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001318 set_server_up(&sv->check);
Simon Horman125d0992013-02-24 17:23:38 +09001319 sv->check.health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001320 }
1321 }
1322
Willy Tarreau532a4502011-09-07 22:37:44 +02001323 return 1;
1324 }
1325 else if (strcmp(args[1], "frontend") == 0) {
1326 struct proxy *px;
1327
1328 px = expect_frontend_admin(s, si, args[2]);
1329 if (!px)
1330 return 1;
1331
1332 if (px->state == PR_STSTOPPED) {
1333 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1334 si->applet.st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337
1338 if (px->state != PR_STPAUSED) {
1339 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1340 si->applet.st0 = STAT_CLI_PRINT;
1341 return 1;
1342 }
1343
1344 if (!resume_proxy(px)) {
1345 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1346 si->applet.st0 = STAT_CLI_PRINT;
1347 return 1;
1348 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001349 return 1;
1350 }
1351 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001352 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1353 si->applet.st0 = STAT_CLI_PRINT;
1354 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001355 }
1356 }
1357 else if (strcmp(args[0], "disable") == 0) {
1358 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001359 struct server *sv;
1360
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001361 sv = expect_server_admin(s, si, args[2]);
1362 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001363 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001364
Cyril Bontécd19e512010-01-31 22:34:03 +01001365 if (! (sv->state & SRV_MAINTAIN)) {
1366 /* Not already in maintenance, we can change the server state */
1367 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001368 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001369 }
1370
Willy Tarreau532a4502011-09-07 22:37:44 +02001371 return 1;
1372 }
1373 else if (strcmp(args[1], "frontend") == 0) {
1374 struct proxy *px;
1375
1376 px = expect_frontend_admin(s, si, args[2]);
1377 if (!px)
1378 return 1;
1379
1380 if (px->state == PR_STSTOPPED) {
1381 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1382 si->applet.st0 = STAT_CLI_PRINT;
1383 return 1;
1384 }
1385
1386 if (px->state == PR_STPAUSED) {
1387 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1388 si->applet.st0 = STAT_CLI_PRINT;
1389 return 1;
1390 }
1391
1392 if (!pause_proxy(px)) {
1393 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1394 si->applet.st0 = STAT_CLI_PRINT;
1395 return 1;
1396 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001397 return 1;
1398 }
1399 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001400 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1401 si->applet.st0 = STAT_CLI_PRINT;
1402 return 1;
1403 }
1404 }
1405 else if (strcmp(args[0], "shutdown") == 0) {
1406 if (strcmp(args[1], "frontend") == 0) {
1407 struct proxy *px;
1408
1409 px = expect_frontend_admin(s, si, args[2]);
1410 if (!px)
1411 return 1;
1412
1413 if (px->state == PR_STSTOPPED) {
1414 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1415 si->applet.st0 = STAT_CLI_PRINT;
1416 return 1;
1417 }
1418
1419 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1420 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1421 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1422 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1423 stop_proxy(px);
1424 return 1;
1425 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001426 else if (strcmp(args[1], "session") == 0) {
1427 struct session *sess, *ptr;
1428
Willy Tarreau290e63a2012-09-20 18:07:14 +02001429 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001430 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1431 si->applet.st0 = STAT_CLI_PRINT;
1432 return 1;
1433 }
1434
1435 if (!*args[2]) {
1436 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1437 si->applet.st0 = STAT_CLI_PRINT;
1438 return 1;
1439 }
1440
1441 ptr = (void *)strtoul(args[2], NULL, 0);
1442
1443 /* first, look for the requested session in the session table */
1444 list_for_each_entry(sess, &sessions, list) {
1445 if (sess == ptr)
1446 break;
1447 }
1448
1449 /* do we have the session ? */
1450 if (sess != ptr) {
1451 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1452 si->applet.st0 = STAT_CLI_PRINT;
1453 return 1;
1454 }
1455
1456 session_shutdown(sess, SN_ERR_KILLED);
1457 return 1;
1458 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001459 else if (strcmp(args[1], "sessions") == 0) {
1460 if (strcmp(args[2], "server") == 0) {
1461 struct server *sv;
1462 struct session *sess, *sess_bck;
1463
1464 sv = expect_server_admin(s, si, args[3]);
1465 if (!sv)
1466 return 1;
1467
1468 /* kill all the session that are on this server */
1469 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1470 if (sess->srv_conn == sv)
1471 session_shutdown(sess, SN_ERR_KILLED);
1472
1473 return 1;
1474 }
1475 else {
1476 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1477 si->applet.st0 = STAT_CLI_PRINT;
1478 return 1;
1479 }
1480 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001481 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001482 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001483 si->applet.st0 = STAT_CLI_PRINT;
1484 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001485 }
1486 }
1487 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001488 return 0;
1489 }
1490 return 1;
1491}
1492
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001493/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001494 * used to processes I/O from/to the stats unix socket. The system relies on a
1495 * state machine handling requests and various responses. We read a request,
1496 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001497 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1498 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001499 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001500 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001501static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001502{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001503 struct channel *req = si->ob;
1504 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001505 int reql;
1506 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001507
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001508 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1509 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001510
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001511 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001514 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001515 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001516 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001517 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001518 /* Let's close for real now. We just close the request
1519 * side, the conditions below will complete if needed.
1520 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001521 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001522 break;
1523 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001524 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001525 /* ensure we have some output room left in the event we
1526 * would want to return some info right after parsing.
1527 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001528 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001529 break;
1530
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001531 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532 if (reql <= 0) { /* closed or EOL not found */
1533 if (reql == 0)
1534 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001535 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001536 continue;
1537 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001538
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001539 /* seek for a possible semi-colon. If we find one, we
1540 * replace it with an LF and skip only this part.
1541 */
1542 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001543 if (trash.str[len] == ';') {
1544 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 reql = len + 1;
1546 break;
1547 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001548
Willy Tarreau816fc222009-10-04 07:36:58 +02001549 /* now it is time to check that we have a full line,
1550 * remove the trailing \n and possibly \r, then cut the
1551 * line.
1552 */
1553 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001554 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001555 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001556 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001557 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001558
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001559 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001560 len--;
1561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001562 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001563
Willy Tarreaubc4af052011-02-13 13:25:14 +01001564 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001565 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001566 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001567 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001568 continue;
1569 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001570 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001571 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001572 else if (strcmp(trash.str, "help") == 0 ||
1573 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001574 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001575 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001576 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001577 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001578 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001579 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001580 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001581 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001582 /* if prompt is disabled, print help on empty lines,
1583 * so that the user at least knows how to enable
1584 * prompt and find help.
1585 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001586 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001587 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001588 }
1589
1590 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001591 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001592 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001593 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001594 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001595 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001596 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597 continue;
1598 }
1599
Willy Tarreaubc4af052011-02-13 13:25:14 +01001600 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001601 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001602 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001603 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001604 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001605 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001606 if (stats_dump_info_to_buffer(si))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001607 si->applet.st0 = STAT_CLI_PROMPT;
1608 break;
1609 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001610 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001611 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001612 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001613 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001614 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001615 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001616 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001617 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001618 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001619 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001620 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001621 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001622 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001623 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001624 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001625 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001626 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001627 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001628 break;
1629 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001630
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001631 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001632 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001633 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001634 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001635 }
1636
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001637 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001638 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001639 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001640
1641 /* Now we close the output if one of the writers did so,
1642 * or if we're not in interactive mode and the request
1643 * buffer is empty. This still allows pipelined requests
1644 * to be sent in non-interactive mode.
1645 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001646 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001647 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001648 continue;
1649 }
1650
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001651 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001652 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001653 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001654 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001655
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001656 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001657 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1658 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001659 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001660 * and nothing more to consume. This is comparable to a broken pipe, so
1661 * we forward the close to the request side so that it flows upstream to
1662 * the client.
1663 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001664 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001665 }
1666
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001667 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001668 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1669 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1670 /* We have no more processing to do, and nothing more to send, and
1671 * the client side has closed. So we'll forward this state downstream
1672 * on the response buffer.
1673 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001674 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001675 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001676 }
1677
1678 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001679 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001680
1681 /* we don't want to expire timeouts while we're processing requests */
1682 si->ib->rex = TICK_ETERNITY;
1683 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001684
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001685 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001686 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 +02001687 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001688 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 +02001689
1690 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1691 /* check that we have released everything then unregister */
1692 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001693 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001694}
1695
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001696/* This function dumps information onto the stream interface's read buffer.
1697 * It returns 0 as long as it does not complete, non-zero upon completion.
1698 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001699 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001700static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001701{
1702 unsigned int up = (now.tv_sec - start_date.tv_sec);
1703
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001704 chunk_printf(&trash,
1705 "Name: " PRODUCT_NAME "\n"
1706 "Version: " HAPROXY_VERSION "\n"
1707 "Release_date: " HAPROXY_DATE "\n"
1708 "Nbproc: %d\n"
1709 "Process_num: %d\n"
1710 "Pid: %d\n"
1711 "Uptime: %dd %dh%02dm%02ds\n"
1712 "Uptime_sec: %d\n"
1713 "Memmax_MB: %d\n"
1714 "Ulimit-n: %d\n"
1715 "Maxsock: %d\n"
1716 "Maxconn: %d\n"
1717 "Hard_maxconn: %d\n"
1718 "Maxpipes: %d\n"
1719 "CurrConns: %d\n"
1720 "PipesUsed: %d\n"
1721 "PipesFree: %d\n"
1722 "ConnRate: %d\n"
1723 "ConnRateLimit: %d\n"
1724 "MaxConnRate: %d\n"
1725 "CompressBpsIn: %u\n"
1726 "CompressBpsOut: %u\n"
1727 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001728#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001729 "ZlibMemUsage: %ld\n"
1730 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001731#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001732 "Tasks: %d\n"
1733 "Run_queue: %d\n"
1734 "Idle_pct: %d\n"
1735 "node: %s\n"
1736 "description: %s\n"
1737 "",
1738 global.nbproc,
1739 relative_pid,
1740 pid,
1741 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1742 up,
1743 global.rlimit_memmax,
1744 global.rlimit_nofile,
1745 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1746 actconn, pipes_used, pipes_free,
1747 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1748 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1749 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001750#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001751 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001752#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001753 nb_tasks_cur, run_queue_cur, idle_pct,
1754 global.node, global.desc ? global.desc : ""
1755 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001756
1757 if (bi_putchk(si->ib, &trash) == -1)
1758 return 0;
1759
1760 return 1;
1761}
1762
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001763/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1764 * the state from stream interface <si>. The caller is responsible for clearing
1765 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001766 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001767static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001768{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001769 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001770
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001771 if (!(px->cap & PR_CAP_FE))
1772 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001773
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001774 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1775 return 0;
1776
Willy Tarreau354898b2012-12-23 18:15:23 +01001777 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001778 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001779 /* name, queue */
1780 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001781
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001782 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1783 /* Column sub-heading for Enable or Disable server */
1784 chunk_appendf(&trash, "<td></td>");
1785 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001786
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001787 chunk_appendf(&trash,
1788 "<td class=ac>"
1789 "<a name=\"%s/Frontend\"></a>"
1790 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1791 "<td colspan=3></td>"
1792 "",
1793 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001794
Willy Tarreau466c9b52012-12-23 02:25:03 +01001795 chunk_appendf(&trash,
1796 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001797 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001798 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1799 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1800 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001801 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1802 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1803 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001804
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001805 if (px->mode == PR_MODE_HTTP)
1806 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001807 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001808 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001809
1810 chunk_appendf(&trash,
1811 "</table></div></u></td>"
1812 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001813 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001814 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1815 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1816 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001817 U2H(px->fe_counters.sps_max),
1818 U2H(px->fe_counters.cps_max),
1819 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001820
1821 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001822 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001823 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001824 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001825
1826 chunk_appendf(&trash,
1827 "</table></div></u></td>"
1828 /* sessions rate : limit */
1829 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001830 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001831
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001832 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001833 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001834 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001835 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001836 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1837 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001838 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001839 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1840 U2H(px->fe_counters.cum_sess),
1841 U2H(px->fe_counters.cum_conn),
1842 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001843
Willy Tarreau466c9b52012-12-23 02:25:03 +01001844 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001845 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001846 chunk_appendf(&trash,
1847 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1848 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1849 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1850 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1851 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1852 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1853 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1854 "<tr><th>- other responses:</th><td>%s</td></tr>"
1855 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1856 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001857 U2H(px->fe_counters.p.http.cum_req),
1858 U2H(px->fe_counters.p.http.rsp[1]),
1859 U2H(px->fe_counters.p.http.rsp[2]),
1860 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001861 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001862 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001863 U2H(px->fe_counters.p.http.rsp[3]),
1864 U2H(px->fe_counters.p.http.rsp[4]),
1865 U2H(px->fe_counters.p.http.rsp[5]),
1866 U2H(px->fe_counters.p.http.rsp[0]),
1867 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001868 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001869
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001870 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001871 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001872 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001873 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001874 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001875 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001876 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001877 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001878
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001879 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001880 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001881 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001882 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001883 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001884 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1885 px->fe_counters.comp_in ?
1886 (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 +01001887 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001888
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001889 chunk_appendf(&trash,
1890 /* denied: req, resp */
1891 "<td>%s</td><td>%s</td>"
1892 /* errors : request, connect, response */
1893 "<td>%s</td><td></td><td></td>"
1894 /* warnings: retries, redispatches */
1895 "<td></td><td></td>"
1896 /* server status : reflect frontend status */
1897 "<td class=ac>%s</td>"
1898 /* rest of server: nothing */
1899 "<td class=ac colspan=8></td></tr>"
1900 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001901 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1902 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001903 px->state == PR_STREADY ? "OPEN" :
1904 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001905 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001906 else { /* CSV mode */
1907 chunk_appendf(&trash,
1908 /* pxid, name, queue cur, queue max, */
1909 "%s,FRONTEND,,,"
1910 /* sessions : current, max, limit, total */
1911 "%d,%d,%d,%lld,"
1912 /* bytes : in, out */
1913 "%lld,%lld,"
1914 /* denied: req, resp */
1915 "%lld,%lld,"
1916 /* errors : request, connect, response */
1917 "%lld,,,"
1918 /* warnings: retries, redispatches */
1919 ",,"
1920 /* server status : reflect frontend status */
1921 "%s,"
1922 /* rest of server: nothing */
1923 ",,,,,,,,"
1924 /* pid, iid, sid, throttle, lbtot, tracked, type */
1925 "%d,%d,0,,,,%d,"
1926 /* rate, rate_lim, rate_max */
1927 "%u,%u,%u,"
1928 /* check_status, check_code, check_duration */
1929 ",,,",
1930 px->id,
1931 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1932 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1933 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1934 px->fe_counters.failed_req,
1935 px->state == PR_STREADY ? "OPEN" :
1936 px->state == PR_STFULL ? "FULL" : "STOP",
1937 relative_pid, px->uuid, STATS_TYPE_FE,
1938 read_freq_ctr(&px->fe_sess_per_sec),
1939 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001940
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001941 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1942 if (px->mode == PR_MODE_HTTP) {
1943 for (i=1; i<6; i++)
1944 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1945 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1946 }
1947 else
1948 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001949
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001950 /* failed health analyses */
1951 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001952
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001953 /* requests : req_rate, req_rate_max, req_tot, */
1954 chunk_appendf(&trash, "%u,%u,%lld,",
1955 read_freq_ctr(&px->fe_req_per_sec),
1956 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1957
1958 /* errors: cli_aborts, srv_aborts */
1959 chunk_appendf(&trash, ",,");
1960
1961 /* compression: in, out, bypassed */
1962 chunk_appendf(&trash, "%lld,%lld,%lld,",
1963 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1964
1965 /* compression: comp_rsp */
1966 chunk_appendf(&trash, "%lld,",
1967 px->fe_counters.p.http.comp_rsp);
1968
1969 /* finish with EOL */
1970 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001971 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001972 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001973}
1974
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001975/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1976 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1977 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1978 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02001979 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001980static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02001981{
Willy Tarreau354898b2012-12-23 18:15:23 +01001982 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001983 chunk_appendf(&trash, "<tr class=socket>");
1984 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1985 /* Column sub-heading for Enable or Disable server */
1986 chunk_appendf(&trash, "<td></td>");
1987 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001988 chunk_appendf(&trash,
1989 /* frontend name, listener name */
1990 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1991 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1992 "",
1993 px->id, l->name,
1994 (flags & ST_SHLGNDS)?"<u>":"",
1995 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02001996
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001997 if (flags & ST_SHLGNDS) {
1998 char str[INET6_ADDRSTRLEN];
1999 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002000
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002001 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002003 port = get_host_port(&l->addr);
2004 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2005 case AF_INET:
2006 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2007 break;
2008 case AF_INET6:
2009 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2010 break;
2011 case AF_UNIX:
2012 chunk_appendf(&trash, "unix, ");
2013 break;
2014 case -1:
2015 chunk_appendf(&trash, "(%s), ", strerror(errno));
2016 break;
2017 }
Willy Tarreau91861262007-10-17 17:06:05 +02002018
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002019 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002020 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002021 }
Willy Tarreau91861262007-10-17 17:06:05 +02002022
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002023 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002024 /* queue */
2025 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002026 /* sessions rate: current, max, limit */
2027 "<td colspan=3>&nbsp;</td>"
2028 /* sessions: current, max, limit, total, lbtot */
2029 "<td>%s</td><td>%s</td><td>%s</td>"
2030 "<td>%s</td><td>&nbsp;</td>"
2031 /* bytes: in, out */
2032 "<td>%s</td><td>%s</td>"
2033 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002034 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002035 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2036 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002037
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002038 chunk_appendf(&trash,
2039 /* denied: req, resp */
2040 "<td>%s</td><td>%s</td>"
2041 /* errors: request, connect, response */
2042 "<td>%s</td><td></td><td></td>"
2043 /* warnings: retries, redispatches */
2044 "<td></td><td></td>"
2045 /* server status: reflect listener status */
2046 "<td class=ac>%s</td>"
2047 /* rest of server: nothing */
2048 "<td class=ac colspan=8></td></tr>"
2049 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002050 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2051 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002052 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2053 }
2054 else { /* CSV mode */
2055 chunk_appendf(&trash,
2056 /* pxid, name, queue cur, queue max, */
2057 "%s,%s,,,"
2058 /* sessions: current, max, limit, total */
2059 "%d,%d,%d,%lld,"
2060 /* bytes: in, out */
2061 "%lld,%lld,"
2062 /* denied: req, resp */
2063 "%lld,%lld,"
2064 /* errors: request, connect, response */
2065 "%lld,,,"
2066 /* warnings: retries, redispatches */
2067 ",,"
2068 /* server status: reflect listener status */
2069 "%s,"
2070 /* rest of server: nothing */
2071 ",,,,,,,,"
2072 /* pid, iid, sid, throttle, lbtot, tracked, type */
2073 "%d,%d,%d,,,,%d,"
2074 /* rate, rate_lim, rate_max */
2075 ",,,"
2076 /* check_status, check_code, check_duration */
2077 ",,,"
2078 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2079 ",,,,,,"
2080 /* failed health analyses */
2081 ","
2082 /* requests : req_rate, req_rate_max, req_tot, */
2083 ",,,"
2084 /* errors: cli_aborts, srv_aborts */
2085 ",,"
2086 /* compression: in, out, bypassed, comp_rsp */
2087 ",,,,"
2088 "\n",
2089 px->id, l->name,
2090 l->nbconn, l->counters->conn_max,
2091 l->maxconn, l->counters->cum_conn,
2092 l->counters->bytes_in, l->counters->bytes_out,
2093 l->counters->denied_req, l->counters->denied_resp,
2094 l->counters->failed_req,
2095 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2096 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2097 }
2098 return 1;
2099}
Willy Tarreau91861262007-10-17 17:06:05 +02002100
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002101/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2102 * from stream interface <si>, stats flags <flags>, and server state <state>.
2103 * The caller is responsible for clearing the trash if needed. Returns non-zero
2104 * if it emits anything, zero otherwise. The <state> parameter can take the
2105 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked.
2106 */
2107static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2108{
2109 struct server *ref = sv->track ? sv->track : sv;
2110 char str[INET6_ADDRSTRLEN];
2111 struct chunk src;
2112 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002113
Willy Tarreau354898b2012-12-23 18:15:23 +01002114 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002115 static char *srv_hlt_st[7] = {
2116 "DOWN",
2117 "DN %d/%d &uarr;",
2118 "UP %d/%d &darr;",
2119 "UP",
2120 "NOLB %d/%d &darr;",
2121 "NOLB",
2122 "<i>no check</i>"
2123 };
Willy Tarreau91861262007-10-17 17:06:05 +02002124
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002125 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2126 chunk_appendf(&trash, "<tr class=\"maintain\">");
Geoff Bucarcc8bb922013-04-18 13:53:16 -07002127 else if (sv->eweight == 0)
2128 chunk_appendf(&trash, "<tr class=\"softstop\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002129 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002130 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002131 "<tr class=\"%s%d\">",
2132 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002133
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002134 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002135 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002136 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2137 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002138
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002139 chunk_appendf(&trash,
2140 "<td class=ac><a name=\"%s/%s\"></a>%s"
2141 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2142 "",
2143 px->id, sv->id,
2144 (flags & ST_SHLGNDS) ? "<u>" : "",
2145 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002146
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002147 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002148 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002149
2150 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2151 case AF_INET:
2152 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2153 break;
2154 case AF_INET6:
2155 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2156 break;
2157 case AF_UNIX:
2158 chunk_appendf(&trash, "unix, ");
2159 break;
2160 case -1:
2161 chunk_appendf(&trash, "(%s), ", strerror(errno));
2162 break;
2163 default: /* address family not supported */
2164 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002165 }
Willy Tarreau91861262007-10-17 17:06:05 +02002166
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002167 /* id */
2168 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002169
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002170 /* cookie */
2171 if (sv->cookie) {
2172 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002173
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002174 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2175 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002176
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002177 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002178 }
2179
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002180 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002181 }
Willy Tarreau91861262007-10-17 17:06:05 +02002182
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002183 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002184 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002185 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002186 /* sessions rate : current, max, limit */
2187 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002188 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002189 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002190 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2191 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002192
Willy Tarreau466c9b52012-12-23 02:25:03 +01002193
2194 chunk_appendf(&trash,
2195 /* sessions: current, max, limit, total */
2196 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002197 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002198 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2199 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002200 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2201 U2H(sv->counters.cum_sess),
2202 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002203
Willy Tarreau466c9b52012-12-23 02:25:03 +01002204 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2205 if (px->mode == PR_MODE_HTTP) {
2206 unsigned long long tot;
2207 for (tot = i = 0; i < 6; i++)
2208 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002209
Willy Tarreau466c9b52012-12-23 02:25:03 +01002210 chunk_appendf(&trash,
2211 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2212 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2213 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2214 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2215 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2216 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2217 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2218 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002219 U2H(tot),
2220 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2221 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2222 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2223 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2224 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2225 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 +02002226 }
Willy Tarreau91861262007-10-17 17:06:05 +02002227
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002228 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002229 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002230 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002231 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002232 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002233
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002234 chunk_appendf(&trash,
2235 /* bytes : in, out */
2236 "<td>%s</td><td>%s</td>"
2237 /* denied: req, resp */
2238 "<td></td><td>%s</td>"
2239 /* errors : request, connect */
2240 "<td></td><td>%s</td>"
2241 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002242 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002243 /* warnings: retries, redispatches */
2244 "<td>%lld</td><td>%lld</td>"
2245 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002246 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2247 U2H(sv->counters.failed_secu),
2248 U2H(sv->counters.failed_conns),
2249 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002250 sv->counters.cli_aborts,
2251 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002252 sv->counters.retries, sv->counters.redispatches);
2253
2254 /* status, lest check */
2255 chunk_appendf(&trash, "<td class=ac>");
2256
2257 if (sv->state & SRV_MAINTAIN) {
2258 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2259 chunk_appendf(&trash, "MAINT");
2260 }
2261 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2262 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2263 chunk_appendf(&trash, "MAINT(via)");
2264 }
2265 else if (ref->state & SRV_CHECKED) {
2266 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2267 chunk_appendf(&trash,
2268 srv_hlt_st[state],
Simon Horman125d0992013-02-24 17:23:38 +09002269 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->rise + 1) : (ref->check.health),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002270 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002271 }
Willy Tarreau91861262007-10-17 17:06:05 +02002272
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002273 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002274 chunk_appendf(&trash,
2275 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002276 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002277 get_check_status_info(sv->check.status));
2278
2279 if (sv->check.status >= HCHK_STATUS_L57DATA)
2280 chunk_appendf(&trash, "/%d", sv->check.code);
2281
2282 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002283 chunk_appendf(&trash, " in %lums", sv->check.duration);
2284
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002285 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002286 get_check_status_description(sv->check.status));
2287 if (*sv->check.desc) {
2288 chunk_appendf(&trash, ": ");
2289 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2290 chunk_htmlencode(&trash, &src);
2291 }
2292 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002293 }
2294 else
2295 chunk_appendf(&trash, "</td><td>");
2296
2297 chunk_appendf(&trash,
2298 /* weight */
2299 "</td><td class=ac>%d</td>"
2300 /* act, bck */
2301 "<td class=ac>%s</td><td class=ac>%s</td>"
2302 "",
2303 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2304 (sv->state & SRV_BACKUP) ? "-" : "Y",
2305 (sv->state & SRV_BACKUP) ? "Y" : "-");
2306
2307 /* check failures: unique, fatal, down time */
2308 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002309 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002310
2311 if (ref->observe)
2312 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2313
2314 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002315 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002316 "<td>%lld</td><td>%s</td>"
2317 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002318 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002319 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2320 }
2321 else if (sv != ref)
2322 chunk_appendf(&trash,
2323 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2324 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2325 else
2326 chunk_appendf(&trash, "<td colspan=3></td>");
2327
2328 /* throttle */
2329 if ((sv->state & SRV_WARMINGUP) &&
2330 now.tv_sec < sv->last_change + sv->slowstart &&
2331 now.tv_sec >= sv->last_change) {
2332 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n",
2333 (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2334 }
2335 else
2336 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2337 }
2338 else { /* CSV mode */
2339 static char *srv_hlt_st[7] = {
2340 "DOWN,",
2341 "DOWN %d/%d,",
2342 "UP %d/%d,",
2343 "UP,",
2344 "NOLB %d/%d,",
2345 "NOLB,",
2346 "no check,"
2347 };
2348
2349 chunk_appendf(&trash,
2350 /* pxid, name */
2351 "%s,%s,"
2352 /* queue : current, max */
2353 "%d,%d,"
2354 /* sessions : current, max, limit, total */
2355 "%d,%d,%s,%lld,"
2356 /* bytes : in, out */
2357 "%lld,%lld,"
2358 /* denied: req, resp */
2359 ",%lld,"
2360 /* errors : request, connect, response */
2361 ",%lld,%lld,"
2362 /* warnings: retries, redispatches */
2363 "%lld,%lld,"
2364 "",
2365 px->id, sv->id,
2366 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002367 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002368 sv->counters.bytes_in, sv->counters.bytes_out,
2369 sv->counters.failed_secu,
2370 sv->counters.failed_conns, sv->counters.failed_resp,
2371 sv->counters.retries, sv->counters.redispatches);
2372
2373 /* status */
2374 if (sv->state & SRV_MAINTAIN)
2375 chunk_appendf(&trash, "MAINT,");
2376 else if (ref != sv && ref->state & SRV_MAINTAIN)
2377 chunk_appendf(&trash, "MAINT(via),");
2378 else
2379 chunk_appendf(&trash,
2380 srv_hlt_st[state],
Simon Horman125d0992013-02-24 17:23:38 +09002381 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->rise + 1) : (ref->check.health),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002382 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
2383
2384 chunk_appendf(&trash,
2385 /* weight, active, backup */
2386 "%d,%d,%d,"
2387 "",
2388 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2389 (sv->state & SRV_BACKUP) ? 0 : 1,
2390 (sv->state & SRV_BACKUP) ? 1 : 0);
2391
2392 /* check failures: unique, fatal; last change, total downtime */
2393 if (sv->state & SRV_CHECKED)
2394 chunk_appendf(&trash,
2395 "%lld,%lld,%d,%d,",
2396 sv->counters.failed_checks, sv->counters.down_trans,
2397 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2398 else
2399 chunk_appendf(&trash, ",,,,");
2400
2401 /* queue limit, pid, iid, sid, */
2402 chunk_appendf(&trash,
2403 "%s,"
2404 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002405 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002406 relative_pid, px->uuid, sv->puid);
2407
2408 /* throttle */
2409 if ((sv->state & SRV_WARMINGUP) &&
2410 now.tv_sec < sv->last_change + sv->slowstart &&
2411 now.tv_sec >= sv->last_change)
2412 chunk_appendf(&trash, "%d", (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2413
2414 /* sessions: lbtot */
2415 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2416
2417 /* tracked */
2418 if (sv->track)
2419 chunk_appendf(&trash, "%s/%s,",
2420 sv->track->proxy->id, sv->track->id);
2421 else
2422 chunk_appendf(&trash, ",");
2423
2424 /* type */
2425 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2426
2427 /* rate */
2428 chunk_appendf(&trash, "%u,,%u,",
2429 read_freq_ctr(&sv->sess_per_sec),
2430 sv->counters.sps_max);
2431
2432 if (sv->state & SRV_CHECKED) {
2433 /* check_status */
2434 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2435
2436 /* check_code */
2437 if (sv->check.status >= HCHK_STATUS_L57DATA)
2438 chunk_appendf(&trash, "%u,", sv->check.code);
2439 else
2440 chunk_appendf(&trash, ",");
2441
2442 /* check_duration */
2443 if (sv->check.status >= HCHK_STATUS_CHECKED)
2444 chunk_appendf(&trash, "%lu,", sv->check.duration);
2445 else
2446 chunk_appendf(&trash, ",");
2447
2448 }
2449 else
2450 chunk_appendf(&trash, ",,,");
2451
2452 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2453 if (px->mode == PR_MODE_HTTP) {
2454 for (i=1; i<6; i++)
2455 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2456
2457 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2458 }
2459 else
2460 chunk_appendf(&trash, ",,,,,,");
2461
2462 /* failed health analyses */
2463 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2464
2465 /* requests : req_rate, req_rate_max, req_tot, */
2466 chunk_appendf(&trash, ",,,");
2467
2468 /* errors: cli_aborts, srv_aborts */
2469 chunk_appendf(&trash, "%lld,%lld,",
2470 sv->counters.cli_aborts, sv->counters.srv_aborts);
2471
2472 /* compression: in, out, bypassed, comp_rsp */
2473 chunk_appendf(&trash, ",,,,");
2474
2475 /* finish with EOL */
2476 chunk_appendf(&trash, "\n");
2477 }
2478 return 1;
2479}
2480
2481/* Dumps a line for backend <px> to the trash for and uses the state from stream
2482 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2483 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2484 */
2485static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2486{
2487 struct chunk src;
2488 int i;
2489
2490 if (!(px->cap & PR_CAP_BE))
2491 return 0;
2492
2493 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2494 return 0;
2495
Willy Tarreau354898b2012-12-23 18:15:23 +01002496 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002497 chunk_appendf(&trash, "<tr class=\"backend\">");
2498 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2499 /* Column sub-heading for Enable or Disable server */
2500 chunk_appendf(&trash, "<td></td>");
2501 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002502 chunk_appendf(&trash,
2503 "<td class=ac>"
2504 /* name */
2505 "%s<a name=\"%s/Backend\"></a>"
2506 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2507 "",
2508 (flags & ST_SHLGNDS)?"<u>":"",
2509 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002510
2511 if (flags & ST_SHLGNDS) {
2512 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002513 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002514 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2515
2516 /* cookie */
2517 if (px->cookie_name) {
2518 chunk_appendf(&trash, ", cookie: '");
2519 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2520 chunk_htmlencode(&trash, &src);
2521 chunk_appendf(&trash, "'");
2522 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002523 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002524 }
2525
2526 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002527 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002528 /* queue : current, max */
2529 "<td>%s</td><td>%s</td><td></td>"
2530 /* sessions rate : current, max, limit */
2531 "<td>%s</td><td>%s</td><td></td>"
2532 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002533 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002534 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2535 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002536
2537 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002538 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002539 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002540 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002541 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002542 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002543 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2544 U2H(px->be_counters.cum_conn),
2545 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002546
Willy Tarreau466c9b52012-12-23 02:25:03 +01002547 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002549 chunk_appendf(&trash,
2550 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2551 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2552 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2553 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2554 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2555 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2556 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2557 "<tr><th>- other responses:</th><td>%s</td></tr>"
2558 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2559 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002560 U2H(px->be_counters.p.http.cum_req),
2561 U2H(px->be_counters.p.http.rsp[1]),
2562 U2H(px->be_counters.p.http.rsp[2]),
2563 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002564 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002565 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002566 U2H(px->be_counters.p.http.rsp[3]),
2567 U2H(px->be_counters.p.http.rsp[4]),
2568 U2H(px->be_counters.p.http.rsp[5]),
2569 U2H(px->be_counters.p.http.rsp[0]),
2570 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 }
2572
2573 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002574 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002575 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002576 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002578 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002580 U2H(px->be_counters.cum_lbconn),
2581 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002584 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002585 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002587 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002588 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2589 px->be_counters.comp_in ?
2590 (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 +01002591 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2592
2593 chunk_appendf(&trash,
2594 /* denied: req, resp */
2595 "<td>%s</td><td>%s</td>"
2596 /* errors : request, connect */
2597 "<td></td><td>%s</td>"
2598 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002599 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002600 /* warnings: retries, redispatches */
2601 "<td>%lld</td><td>%lld</td>"
2602 /* backend status: reflect backend status (up/down): we display UP
2603 * if the backend has known working servers or if it has no server at
2604 * all (eg: for stats). Then we display the total weight, number of
2605 * active and backups. */
2606 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2607 "<td class=ac>%d</td><td class=ac>%d</td>"
2608 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002609 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2610 U2H(px->be_counters.failed_conns),
2611 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 px->be_counters.cli_aborts,
2613 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614 px->be_counters.retries, px->be_counters.redispatches,
2615 human_time(now.tv_sec - px->last_change, 1),
2616 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2617 "<font color=\"red\"><b>DOWN</b></font>",
2618 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2619 px->srv_act, px->srv_bck);
2620
2621 chunk_appendf(&trash,
2622 /* rest of backend: nothing, down transitions, total downtime, throttle */
2623 "<td class=ac>&nbsp;</td><td>%d</td>"
2624 "<td>%s</td>"
2625 "<td></td>"
2626 "</tr>",
2627 px->down_trans,
2628 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2629 }
2630 else { /* CSV mode */
2631 chunk_appendf(&trash,
2632 /* pxid, name */
2633 "%s,BACKEND,"
2634 /* queue : current, max */
2635 "%d,%d,"
2636 /* sessions : current, max, limit, total */
2637 "%d,%d,%d,%lld,"
2638 /* bytes : in, out */
2639 "%lld,%lld,"
2640 /* denied: req, resp */
2641 "%lld,%lld,"
2642 /* errors : request, connect, response */
2643 ",%lld,%lld,"
2644 /* warnings: retries, redispatches */
2645 "%lld,%lld,"
2646 /* backend status: reflect backend status (up/down): we display UP
2647 * if the backend has known working servers or if it has no server at
2648 * all (eg: for stats). Then we display the total weight, number of
2649 * active and backups. */
2650 "%s,"
2651 "%d,%d,%d,"
2652 /* rest of backend: nothing, down transitions, last change, total downtime */
2653 ",%d,%d,%d,,"
2654 /* pid, iid, sid, throttle, lbtot, tracked, type */
2655 "%d,%d,0,,%lld,,%d,"
2656 /* rate, rate_lim, rate_max, */
2657 "%u,,%u,"
2658 /* check_status, check_code, check_duration */
2659 ",,,",
2660 px->id,
2661 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2662 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2663 px->be_counters.bytes_in, px->be_counters.bytes_out,
2664 px->be_counters.denied_req, px->be_counters.denied_resp,
2665 px->be_counters.failed_conns, px->be_counters.failed_resp,
2666 px->be_counters.retries, px->be_counters.redispatches,
2667 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2668 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2669 px->srv_act, px->srv_bck,
2670 px->down_trans, (int)(now.tv_sec - px->last_change),
2671 px->srv?be_downtime(px):0,
2672 relative_pid, px->uuid,
2673 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2674 read_freq_ctr(&px->be_sess_per_sec),
2675 px->be_counters.sps_max);
2676
2677 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2678 if (px->mode == PR_MODE_HTTP) {
2679 for (i=1; i<6; i++)
2680 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2681 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2682 }
2683 else
2684 chunk_appendf(&trash, ",,,,,,");
2685
2686 /* failed health analyses */
2687 chunk_appendf(&trash, ",");
2688
2689 /* requests : req_rate, req_rate_max, req_tot, */
2690 chunk_appendf(&trash, ",,,");
2691
2692 /* errors: cli_aborts, srv_aborts */
2693 chunk_appendf(&trash, "%lld,%lld,",
2694 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2695
2696 /* compression: in, out, bypassed */
2697 chunk_appendf(&trash, "%lld,%lld,%lld,",
2698 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2699
2700 /* compression: comp_rsp */
2701 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2702
2703 /* finish with EOL */
2704 chunk_appendf(&trash, "\n");
2705 }
2706 return 1;
2707}
2708
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002709/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002710 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002711 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002712 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002713static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002714{
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002715 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2716
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002717 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2718 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002719
2720 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2721 scope_txt[0] = 0;
2722 if (si->applet.ctx.stats.scope_len) {
2723 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2724 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
2725 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
2726 }
2727
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002728 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002729 "<form action=\"%s%s%s%s\" method=\"post\">",
2730 uri->uri_prefix,
2731 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2732 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2733 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002734 }
2735
2736 /* print a new table */
2737 chunk_appendf(&trash,
2738 "<table class=\"tbl\" width=\"100%%\">\n"
2739 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002740 "<th class=\"pxname\" width=\"10%%\">");
2741
2742 chunk_appendf(&trash,
2743 "<a name=\"%s\"></a>%s"
2744 "<a class=px href=\"#%s\">%s</a>",
2745 px->id,
2746 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2747 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002748
2749 if (uri->flags & ST_SHLGNDS) {
2750 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002751 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002752 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2753 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002754 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755 }
2756
2757 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002758 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002759 "<th class=\"%s\" width=\"90%%\">%s</th>"
2760 "</tr>\n"
2761 "</table>\n"
2762 "<table class=\"tbl\" width=\"100%%\">\n"
2763 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2765 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2766
2767 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2768 /* Column heading for Enable or Disable server */
2769 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002770 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002771
2772 chunk_appendf(&trash,
2773 "<th rowspan=2></th>"
2774 "<th colspan=3>Queue</th>"
2775 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2776 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2777 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2778 "<th colspan=9>Server</th>"
2779 "</tr>\n"
2780 "<tr class=\"titre\">"
2781 "<th>Cur</th><th>Max</th><th>Limit</th>"
2782 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2783 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2784 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2785 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2786 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2787 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2788 "<th>Thrtle</th>\n"
2789 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002790}
2791
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002792/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002793 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002795static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797 chunk_appendf(&trash, "</table>");
2798
2799 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2800 /* close the form used to enable/disable this proxy servers */
2801 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002802 "Choose the action to perform on the checked servers : "
2803 "<select name=action>"
2804 "<option value=\"\"></option>"
2805 "<option value=\"disable\">Disable</option>"
2806 "<option value=\"enable\">Enable</option>"
2807 "<option value=\"stop\">Soft Stop</option>"
2808 "<option value=\"start\">Soft Start</option>"
2809 "<option value=\"shutdown\">Kill Sessions</option>"
2810 "</select>"
2811 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2812 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2813 "</form>",
2814 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002815 }
2816
2817 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818}
Willy Tarreau91861262007-10-17 17:06:05 +02002819
2820/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002821 * Dumps statistics for a proxy. The output is sent to the stream interface's
2822 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2823 * buffer space, or non-zero if everything completed. This function is used
2824 * both by the CLI and the HTTP entry points, and is able to dump the output
2825 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002826 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002827static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002828{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002829 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002830 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002831 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002832 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002833
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002834 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002835
Willy Tarreau295a8372011-03-10 11:25:07 +01002836 switch (si->applet.ctx.stats.px_st) {
2837 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002838 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002839 if (uri && uri->scope) {
2840 /* we have a limited scope, we have to check the proxy name */
2841 struct stat_scope *scope;
2842 int len;
2843
2844 len = strlen(px->id);
2845 scope = uri->scope;
2846
2847 while (scope) {
2848 /* match exact proxy name */
2849 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2850 break;
2851
2852 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002853 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002854 break;
2855 scope = scope->next;
2856 }
2857
2858 /* proxy name not found : don't dump anything */
2859 if (scope == NULL)
2860 return 1;
2861 }
2862
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002863 /* if the user has requested a limited output and the proxy
2864 * name does not match, skip it.
2865 */
2866 if (si->applet.ctx.stats.scope_len &&
2867 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len) == NULL)
2868 return 1;
2869
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002870 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2871 (si->applet.ctx.stats.iid != -1) &&
2872 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002873 return 1;
2874
Willy Tarreau295a8372011-03-10 11:25:07 +01002875 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002876 /* fall through */
2877
Willy Tarreau295a8372011-03-10 11:25:07 +01002878 case STAT_PX_ST_TH:
Willy Tarreau354898b2012-12-23 18:15:23 +01002879 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002880 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002881 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002882 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002883 }
Willy Tarreau91861262007-10-17 17:06:05 +02002884
Willy Tarreau295a8372011-03-10 11:25:07 +01002885 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002886 /* fall through */
2887
Willy Tarreau295a8372011-03-10 11:25:07 +01002888 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002889 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002890 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002891 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002892 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002893
Willy Tarreau4348fad2012-09-20 16:48:07 +02002894 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002895 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002896 /* fall through */
2897
Willy Tarreau295a8372011-03-10 11:25:07 +01002898 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002899 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002900 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 +02002901 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002902 return 0;
2903
Willy Tarreau4348fad2012-09-20 16:48:07 +02002904 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002905 if (!l->counters)
2906 continue;
2907
Willy Tarreau295a8372011-03-10 11:25:07 +01002908 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2909 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002910 break;
2911
Willy Tarreau295a8372011-03-10 11:25:07 +01002912 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002913 continue;
2914 }
2915
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 /* print the frontend */
2917 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2918 if (bi_putchk(rep, &trash) == -1)
2919 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002920 }
2921
Willy Tarreau295a8372011-03-10 11:25:07 +01002922 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2923 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002924 /* fall through */
2925
Willy Tarreau295a8372011-03-10 11:25:07 +01002926 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002927 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002929 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 +02002930
Willy Tarreau9b28e032012-10-12 23:49:43 +02002931 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002932 return 0;
2933
Willy Tarreau295a8372011-03-10 11:25:07 +01002934 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002935
Willy Tarreau295a8372011-03-10 11:25:07 +01002936 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2937 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002938 break;
2939
Willy Tarreau295a8372011-03-10 11:25:07 +01002940 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002941 continue;
2942 }
2943
Willy Tarreau44267702011-10-28 15:35:33 +02002944 if (sv->track)
2945 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002946 else
2947 svs = sv;
2948
Willy Tarreau91861262007-10-17 17:06:05 +02002949 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002950 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002951 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002952 else if (svs->state & SRV_RUNNING) {
Simon Horman125d0992013-02-24 17:23:38 +09002953 if (svs->check.health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002954 sv_state = 3; /* UP */
2955 else
2956 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002957
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002958 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002959 sv_state += 2;
2960 }
Willy Tarreau91861262007-10-17 17:06:05 +02002961 else
Simon Horman125d0992013-02-24 17:23:38 +09002962 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02002963 sv_state = 1; /* going up */
2964 else
2965 sv_state = 0; /* DOWN */
2966
Willy Tarreau295a8372011-03-10 11:25:07 +01002967 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002968 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002970 continue;
2971 }
2972
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
2974 if (bi_putchk(rep, &trash) == -1)
2975 return 0;
2976 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02002977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002978 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
2979 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002980
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002981 case STAT_PX_ST_BE:
2982 /* print the backend */
2983 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
2984 if (bi_putchk(rep, &trash) == -1)
2985 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002986
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002987 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
2988 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002989
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990 case STAT_PX_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01002991 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002992 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993 if (bi_putchk(rep, &trash) == -1)
2994 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002995 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002996
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002997 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
2998 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003000 case STAT_PX_ST_FIN:
3001 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003 default:
3004 /* unknown state, we should put an abort() here ! */
3005 return 1;
3006 }
3007}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003008
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003009/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3010 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003012static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013{
3014 /* WARNING! This must fit in the first buffer !!! */
3015 chunk_appendf(&trash,
3016 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3017 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3018 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3019 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3020 "<style type=\"text/css\"><!--\n"
3021 "body {"
3022 " font-family: arial, helvetica, sans-serif;"
3023 " font-size: 12px;"
3024 " font-weight: normal;"
3025 " color: black;"
3026 " background: white;"
3027 "}\n"
3028 "th,td {"
3029 " font-size: 10px;"
3030 "}\n"
3031 "h1 {"
3032 " font-size: x-large;"
3033 " margin-bottom: 0.5em;"
3034 "}\n"
3035 "h2 {"
3036 " font-family: helvetica, arial;"
3037 " font-size: x-large;"
3038 " font-weight: bold;"
3039 " font-style: italic;"
3040 " color: #6020a0;"
3041 " margin-top: 0em;"
3042 " margin-bottom: 0em;"
3043 "}\n"
3044 "h3 {"
3045 " font-family: helvetica, arial;"
3046 " font-size: 16px;"
3047 " font-weight: bold;"
3048 " color: #b00040;"
3049 " background: #e8e8d0;"
3050 " margin-top: 0em;"
3051 " margin-bottom: 0em;"
3052 "}\n"
3053 "li {"
3054 " margin-top: 0.25em;"
3055 " margin-right: 2em;"
3056 "}\n"
3057 ".hr {margin-top: 0.25em;"
3058 " border-color: black;"
3059 " border-bottom-style: solid;"
3060 "}\n"
3061 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3062 ".total {background: #20D0D0;color: #ffff80;}\n"
3063 ".frontend {background: #e8e8d0;}\n"
3064 ".socket {background: #d0d0d0;}\n"
3065 ".backend {background: #e8e8d0;}\n"
3066 ".active0 {background: #ff9090;}\n"
3067 ".active1 {background: #ffd020;}\n"
3068 ".active2 {background: #ffffa0;}\n"
3069 ".active3 {background: #c0ffc0;}\n"
3070 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3071 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
3072 ".active6 {background: #e0e0e0;}\n"
3073 ".backup0 {background: #ff9090;}\n"
3074 ".backup1 {background: #ff80ff;}\n"
3075 ".backup2 {background: #c060ff;}\n"
3076 ".backup3 {background: #b0d0ff;}\n"
3077 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3078 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3079 ".backup6 {background: #e0e0e0;}\n"
3080 ".maintain {background: #c07820;}\n"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07003081 ".softstop {background: #0067FF;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3083 "\n"
3084 "a.px:link {color: #ffff40; text-decoration: none;}"
3085 "a.px:visited {color: #ffff40; text-decoration: none;}"
3086 "a.px:hover {color: #ffffff; text-decoration: none;}"
3087 "a.lfsb:link {color: #000000; text-decoration: none;}"
3088 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3089 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3090 "\n"
3091 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3092 "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"
3093 "table.tbl td.ac { text-align: center;}\n"
3094 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3095 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3096 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3097 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3098 "\n"
3099 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3100 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3101 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003102 "table.det { border-collapse: collapse; border-style: none; }\n"
3103 "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"
3104 "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 +01003105 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003106 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003107 " display:block;\n"
3108 " visibility:hidden;\n"
3109 " z-index:2147483647;\n"
3110 " position:absolute;\n"
3111 " padding:2px 4px 3px;\n"
3112 " background:#f0f060; color:#000000;\n"
3113 " border:1px solid #7040c0;\n"
3114 " white-space:nowrap;\n"
3115 " font-style:normal;font-size:11px;font-weight:normal;\n"
3116 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3117 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3118 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003119 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120 "-->\n"
3121 "</style></head>\n",
3122 (uri->flags & ST_SHNODE) ? " on " : "",
3123 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3124 );
3125}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003126
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003127/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003128 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003129 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003131static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132{
3133 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003134 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003135
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003136 /* WARNING! this has to fit the first packet too.
3137 * We are around 3.5 kB, add adding entries will
3138 * become tricky if we want to support 4kB buffers !
3139 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140 chunk_appendf(&trash,
3141 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3142 PRODUCT_NAME "%s</a></h1>\n"
3143 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3144 "<hr width=\"100%%\" class=\"hr\">\n"
3145 "<h3>&gt; General process information</h3>\n"
3146 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3147 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3148 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3149 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3150 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3151 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3152 "Running tasks: %d/%d; idle = %d %%<br>\n"
3153 "</td><td align=\"center\" nowrap>\n"
3154 "<table class=\"lgd\"><tr>\n"
3155 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3156 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3157 "</tr><tr>\n"
3158 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3159 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3160 "</tr><tr>\n"
3161 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3162 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3163 "</tr><tr>\n"
3164 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3165 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
3166 "</tr><tr>\n"
3167 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07003168 "</tr><tr>\n"
3169 "<td class=\"softstop\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003170 "</tr></table>\n"
3171 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3172 "</td>"
3173 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3174 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3175 "",
3176 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3177 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3178 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3179 (uri->flags & ST_SHDESC) ? ": " : "",
3180 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3181 pid, relative_pid, global.nbproc,
3182 up / 86400, (up % 86400) / 3600,
3183 (up % 3600) / 60, (up % 60),
3184 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3185 global.rlimit_memmax ? " MB" : "",
3186 global.rlimit_nofile,
3187 global.maxsock, global.maxconn, global.maxpipes,
3188 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3189 run_queue_cur, nb_tasks_cur, idle_pct
3190 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003191
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003192 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3193 memcpy(scope_txt, bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3194 scope_txt[si->applet.ctx.stats.scope_len] = '\0';
3195
3196 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003197 "<li><form method=\"GET\" action=\"%s%s%s\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003198 uri->uri_prefix,
3199 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3200 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3201 (si->applet.ctx.stats.scope_len > 0) ? scope_txt : "",
3202 STAT_SCOPE_TXT_MAXLEN);
3203
3204 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3205 scope_txt[0] = 0;
3206 if (si->applet.ctx.stats.scope_len) {
3207 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3208 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3209 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3210 }
3211
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3213 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003214 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 uri->uri_prefix,
3216 "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003217 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3218 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 else
3220 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003221 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 uri->uri_prefix,
3223 ";up",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003224 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3225 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003226
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003227 if (uri->refresh > 0) {
3228 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3229 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003230 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231 uri->uri_prefix,
3232 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003233 "",
3234 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235 else
3236 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003237 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003238 uri->uri_prefix,
3239 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003240 ";norefresh",
3241 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003242 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003243
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003244 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003245 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003246 uri->uri_prefix,
3247 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003248 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3249 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003250
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003252 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003254 (uri->refresh > 0) ? ";norefresh" : "",
3255 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003256
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003257 chunk_appendf(&trash,
3258 "</ul></td>"
3259 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3260 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3261 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3262 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3263 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3264 "</ul>"
3265 "</td>"
3266 "</tr></table>\n"
3267 ""
3268 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003269
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 if (si->applet.ctx.stats.st_code) {
3271 switch (si->applet.ctx.stats.st_code) {
3272 case STAT_STATUS_DONE:
3273 chunk_appendf(&trash,
3274 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003275 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003276 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003277 "</div>\n", uri->uri_prefix,
3278 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3279 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3280 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003281 break;
3282 case STAT_STATUS_NONE:
3283 chunk_appendf(&trash,
3284 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003285 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003286 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003287 "</div>\n", uri->uri_prefix,
3288 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3289 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3290 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003291 break;
3292 case STAT_STATUS_PART:
3293 chunk_appendf(&trash,
3294 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003295 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003296 "Action partially processed.<br>"
3297 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003298 "</div>\n", uri->uri_prefix,
3299 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3300 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3301 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003302 break;
3303 case STAT_STATUS_ERRP:
3304 chunk_appendf(&trash,
3305 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003306 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003307 "Action not processed because of invalid parameters."
3308 "<ul>"
3309 "<li>The action is maybe unknown.</li>"
3310 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3311 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3312 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003313 "</div>\n", uri->uri_prefix,
3314 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3315 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3316 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 break;
3318 case STAT_STATUS_EXCD:
3319 chunk_appendf(&trash,
3320 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003321 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3323 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003324 "</div>\n", uri->uri_prefix,
3325 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3326 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3327 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003328 break;
3329 case STAT_STATUS_DENY:
3330 chunk_appendf(&trash,
3331 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003332 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003334 "</div>\n", uri->uri_prefix,
3335 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3336 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3337 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003338 break;
3339 default:
3340 chunk_appendf(&trash,
3341 "<p><div class=active6>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003342 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003344 "</div>\n", uri->uri_prefix,
3345 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3346 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3347 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003348 }
3349 chunk_appendf(&trash, "<p>\n");
3350 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003351}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003352
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003353/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3354 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003356static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003359}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003360
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003361/* This function dumps statistics onto the stream interface's read buffer in
3362 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3363 * are ignored for CSV format (hence <uri> may be NULL there). The xprt_ctx must
3364 * have been zeroed first, and the flags properly set. It returns 0 if it had to
3365 * stop writing data and an I/O is needed, 1 if the dump is finished and the
3366 * session must be closed, or -1 in case of any error. This function is used by
3367 * both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003369static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003371 struct channel *rep = si->ib;
3372 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003373
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003375
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 switch (si->conn->xprt_st) {
3377 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3379 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003380
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003381 case STAT_ST_HEAD:
Willy Tarreau354898b2012-12-23 18:15:23 +01003382 if (si->applet.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003383 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003384 else
3385 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003386
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003387 if (bi_putchk(rep, &trash) == -1)
3388 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003389
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003390 si->conn->xprt_st = STAT_ST_INFO;
3391 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003392
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 case STAT_ST_INFO:
Willy Tarreau354898b2012-12-23 18:15:23 +01003394 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003395 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003396 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003397 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398 }
Willy Tarreau91861262007-10-17 17:06:05 +02003399
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 si->applet.ctx.stats.px = proxy;
3401 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3402 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003403 /* fall through */
3404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 case STAT_ST_LIST:
3406 /* dump proxies */
3407 while (si->applet.ctx.stats.px) {
3408 if (buffer_almost_full(rep->buf))
3409 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003410
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411 px = si->applet.ctx.stats.px;
3412 /* skip the disabled proxies, global frontend and non-networked ones */
3413 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003414 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003416
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 si->applet.ctx.stats.px = px->next;
3418 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3419 }
3420 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003421
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 si->conn->xprt_st = STAT_ST_END;
3423 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425 case STAT_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003426 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003427 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 if (bi_putchk(rep, &trash) == -1)
3429 return 0;
3430 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003431
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432 si->conn->xprt_st = STAT_ST_FIN;
3433 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435 case STAT_ST_FIN:
3436 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 default:
3439 /* unknown state ! */
3440 si->conn->xprt_st = STAT_ST_FIN;
3441 return -1;
3442 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443}
Willy Tarreauae526782010-03-04 20:34:23 +01003444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445/* This I/O handler runs as an applet embedded in a stream interface. It is
3446 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3447 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3448 * automatically unregisters itself once transfer is complete.
3449 */
3450static void http_stats_io_handler(struct stream_interface *si)
3451{
3452 struct session *s = si->conn->xprt_ctx;
3453 struct channel *req = si->ob;
3454 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003455
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3457 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003458
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 /* check that the output is not closed */
3460 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3461 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003462
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 if (!si->applet.st0) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003464 if (stats_dump_stat_to_buffer(si, s->be->uri_auth)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003465 si->applet.st0 = 1;
3466 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003467 }
3468 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003469
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3471 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3474 si_shutr(si);
3475 res->flags |= CF_READ_NULL;
3476 }
Willy Tarreau91861262007-10-17 17:06:05 +02003477
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478 /* update all other flags and resync with the other side */
3479 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003480
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003481 /* we don't want to expire timeouts while we're processing requests */
3482 si->ib->rex = TICK_ETERNITY;
3483 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003484
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 out:
3486 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3487 /* check that we have released everything then unregister */
3488 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003489 }
3490}
3491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492
Willy Tarreau909d5172012-11-26 03:04:41 +01003493static inline const char *get_conn_ctrl_name(const struct connection *conn)
3494{
3495 if (!conn->ctrl)
3496 return "NONE";
3497 return conn->ctrl->name;
3498}
3499
3500static inline const char *get_conn_xprt_name(const struct connection *conn)
3501{
3502 static char ptr[17];
3503
3504 if (!conn->xprt)
3505 return "NONE";
3506
3507 if (conn->xprt == &raw_sock)
3508 return "RAW";
3509
3510#ifdef USE_OPENSSL
3511 if (conn->xprt == &ssl_sock)
3512 return "SSL";
3513#endif
3514 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3515 return ptr;
3516}
3517
3518static inline const char *get_conn_data_name(const struct connection *conn)
3519{
3520 static char ptr[17];
3521
3522 if (!conn->data)
3523 return "NONE";
3524
3525 if (conn->data == &sess_conn_cb)
3526 return "SESS";
3527
3528 if (conn->data == &si_conn_cb)
3529 return "STRM";
3530
3531 if (conn->data == &check_conn_cb)
3532 return "CHCK";
3533
3534 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3535 return ptr;
3536}
3537
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003538/* This function dumps a complete session state onto the stream interface's
3539 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3540 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003541 * 0 if the output buffer is full and it needs to be called again, otherwise
3542 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003543 */
Willy Tarreau76153662012-11-26 01:16:39 +01003544static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003545{
3546 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003547 extern const char *monthname[12];
3548 char pn[INET6_ADDRSTRLEN];
3549
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003550 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003551
Willy Tarreau295a8372011-03-10 11:25:07 +01003552 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003553 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003554 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3555 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003556 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003557 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003558 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003559 return 1;
3560 }
3561
Willy Tarreau295a8372011-03-10 11:25:07 +01003562 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003563 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003564 si->applet.ctx.sess.uid = sess->uniq_id;
3565 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003566 /* fall through */
3567
3568 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003569 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003570 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003571 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003572 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003573 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3574 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003575 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003576 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003577
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003578 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003579 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003580 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003581 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003582 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003583 break;
3584 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003585 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003586 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003587 default:
3588 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003589 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003590 break;
3591 }
3592
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003593 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003594 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003595 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003596
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003597 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003598 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003599 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3600 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3601 sess->listener ? sess->listener->luid : 0);
3602
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003603 conn_get_to_addr(sess->si[0].conn);
3604 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003605 case AF_INET:
3606 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003607 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003608 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003609 break;
3610 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003611 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003612 break;
3613 default:
3614 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003615 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003616 break;
3617 }
3618
Willy Tarreau50943332011-09-02 17:33:05 +02003619 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003620 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003621 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003622 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003623 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3624 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003625 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003626
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003627 conn_get_from_addr(sess->si[1].conn);
3628 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003629 case AF_INET:
3630 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003631 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003632 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003633 break;
3634 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003635 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003636 break;
3637 default:
3638 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003639 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003640 break;
3641 }
3642
3643 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003644 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003645 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003646 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3647 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003648 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003649 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003650
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003651 conn_get_to_addr(sess->si[1].conn);
3652 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003653 case AF_INET:
3654 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003655 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003656 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003657 break;
3658 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003659 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003660 break;
3661 default:
3662 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003663 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003664 break;
3665 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003667 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003668 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003669 sess->task,
3670 sess->task->state,
3671 sess->task->nice, sess->task->calls,
3672 sess->task->expire ?
3673 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3674 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3675 TICKS_TO_MS(1000)) : "<NEVER>",
3676 task_in_rq(sess->task) ? ", running" : "");
3677
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003678 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003679 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003680 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3681
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003682 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003683 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3684 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3685 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3686
3687 chunk_appendf(&trash,
3688 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003689 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003690 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003691 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003692 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003693 sess->si[0].exp ?
3694 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3695 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3696 TICKS_TO_MS(1000)) : "<NEVER>",
3697 sess->si[0].err_type);
3698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003699 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003700 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003701 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003702 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003703 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003704 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003705 sess->si[1].exp ?
3706 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3707 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3708 TICKS_TO_MS(1000)) : "<NEVER>",
3709 sess->si[1].err_type);
3710
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003711 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003712 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003713 sess->si[0].conn,
3714 get_conn_ctrl_name(sess->si[0].conn),
3715 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003716 get_conn_data_name(sess->si[0].conn),
3717 obj_type_name(sess->si[0].conn->target),
3718 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003719
3720 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003721 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003722 sess->si[0].conn->flags,
3723 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003724 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3725 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3726 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 +01003727
3728 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003729 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003730 sess->si[1].conn,
3731 get_conn_ctrl_name(sess->si[1].conn),
3732 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003733 get_conn_data_name(sess->si[1].conn),
3734 obj_type_name(sess->si[1].conn->target),
3735 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003736
3737 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003738 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003739 sess->si[1].conn->flags,
3740 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003741 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3742 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3743 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 +01003744
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003745 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003746 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003747 " an_exp=%s",
3748 sess->req,
3749 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003750 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003751 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003752 sess->req->analyse_exp ?
3753 human_time(TICKS_TO_MS(sess->req->analyse_exp - 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 " rex=%s",
3758 sess->req->rex ?
3759 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3760 TICKS_TO_MS(1000)) : "<NEVER>");
3761
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003762 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003763 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003764 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003765 sess->req->wex ?
3766 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3767 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003768 sess->req->buf,
3769 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003770 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003771 sess->txn.req.next, sess->req->buf->i,
3772 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003773
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003774 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003775 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003776 " an_exp=%s",
3777 sess->rep,
3778 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003779 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003780 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003781 sess->rep->analyse_exp ?
3782 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3783 TICKS_TO_MS(1000)) : "<NEVER>");
3784
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003785 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003786 " rex=%s",
3787 sess->rep->rex ?
3788 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3789 TICKS_TO_MS(1000)) : "<NEVER>");
3790
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003791 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003792 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003793 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003794 sess->rep->wex ?
3795 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3796 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003797 sess->rep->buf,
3798 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003799 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003800 sess->txn.rsp.next, sess->rep->buf->i,
3801 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003802
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003803 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003804 return 0;
3805
3806 /* use other states to dump the contents */
3807 }
3808 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003809 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003810 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003811 return 1;
3812}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003813
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003814/* This function dumps all sessions' states onto the stream interface's
3815 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003816 * properly set. It returns 0 if the output buffer is full and it needs
3817 * to be called again, otherwise non-zero. It is designed to be called
3818 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003819 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003820static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003821{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003822 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003823 /* If we're forced to shut down, we might have to remove our
3824 * reference to the last session being dumped.
3825 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003826 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003827 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3828 LIST_DEL(&si->applet.ctx.sess.bref.users);
3829 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003830 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003831 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003832 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003833 }
3834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003835 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003836
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003837 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003838 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003839 /* the function had not been called yet, let's prepare the
3840 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003841 * pointer to the first in the global list. When a target
3842 * session is being destroyed, it is responsible for updating
3843 * this pointer. We know we have reached the end when this
3844 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003845 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003846 LIST_INIT(&si->applet.ctx.sess.bref.users);
3847 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003848 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003849 /* fall through */
3850
Willy Tarreau295a8372011-03-10 11:25:07 +01003851 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003852 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003853 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3854 LIST_DEL(&si->applet.ctx.sess.bref.users);
3855 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003856 }
3857
3858 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003859 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003860 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003861 struct session *curr_sess;
3862
Willy Tarreau295a8372011-03-10 11:25:07 +01003863 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003864
Willy Tarreau295a8372011-03-10 11:25:07 +01003865 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003866 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003867 goto next_sess;
3868
Willy Tarreau295a8372011-03-10 11:25:07 +01003869 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003870 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003871 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003872 return 0;
3873
3874 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003875 LIST_DEL(&si->applet.ctx.sess.bref.users);
3876 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003877 if (si->applet.ctx.sess.target != (void *)-1) {
3878 si->applet.ctx.sess.target = NULL;
3879 break;
3880 }
3881 else
3882 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003883 }
3884
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003885 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003886 "%p: proto=%s",
3887 curr_sess,
3888 curr_sess->listener->proto->name);
3889
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003890
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003891 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003892 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003893 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003894 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003895 " src=%s:%d fe=%s be=%s srv=%s",
3896 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003897 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003898 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003899 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003900 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003901 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003902 break;
3903 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003904 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003905 " src=unix:%d fe=%s be=%s srv=%s",
3906 curr_sess->listener->luid,
3907 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003908 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003909 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003910 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003911 break;
3912 }
3913
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003914 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003915 " ts=%02x age=%s calls=%d",
3916 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003917 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3918 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003919
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003920 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003921 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003922 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003923 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003924 curr_sess->req->analysers,
3925 curr_sess->req->rex ?
3926 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3927 TICKS_TO_MS(1000)) : "");
3928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003929 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003930 ",wx=%s",
3931 curr_sess->req->wex ?
3932 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3933 TICKS_TO_MS(1000)) : "");
3934
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003935 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003936 ",ax=%s]",
3937 curr_sess->req->analyse_exp ?
3938 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3939 TICKS_TO_MS(1000)) : "");
3940
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003941 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003942 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003943 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003944 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003945 curr_sess->rep->analysers,
3946 curr_sess->rep->rex ?
3947 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3948 TICKS_TO_MS(1000)) : "");
3949
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003950 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003951 ",wx=%s",
3952 curr_sess->rep->wex ?
3953 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3954 TICKS_TO_MS(1000)) : "");
3955
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003956 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003957 ",ax=%s]",
3958 curr_sess->rep->analyse_exp ?
3959 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3960 TICKS_TO_MS(1000)) : "");
3961
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003963 " s0=[%d,%1xh,fd=%d,ex=%s]",
3964 curr_sess->si[0].state,
3965 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003966 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003967 curr_sess->si[0].exp ?
3968 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3969 TICKS_TO_MS(1000)) : "");
3970
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003971 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003972 " s1=[%d,%1xh,fd=%d,ex=%s]",
3973 curr_sess->si[1].state,
3974 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003975 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003976 curr_sess->si[1].exp ?
3977 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3978 TICKS_TO_MS(1000)) : "");
3979
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003980 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003981 " exp=%s",
3982 curr_sess->task->expire ?
3983 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3984 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003985 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003986 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003987
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003989
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003990 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003991 /* let's try again later from this session. We add ourselves into
3992 * this session's users so that it can remove us upon termination.
3993 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003994 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003995 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003996 }
3997
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003998 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003999 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004000 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004001
Willy Tarreau76153662012-11-26 01:16:39 +01004002 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004003 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01004004 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004005 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004006 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004007 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004008
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004009 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004010 return 0;
4011
Willy Tarreau295a8372011-03-10 11:25:07 +01004012 si->applet.ctx.sess.target = NULL;
4013 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004014 return 1;
4015 }
4016
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004017 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004018 /* fall through */
4019
4020 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004021 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004022 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004023 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004024}
4025
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004026/* This is called when the stream interface is closed. For instance, upon an
4027 * external abort, we won't call the i/o handler anymore so we may need to
4028 * remove back references to the session currently being dumped.
4029 */
Simon Horman74d88312013-02-12 10:45:50 +09004030static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004031{
4032 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
4033 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
4034 LIST_DEL(&si->applet.ctx.sess.bref.users);
4035 }
4036}
4037
Willy Tarreau20e99322013-04-13 09:22:25 +02004038/* This function is used to either dump tables states (when action is set
4039 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
4040 * The xprt_ctx must have been zeroed first, and the flags properly set.
4041 * It returns 0 if the output buffer is full and it needs to be called
4042 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004043 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004044static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004045{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004046 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004047 struct ebmb_node *eb;
4048 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004049 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004050 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004051
4052 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004053 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01004054 * - STAT_ST_INIT : the first call
4055 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004056 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004057 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004058 * and the entry pointer points to the next entry to be dumped,
4059 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004060 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004061 * data though.
4062 */
4063
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004064 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004065 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004066 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004067 si->applet.ctx.table.entry->ref_cnt--;
4068 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004069 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004070 return 1;
4071 }
4072
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004073 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004074
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004075 while (si->conn->xprt_st != STAT_ST_FIN) {
4076 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004077 case STAT_ST_INIT:
4078 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4079 if (!si->applet.ctx.table.proxy)
4080 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004081
Willy Tarreau295a8372011-03-10 11:25:07 +01004082 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004083 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004084 break;
4085
Willy Tarreau295a8372011-03-10 11:25:07 +01004086 case STAT_ST_INFO:
4087 if (!si->applet.ctx.table.proxy ||
4088 (si->applet.ctx.table.target &&
4089 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004090 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004091 break;
4092 }
4093
Willy Tarreau295a8372011-03-10 11:25:07 +01004094 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004095 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004096 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004097 return 0;
4098
Willy Tarreau295a8372011-03-10 11:25:07 +01004099 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004100 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004101 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004102 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004103 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004104 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4105 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004106 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004107 break;
4108 }
4109 }
4110 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004111 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004112 break;
4113
Willy Tarreau295a8372011-03-10 11:25:07 +01004114 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004115 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004116
Willy Tarreau295a8372011-03-10 11:25:07 +01004117 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004118 /* we're filtering on some data contents */
4119 void *ptr;
4120 long long data;
4121
Willy Tarreau295a8372011-03-10 11:25:07 +01004122 dt = si->applet.ctx.table.data_type;
4123 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4124 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004125 dt);
4126
4127 data = 0;
4128 switch (stktable_data_types[dt].std_type) {
4129 case STD_T_SINT:
4130 data = stktable_data_cast(ptr, std_t_sint);
4131 break;
4132 case STD_T_UINT:
4133 data = stktable_data_cast(ptr, std_t_uint);
4134 break;
4135 case STD_T_ULL:
4136 data = stktable_data_cast(ptr, std_t_ull);
4137 break;
4138 case STD_T_FRQP:
4139 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004140 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004141 break;
4142 }
4143
4144 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004145 if ((data < si->applet.ctx.table.value &&
4146 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4147 si->applet.ctx.table.data_op == STD_OP_GT ||
4148 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4149 (data == si->applet.ctx.table.value &&
4150 (si->applet.ctx.table.data_op == STD_OP_NE ||
4151 si->applet.ctx.table.data_op == STD_OP_GT ||
4152 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4153 (data > si->applet.ctx.table.value &&
4154 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4155 si->applet.ctx.table.data_op == STD_OP_LT ||
4156 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004157 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004158 }
4159
Simon Hormanc88b8872011-06-15 15:18:49 +09004160 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004161 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004162 si->applet.ctx.table.entry))
4163 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004164
Willy Tarreau295a8372011-03-10 11:25:07 +01004165 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004166
Willy Tarreau295a8372011-03-10 11:25:07 +01004167 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004168 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004169 struct stksess *old = si->applet.ctx.table.entry;
4170 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004171 if (show)
4172 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau33fba6f2013-08-13 16:44:40 +02004173 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004174 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004175 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004176 break;
4177 }
4178
Simon Hormanc88b8872011-06-15 15:18:49 +09004179
4180 if (show)
4181 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4182 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4183 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4184
Willy Tarreau295a8372011-03-10 11:25:07 +01004185 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004186 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004187 break;
4188
Willy Tarreau295a8372011-03-10 11:25:07 +01004189 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004190 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004191 break;
4192 }
4193 }
4194 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004195}
4196
Willy Tarreaud426a182010-03-05 14:58:26 +01004197/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004198 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4199 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4200 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4201 * lines are respected within the limit of 70 output chars. Lines that are
4202 * continuation of a previous truncated line begin with "+" instead of " "
4203 * after the offset. The new pointer is returned.
4204 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004205static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4206 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004207{
4208 int end;
4209 unsigned char c;
4210
4211 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004212 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004213 return ptr;
4214
Willy Tarreau77804732012-10-29 16:14:26 +01004215 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004216
Willy Tarreaud426a182010-03-05 14:58:26 +01004217 while (ptr < len && ptr < bsize) {
4218 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004219 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004220 if (out->len > end - 2)
4221 break;
4222 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004223 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004224 if (out->len > end - 3)
4225 break;
4226 out->str[out->len++] = '\\';
4227 switch (c) {
4228 case '\t': c = 't'; break;
4229 case '\n': c = 'n'; break;
4230 case '\r': c = 'r'; break;
4231 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004232 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004233 }
4234 out->str[out->len++] = c;
4235 } else {
4236 if (out->len > end - 5)
4237 break;
4238 out->str[out->len++] = '\\';
4239 out->str[out->len++] = 'x';
4240 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4241 out->str[out->len++] = hextab[c & 0xF];
4242 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004243 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004244 /* we had a line break, let's return now */
4245 out->str[out->len++] = '\n';
4246 *line = ptr;
4247 return ptr;
4248 }
4249 }
4250 /* we have an incomplete line, we return it as-is */
4251 out->str[out->len++] = '\n';
4252 return ptr;
4253}
4254
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004255/* This function dumps all captured errors onto the stream interface's
4256 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004257 * properly set. It returns 0 if the output buffer is full and it needs
4258 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004259 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004260static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004261{
4262 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004263
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004264 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004265 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004266
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004267 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004268
Willy Tarreau295a8372011-03-10 11:25:07 +01004269 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004270 /* the function had not been called yet, let's prepare the
4271 * buffer for a response.
4272 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004273 struct tm tm;
4274
4275 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004276 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004277 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4278 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4279 error_snapshot_id);
4280
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004281 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004282 /* Socket buffer full. Let's try again later from the same point */
4283 return 0;
4284 }
4285
Willy Tarreau295a8372011-03-10 11:25:07 +01004286 si->applet.ctx.errors.px = proxy;
4287 si->applet.ctx.errors.buf = 0;
4288 si->applet.ctx.errors.bol = 0;
4289 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004290 }
4291
4292 /* we have two inner loops here, one for the proxy, the other one for
4293 * the buffer.
4294 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004295 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004296 struct error_snapshot *es;
4297
Willy Tarreau295a8372011-03-10 11:25:07 +01004298 if (si->applet.ctx.errors.buf == 0)
4299 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004300 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004301 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004302
4303 if (!es->when.tv_sec)
4304 goto next;
4305
Willy Tarreau295a8372011-03-10 11:25:07 +01004306 if (si->applet.ctx.errors.iid >= 0 &&
4307 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4308 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004309 goto next;
4310
Willy Tarreau295a8372011-03-10 11:25:07 +01004311 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004312 /* just print headers now */
4313
4314 char pn[INET6_ADDRSTRLEN];
4315 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004316 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004317
4318 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004319 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004320 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004321 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004322
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004323 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4324 case AF_INET:
4325 case AF_INET6:
4326 port = get_host_port(&es->src);
4327 break;
4328 default:
4329 port = 0;
4330 }
4331
Willy Tarreau295a8372011-03-10 11:25:07 +01004332 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004333 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004334 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004335 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004336 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004337 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004338 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4339 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004340 break;
4341 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004342 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004343 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004344 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004345 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004346 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004347 break;
4348 }
4349
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004350 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004351 ", server %s (#%d), event #%u\n"
4352 " src %s:%d, session #%d, session flags 0x%08x\n"
4353 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4354 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4355 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4356 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4357 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4358 es->ev_id,
4359 pn, port, es->sid, es->s_flags,
4360 es->state, es->m_flags, es->t_flags,
4361 es->m_clen, es->m_blen,
4362 es->b_flags, es->b_out, es->b_tot,
4363 es->len, es->b_wrap, es->pos);
4364
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004365 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004366 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004367 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004368 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004369 si->applet.ctx.errors.ptr = 0;
4370 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004371 }
4372
Willy Tarreau295a8372011-03-10 11:25:07 +01004373 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004374 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004375 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004376 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004377 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004378 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004379 goto next;
4380 }
4381
4382 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004383 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004384 int newptr;
4385 int newline;
4386
Willy Tarreau295a8372011-03-10 11:25:07 +01004387 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004388 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 +01004389 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004390 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004391
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004392 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004393 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004394 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004395 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004396 si->applet.ctx.errors.ptr = newptr;
4397 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004398 };
4399 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004400 si->applet.ctx.errors.bol = 0;
4401 si->applet.ctx.errors.ptr = -1;
4402 si->applet.ctx.errors.buf++;
4403 if (si->applet.ctx.errors.buf > 1) {
4404 si->applet.ctx.errors.buf = 0;
4405 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004406 }
4407 }
4408
4409 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004410 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004411}
4412
Willy Tarreaud5781202012-09-22 19:32:35 +02004413/* parse the "level" argument on the bind lines */
4414static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4415{
4416 if (!*args[cur_arg + 1]) {
4417 memprintf(err, "'%s' : missing level", args[cur_arg]);
4418 return ERR_ALERT | ERR_FATAL;
4419 }
4420
4421 if (!strcmp(args[cur_arg+1], "user"))
4422 conf->level = ACCESS_LVL_USER;
4423 else if (!strcmp(args[cur_arg+1], "operator"))
4424 conf->level = ACCESS_LVL_OPER;
4425 else if (!strcmp(args[cur_arg+1], "admin"))
4426 conf->level = ACCESS_LVL_ADMIN;
4427 else {
4428 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4429 args[cur_arg], args[cur_arg+1]);
4430 return ERR_ALERT | ERR_FATAL;
4431 }
4432
4433 return 0;
4434}
4435
Willy Tarreaub24281b2011-02-13 13:16:36 +01004436struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004437 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004438 .name = "<STATS>", /* used for logging */
4439 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004440 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004441};
4442
Simon Horman9bd2c732011-06-15 15:18:44 +09004443static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004444 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004445 .name = "<CLI>", /* used for logging */
4446 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004447 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004448};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004449
Willy Tarreaudc13c112013-06-21 23:16:39 +02004450static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004451 { CFG_GLOBAL, "stats", stats_parse_global },
4452 { 0, NULL, NULL },
4453}};
4454
Willy Tarreaud5781202012-09-22 19:32:35 +02004455static struct bind_kw_list bind_kws = { "STAT", { }, {
4456 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4457 { NULL, NULL, 0 },
4458}};
4459
Willy Tarreau10522fd2008-07-09 20:12:41 +02004460__attribute__((constructor))
4461static void __dumpstats_module_init(void)
4462{
4463 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004464 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004465}
4466
Willy Tarreau91861262007-10-17 17:06:05 +02004467/*
4468 * Local variables:
4469 * c-indent-level: 8
4470 * c-basic-offset: 8
4471 * End:
4472 */