blob: 25f54416c29d2ec00012284cd2bdf209e2085061 [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;
593 void *ptr;
594 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900595
Simon Hormand5b9fd92011-06-15 15:18:48 +0900596 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900597
598 if (!*args[4]) {
599 si->applet.ctx.cli.msg = "Key value expected\n";
600 si->applet.st0 = STAT_CLI_PRINT;
601 return;
602 }
603
Simon Hormanc5b89f62011-06-15 15:18:50 +0900604 switch (px->table.type) {
605 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900606 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100607 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900608 break;
609 case STKTABLE_TYPE_IPV6:
610 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100611 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900612 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900613 case STKTABLE_TYPE_INTEGER:
614 {
615 char *endptr;
616 unsigned long val;
617 errno = 0;
618 val = strtoul(args[4], &endptr, 10);
619 if ((errno == ERANGE && val == ULONG_MAX) ||
620 (errno != 0 && val == 0) || endptr == args[4] ||
621 val > 0xffffffff) {
622 si->applet.ctx.cli.msg = "Invalid key\n";
623 si->applet.st0 = STAT_CLI_PRINT;
624 return;
625 }
626 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100627 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900628 break;
629 }
630 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900631 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100632 static_table_key->key = args[4];
633 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900634 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900635 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200636 switch (action) {
637 case STAT_CLI_O_TAB:
638 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
639 break;
640 case STAT_CLI_O_CLR:
641 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
642 break;
643 default:
644 si->applet.ctx.cli.msg = "Unknown action\n";
645 break;
646 }
Simon Horman121f3052011-06-15 15:18:46 +0900647 si->applet.st0 = STAT_CLI_PRINT;
648 return;
649 }
650
651 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200652 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900653 si->applet.ctx.cli.msg = stats_permission_denied_msg;
654 si->applet.st0 = STAT_CLI_PRINT;
655 return;
656 }
657
Willy Tarreau07115412012-10-29 21:56:59 +0100658 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900659
Willy Tarreaudec98142012-06-06 23:37:08 +0200660 switch (action) {
661 case STAT_CLI_O_TAB:
662 if (!ts)
663 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100664 chunk_reset(&trash);
665 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900666 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100667 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900668 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200669
670 case STAT_CLI_O_CLR:
671 if (!ts)
672 return;
673 if (ts->ref_cnt) {
674 /* don't delete an entry which is currently referenced */
675 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
676 si->applet.st0 = STAT_CLI_PRINT;
677 return;
678 }
679 stksess_kill(&px->table, ts);
680 break;
Simon Horman17bce342011-06-15 15:18:47 +0900681
Willy Tarreau654694e2012-06-07 01:03:16 +0200682 case STAT_CLI_O_SET:
683 if (strncmp(args[5], "data.", 5) != 0) {
684 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
685 si->applet.st0 = STAT_CLI_PRINT;
686 return;
687 }
688
689 data_type = stktable_get_data_type(args[5] + 5);
690 if (data_type < 0) {
691 si->applet.ctx.cli.msg = "Unknown data type\n";
692 si->applet.st0 = STAT_CLI_PRINT;
693 return;
694 }
695
696 if (!px->table.data_ofs[data_type]) {
697 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
698 si->applet.st0 = STAT_CLI_PRINT;
699 return;
700 }
701
702 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
703 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
704 si->applet.st0 = STAT_CLI_PRINT;
705 return;
706 }
707
708 if (ts)
709 stktable_touch(&px->table, ts, 1);
710 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100711 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200712 if (!ts) {
713 /* don't delete an entry which is currently referenced */
714 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
715 si->applet.st0 = STAT_CLI_PRINT;
716 return;
717 }
718 stktable_store(&px->table, ts, 1);
719 }
720
721 ptr = stktable_data_ptr(&px->table, ts, data_type);
722 switch (stktable_data_types[data_type].std_type) {
723 case STD_T_SINT:
724 stktable_data_cast(ptr, std_t_sint) = value;
725 break;
726 case STD_T_UINT:
727 stktable_data_cast(ptr, std_t_uint) = value;
728 break;
729 case STD_T_ULL:
730 stktable_data_cast(ptr, std_t_ull) = value;
731 break;
732 case STD_T_FRQP:
733 /* We only reset the previous value so that it slowly fades out */
734 frqp = &stktable_data_cast(ptr, std_t_frqp);
735 frqp->curr_tick = now_ms;
736 frqp->prev_ctr = value;
737 frqp->curr_ctr = 0;
738 break;
739 }
740 break;
741
Willy Tarreaudec98142012-06-06 23:37:08 +0200742 default:
743 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900744 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200745 break;
Simon Horman121f3052011-06-15 15:18:46 +0900746 }
Simon Horman121f3052011-06-15 15:18:46 +0900747}
748
Willy Tarreau654694e2012-06-07 01:03:16 +0200749static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900750{
Willy Tarreau04b3a192013-04-13 09:41:37 +0200751 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
752 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau654694e2012-06-07 01:03:16 +0200753 si->applet.st0 = STAT_CLI_PRINT;
754 return;
755 }
756
Simon Hormand5b9fd92011-06-15 15:18:48 +0900757 /* condition on stored data value */
758 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
759 if (si->applet.ctx.table.data_type < 0) {
760 si->applet.ctx.cli.msg = "Unknown data type\n";
761 si->applet.st0 = STAT_CLI_PRINT;
762 return;
763 }
764
765 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
766 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
767 si->applet.st0 = STAT_CLI_PRINT;
768 return;
769 }
770
771 si->applet.ctx.table.data_op = get_std_op(args[4]);
772 if (si->applet.ctx.table.data_op < 0) {
773 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
774 si->applet.st0 = STAT_CLI_PRINT;
775 return;
776 }
777
778 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
779 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
780 si->applet.st0 = STAT_CLI_PRINT;
781 return;
782 }
783}
784
Willy Tarreaudec98142012-06-06 23:37:08 +0200785static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900786{
787 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200788 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200789 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900790 si->applet.ctx.table.proxy = NULL;
791 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200792 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900793
794 if (*args[2]) {
795 si->applet.ctx.table.target = find_stktable(args[2]);
796 if (!si->applet.ctx.table.target) {
797 si->applet.ctx.cli.msg = "No such table\n";
798 si->applet.st0 = STAT_CLI_PRINT;
799 return;
800 }
801 }
802 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200803 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900804 goto err_args;
805 return;
806 }
807
808 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200809 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900810 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200811 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900812 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900813 goto err_args;
814
815 return;
816
817err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200818 switch (action) {
819 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820 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 +0200821 break;
822 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900823 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 +0200824 break;
825 default:
826 si->applet.ctx.cli.msg = "Unknown action\n";
827 break;
828 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900829 si->applet.st0 = STAT_CLI_PRINT;
830}
831
Willy Tarreau532a4502011-09-07 22:37:44 +0200832/* Expects to find a frontend named <arg> and returns it, otherwise displays various
833 * adequate error messages and returns NULL. This function also expects the session
834 * level to be admin.
835 */
836static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
837{
838 struct proxy *px;
839
Willy Tarreau290e63a2012-09-20 18:07:14 +0200840 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200841 si->applet.ctx.cli.msg = stats_permission_denied_msg;
842 si->applet.st0 = STAT_CLI_PRINT;
843 return NULL;
844 }
845
846 if (!*arg) {
847 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
848 si->applet.st0 = STAT_CLI_PRINT;
849 return NULL;
850 }
851
852 px = findproxy(arg, PR_CAP_FE);
853 if (!px) {
854 si->applet.ctx.cli.msg = "No such frontend.\n";
855 si->applet.st0 = STAT_CLI_PRINT;
856 return NULL;
857 }
858 return px;
859}
860
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200861/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
862 * and returns the pointer to the server. Otherwise, display adequate error messages
863 * and returns NULL. This function also expects the session level to be admin. Note:
864 * the <arg> is modified to remove the '/'.
865 */
866static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
867{
868 struct proxy *px;
869 struct server *sv;
870 char *line;
871
Willy Tarreau290e63a2012-09-20 18:07:14 +0200872 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200873 si->applet.ctx.cli.msg = stats_permission_denied_msg;
874 si->applet.st0 = STAT_CLI_PRINT;
875 return NULL;
876 }
877
878 /* split "backend/server" and make <line> point to server */
879 for (line = arg; *line; line++)
880 if (*line == '/') {
881 *line++ = '\0';
882 break;
883 }
884
885 if (!*line || !*arg) {
886 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
887 si->applet.st0 = STAT_CLI_PRINT;
888 return NULL;
889 }
890
891 if (!get_backend_server(arg, line, &px, &sv)) {
892 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
893 si->applet.st0 = STAT_CLI_PRINT;
894 return NULL;
895 }
896
897 if (px->state == PR_STSTOPPED) {
898 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
899 si->applet.st0 = STAT_CLI_PRINT;
900 return NULL;
901 }
902
903 return sv;
904}
905
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200906/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200907 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100908 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200909 * designating the function which will have to process the request, which can
910 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200911 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900912static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200913{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200914 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200915 char *args[MAX_STATS_ARGS + 1];
916 int arg;
917
918 while (isspace((unsigned char)*line))
919 line++;
920
921 arg = 0;
922 args[arg] = line;
923
924 while (*line && arg < MAX_STATS_ARGS) {
925 if (isspace((unsigned char)*line)) {
926 *line++ = '\0';
927
928 while (isspace((unsigned char)*line))
929 line++;
930
931 args[++arg] = line;
932 continue;
933 }
934
935 line++;
936 }
937
938 while (++arg <= MAX_STATS_ARGS)
939 args[arg] = line;
940
Willy Tarreau295a8372011-03-10 11:25:07 +0100941 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200942 if (strcmp(args[0], "show") == 0) {
943 if (strcmp(args[1], "stat") == 0) {
944 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100945 si->applet.ctx.stats.flags |= STAT_BOUND;
946 si->applet.ctx.stats.iid = atoi(args[2]);
947 si->applet.ctx.stats.type = atoi(args[3]);
948 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200949 }
950
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200951 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100952 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200953 }
954 else if (strcmp(args[1], "info") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200955 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100956 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200957 }
958 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200959 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200960 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100961 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100962 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200963 return 1;
964 }
Willy Tarreau76153662012-11-26 01:16:39 +0100965 if (*args[2] && strcmp(args[2], "all") == 0)
966 si->applet.ctx.sess.target = (void *)-1;
967 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100968 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100969 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100970 si->applet.ctx.sess.target = NULL;
971 si->applet.ctx.sess.section = 0; /* start with session status */
972 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100973 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200974 }
975 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200976 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100978 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200979 return 1;
980 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200981 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100982 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200983 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100984 si->applet.ctx.errors.iid = -1;
985 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200986 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100987 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200988 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200989 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200990 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200991 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700992 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200993 return 0;
994 }
995 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200996 else if (strcmp(args[0], "clear") == 0) {
997 if (strcmp(args[1], "counters") == 0) {
998 struct proxy *px;
999 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001000 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001001 int clrall = 0;
1002
1003 if (strcmp(args[2], "all") == 0)
1004 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001005
Willy Tarreau6162db22009-10-10 17:13:00 +02001006 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001007 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1008 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001009 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001010 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001011 return 1;
1012 }
1013
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001014 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001015 if (clrall) {
1016 memset(&px->be_counters, 0, sizeof(px->be_counters));
1017 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1018 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001019 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001020 px->be_counters.conn_max = 0;
1021 px->be_counters.p.http.rps_max = 0;
1022 px->be_counters.sps_max = 0;
1023 px->be_counters.cps_max = 0;
1024 px->be_counters.nbpend_max = 0;
1025
1026 px->fe_counters.conn_max = 0;
1027 px->fe_counters.p.http.rps_max = 0;
1028 px->fe_counters.sps_max = 0;
1029 px->fe_counters.cps_max = 0;
1030 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001031 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001032
1033 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001034 if (clrall)
1035 memset(&sv->counters, 0, sizeof(sv->counters));
1036 else {
1037 sv->counters.cur_sess_max = 0;
1038 sv->counters.nbpend_max = 0;
1039 sv->counters.sps_max = 0;
1040 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001041
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001042 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001043 if (li->counters) {
1044 if (clrall)
1045 memset(li->counters, 0, sizeof(*li->counters));
1046 else
1047 li->counters->conn_max = 0;
1048 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001049 }
1050
Willy Tarreau81c25d02011-09-07 15:17:21 +02001051 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001052 return 1;
1053 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001054 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001055 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001056 /* end of processing */
1057 return 1;
1058 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001059 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001060 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001061 return 0;
1062 }
1063 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001064 else if (strcmp(args[0], "get") == 0) {
1065 if (strcmp(args[1], "weight") == 0) {
1066 struct proxy *px;
1067 struct server *sv;
1068
1069 /* split "backend/server" and make <line> point to server */
1070 for (line = args[2]; *line; line++)
1071 if (*line == '/') {
1072 *line++ = '\0';
1073 break;
1074 }
1075
1076 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001077 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001078 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001079 return 1;
1080 }
1081
1082 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001083 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001084 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001085 return 1;
1086 }
1087
1088 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001089 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1090 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001091 return 1;
1092 }
1093 else { /* not "get weight" */
1094 return 0;
1095 }
1096 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001097 else if (strcmp(args[0], "set") == 0) {
1098 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001099 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001100 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001101
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001102 sv = expect_server_admin(s, si, args[2]);
1103 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001104 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001105
Simon Horman7d09b9a2013-02-12 10:45:51 +09001106 warning = server_parse_weight_change_request(sv, args[3]);
1107 if (warning) {
1108 si->applet.ctx.cli.msg = warning;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001109 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001110 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001111 return 1;
1112 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001113 else if (strcmp(args[1], "timeout") == 0) {
1114 if (strcmp(args[2], "cli") == 0) {
1115 unsigned timeout;
1116 const char *res;
1117
1118 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001119 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001120 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001121 return 1;
1122 }
1123
1124 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1125 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001126 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001127 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001128 return 1;
1129 }
1130
1131 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1132 return 1;
1133 }
1134 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001135 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001136 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001137 return 1;
1138 }
1139 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001140 else if (strcmp(args[1], "maxconn") == 0) {
1141 if (strcmp(args[2], "frontend") == 0) {
1142 struct proxy *px;
1143 struct listener *l;
1144 int v;
1145
Willy Tarreau532a4502011-09-07 22:37:44 +02001146 px = expect_frontend_admin(s, si, args[3]);
1147 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001148 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001149
1150 if (!*args[4]) {
1151 si->applet.ctx.cli.msg = "Integer value expected.\n";
1152 si->applet.st0 = STAT_CLI_PRINT;
1153 return 1;
1154 }
1155
1156 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001157 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001158 si->applet.ctx.cli.msg = "Value out of range.\n";
1159 si->applet.st0 = STAT_CLI_PRINT;
1160 return 1;
1161 }
1162
1163 /* OK, the value is fine, so we assign it to the proxy and to all of
1164 * its listeners. The blocked ones will be dequeued.
1165 */
1166 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001167 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001168 l->maxconn = v;
1169 if (l->state == LI_FULL)
1170 resume_listener(l);
1171 }
1172
1173 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1174 dequeue_all_listeners(&s->fe->listener_queue);
1175
1176 return 1;
1177 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001178 else if (strcmp(args[2], "global") == 0) {
1179 int v;
1180
Willy Tarreau290e63a2012-09-20 18:07:14 +02001181 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001182 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1183 si->applet.st0 = STAT_CLI_PRINT;
1184 return 1;
1185 }
1186
1187 if (!*args[3]) {
1188 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1189 si->applet.st0 = STAT_CLI_PRINT;
1190 return 1;
1191 }
1192
1193 v = atoi(args[3]);
1194 if (v > global.hardmaxconn) {
1195 si->applet.ctx.cli.msg = "Value out of range.\n";
1196 si->applet.st0 = STAT_CLI_PRINT;
1197 return 1;
1198 }
1199
1200 /* check for unlimited values */
1201 if (v <= 0)
1202 v = global.hardmaxconn;
1203
1204 global.maxconn = v;
1205
1206 /* Dequeues all of the listeners waiting for a resource */
1207 if (!LIST_ISEMPTY(&global_listener_queue))
1208 dequeue_all_listeners(&global_listener_queue);
1209
1210 return 1;
1211 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001212 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001213 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001214 si->applet.st0 = STAT_CLI_PRINT;
1215 return 1;
1216 }
1217 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001218 else if (strcmp(args[1], "rate-limit") == 0) {
1219 if (strcmp(args[2], "connections") == 0) {
1220 if (strcmp(args[3], "global") == 0) {
1221 int v;
1222
Willy Tarreau290e63a2012-09-20 18:07:14 +02001223 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001224 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1225 si->applet.st0 = STAT_CLI_PRINT;
1226 return 1;
1227 }
1228
1229 if (!*args[4]) {
1230 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1231 si->applet.st0 = STAT_CLI_PRINT;
1232 return 1;
1233 }
1234
1235 v = atoi(args[4]);
1236 if (v < 0) {
1237 si->applet.ctx.cli.msg = "Value out of range.\n";
1238 si->applet.st0 = STAT_CLI_PRINT;
1239 return 1;
1240 }
1241
1242 global.cps_lim = v;
1243
1244 /* Dequeues all of the listeners waiting for a resource */
1245 if (!LIST_ISEMPTY(&global_listener_queue))
1246 dequeue_all_listeners(&global_listener_queue);
1247
1248 return 1;
1249 }
1250 else {
1251 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1252 si->applet.st0 = STAT_CLI_PRINT;
1253 return 1;
1254 }
1255 }
William Lallemandd85f9172012-11-09 17:05:39 +01001256 else if (strcmp(args[2], "http-compression") == 0) {
1257 if (strcmp(args[3], "global") == 0) {
1258 int v;
1259
Willy Tarreau85d47f92012-11-21 00:29:50 +01001260 if (!*args[4]) {
1261 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1262 si->applet.st0 = STAT_CLI_PRINT;
1263 return 1;
1264 }
1265
William Lallemandd85f9172012-11-09 17:05:39 +01001266 v = atoi(args[4]);
1267 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1268 }
1269 else {
1270 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1271 si->applet.st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001275 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001276 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001277 si->applet.st0 = STAT_CLI_PRINT;
1278 return 1;
1279 }
1280 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001281 else if (strcmp(args[1], "table") == 0) {
1282 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1283 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001284 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001285 return 0;
1286 }
1287 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001288 else if (strcmp(args[0], "enable") == 0) {
1289 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001290 struct server *sv;
1291
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001292 sv = expect_server_admin(s, si, args[2]);
1293 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001294 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001295
Cyril Bontécd19e512010-01-31 22:34:03 +01001296 if (sv->state & SRV_MAINTAIN) {
1297 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001298 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001299 /* If this server tracks the status of another one,
1300 * we must restore the good status.
1301 */
Willy Tarreau44267702011-10-28 15:35:33 +02001302 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001303 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001304 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001305 } else {
1306 sv->state &= ~SRV_MAINTAIN;
1307 set_server_down(sv);
1308 }
1309 } else {
1310 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001311 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001312 }
1313 }
1314
Willy Tarreau532a4502011-09-07 22:37:44 +02001315 return 1;
1316 }
1317 else if (strcmp(args[1], "frontend") == 0) {
1318 struct proxy *px;
1319
1320 px = expect_frontend_admin(s, si, args[2]);
1321 if (!px)
1322 return 1;
1323
1324 if (px->state == PR_STSTOPPED) {
1325 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1326 si->applet.st0 = STAT_CLI_PRINT;
1327 return 1;
1328 }
1329
1330 if (px->state != PR_STPAUSED) {
1331 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1332 si->applet.st0 = STAT_CLI_PRINT;
1333 return 1;
1334 }
1335
1336 if (!resume_proxy(px)) {
1337 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1338 si->applet.st0 = STAT_CLI_PRINT;
1339 return 1;
1340 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001341 return 1;
1342 }
1343 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001344 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1345 si->applet.st0 = STAT_CLI_PRINT;
1346 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001347 }
1348 }
1349 else if (strcmp(args[0], "disable") == 0) {
1350 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001351 struct server *sv;
1352
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001353 sv = expect_server_admin(s, si, args[2]);
1354 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001355 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001356
Cyril Bontécd19e512010-01-31 22:34:03 +01001357 if (! (sv->state & SRV_MAINTAIN)) {
1358 /* Not already in maintenance, we can change the server state */
1359 sv->state |= SRV_MAINTAIN;
1360 set_server_down(sv);
1361 }
1362
Willy Tarreau532a4502011-09-07 22:37:44 +02001363 return 1;
1364 }
1365 else if (strcmp(args[1], "frontend") == 0) {
1366 struct proxy *px;
1367
1368 px = expect_frontend_admin(s, si, args[2]);
1369 if (!px)
1370 return 1;
1371
1372 if (px->state == PR_STSTOPPED) {
1373 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1374 si->applet.st0 = STAT_CLI_PRINT;
1375 return 1;
1376 }
1377
1378 if (px->state == PR_STPAUSED) {
1379 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1380 si->applet.st0 = STAT_CLI_PRINT;
1381 return 1;
1382 }
1383
1384 if (!pause_proxy(px)) {
1385 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1386 si->applet.st0 = STAT_CLI_PRINT;
1387 return 1;
1388 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001389 return 1;
1390 }
1391 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001392 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1393 si->applet.st0 = STAT_CLI_PRINT;
1394 return 1;
1395 }
1396 }
1397 else if (strcmp(args[0], "shutdown") == 0) {
1398 if (strcmp(args[1], "frontend") == 0) {
1399 struct proxy *px;
1400
1401 px = expect_frontend_admin(s, si, args[2]);
1402 if (!px)
1403 return 1;
1404
1405 if (px->state == PR_STSTOPPED) {
1406 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1407 si->applet.st0 = STAT_CLI_PRINT;
1408 return 1;
1409 }
1410
1411 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1412 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1413 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1414 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1415 stop_proxy(px);
1416 return 1;
1417 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001418 else if (strcmp(args[1], "session") == 0) {
1419 struct session *sess, *ptr;
1420
Willy Tarreau290e63a2012-09-20 18:07:14 +02001421 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001422 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1423 si->applet.st0 = STAT_CLI_PRINT;
1424 return 1;
1425 }
1426
1427 if (!*args[2]) {
1428 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1429 si->applet.st0 = STAT_CLI_PRINT;
1430 return 1;
1431 }
1432
1433 ptr = (void *)strtoul(args[2], NULL, 0);
1434
1435 /* first, look for the requested session in the session table */
1436 list_for_each_entry(sess, &sessions, list) {
1437 if (sess == ptr)
1438 break;
1439 }
1440
1441 /* do we have the session ? */
1442 if (sess != ptr) {
1443 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1444 si->applet.st0 = STAT_CLI_PRINT;
1445 return 1;
1446 }
1447
1448 session_shutdown(sess, SN_ERR_KILLED);
1449 return 1;
1450 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001451 else if (strcmp(args[1], "sessions") == 0) {
1452 if (strcmp(args[2], "server") == 0) {
1453 struct server *sv;
1454 struct session *sess, *sess_bck;
1455
1456 sv = expect_server_admin(s, si, args[3]);
1457 if (!sv)
1458 return 1;
1459
1460 /* kill all the session that are on this server */
1461 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1462 if (sess->srv_conn == sv)
1463 session_shutdown(sess, SN_ERR_KILLED);
1464
1465 return 1;
1466 }
1467 else {
1468 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1469 si->applet.st0 = STAT_CLI_PRINT;
1470 return 1;
1471 }
1472 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001473 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001474 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001475 si->applet.st0 = STAT_CLI_PRINT;
1476 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001477 }
1478 }
1479 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001480 return 0;
1481 }
1482 return 1;
1483}
1484
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001485/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001486 * used to processes I/O from/to the stats unix socket. The system relies on a
1487 * state machine handling requests and various responses. We read a request,
1488 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001489 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1490 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001491 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001492 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001493static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001494{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001495 struct channel *req = si->ob;
1496 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001497 int reql;
1498 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001499
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001500 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1501 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001502
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001503 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001504 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001505 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001506 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001507 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001508 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001509 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001510 /* Let's close for real now. We just close the request
1511 * side, the conditions below will complete if needed.
1512 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001513 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001514 break;
1515 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001516 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001517 /* ensure we have some output room left in the event we
1518 * would want to return some info right after parsing.
1519 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001520 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001521 break;
1522
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001523 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001524 if (reql <= 0) { /* closed or EOL not found */
1525 if (reql == 0)
1526 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001527 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001528 continue;
1529 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001530
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001531 /* seek for a possible semi-colon. If we find one, we
1532 * replace it with an LF and skip only this part.
1533 */
1534 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001535 if (trash.str[len] == ';') {
1536 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001537 reql = len + 1;
1538 break;
1539 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540
Willy Tarreau816fc222009-10-04 07:36:58 +02001541 /* now it is time to check that we have a full line,
1542 * remove the trailing \n and possibly \r, then cut the
1543 * line.
1544 */
1545 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001546 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001547 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001548 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001550
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001551 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001552 len--;
1553
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001554 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001555
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001557 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001558 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001559 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001560 continue;
1561 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001562 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001564 else if (strcmp(trash.str, "help") == 0 ||
1565 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001566 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001567 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001568 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001569 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001570 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001571 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001573 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001574 /* if prompt is disabled, print help on empty lines,
1575 * so that the user at least knows how to enable
1576 * prompt and find help.
1577 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001578 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001579 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001580 }
1581
1582 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001583 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001584 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001585 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001586 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001587 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001588 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001589 continue;
1590 }
1591
Willy Tarreaubc4af052011-02-13 13:25:14 +01001592 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001593 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001594 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001595 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001596 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001597 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001598 if (stats_dump_info_to_buffer(si))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001599 si->applet.st0 = STAT_CLI_PROMPT;
1600 break;
1601 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001602 if (stats_dump_stat_to_buffer(si, NULL))
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_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001606 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001607 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001608 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001609 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001610 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001611 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001612 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001613 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001614 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001615 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001616 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001617 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001618 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001619 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001620 break;
1621 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001622
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001623 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001624 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001625 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001626 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001627 }
1628
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001629 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001630 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001631 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001632
1633 /* Now we close the output if one of the writers did so,
1634 * or if we're not in interactive mode and the request
1635 * buffer is empty. This still allows pipelined requests
1636 * to be sent in non-interactive mode.
1637 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001638 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001639 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001640 continue;
1641 }
1642
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001643 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001644 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001645 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001646 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001647
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001648 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001649 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1650 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001651 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001652 * and nothing more to consume. This is comparable to a broken pipe, so
1653 * we forward the close to the request side so that it flows upstream to
1654 * the client.
1655 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001656 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001657 }
1658
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001659 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001660 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1661 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1662 /* We have no more processing to do, and nothing more to send, and
1663 * the client side has closed. So we'll forward this state downstream
1664 * on the response buffer.
1665 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001666 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001667 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001668 }
1669
1670 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001671 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001672
1673 /* we don't want to expire timeouts while we're processing requests */
1674 si->ib->rex = TICK_ETERNITY;
1675 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001676
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001677 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001678 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 +02001679 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001680 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 +02001681
1682 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1683 /* check that we have released everything then unregister */
1684 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001685 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001686}
1687
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001688/* This function dumps information onto the stream interface's read buffer.
1689 * It returns 0 as long as it does not complete, non-zero upon completion.
1690 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001691 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001692static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001693{
1694 unsigned int up = (now.tv_sec - start_date.tv_sec);
1695
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001696 chunk_printf(&trash,
1697 "Name: " PRODUCT_NAME "\n"
1698 "Version: " HAPROXY_VERSION "\n"
1699 "Release_date: " HAPROXY_DATE "\n"
1700 "Nbproc: %d\n"
1701 "Process_num: %d\n"
1702 "Pid: %d\n"
1703 "Uptime: %dd %dh%02dm%02ds\n"
1704 "Uptime_sec: %d\n"
1705 "Memmax_MB: %d\n"
1706 "Ulimit-n: %d\n"
1707 "Maxsock: %d\n"
1708 "Maxconn: %d\n"
1709 "Hard_maxconn: %d\n"
1710 "Maxpipes: %d\n"
1711 "CurrConns: %d\n"
1712 "PipesUsed: %d\n"
1713 "PipesFree: %d\n"
1714 "ConnRate: %d\n"
1715 "ConnRateLimit: %d\n"
1716 "MaxConnRate: %d\n"
1717 "CompressBpsIn: %u\n"
1718 "CompressBpsOut: %u\n"
1719 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001720#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001721 "ZlibMemUsage: %ld\n"
1722 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001723#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001724 "Tasks: %d\n"
1725 "Run_queue: %d\n"
1726 "Idle_pct: %d\n"
1727 "node: %s\n"
1728 "description: %s\n"
1729 "",
1730 global.nbproc,
1731 relative_pid,
1732 pid,
1733 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1734 up,
1735 global.rlimit_memmax,
1736 global.rlimit_nofile,
1737 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1738 actconn, pipes_used, pipes_free,
1739 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1740 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1741 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001742#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001743 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001744#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001745 nb_tasks_cur, run_queue_cur, idle_pct,
1746 global.node, global.desc ? global.desc : ""
1747 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001748
1749 if (bi_putchk(si->ib, &trash) == -1)
1750 return 0;
1751
1752 return 1;
1753}
1754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001755/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1756 * the state from stream interface <si>. The caller is responsible for clearing
1757 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001758 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001759static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001760{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001761 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001762
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001763 if (!(px->cap & PR_CAP_FE))
1764 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001765
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001766 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1767 return 0;
1768
Willy Tarreau354898b2012-12-23 18:15:23 +01001769 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001770 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001771 /* name, queue */
1772 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001773
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001774 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1775 /* Column sub-heading for Enable or Disable server */
1776 chunk_appendf(&trash, "<td></td>");
1777 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001778
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001779 chunk_appendf(&trash,
1780 "<td class=ac>"
1781 "<a name=\"%s/Frontend\"></a>"
1782 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1783 "<td colspan=3></td>"
1784 "",
1785 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001786
Willy Tarreau466c9b52012-12-23 02:25:03 +01001787 chunk_appendf(&trash,
1788 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001789 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001790 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1791 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1792 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001793 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1794 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1795 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001796
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001797 if (px->mode == PR_MODE_HTTP)
1798 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001799 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001800 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001801
1802 chunk_appendf(&trash,
1803 "</table></div></u></td>"
1804 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001805 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001806 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1807 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1808 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001809 U2H(px->fe_counters.sps_max),
1810 U2H(px->fe_counters.cps_max),
1811 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001812
1813 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001814 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001815 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001816 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001817
1818 chunk_appendf(&trash,
1819 "</table></div></u></td>"
1820 /* sessions rate : limit */
1821 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001822 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001823
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001824 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001825 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001826 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001827 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001828 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1829 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001830 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001831 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1832 U2H(px->fe_counters.cum_sess),
1833 U2H(px->fe_counters.cum_conn),
1834 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001835
Willy Tarreau466c9b52012-12-23 02:25:03 +01001836 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001837 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001838 chunk_appendf(&trash,
1839 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1840 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1841 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1842 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1843 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1844 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1845 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1846 "<tr><th>- other responses:</th><td>%s</td></tr>"
1847 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1848 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001849 U2H(px->fe_counters.p.http.cum_req),
1850 U2H(px->fe_counters.p.http.rsp[1]),
1851 U2H(px->fe_counters.p.http.rsp[2]),
1852 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001853 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001854 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001855 U2H(px->fe_counters.p.http.rsp[3]),
1856 U2H(px->fe_counters.p.http.rsp[4]),
1857 U2H(px->fe_counters.p.http.rsp[5]),
1858 U2H(px->fe_counters.p.http.rsp[0]),
1859 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001860 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001861
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001862 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001863 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001864 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001865 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001866 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001867 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001868 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001869 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001870
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001871 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001872 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001873 "<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 +01001874 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001875 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001876 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1877 px->fe_counters.comp_in ?
1878 (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 +01001879 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001880
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001881 chunk_appendf(&trash,
1882 /* denied: req, resp */
1883 "<td>%s</td><td>%s</td>"
1884 /* errors : request, connect, response */
1885 "<td>%s</td><td></td><td></td>"
1886 /* warnings: retries, redispatches */
1887 "<td></td><td></td>"
1888 /* server status : reflect frontend status */
1889 "<td class=ac>%s</td>"
1890 /* rest of server: nothing */
1891 "<td class=ac colspan=8></td></tr>"
1892 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001893 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1894 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001895 px->state == PR_STREADY ? "OPEN" :
1896 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001897 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001898 else { /* CSV mode */
1899 chunk_appendf(&trash,
1900 /* pxid, name, queue cur, queue max, */
1901 "%s,FRONTEND,,,"
1902 /* sessions : current, max, limit, total */
1903 "%d,%d,%d,%lld,"
1904 /* bytes : in, out */
1905 "%lld,%lld,"
1906 /* denied: req, resp */
1907 "%lld,%lld,"
1908 /* errors : request, connect, response */
1909 "%lld,,,"
1910 /* warnings: retries, redispatches */
1911 ",,"
1912 /* server status : reflect frontend status */
1913 "%s,"
1914 /* rest of server: nothing */
1915 ",,,,,,,,"
1916 /* pid, iid, sid, throttle, lbtot, tracked, type */
1917 "%d,%d,0,,,,%d,"
1918 /* rate, rate_lim, rate_max */
1919 "%u,%u,%u,"
1920 /* check_status, check_code, check_duration */
1921 ",,,",
1922 px->id,
1923 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1924 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1925 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1926 px->fe_counters.failed_req,
1927 px->state == PR_STREADY ? "OPEN" :
1928 px->state == PR_STFULL ? "FULL" : "STOP",
1929 relative_pid, px->uuid, STATS_TYPE_FE,
1930 read_freq_ctr(&px->fe_sess_per_sec),
1931 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001932
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001933 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1934 if (px->mode == PR_MODE_HTTP) {
1935 for (i=1; i<6; i++)
1936 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1937 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1938 }
1939 else
1940 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001941
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001942 /* failed health analyses */
1943 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001944
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001945 /* requests : req_rate, req_rate_max, req_tot, */
1946 chunk_appendf(&trash, "%u,%u,%lld,",
1947 read_freq_ctr(&px->fe_req_per_sec),
1948 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1949
1950 /* errors: cli_aborts, srv_aborts */
1951 chunk_appendf(&trash, ",,");
1952
1953 /* compression: in, out, bypassed */
1954 chunk_appendf(&trash, "%lld,%lld,%lld,",
1955 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1956
1957 /* compression: comp_rsp */
1958 chunk_appendf(&trash, "%lld,",
1959 px->fe_counters.p.http.comp_rsp);
1960
1961 /* finish with EOL */
1962 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001963 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001964 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001965}
1966
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001967/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1968 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1969 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1970 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02001971 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001972static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02001973{
Willy Tarreau354898b2012-12-23 18:15:23 +01001974 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001975 chunk_appendf(&trash, "<tr class=socket>");
1976 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1977 /* Column sub-heading for Enable or Disable server */
1978 chunk_appendf(&trash, "<td></td>");
1979 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001980 chunk_appendf(&trash,
1981 /* frontend name, listener name */
1982 "<td class=ac><a name=\"%s/+%s\"></a>%s"
1983 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
1984 "",
1985 px->id, l->name,
1986 (flags & ST_SHLGNDS)?"<u>":"",
1987 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02001988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001989 if (flags & ST_SHLGNDS) {
1990 char str[INET6_ADDRSTRLEN];
1991 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02001992
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001993 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02001994
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001995 port = get_host_port(&l->addr);
1996 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1997 case AF_INET:
1998 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
1999 break;
2000 case AF_INET6:
2001 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2002 break;
2003 case AF_UNIX:
2004 chunk_appendf(&trash, "unix, ");
2005 break;
2006 case -1:
2007 chunk_appendf(&trash, "(%s), ", strerror(errno));
2008 break;
2009 }
Willy Tarreau91861262007-10-17 17:06:05 +02002010
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002011 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002012 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002013 }
Willy Tarreau91861262007-10-17 17:06:05 +02002014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002015 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002016 /* queue */
2017 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002018 /* sessions rate: current, max, limit */
2019 "<td colspan=3>&nbsp;</td>"
2020 /* sessions: current, max, limit, total, lbtot */
2021 "<td>%s</td><td>%s</td><td>%s</td>"
2022 "<td>%s</td><td>&nbsp;</td>"
2023 /* bytes: in, out */
2024 "<td>%s</td><td>%s</td>"
2025 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002026 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002027 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2028 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002029
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002030 chunk_appendf(&trash,
2031 /* denied: req, resp */
2032 "<td>%s</td><td>%s</td>"
2033 /* errors: request, connect, response */
2034 "<td>%s</td><td></td><td></td>"
2035 /* warnings: retries, redispatches */
2036 "<td></td><td></td>"
2037 /* server status: reflect listener status */
2038 "<td class=ac>%s</td>"
2039 /* rest of server: nothing */
2040 "<td class=ac colspan=8></td></tr>"
2041 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002042 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2043 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002044 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2045 }
2046 else { /* CSV mode */
2047 chunk_appendf(&trash,
2048 /* pxid, name, queue cur, queue max, */
2049 "%s,%s,,,"
2050 /* sessions: current, max, limit, total */
2051 "%d,%d,%d,%lld,"
2052 /* bytes: in, out */
2053 "%lld,%lld,"
2054 /* denied: req, resp */
2055 "%lld,%lld,"
2056 /* errors: request, connect, response */
2057 "%lld,,,"
2058 /* warnings: retries, redispatches */
2059 ",,"
2060 /* server status: reflect listener status */
2061 "%s,"
2062 /* rest of server: nothing */
2063 ",,,,,,,,"
2064 /* pid, iid, sid, throttle, lbtot, tracked, type */
2065 "%d,%d,%d,,,,%d,"
2066 /* rate, rate_lim, rate_max */
2067 ",,,"
2068 /* check_status, check_code, check_duration */
2069 ",,,"
2070 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2071 ",,,,,,"
2072 /* failed health analyses */
2073 ","
2074 /* requests : req_rate, req_rate_max, req_tot, */
2075 ",,,"
2076 /* errors: cli_aborts, srv_aborts */
2077 ",,"
2078 /* compression: in, out, bypassed, comp_rsp */
2079 ",,,,"
2080 "\n",
2081 px->id, l->name,
2082 l->nbconn, l->counters->conn_max,
2083 l->maxconn, l->counters->cum_conn,
2084 l->counters->bytes_in, l->counters->bytes_out,
2085 l->counters->denied_req, l->counters->denied_resp,
2086 l->counters->failed_req,
2087 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2088 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2089 }
2090 return 1;
2091}
Willy Tarreau91861262007-10-17 17:06:05 +02002092
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002093/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2094 * from stream interface <si>, stats flags <flags>, and server state <state>.
2095 * The caller is responsible for clearing the trash if needed. Returns non-zero
2096 * if it emits anything, zero otherwise. The <state> parameter can take the
2097 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked.
2098 */
2099static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2100{
2101 struct server *ref = sv->track ? sv->track : sv;
2102 char str[INET6_ADDRSTRLEN];
2103 struct chunk src;
2104 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002105
Willy Tarreau354898b2012-12-23 18:15:23 +01002106 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002107 static char *srv_hlt_st[7] = {
2108 "DOWN",
2109 "DN %d/%d &uarr;",
2110 "UP %d/%d &darr;",
2111 "UP",
2112 "NOLB %d/%d &darr;",
2113 "NOLB",
2114 "<i>no check</i>"
2115 };
Willy Tarreau91861262007-10-17 17:06:05 +02002116
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002117 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2118 chunk_appendf(&trash, "<tr class=\"maintain\">");
Geoff Bucarcc8bb922013-04-18 13:53:16 -07002119 else if (sv->eweight == 0)
2120 chunk_appendf(&trash, "<tr class=\"softstop\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002121 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002122 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002123 "<tr class=\"%s%d\">",
2124 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002125
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002126 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002127 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002128 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2129 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002130
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002131 chunk_appendf(&trash,
2132 "<td class=ac><a name=\"%s/%s\"></a>%s"
2133 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2134 "",
2135 px->id, sv->id,
2136 (flags & ST_SHLGNDS) ? "<u>" : "",
2137 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002138
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002139 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002140 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002141
2142 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2143 case AF_INET:
2144 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2145 break;
2146 case AF_INET6:
2147 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2148 break;
2149 case AF_UNIX:
2150 chunk_appendf(&trash, "unix, ");
2151 break;
2152 case -1:
2153 chunk_appendf(&trash, "(%s), ", strerror(errno));
2154 break;
2155 default: /* address family not supported */
2156 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002157 }
Willy Tarreau91861262007-10-17 17:06:05 +02002158
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002159 /* id */
2160 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002161
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002162 /* cookie */
2163 if (sv->cookie) {
2164 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002165
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002166 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2167 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002168
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002169 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002170 }
2171
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002172 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002173 }
Willy Tarreau91861262007-10-17 17:06:05 +02002174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002175 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002176 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002177 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002178 /* sessions rate : current, max, limit */
2179 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002180 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002181 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002182 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2183 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002184
Willy Tarreau466c9b52012-12-23 02:25:03 +01002185
2186 chunk_appendf(&trash,
2187 /* sessions: current, max, limit, total */
2188 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002189 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002190 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2191 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002192 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2193 U2H(sv->counters.cum_sess),
2194 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002195
Willy Tarreau466c9b52012-12-23 02:25:03 +01002196 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2197 if (px->mode == PR_MODE_HTTP) {
2198 unsigned long long tot;
2199 for (tot = i = 0; i < 6; i++)
2200 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002201
Willy Tarreau466c9b52012-12-23 02:25:03 +01002202 chunk_appendf(&trash,
2203 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2204 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2205 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2206 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2207 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2208 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2209 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2210 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002211 U2H(tot),
2212 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2213 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2214 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2215 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2216 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2217 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 +02002218 }
Willy Tarreau91861262007-10-17 17:06:05 +02002219
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002220 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002221 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002222 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002223 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002224 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002225
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002226 chunk_appendf(&trash,
2227 /* bytes : in, out */
2228 "<td>%s</td><td>%s</td>"
2229 /* denied: req, resp */
2230 "<td></td><td>%s</td>"
2231 /* errors : request, connect */
2232 "<td></td><td>%s</td>"
2233 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002234 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002235 /* warnings: retries, redispatches */
2236 "<td>%lld</td><td>%lld</td>"
2237 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002238 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2239 U2H(sv->counters.failed_secu),
2240 U2H(sv->counters.failed_conns),
2241 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002242 sv->counters.cli_aborts,
2243 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002244 sv->counters.retries, sv->counters.redispatches);
2245
2246 /* status, lest check */
2247 chunk_appendf(&trash, "<td class=ac>");
2248
2249 if (sv->state & SRV_MAINTAIN) {
2250 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2251 chunk_appendf(&trash, "MAINT");
2252 }
2253 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2254 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2255 chunk_appendf(&trash, "MAINT(via)");
2256 }
2257 else if (ref->state & SRV_CHECKED) {
2258 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2259 chunk_appendf(&trash,
2260 srv_hlt_st[state],
2261 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2262 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002263 }
Willy Tarreau91861262007-10-17 17:06:05 +02002264
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002265 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002266 chunk_appendf(&trash,
2267 "</td><td class=ac><u> %s%s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002268 (sv->state & SRV_CHK_RUNNING) ? "* " : "",
2269 get_check_status_info(sv->check.status));
2270
2271 if (sv->check.status >= HCHK_STATUS_L57DATA)
2272 chunk_appendf(&trash, "/%d", sv->check.code);
2273
2274 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002275 chunk_appendf(&trash, " in %lums", sv->check.duration);
2276
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002277 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002278 get_check_status_description(sv->check.status));
2279 if (*sv->check.desc) {
2280 chunk_appendf(&trash, ": ");
2281 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2282 chunk_htmlencode(&trash, &src);
2283 }
2284 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002285 }
2286 else
2287 chunk_appendf(&trash, "</td><td>");
2288
2289 chunk_appendf(&trash,
2290 /* weight */
2291 "</td><td class=ac>%d</td>"
2292 /* act, bck */
2293 "<td class=ac>%s</td><td class=ac>%s</td>"
2294 "",
2295 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2296 (sv->state & SRV_BACKUP) ? "-" : "Y",
2297 (sv->state & SRV_BACKUP) ? "Y" : "-");
2298
2299 /* check failures: unique, fatal, down time */
2300 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002301 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002302
2303 if (ref->observe)
2304 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2305
2306 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002307 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002308 "<td>%lld</td><td>%s</td>"
2309 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002310 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002311 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2312 }
2313 else if (sv != ref)
2314 chunk_appendf(&trash,
2315 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2316 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2317 else
2318 chunk_appendf(&trash, "<td colspan=3></td>");
2319
2320 /* throttle */
2321 if ((sv->state & SRV_WARMINGUP) &&
2322 now.tv_sec < sv->last_change + sv->slowstart &&
2323 now.tv_sec >= sv->last_change) {
2324 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n",
2325 (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2326 }
2327 else
2328 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2329 }
2330 else { /* CSV mode */
2331 static char *srv_hlt_st[7] = {
2332 "DOWN,",
2333 "DOWN %d/%d,",
2334 "UP %d/%d,",
2335 "UP,",
2336 "NOLB %d/%d,",
2337 "NOLB,",
2338 "no check,"
2339 };
2340
2341 chunk_appendf(&trash,
2342 /* pxid, name */
2343 "%s,%s,"
2344 /* queue : current, max */
2345 "%d,%d,"
2346 /* sessions : current, max, limit, total */
2347 "%d,%d,%s,%lld,"
2348 /* bytes : in, out */
2349 "%lld,%lld,"
2350 /* denied: req, resp */
2351 ",%lld,"
2352 /* errors : request, connect, response */
2353 ",%lld,%lld,"
2354 /* warnings: retries, redispatches */
2355 "%lld,%lld,"
2356 "",
2357 px->id, sv->id,
2358 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002359 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002360 sv->counters.bytes_in, sv->counters.bytes_out,
2361 sv->counters.failed_secu,
2362 sv->counters.failed_conns, sv->counters.failed_resp,
2363 sv->counters.retries, sv->counters.redispatches);
2364
2365 /* status */
2366 if (sv->state & SRV_MAINTAIN)
2367 chunk_appendf(&trash, "MAINT,");
2368 else if (ref != sv && ref->state & SRV_MAINTAIN)
2369 chunk_appendf(&trash, "MAINT(via),");
2370 else
2371 chunk_appendf(&trash,
2372 srv_hlt_st[state],
2373 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2374 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
2375
2376 chunk_appendf(&trash,
2377 /* weight, active, backup */
2378 "%d,%d,%d,"
2379 "",
2380 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2381 (sv->state & SRV_BACKUP) ? 0 : 1,
2382 (sv->state & SRV_BACKUP) ? 1 : 0);
2383
2384 /* check failures: unique, fatal; last change, total downtime */
2385 if (sv->state & SRV_CHECKED)
2386 chunk_appendf(&trash,
2387 "%lld,%lld,%d,%d,",
2388 sv->counters.failed_checks, sv->counters.down_trans,
2389 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2390 else
2391 chunk_appendf(&trash, ",,,,");
2392
2393 /* queue limit, pid, iid, sid, */
2394 chunk_appendf(&trash,
2395 "%s,"
2396 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002397 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002398 relative_pid, px->uuid, sv->puid);
2399
2400 /* throttle */
2401 if ((sv->state & SRV_WARMINGUP) &&
2402 now.tv_sec < sv->last_change + sv->slowstart &&
2403 now.tv_sec >= sv->last_change)
2404 chunk_appendf(&trash, "%d", (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2405
2406 /* sessions: lbtot */
2407 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2408
2409 /* tracked */
2410 if (sv->track)
2411 chunk_appendf(&trash, "%s/%s,",
2412 sv->track->proxy->id, sv->track->id);
2413 else
2414 chunk_appendf(&trash, ",");
2415
2416 /* type */
2417 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2418
2419 /* rate */
2420 chunk_appendf(&trash, "%u,,%u,",
2421 read_freq_ctr(&sv->sess_per_sec),
2422 sv->counters.sps_max);
2423
2424 if (sv->state & SRV_CHECKED) {
2425 /* check_status */
2426 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2427
2428 /* check_code */
2429 if (sv->check.status >= HCHK_STATUS_L57DATA)
2430 chunk_appendf(&trash, "%u,", sv->check.code);
2431 else
2432 chunk_appendf(&trash, ",");
2433
2434 /* check_duration */
2435 if (sv->check.status >= HCHK_STATUS_CHECKED)
2436 chunk_appendf(&trash, "%lu,", sv->check.duration);
2437 else
2438 chunk_appendf(&trash, ",");
2439
2440 }
2441 else
2442 chunk_appendf(&trash, ",,,");
2443
2444 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2445 if (px->mode == PR_MODE_HTTP) {
2446 for (i=1; i<6; i++)
2447 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2448
2449 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2450 }
2451 else
2452 chunk_appendf(&trash, ",,,,,,");
2453
2454 /* failed health analyses */
2455 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2456
2457 /* requests : req_rate, req_rate_max, req_tot, */
2458 chunk_appendf(&trash, ",,,");
2459
2460 /* errors: cli_aborts, srv_aborts */
2461 chunk_appendf(&trash, "%lld,%lld,",
2462 sv->counters.cli_aborts, sv->counters.srv_aborts);
2463
2464 /* compression: in, out, bypassed, comp_rsp */
2465 chunk_appendf(&trash, ",,,,");
2466
2467 /* finish with EOL */
2468 chunk_appendf(&trash, "\n");
2469 }
2470 return 1;
2471}
2472
2473/* Dumps a line for backend <px> to the trash for and uses the state from stream
2474 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2475 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2476 */
2477static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2478{
2479 struct chunk src;
2480 int i;
2481
2482 if (!(px->cap & PR_CAP_BE))
2483 return 0;
2484
2485 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2486 return 0;
2487
Willy Tarreau354898b2012-12-23 18:15:23 +01002488 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002489 chunk_appendf(&trash, "<tr class=\"backend\">");
2490 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2491 /* Column sub-heading for Enable or Disable server */
2492 chunk_appendf(&trash, "<td></td>");
2493 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002494 chunk_appendf(&trash,
2495 "<td class=ac>"
2496 /* name */
2497 "%s<a name=\"%s/Backend\"></a>"
2498 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2499 "",
2500 (flags & ST_SHLGNDS)?"<u>":"",
2501 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002502
2503 if (flags & ST_SHLGNDS) {
2504 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002505 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002506 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2507
2508 /* cookie */
2509 if (px->cookie_name) {
2510 chunk_appendf(&trash, ", cookie: '");
2511 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2512 chunk_htmlencode(&trash, &src);
2513 chunk_appendf(&trash, "'");
2514 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002515 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002516 }
2517
2518 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002519 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002520 /* queue : current, max */
2521 "<td>%s</td><td>%s</td><td></td>"
2522 /* sessions rate : current, max, limit */
2523 "<td>%s</td><td>%s</td><td></td>"
2524 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002525 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002526 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2527 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002528
2529 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002530 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002531 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002532 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002533 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002535 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2536 U2H(px->be_counters.cum_conn),
2537 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002538
Willy Tarreau466c9b52012-12-23 02:25:03 +01002539 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002540 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002541 chunk_appendf(&trash,
2542 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2543 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2544 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2545 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2546 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2547 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2548 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2549 "<tr><th>- other responses:</th><td>%s</td></tr>"
2550 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2551 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002552 U2H(px->be_counters.p.http.cum_req),
2553 U2H(px->be_counters.p.http.rsp[1]),
2554 U2H(px->be_counters.p.http.rsp[2]),
2555 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002556 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002557 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002558 U2H(px->be_counters.p.http.rsp[3]),
2559 U2H(px->be_counters.p.http.rsp[4]),
2560 U2H(px->be_counters.p.http.rsp[5]),
2561 U2H(px->be_counters.p.http.rsp[0]),
2562 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 }
2564
2565 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002566 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002567 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002568 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002570 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002572 U2H(px->be_counters.cum_lbconn),
2573 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002574
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002576 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002577 "<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 +01002578 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002579 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002580 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2581 px->be_counters.comp_in ?
2582 (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 +01002583 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2584
2585 chunk_appendf(&trash,
2586 /* denied: req, resp */
2587 "<td>%s</td><td>%s</td>"
2588 /* errors : request, connect */
2589 "<td></td><td>%s</td>"
2590 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002591 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002592 /* warnings: retries, redispatches */
2593 "<td>%lld</td><td>%lld</td>"
2594 /* backend status: reflect backend status (up/down): we display UP
2595 * if the backend has known working servers or if it has no server at
2596 * all (eg: for stats). Then we display the total weight, number of
2597 * active and backups. */
2598 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2599 "<td class=ac>%d</td><td class=ac>%d</td>"
2600 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002601 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2602 U2H(px->be_counters.failed_conns),
2603 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002604 px->be_counters.cli_aborts,
2605 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002606 px->be_counters.retries, px->be_counters.redispatches,
2607 human_time(now.tv_sec - px->last_change, 1),
2608 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2609 "<font color=\"red\"><b>DOWN</b></font>",
2610 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2611 px->srv_act, px->srv_bck);
2612
2613 chunk_appendf(&trash,
2614 /* rest of backend: nothing, down transitions, total downtime, throttle */
2615 "<td class=ac>&nbsp;</td><td>%d</td>"
2616 "<td>%s</td>"
2617 "<td></td>"
2618 "</tr>",
2619 px->down_trans,
2620 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2621 }
2622 else { /* CSV mode */
2623 chunk_appendf(&trash,
2624 /* pxid, name */
2625 "%s,BACKEND,"
2626 /* queue : current, max */
2627 "%d,%d,"
2628 /* sessions : current, max, limit, total */
2629 "%d,%d,%d,%lld,"
2630 /* bytes : in, out */
2631 "%lld,%lld,"
2632 /* denied: req, resp */
2633 "%lld,%lld,"
2634 /* errors : request, connect, response */
2635 ",%lld,%lld,"
2636 /* warnings: retries, redispatches */
2637 "%lld,%lld,"
2638 /* backend status: reflect backend status (up/down): we display UP
2639 * if the backend has known working servers or if it has no server at
2640 * all (eg: for stats). Then we display the total weight, number of
2641 * active and backups. */
2642 "%s,"
2643 "%d,%d,%d,"
2644 /* rest of backend: nothing, down transitions, last change, total downtime */
2645 ",%d,%d,%d,,"
2646 /* pid, iid, sid, throttle, lbtot, tracked, type */
2647 "%d,%d,0,,%lld,,%d,"
2648 /* rate, rate_lim, rate_max, */
2649 "%u,,%u,"
2650 /* check_status, check_code, check_duration */
2651 ",,,",
2652 px->id,
2653 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2654 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2655 px->be_counters.bytes_in, px->be_counters.bytes_out,
2656 px->be_counters.denied_req, px->be_counters.denied_resp,
2657 px->be_counters.failed_conns, px->be_counters.failed_resp,
2658 px->be_counters.retries, px->be_counters.redispatches,
2659 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2660 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2661 px->srv_act, px->srv_bck,
2662 px->down_trans, (int)(now.tv_sec - px->last_change),
2663 px->srv?be_downtime(px):0,
2664 relative_pid, px->uuid,
2665 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2666 read_freq_ctr(&px->be_sess_per_sec),
2667 px->be_counters.sps_max);
2668
2669 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2670 if (px->mode == PR_MODE_HTTP) {
2671 for (i=1; i<6; i++)
2672 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2673 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2674 }
2675 else
2676 chunk_appendf(&trash, ",,,,,,");
2677
2678 /* failed health analyses */
2679 chunk_appendf(&trash, ",");
2680
2681 /* requests : req_rate, req_rate_max, req_tot, */
2682 chunk_appendf(&trash, ",,,");
2683
2684 /* errors: cli_aborts, srv_aborts */
2685 chunk_appendf(&trash, "%lld,%lld,",
2686 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2687
2688 /* compression: in, out, bypassed */
2689 chunk_appendf(&trash, "%lld,%lld,%lld,",
2690 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2691
2692 /* compression: comp_rsp */
2693 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2694
2695 /* finish with EOL */
2696 chunk_appendf(&trash, "\n");
2697 }
2698 return 1;
2699}
2700
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002701/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002702 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002703 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002704 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002705static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002706{
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002707 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2708
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002709 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2710 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002711
2712 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2713 scope_txt[0] = 0;
2714 if (si->applet.ctx.stats.scope_len) {
2715 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2716 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
2717 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
2718 }
2719
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002720 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002721 "<form action=\"%s%s%s%s\" method=\"post\">",
2722 uri->uri_prefix,
2723 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2724 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2725 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002726 }
2727
2728 /* print a new table */
2729 chunk_appendf(&trash,
2730 "<table class=\"tbl\" width=\"100%%\">\n"
2731 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002732 "<th class=\"pxname\" width=\"10%%\">");
2733
2734 chunk_appendf(&trash,
2735 "<a name=\"%s\"></a>%s"
2736 "<a class=px href=\"#%s\">%s</a>",
2737 px->id,
2738 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2739 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002740
2741 if (uri->flags & ST_SHLGNDS) {
2742 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002743 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2745 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002746 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 }
2748
2749 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002750 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002751 "<th class=\"%s\" width=\"90%%\">%s</th>"
2752 "</tr>\n"
2753 "</table>\n"
2754 "<table class=\"tbl\" width=\"100%%\">\n"
2755 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002756 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2757 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2758
2759 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2760 /* Column heading for Enable or Disable server */
2761 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002762 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002763
2764 chunk_appendf(&trash,
2765 "<th rowspan=2></th>"
2766 "<th colspan=3>Queue</th>"
2767 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2768 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2769 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2770 "<th colspan=9>Server</th>"
2771 "</tr>\n"
2772 "<tr class=\"titre\">"
2773 "<th>Cur</th><th>Max</th><th>Limit</th>"
2774 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2775 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2776 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2777 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2778 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2779 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2780 "<th>Thrtle</th>\n"
2781 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002782}
2783
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002784/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002786 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002787static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002788{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 chunk_appendf(&trash, "</table>");
2790
2791 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2792 /* close the form used to enable/disable this proxy servers */
2793 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002794 "Choose the action to perform on the checked servers : "
2795 "<select name=action>"
2796 "<option value=\"\"></option>"
2797 "<option value=\"disable\">Disable</option>"
2798 "<option value=\"enable\">Enable</option>"
2799 "<option value=\"stop\">Soft Stop</option>"
2800 "<option value=\"start\">Soft Start</option>"
2801 "<option value=\"shutdown\">Kill Sessions</option>"
2802 "</select>"
2803 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2804 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2805 "</form>",
2806 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002807 }
2808
2809 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810}
Willy Tarreau91861262007-10-17 17:06:05 +02002811
2812/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002813 * Dumps statistics for a proxy. The output is sent to the stream interface's
2814 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2815 * buffer space, or non-zero if everything completed. This function is used
2816 * both by the CLI and the HTTP entry points, and is able to dump the output
2817 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002818 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002819static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002820{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002821 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002822 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002823 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002824 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002825
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002826 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002827
Willy Tarreau295a8372011-03-10 11:25:07 +01002828 switch (si->applet.ctx.stats.px_st) {
2829 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002830 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002831 if (uri && uri->scope) {
2832 /* we have a limited scope, we have to check the proxy name */
2833 struct stat_scope *scope;
2834 int len;
2835
2836 len = strlen(px->id);
2837 scope = uri->scope;
2838
2839 while (scope) {
2840 /* match exact proxy name */
2841 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2842 break;
2843
2844 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002845 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002846 break;
2847 scope = scope->next;
2848 }
2849
2850 /* proxy name not found : don't dump anything */
2851 if (scope == NULL)
2852 return 1;
2853 }
2854
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002855 /* if the user has requested a limited output and the proxy
2856 * name does not match, skip it.
2857 */
2858 if (si->applet.ctx.stats.scope_len &&
2859 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len) == NULL)
2860 return 1;
2861
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2863 (si->applet.ctx.stats.iid != -1) &&
2864 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002865 return 1;
2866
Willy Tarreau295a8372011-03-10 11:25:07 +01002867 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002868 /* fall through */
2869
Willy Tarreau295a8372011-03-10 11:25:07 +01002870 case STAT_PX_ST_TH:
Willy Tarreau354898b2012-12-23 18:15:23 +01002871 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002872 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002873 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002874 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002875 }
Willy Tarreau91861262007-10-17 17:06:05 +02002876
Willy Tarreau295a8372011-03-10 11:25:07 +01002877 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002878 /* fall through */
2879
Willy Tarreau295a8372011-03-10 11:25:07 +01002880 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002881 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002882 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002883 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002884 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002885
Willy Tarreau4348fad2012-09-20 16:48:07 +02002886 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002887 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002888 /* fall through */
2889
Willy Tarreau295a8372011-03-10 11:25:07 +01002890 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002891 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002892 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 +02002893 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002894 return 0;
2895
Willy Tarreau4348fad2012-09-20 16:48:07 +02002896 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002897 if (!l->counters)
2898 continue;
2899
Willy Tarreau295a8372011-03-10 11:25:07 +01002900 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2901 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002902 break;
2903
Willy Tarreau295a8372011-03-10 11:25:07 +01002904 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002905 continue;
2906 }
2907
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002908 /* print the frontend */
2909 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2910 if (bi_putchk(rep, &trash) == -1)
2911 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002912 }
2913
Willy Tarreau295a8372011-03-10 11:25:07 +01002914 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2915 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002916 /* fall through */
2917
Willy Tarreau295a8372011-03-10 11:25:07 +01002918 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002919 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002920 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002921 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 +02002922
Willy Tarreau9b28e032012-10-12 23:49:43 +02002923 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002924 return 0;
2925
Willy Tarreau295a8372011-03-10 11:25:07 +01002926 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002927
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2929 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002930 break;
2931
Willy Tarreau295a8372011-03-10 11:25:07 +01002932 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002933 continue;
2934 }
2935
Willy Tarreau44267702011-10-28 15:35:33 +02002936 if (sv->track)
2937 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002938 else
2939 svs = sv;
2940
Willy Tarreau91861262007-10-17 17:06:05 +02002941 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002942 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002943 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002944 else if (svs->state & SRV_RUNNING) {
2945 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002946 sv_state = 3; /* UP */
2947 else
2948 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002949
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002950 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002951 sv_state += 2;
2952 }
Willy Tarreau91861262007-10-17 17:06:05 +02002953 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002954 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002955 sv_state = 1; /* going up */
2956 else
2957 sv_state = 0; /* DOWN */
2958
Willy Tarreau295a8372011-03-10 11:25:07 +01002959 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002960 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002961 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002962 continue;
2963 }
2964
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
2966 if (bi_putchk(rep, &trash) == -1)
2967 return 0;
2968 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02002969
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002970 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
2971 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002972
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 case STAT_PX_ST_BE:
2974 /* print the backend */
2975 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
2976 if (bi_putchk(rep, &trash) == -1)
2977 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002978
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002979 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
2980 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002981
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002982 case STAT_PX_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01002983 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002984 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002985 if (bi_putchk(rep, &trash) == -1)
2986 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002987 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002989 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
2990 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002991
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002992 case STAT_PX_ST_FIN:
2993 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002994
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995 default:
2996 /* unknown state, we should put an abort() here ! */
2997 return 1;
2998 }
2999}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003000
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003001/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3002 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003004static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005{
3006 /* WARNING! This must fit in the first buffer !!! */
3007 chunk_appendf(&trash,
3008 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3009 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3010 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3011 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3012 "<style type=\"text/css\"><!--\n"
3013 "body {"
3014 " font-family: arial, helvetica, sans-serif;"
3015 " font-size: 12px;"
3016 " font-weight: normal;"
3017 " color: black;"
3018 " background: white;"
3019 "}\n"
3020 "th,td {"
3021 " font-size: 10px;"
3022 "}\n"
3023 "h1 {"
3024 " font-size: x-large;"
3025 " margin-bottom: 0.5em;"
3026 "}\n"
3027 "h2 {"
3028 " font-family: helvetica, arial;"
3029 " font-size: x-large;"
3030 " font-weight: bold;"
3031 " font-style: italic;"
3032 " color: #6020a0;"
3033 " margin-top: 0em;"
3034 " margin-bottom: 0em;"
3035 "}\n"
3036 "h3 {"
3037 " font-family: helvetica, arial;"
3038 " font-size: 16px;"
3039 " font-weight: bold;"
3040 " color: #b00040;"
3041 " background: #e8e8d0;"
3042 " margin-top: 0em;"
3043 " margin-bottom: 0em;"
3044 "}\n"
3045 "li {"
3046 " margin-top: 0.25em;"
3047 " margin-right: 2em;"
3048 "}\n"
3049 ".hr {margin-top: 0.25em;"
3050 " border-color: black;"
3051 " border-bottom-style: solid;"
3052 "}\n"
3053 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3054 ".total {background: #20D0D0;color: #ffff80;}\n"
3055 ".frontend {background: #e8e8d0;}\n"
3056 ".socket {background: #d0d0d0;}\n"
3057 ".backend {background: #e8e8d0;}\n"
3058 ".active0 {background: #ff9090;}\n"
3059 ".active1 {background: #ffd020;}\n"
3060 ".active2 {background: #ffffa0;}\n"
3061 ".active3 {background: #c0ffc0;}\n"
3062 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3063 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
3064 ".active6 {background: #e0e0e0;}\n"
3065 ".backup0 {background: #ff9090;}\n"
3066 ".backup1 {background: #ff80ff;}\n"
3067 ".backup2 {background: #c060ff;}\n"
3068 ".backup3 {background: #b0d0ff;}\n"
3069 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3070 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3071 ".backup6 {background: #e0e0e0;}\n"
3072 ".maintain {background: #c07820;}\n"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07003073 ".softstop {background: #0067FF;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003074 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3075 "\n"
3076 "a.px:link {color: #ffff40; text-decoration: none;}"
3077 "a.px:visited {color: #ffff40; text-decoration: none;}"
3078 "a.px:hover {color: #ffffff; text-decoration: none;}"
3079 "a.lfsb:link {color: #000000; text-decoration: none;}"
3080 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3081 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3082 "\n"
3083 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3084 "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"
3085 "table.tbl td.ac { text-align: center;}\n"
3086 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3087 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3088 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3089 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3090 "\n"
3091 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3092 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3093 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003094 "table.det { border-collapse: collapse; border-style: none; }\n"
3095 "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"
3096 "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 +01003097 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003098 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003099 " display:block;\n"
3100 " visibility:hidden;\n"
3101 " z-index:2147483647;\n"
3102 " position:absolute;\n"
3103 " padding:2px 4px 3px;\n"
3104 " background:#f0f060; color:#000000;\n"
3105 " border:1px solid #7040c0;\n"
3106 " white-space:nowrap;\n"
3107 " font-style:normal;font-size:11px;font-weight:normal;\n"
3108 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3109 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3110 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003111 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112 "-->\n"
3113 "</style></head>\n",
3114 (uri->flags & ST_SHNODE) ? " on " : "",
3115 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3116 );
3117}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003118
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003119/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003121 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003122 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003123static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003124{
3125 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003126 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003128 /* WARNING! this has to fit the first packet too.
3129 * We are around 3.5 kB, add adding entries will
3130 * become tricky if we want to support 4kB buffers !
3131 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132 chunk_appendf(&trash,
3133 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3134 PRODUCT_NAME "%s</a></h1>\n"
3135 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3136 "<hr width=\"100%%\" class=\"hr\">\n"
3137 "<h3>&gt; General process information</h3>\n"
3138 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3139 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3140 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3141 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3142 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3143 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3144 "Running tasks: %d/%d; idle = %d %%<br>\n"
3145 "</td><td align=\"center\" nowrap>\n"
3146 "<table class=\"lgd\"><tr>\n"
3147 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3148 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3149 "</tr><tr>\n"
3150 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3151 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3152 "</tr><tr>\n"
3153 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3154 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3155 "</tr><tr>\n"
3156 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3157 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
3158 "</tr><tr>\n"
3159 "<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 -07003160 "</tr><tr>\n"
3161 "<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 +01003162 "</tr></table>\n"
3163 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3164 "</td>"
3165 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3166 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3167 "",
3168 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3169 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3170 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3171 (uri->flags & ST_SHDESC) ? ": " : "",
3172 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3173 pid, relative_pid, global.nbproc,
3174 up / 86400, (up % 86400) / 3600,
3175 (up % 3600) / 60, (up % 60),
3176 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3177 global.rlimit_memmax ? " MB" : "",
3178 global.rlimit_nofile,
3179 global.maxsock, global.maxconn, global.maxpipes,
3180 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3181 run_queue_cur, nb_tasks_cur, idle_pct
3182 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003183
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003184 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3185 memcpy(scope_txt, bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3186 scope_txt[si->applet.ctx.stats.scope_len] = '\0';
3187
3188 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003189 "<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 +02003190 uri->uri_prefix,
3191 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3192 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3193 (si->applet.ctx.stats.scope_len > 0) ? scope_txt : "",
3194 STAT_SCOPE_TXT_MAXLEN);
3195
3196 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3197 scope_txt[0] = 0;
3198 if (si->applet.ctx.stats.scope_len) {
3199 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3200 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3201 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3202 }
3203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003204 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3205 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003206 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003207 uri->uri_prefix,
3208 "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003209 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3210 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211 else
3212 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003213 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003214 uri->uri_prefix,
3215 ";up",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003216 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3217 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003218
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 if (uri->refresh > 0) {
3220 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3221 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003222 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 uri->uri_prefix,
3224 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003225 "",
3226 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003227 else
3228 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003229 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003230 uri->uri_prefix,
3231 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003232 ";norefresh",
3233 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003234 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003235
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003236 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003237 "<li><a href=\"%s%s%s%s\">Refresh now</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 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3241 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003242
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003243 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003244 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003245 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003246 (uri->refresh > 0) ? ";norefresh" : "",
3247 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003249 chunk_appendf(&trash,
3250 "</ul></td>"
3251 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3252 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3253 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3254 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3255 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3256 "</ul>"
3257 "</td>"
3258 "</tr></table>\n"
3259 ""
3260 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003261
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003262 if (si->applet.ctx.stats.st_code) {
3263 switch (si->applet.ctx.stats.st_code) {
3264 case STAT_STATUS_DONE:
3265 chunk_appendf(&trash,
3266 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003267 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003268 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003269 "</div>\n", uri->uri_prefix,
3270 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3271 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3272 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003273 break;
3274 case STAT_STATUS_NONE:
3275 chunk_appendf(&trash,
3276 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003277 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003279 "</div>\n", uri->uri_prefix,
3280 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3281 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3282 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283 break;
3284 case STAT_STATUS_PART:
3285 chunk_appendf(&trash,
3286 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003287 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003288 "Action partially processed.<br>"
3289 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003290 "</div>\n", uri->uri_prefix,
3291 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3292 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3293 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 break;
3295 case STAT_STATUS_ERRP:
3296 chunk_appendf(&trash,
3297 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003298 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003299 "Action not processed because of invalid parameters."
3300 "<ul>"
3301 "<li>The action is maybe unknown.</li>"
3302 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3303 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3304 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003305 "</div>\n", uri->uri_prefix,
3306 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3307 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3308 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309 break;
3310 case STAT_STATUS_EXCD:
3311 chunk_appendf(&trash,
3312 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003313 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3315 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003316 "</div>\n", uri->uri_prefix,
3317 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3318 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3319 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 break;
3321 case STAT_STATUS_DENY:
3322 chunk_appendf(&trash,
3323 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003324 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003325 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003326 "</div>\n", uri->uri_prefix,
3327 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3328 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3329 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003330 break;
3331 default:
3332 chunk_appendf(&trash,
3333 "<p><div class=active6>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003334 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003336 "</div>\n", uri->uri_prefix,
3337 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3338 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3339 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003340 }
3341 chunk_appendf(&trash, "<p>\n");
3342 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003344
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003345/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3346 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003348static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003349{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003351}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003352
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003353/* This function dumps statistics onto the stream interface's read buffer in
3354 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3355 * are ignored for CSV format (hence <uri> may be NULL there). The xprt_ctx must
3356 * have been zeroed first, and the flags properly set. It returns 0 if it had to
3357 * stop writing data and an I/O is needed, 1 if the dump is finished and the
3358 * session must be closed, or -1 in case of any error. This function is used by
3359 * both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003361static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 struct channel *rep = si->ib;
3364 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003365
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368 switch (si->conn->xprt_st) {
3369 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3371 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003372
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 case STAT_ST_HEAD:
Willy Tarreau354898b2012-12-23 18:15:23 +01003374 if (si->applet.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003375 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003376 else
3377 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003378
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 if (bi_putchk(rep, &trash) == -1)
3380 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003381
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382 si->conn->xprt_st = STAT_ST_INFO;
3383 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 case STAT_ST_INFO:
Willy Tarreau354898b2012-12-23 18:15:23 +01003386 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003387 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003388 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003389 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003390 }
Willy Tarreau91861262007-10-17 17:06:05 +02003391
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392 si->applet.ctx.stats.px = proxy;
3393 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3394 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003395 /* fall through */
3396
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397 case STAT_ST_LIST:
3398 /* dump proxies */
3399 while (si->applet.ctx.stats.px) {
3400 if (buffer_almost_full(rep->buf))
3401 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 px = si->applet.ctx.stats.px;
3404 /* skip the disabled proxies, global frontend and non-networked ones */
3405 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003406 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003407 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003408
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 si->applet.ctx.stats.px = px->next;
3410 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3411 }
3412 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003413
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 si->conn->xprt_st = STAT_ST_END;
3415 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003416
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 case STAT_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003418 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003419 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420 if (bi_putchk(rep, &trash) == -1)
3421 return 0;
3422 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003423
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 si->conn->xprt_st = STAT_ST_FIN;
3425 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003426
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 case STAT_ST_FIN:
3428 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003429
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430 default:
3431 /* unknown state ! */
3432 si->conn->xprt_st = STAT_ST_FIN;
3433 return -1;
3434 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435}
Willy Tarreauae526782010-03-04 20:34:23 +01003436
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437/* This I/O handler runs as an applet embedded in a stream interface. It is
3438 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3439 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3440 * automatically unregisters itself once transfer is complete.
3441 */
3442static void http_stats_io_handler(struct stream_interface *si)
3443{
3444 struct session *s = si->conn->xprt_ctx;
3445 struct channel *req = si->ob;
3446 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003447
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3449 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003450
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 /* check that the output is not closed */
3452 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3453 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003454
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 if (!si->applet.st0) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003456 if (stats_dump_stat_to_buffer(si, s->be->uri_auth)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003457 si->applet.st0 = 1;
3458 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 }
3460 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003461
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3463 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003464
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003465 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3466 si_shutr(si);
3467 res->flags |= CF_READ_NULL;
3468 }
Willy Tarreau91861262007-10-17 17:06:05 +02003469
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 /* update all other flags and resync with the other side */
3471 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 /* we don't want to expire timeouts while we're processing requests */
3474 si->ib->rex = TICK_ETERNITY;
3475 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003476
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003477 out:
3478 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3479 /* check that we have released everything then unregister */
3480 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003481 }
3482}
3483
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003484
Willy Tarreau909d5172012-11-26 03:04:41 +01003485static inline const char *get_conn_ctrl_name(const struct connection *conn)
3486{
3487 if (!conn->ctrl)
3488 return "NONE";
3489 return conn->ctrl->name;
3490}
3491
3492static inline const char *get_conn_xprt_name(const struct connection *conn)
3493{
3494 static char ptr[17];
3495
3496 if (!conn->xprt)
3497 return "NONE";
3498
3499 if (conn->xprt == &raw_sock)
3500 return "RAW";
3501
3502#ifdef USE_OPENSSL
3503 if (conn->xprt == &ssl_sock)
3504 return "SSL";
3505#endif
3506 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3507 return ptr;
3508}
3509
3510static inline const char *get_conn_data_name(const struct connection *conn)
3511{
3512 static char ptr[17];
3513
3514 if (!conn->data)
3515 return "NONE";
3516
3517 if (conn->data == &sess_conn_cb)
3518 return "SESS";
3519
3520 if (conn->data == &si_conn_cb)
3521 return "STRM";
3522
3523 if (conn->data == &check_conn_cb)
3524 return "CHCK";
3525
3526 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3527 return ptr;
3528}
3529
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003530/* This function dumps a complete session state onto the stream interface's
3531 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3532 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003533 * 0 if the output buffer is full and it needs to be called again, otherwise
3534 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003535 */
Willy Tarreau76153662012-11-26 01:16:39 +01003536static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003537{
3538 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003539 extern const char *monthname[12];
3540 char pn[INET6_ADDRSTRLEN];
3541
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003542 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003543
Willy Tarreau295a8372011-03-10 11:25:07 +01003544 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003545 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003546 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3547 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003548 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003549 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003550 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003551 return 1;
3552 }
3553
Willy Tarreau295a8372011-03-10 11:25:07 +01003554 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003555 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 si->applet.ctx.sess.uid = sess->uniq_id;
3557 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003558 /* fall through */
3559
3560 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003561 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003562 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003563 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003564 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003565 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3566 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003567 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003568 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003569
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003570 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003571 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003572 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003573 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003574 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003575 break;
3576 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003577 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003578 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003579 default:
3580 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003581 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003582 break;
3583 }
3584
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003585 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003586 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003587 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003589 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003590 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003591 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3592 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3593 sess->listener ? sess->listener->luid : 0);
3594
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003595 conn_get_to_addr(sess->si[0].conn);
3596 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003597 case AF_INET:
3598 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003599 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003600 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003601 break;
3602 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003603 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003604 break;
3605 default:
3606 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003607 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003608 break;
3609 }
3610
Willy Tarreau50943332011-09-02 17:33:05 +02003611 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003612 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003613 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003614 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003615 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3616 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003617 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003618
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003619 conn_get_from_addr(sess->si[1].conn);
3620 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003621 case AF_INET:
3622 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003623 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003624 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003625 break;
3626 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003627 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003628 break;
3629 default:
3630 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003631 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003632 break;
3633 }
3634
3635 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003636 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003637 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003638 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3639 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003640 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003641 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003642
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003643 conn_get_to_addr(sess->si[1].conn);
3644 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003645 case AF_INET:
3646 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003647 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003648 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003649 break;
3650 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003651 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003652 break;
3653 default:
3654 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003655 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003656 break;
3657 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003658
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003659 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003660 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003661 sess->task,
3662 sess->task->state,
3663 sess->task->nice, sess->task->calls,
3664 sess->task->expire ?
3665 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3666 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3667 TICKS_TO_MS(1000)) : "<NEVER>",
3668 task_in_rq(sess->task) ? ", running" : "");
3669
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003670 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003671 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003672 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3673
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003674 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003675 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3676 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3677 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3678
3679 chunk_appendf(&trash,
3680 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003681 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003682 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003683 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003684 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003685 sess->si[0].exp ?
3686 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3687 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3688 TICKS_TO_MS(1000)) : "<NEVER>",
3689 sess->si[0].err_type);
3690
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003691 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003692 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003693 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003694 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003695 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003696 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003697 sess->si[1].exp ?
3698 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3699 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3700 TICKS_TO_MS(1000)) : "<NEVER>",
3701 sess->si[1].err_type);
3702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003703 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003704 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003705 sess->si[0].conn,
3706 get_conn_ctrl_name(sess->si[0].conn),
3707 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003708 get_conn_data_name(sess->si[0].conn),
3709 obj_type_name(sess->si[0].conn->target),
3710 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003711
3712 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003713 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003714 sess->si[0].conn->flags,
3715 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003716 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3717 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3718 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 +01003719
3720 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003721 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003722 sess->si[1].conn,
3723 get_conn_ctrl_name(sess->si[1].conn),
3724 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003725 get_conn_data_name(sess->si[1].conn),
3726 obj_type_name(sess->si[1].conn->target),
3727 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003728
3729 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003730 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003731 sess->si[1].conn->flags,
3732 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003733 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3734 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3735 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 +01003736
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003737 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003738 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003739 " an_exp=%s",
3740 sess->req,
3741 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003742 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003743 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003744 sess->req->analyse_exp ?
3745 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3746 TICKS_TO_MS(1000)) : "<NEVER>");
3747
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003748 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003749 " rex=%s",
3750 sess->req->rex ?
3751 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3752 TICKS_TO_MS(1000)) : "<NEVER>");
3753
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003754 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003755 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003756 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003757 sess->req->wex ?
3758 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3759 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003760 sess->req->buf,
3761 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003762 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003763 sess->txn.req.next, sess->req->buf->i,
3764 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003765
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003766 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003767 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003768 " an_exp=%s",
3769 sess->rep,
3770 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003771 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003772 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003773 sess->rep->analyse_exp ?
3774 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3775 TICKS_TO_MS(1000)) : "<NEVER>");
3776
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003777 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003778 " rex=%s",
3779 sess->rep->rex ?
3780 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3781 TICKS_TO_MS(1000)) : "<NEVER>");
3782
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003783 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003784 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003785 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003786 sess->rep->wex ?
3787 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3788 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003789 sess->rep->buf,
3790 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003791 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003792 sess->txn.rsp.next, sess->rep->buf->i,
3793 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003794
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003795 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003796 return 0;
3797
3798 /* use other states to dump the contents */
3799 }
3800 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003801 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003802 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003803 return 1;
3804}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003805
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003806/* This function dumps all sessions' states onto the stream interface's
3807 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003808 * properly set. It returns 0 if the output buffer is full and it needs
3809 * to be called again, otherwise non-zero. It is designed to be called
3810 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003811 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003812static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003813{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003814 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003815 /* If we're forced to shut down, we might have to remove our
3816 * reference to the last session being dumped.
3817 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003818 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003819 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3820 LIST_DEL(&si->applet.ctx.sess.bref.users);
3821 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003822 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003823 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003824 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003825 }
3826
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003827 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003828
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003829 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003830 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003831 /* the function had not been called yet, let's prepare the
3832 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003833 * pointer to the first in the global list. When a target
3834 * session is being destroyed, it is responsible for updating
3835 * this pointer. We know we have reached the end when this
3836 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003837 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003838 LIST_INIT(&si->applet.ctx.sess.bref.users);
3839 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003840 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003841 /* fall through */
3842
Willy Tarreau295a8372011-03-10 11:25:07 +01003843 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003844 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003845 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3846 LIST_DEL(&si->applet.ctx.sess.bref.users);
3847 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003848 }
3849
3850 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003851 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003852 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003853 struct session *curr_sess;
3854
Willy Tarreau295a8372011-03-10 11:25:07 +01003855 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003856
Willy Tarreau295a8372011-03-10 11:25:07 +01003857 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003858 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003859 goto next_sess;
3860
Willy Tarreau295a8372011-03-10 11:25:07 +01003861 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003862 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003863 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003864 return 0;
3865
3866 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003867 LIST_DEL(&si->applet.ctx.sess.bref.users);
3868 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003869 if (si->applet.ctx.sess.target != (void *)-1) {
3870 si->applet.ctx.sess.target = NULL;
3871 break;
3872 }
3873 else
3874 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003875 }
3876
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003877 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003878 "%p: proto=%s",
3879 curr_sess,
3880 curr_sess->listener->proto->name);
3881
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003882
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003883 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003884 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003885 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003886 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003887 " src=%s:%d fe=%s be=%s srv=%s",
3888 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003889 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003890 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003891 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003892 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003893 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003894 break;
3895 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003896 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003897 " src=unix:%d fe=%s be=%s srv=%s",
3898 curr_sess->listener->luid,
3899 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003900 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003901 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003902 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003903 break;
3904 }
3905
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003906 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003907 " ts=%02x age=%s calls=%d",
3908 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003909 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3910 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003911
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003912 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003913 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003914 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003915 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003916 curr_sess->req->analysers,
3917 curr_sess->req->rex ?
3918 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3919 TICKS_TO_MS(1000)) : "");
3920
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003921 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003922 ",wx=%s",
3923 curr_sess->req->wex ?
3924 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3925 TICKS_TO_MS(1000)) : "");
3926
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003927 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003928 ",ax=%s]",
3929 curr_sess->req->analyse_exp ?
3930 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3931 TICKS_TO_MS(1000)) : "");
3932
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003933 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003934 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003935 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003936 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003937 curr_sess->rep->analysers,
3938 curr_sess->rep->rex ?
3939 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3940 TICKS_TO_MS(1000)) : "");
3941
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003942 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003943 ",wx=%s",
3944 curr_sess->rep->wex ?
3945 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3946 TICKS_TO_MS(1000)) : "");
3947
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003948 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003949 ",ax=%s]",
3950 curr_sess->rep->analyse_exp ?
3951 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3952 TICKS_TO_MS(1000)) : "");
3953
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003954 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003955 " s0=[%d,%1xh,fd=%d,ex=%s]",
3956 curr_sess->si[0].state,
3957 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003958 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003959 curr_sess->si[0].exp ?
3960 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3961 TICKS_TO_MS(1000)) : "");
3962
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003963 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003964 " s1=[%d,%1xh,fd=%d,ex=%s]",
3965 curr_sess->si[1].state,
3966 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003967 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003968 curr_sess->si[1].exp ?
3969 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3970 TICKS_TO_MS(1000)) : "");
3971
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003972 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003973 " exp=%s",
3974 curr_sess->task->expire ?
3975 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3976 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003977 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003978 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003979
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003980 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003981
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003982 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003983 /* let's try again later from this session. We add ourselves into
3984 * this session's users so that it can remove us upon termination.
3985 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003986 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003987 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003988 }
3989
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003990 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003991 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003992 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003993
Willy Tarreau76153662012-11-26 01:16:39 +01003994 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003995 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003996 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003997 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003998 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003999 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004000
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004001 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004002 return 0;
4003
Willy Tarreau295a8372011-03-10 11:25:07 +01004004 si->applet.ctx.sess.target = NULL;
4005 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004006 return 1;
4007 }
4008
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004009 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004010 /* fall through */
4011
4012 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004013 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004014 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004015 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004016}
4017
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004018/* This is called when the stream interface is closed. For instance, upon an
4019 * external abort, we won't call the i/o handler anymore so we may need to
4020 * remove back references to the session currently being dumped.
4021 */
Simon Horman74d88312013-02-12 10:45:50 +09004022static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004023{
4024 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
4025 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
4026 LIST_DEL(&si->applet.ctx.sess.bref.users);
4027 }
4028}
4029
Willy Tarreau20e99322013-04-13 09:22:25 +02004030/* This function is used to either dump tables states (when action is set
4031 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
4032 * The xprt_ctx must have been zeroed first, and the flags properly set.
4033 * It returns 0 if the output buffer is full and it needs to be called
4034 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004035 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004036static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004037{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004038 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004039 struct ebmb_node *eb;
4040 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004041 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004042 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004043
4044 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004045 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01004046 * - STAT_ST_INIT : the first call
4047 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004048 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004049 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004050 * and the entry pointer points to the next entry to be dumped,
4051 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004052 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004053 * data though.
4054 */
4055
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004056 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004057 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004058 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004059 si->applet.ctx.table.entry->ref_cnt--;
4060 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004061 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004062 return 1;
4063 }
4064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004065 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004066
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004067 while (si->conn->xprt_st != STAT_ST_FIN) {
4068 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004069 case STAT_ST_INIT:
4070 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4071 if (!si->applet.ctx.table.proxy)
4072 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004073
Willy Tarreau295a8372011-03-10 11:25:07 +01004074 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004075 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004076 break;
4077
Willy Tarreau295a8372011-03-10 11:25:07 +01004078 case STAT_ST_INFO:
4079 if (!si->applet.ctx.table.proxy ||
4080 (si->applet.ctx.table.target &&
4081 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004082 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004083 break;
4084 }
4085
Willy Tarreau295a8372011-03-10 11:25:07 +01004086 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004087 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004088 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004089 return 0;
4090
Willy Tarreau295a8372011-03-10 11:25:07 +01004091 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004092 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004093 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004094 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004095 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004096 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4097 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004098 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004099 break;
4100 }
4101 }
4102 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004103 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004104 break;
4105
Willy Tarreau295a8372011-03-10 11:25:07 +01004106 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004107 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004108
Willy Tarreau295a8372011-03-10 11:25:07 +01004109 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004110 /* we're filtering on some data contents */
4111 void *ptr;
4112 long long data;
4113
Willy Tarreau295a8372011-03-10 11:25:07 +01004114 dt = si->applet.ctx.table.data_type;
4115 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4116 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004117 dt);
4118
4119 data = 0;
4120 switch (stktable_data_types[dt].std_type) {
4121 case STD_T_SINT:
4122 data = stktable_data_cast(ptr, std_t_sint);
4123 break;
4124 case STD_T_UINT:
4125 data = stktable_data_cast(ptr, std_t_uint);
4126 break;
4127 case STD_T_ULL:
4128 data = stktable_data_cast(ptr, std_t_ull);
4129 break;
4130 case STD_T_FRQP:
4131 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004132 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004133 break;
4134 }
4135
4136 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004137 if ((data < si->applet.ctx.table.value &&
4138 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4139 si->applet.ctx.table.data_op == STD_OP_GT ||
4140 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4141 (data == si->applet.ctx.table.value &&
4142 (si->applet.ctx.table.data_op == STD_OP_NE ||
4143 si->applet.ctx.table.data_op == STD_OP_GT ||
4144 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4145 (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_LT ||
4148 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004149 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004150 }
4151
Simon Hormanc88b8872011-06-15 15:18:49 +09004152 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004153 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004154 si->applet.ctx.table.entry))
4155 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004156
Willy Tarreau295a8372011-03-10 11:25:07 +01004157 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004158
Willy Tarreau295a8372011-03-10 11:25:07 +01004159 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004160 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004161 struct stksess *old = si->applet.ctx.table.entry;
4162 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004163 if (show)
4164 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
4165 else
4166 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004167 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004168 break;
4169 }
4170
Simon Hormanc88b8872011-06-15 15:18:49 +09004171
4172 if (show)
4173 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4174 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4175 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4176
Willy Tarreau295a8372011-03-10 11:25:07 +01004177 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004178 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004179 break;
4180
Willy Tarreau295a8372011-03-10 11:25:07 +01004181 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004182 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004183 break;
4184 }
4185 }
4186 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004187}
4188
Willy Tarreaud426a182010-03-05 14:58:26 +01004189/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004190 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4191 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4192 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4193 * lines are respected within the limit of 70 output chars. Lines that are
4194 * continuation of a previous truncated line begin with "+" instead of " "
4195 * after the offset. The new pointer is returned.
4196 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004197static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4198 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004199{
4200 int end;
4201 unsigned char c;
4202
4203 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004204 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004205 return ptr;
4206
Willy Tarreau77804732012-10-29 16:14:26 +01004207 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004208
Willy Tarreaud426a182010-03-05 14:58:26 +01004209 while (ptr < len && ptr < bsize) {
4210 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004211 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004212 if (out->len > end - 2)
4213 break;
4214 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004215 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004216 if (out->len > end - 3)
4217 break;
4218 out->str[out->len++] = '\\';
4219 switch (c) {
4220 case '\t': c = 't'; break;
4221 case '\n': c = 'n'; break;
4222 case '\r': c = 'r'; break;
4223 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004224 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004225 }
4226 out->str[out->len++] = c;
4227 } else {
4228 if (out->len > end - 5)
4229 break;
4230 out->str[out->len++] = '\\';
4231 out->str[out->len++] = 'x';
4232 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4233 out->str[out->len++] = hextab[c & 0xF];
4234 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004235 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004236 /* we had a line break, let's return now */
4237 out->str[out->len++] = '\n';
4238 *line = ptr;
4239 return ptr;
4240 }
4241 }
4242 /* we have an incomplete line, we return it as-is */
4243 out->str[out->len++] = '\n';
4244 return ptr;
4245}
4246
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004247/* This function dumps all captured errors onto the stream interface's
4248 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004249 * properly set. It returns 0 if the output buffer is full and it needs
4250 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004251 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004252static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004253{
4254 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004255
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004256 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004257 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004258
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004259 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004260
Willy Tarreau295a8372011-03-10 11:25:07 +01004261 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004262 /* the function had not been called yet, let's prepare the
4263 * buffer for a response.
4264 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004265 struct tm tm;
4266
4267 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004268 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004269 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4270 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4271 error_snapshot_id);
4272
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004273 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004274 /* Socket buffer full. Let's try again later from the same point */
4275 return 0;
4276 }
4277
Willy Tarreau295a8372011-03-10 11:25:07 +01004278 si->applet.ctx.errors.px = proxy;
4279 si->applet.ctx.errors.buf = 0;
4280 si->applet.ctx.errors.bol = 0;
4281 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004282 }
4283
4284 /* we have two inner loops here, one for the proxy, the other one for
4285 * the buffer.
4286 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004287 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004288 struct error_snapshot *es;
4289
Willy Tarreau295a8372011-03-10 11:25:07 +01004290 if (si->applet.ctx.errors.buf == 0)
4291 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004292 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004293 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004294
4295 if (!es->when.tv_sec)
4296 goto next;
4297
Willy Tarreau295a8372011-03-10 11:25:07 +01004298 if (si->applet.ctx.errors.iid >= 0 &&
4299 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4300 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004301 goto next;
4302
Willy Tarreau295a8372011-03-10 11:25:07 +01004303 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004304 /* just print headers now */
4305
4306 char pn[INET6_ADDRSTRLEN];
4307 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004308 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004309
4310 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004311 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004312 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004313 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004314
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004315 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4316 case AF_INET:
4317 case AF_INET6:
4318 port = get_host_port(&es->src);
4319 break;
4320 default:
4321 port = 0;
4322 }
4323
Willy Tarreau295a8372011-03-10 11:25:07 +01004324 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004325 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004326 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004327 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004328 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004329 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004330 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4331 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004332 break;
4333 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004334 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004335 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004336 " frontend %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->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004339 break;
4340 }
4341
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004342 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004343 ", server %s (#%d), event #%u\n"
4344 " src %s:%d, session #%d, session flags 0x%08x\n"
4345 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4346 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4347 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4348 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4349 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4350 es->ev_id,
4351 pn, port, es->sid, es->s_flags,
4352 es->state, es->m_flags, es->t_flags,
4353 es->m_clen, es->m_blen,
4354 es->b_flags, es->b_out, es->b_tot,
4355 es->len, es->b_wrap, es->pos);
4356
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004357 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004358 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004359 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004360 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004361 si->applet.ctx.errors.ptr = 0;
4362 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004363 }
4364
Willy Tarreau295a8372011-03-10 11:25:07 +01004365 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004366 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004367 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004368 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004369 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004370 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004371 goto next;
4372 }
4373
4374 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004375 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004376 int newptr;
4377 int newline;
4378
Willy Tarreau295a8372011-03-10 11:25:07 +01004379 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004380 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 +01004381 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004382 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004383
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004384 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004385 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004386 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004387 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004388 si->applet.ctx.errors.ptr = newptr;
4389 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004390 };
4391 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004392 si->applet.ctx.errors.bol = 0;
4393 si->applet.ctx.errors.ptr = -1;
4394 si->applet.ctx.errors.buf++;
4395 if (si->applet.ctx.errors.buf > 1) {
4396 si->applet.ctx.errors.buf = 0;
4397 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004398 }
4399 }
4400
4401 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004402 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004403}
4404
Willy Tarreaud5781202012-09-22 19:32:35 +02004405/* parse the "level" argument on the bind lines */
4406static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4407{
4408 if (!*args[cur_arg + 1]) {
4409 memprintf(err, "'%s' : missing level", args[cur_arg]);
4410 return ERR_ALERT | ERR_FATAL;
4411 }
4412
4413 if (!strcmp(args[cur_arg+1], "user"))
4414 conf->level = ACCESS_LVL_USER;
4415 else if (!strcmp(args[cur_arg+1], "operator"))
4416 conf->level = ACCESS_LVL_OPER;
4417 else if (!strcmp(args[cur_arg+1], "admin"))
4418 conf->level = ACCESS_LVL_ADMIN;
4419 else {
4420 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4421 args[cur_arg], args[cur_arg+1]);
4422 return ERR_ALERT | ERR_FATAL;
4423 }
4424
4425 return 0;
4426}
4427
Willy Tarreaub24281b2011-02-13 13:16:36 +01004428struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004429 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004430 .name = "<STATS>", /* used for logging */
4431 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004432 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004433};
4434
Simon Horman9bd2c732011-06-15 15:18:44 +09004435static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004436 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004437 .name = "<CLI>", /* used for logging */
4438 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004439 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004440};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004441
Willy Tarreau10522fd2008-07-09 20:12:41 +02004442static struct cfg_kw_list cfg_kws = {{ },{
4443 { CFG_GLOBAL, "stats", stats_parse_global },
4444 { 0, NULL, NULL },
4445}};
4446
Willy Tarreaud5781202012-09-22 19:32:35 +02004447static struct bind_kw_list bind_kws = { "STAT", { }, {
4448 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4449 { NULL, NULL, 0 },
4450}};
4451
Willy Tarreau10522fd2008-07-09 20:12:41 +02004452__attribute__((constructor))
4453static void __dumpstats_module_init(void)
4454{
4455 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004456 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004457}
4458
Willy Tarreau91861262007-10-17 17:06:05 +02004459/*
4460 * Local variables:
4461 * c-indent-level: 8
4462 * c-basic-offset: 8
4463 * End:
4464 */