blob: 96c5cead4aea2ee125556762c836365af054554a [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 Tarreaub363a1f2013-10-01 10:45:07 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200155 * 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 Tarreau7b4b4992013-12-01 09:15:12 +0100159 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100160 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200161
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200162 tv_zero(&s->logs.tv_request);
163 s->logs.t_queue = 0;
164 s->logs.t_connect = 0;
165 s->logs.t_data = 0;
166 s->logs.t_close = 0;
167 s->logs.bytes_in = s->logs.bytes_out = 0;
168 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
169 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200170
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200171 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200172
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200173 if (s->listener->timeout) {
174 s->req->rto = *s->listener->timeout;
175 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200176 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200177 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200178}
179
Willy Tarreau07e9e642010-08-17 21:48:17 +0200180/* allocate a new stats frontend named <name>, and return it
181 * (or NULL in case of lack of memory).
182 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200183static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200184{
185 struct proxy *fe;
186
187 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
188 if (!fe)
189 return NULL;
190
Willy Tarreau237250c2011-07-29 01:49:03 +0200191 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200192 fe->next = proxy;
193 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200194 fe->last_change = now.tv_sec;
195 fe->id = strdup("GLOBAL");
196 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200197 fe->maxconn = 10; /* default to 10 concurrent connections */
198 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200199 fe->conf.file = strdup(file);
200 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200201 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200202
203 /* the stats frontend is the only one able to assign ID #0 */
204 fe->conf.id.key = fe->uuid = 0;
205 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200206 return fe;
207}
208
Willy Tarreaufbee7132007-10-18 13:53:22 +0200209/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200210 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
211 * error message into the <err> buffer which will be preallocated. The trailing
212 * '\n' must not be written. The function must be called with <args> pointing to
213 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200214 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200215static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200216 struct proxy *defpx, const char *file, int line,
217 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200218{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200219 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200220 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200221
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200222 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200223 int cur_arg;
224
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200226 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 +0200227 return -1;
228 }
229
Willy Tarreau89a63132009-08-16 17:41:45 +0200230 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200231 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200232 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200233 return -1;
234 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200235 }
236
Willy Tarreau4348fad2012-09-20 16:48:07 +0200237 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200238 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200239
Willy Tarreauc53d4222012-09-20 20:19:28 +0200240 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
241 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
242 file, line, args[0], args[1], err && *err ? *err : "error");
243 return -1;
244 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200245
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200246 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200247 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200248 static int bind_dumped;
249 struct bind_kw *kw;
250
251 kw = bind_find_kw(args[cur_arg]);
252 if (kw) {
253 if (!kw->parse) {
254 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
255 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200256 return -1;
257 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200258
259 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
260 if (err && *err)
261 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
262 else
263 memprintf(err, "'%s %s' : error encountered while processing '%s'",
264 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200265 return -1;
266 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200267
268 cur_arg += 1 + kw->skip;
269 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200270 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200271
272 if (!bind_dumped) {
273 bind_dump_kws(err);
274 indent_msg(err, 4);
275 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200276 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200277
278 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
279 args[0], args[1], args[cur_arg],
280 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
281 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100283
Willy Tarreauc53d4222012-09-20 20:19:28 +0200284 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
285 l->maxconn = global.stats_fe->maxconn;
286 l->backlog = global.stats_fe->backlog;
287 l->timeout = &global.stats_fe->timeout.client;
288 l->accept = session_accept;
289 l->handler = process_session;
290 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
291 l->nice = -64; /* we want to boost priority for local stats */
292 global.maxsock += l->maxconn;
293 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200294 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200295 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100296 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100298
299 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100301 return -1;
302 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200303
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200305 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306 return -1;
307 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200308 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200309 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200310 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200311 return -1;
312 }
313 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200314 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200315 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200316 else if (!strcmp(args[1], "maxconn")) {
317 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318
319 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321 return -1;
322 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200323
324 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200325 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200326 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200327 return -1;
328 }
329 }
330 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200332 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
333 int cur_arg = 2;
334 unsigned int set = 0;
335
Willy Tarreau91319572013-04-20 09:48:50 +0200336 if (!global.stats_fe) {
337 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
338 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
339 return -1;
340 }
341 }
342
Willy Tarreau35b7b162012-10-22 23:17:18 +0200343 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100344 unsigned int low, high;
345
Willy Tarreau35b7b162012-10-22 23:17:18 +0200346 if (strcmp(args[cur_arg], "all") == 0) {
347 set = 0;
348 break;
349 }
350 else if (strcmp(args[cur_arg], "odd") == 0) {
351 set |= 0x55555555;
352 }
353 else if (strcmp(args[cur_arg], "even") == 0) {
354 set |= 0xAAAAAAAA;
355 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100356 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100357 char *dash = strchr(args[cur_arg], '-');
358
359 low = high = str2uic(args[cur_arg]);
360 if (dash)
361 high = str2uic(dash + 1);
362
363 if (high < low) {
364 unsigned int swap = low;
365 low = high;
366 high = swap;
367 }
368
369 if (low < 1 || high > 32) {
370 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
371 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200372 return -1;
373 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100374
375 while (low <= high)
376 set |= 1 << (low++ - 1);
377 }
378 else {
379 memprintf(err,
380 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
381 args[0], args[1]);
382 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200383 }
384 cur_arg++;
385 }
386 global.stats_fe->bind_proc = set;
387 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200388 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200389 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200390 return -1;
391 }
392 return 0;
393}
394
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100395/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
396 * for clearing it if needed.
397 */
398static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100399{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100400 chunk_appendf(&trash,
401 "# pxname,svname,"
402 "qcur,qmax,"
403 "scur,smax,slim,stot,"
404 "bin,bout,"
405 "dreq,dresp,"
406 "ereq,econ,eresp,"
407 "wretr,wredis,"
408 "status,weight,act,bck,"
409 "chkfail,chkdown,lastchg,downtime,qlimit,"
410 "pid,iid,sid,throttle,lbtot,tracked,type,"
411 "rate,rate_lim,rate_max,"
412 "check_status,check_code,check_duration,"
413 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
414 "req_rate,req_rate_max,req_tot,"
415 "cli_abrt,srv_abrt,"
416 "comp_in,comp_out,comp_byp,comp_rsp,"
417 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100418}
419
Simon Hormand9366582011-06-15 15:18:45 +0900420/* print a string of text buffer to <out>. The format is :
421 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
422 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
423 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
424 */
425static int dump_text(struct chunk *out, const char *buf, int bsize)
426{
427 unsigned char c;
428 int ptr = 0;
429
430 while (buf[ptr] && ptr < bsize) {
431 c = buf[ptr];
432 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
433 if (out->len > out->size - 1)
434 break;
435 out->str[out->len++] = c;
436 }
437 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
438 if (out->len > out->size - 2)
439 break;
440 out->str[out->len++] = '\\';
441 switch (c) {
442 case ' ': c = ' '; break;
443 case '\t': c = 't'; break;
444 case '\n': c = 'n'; break;
445 case '\r': c = 'r'; break;
446 case '\e': c = 'e'; break;
447 case '\\': c = '\\'; break;
448 }
449 out->str[out->len++] = c;
450 }
451 else {
452 if (out->len > out->size - 4)
453 break;
454 out->str[out->len++] = '\\';
455 out->str[out->len++] = 'x';
456 out->str[out->len++] = hextab[(c >> 4) & 0xF];
457 out->str[out->len++] = hextab[c & 0xF];
458 }
459 ptr++;
460 }
461
462 return ptr;
463}
464
465/* print a buffer in hexa.
466 * Print stopped if <bsize> is reached, or if no more place in the chunk.
467 */
468static int dump_binary(struct chunk *out, const char *buf, int bsize)
469{
470 unsigned char c;
471 int ptr = 0;
472
473 while (ptr < bsize) {
474 c = buf[ptr];
475
476 if (out->len > out->size - 2)
477 break;
478 out->str[out->len++] = hextab[(c >> 4) & 0xF];
479 out->str[out->len++] = hextab[c & 0xF];
480
481 ptr++;
482 }
483 return ptr;
484}
485
486/* Dump the status of a table to a stream interface's
487 * read buffer. It returns 0 if the output buffer is full
488 * and needs to be called again, otherwise non-zero.
489 */
490static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
491 struct proxy *proxy, struct proxy *target)
492{
Willy Tarreau306f8302013-07-08 15:53:06 +0200493 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900494
Willy Tarreau77804732012-10-29 16:14:26 +0100495 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900496 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
497
498 /* any other information should be dumped here */
499
Willy Tarreau290e63a2012-09-20 18:07:14 +0200500 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100501 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900502
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200503 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900504 return 0;
505
506 return 1;
507}
508
509/* Dump the a table entry to a stream interface's
510 * read buffer. It returns 0 if the output buffer is full
511 * and needs to be called again, otherwise non-zero.
512 */
513static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
514 struct proxy *proxy, struct stksess *entry)
515{
516 int dt;
517
Willy Tarreau77804732012-10-29 16:14:26 +0100518 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900519
520 if (proxy->table.type == STKTABLE_TYPE_IP) {
521 char addr[INET_ADDRSTRLEN];
522 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100523 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900524 }
525 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
526 char addr[INET6_ADDRSTRLEN];
527 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100528 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900529 }
530 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900532 }
533 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900535 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
536 }
537 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900539 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
540 }
541
Willy Tarreau77804732012-10-29 16:14:26 +0100542 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900543
544 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
545 void *ptr;
546
547 if (proxy->table.data_ofs[dt] == 0)
548 continue;
549 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100550 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900551 else
Willy Tarreau77804732012-10-29 16:14:26 +0100552 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900553
554 ptr = stktable_data_ptr(&proxy->table, entry, dt);
555 switch (stktable_data_types[dt].std_type) {
556 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100557 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900558 break;
559 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900561 break;
562 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900564 break;
565 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900567 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
568 proxy->table.data_arg[dt].u));
569 break;
570 }
571 }
Willy Tarreau77804732012-10-29 16:14:26 +0100572 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900573
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200574 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900575 return 0;
576
577 return 1;
578}
579
Willy Tarreaudec98142012-06-06 23:37:08 +0200580static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900581{
Willy Tarreau306f8302013-07-08 15:53:06 +0200582 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100583 struct appctx *appctx = __objt_appctx(si->end);
584 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900585 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900586 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900587 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200588 long long value;
589 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200590 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200591 void *ptr;
592 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900593
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100594 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900595
596 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100597 appctx->ctx.cli.msg = "Key value expected\n";
598 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900599 return;
600 }
601
Simon Hormanc5b89f62011-06-15 15:18:50 +0900602 switch (px->table.type) {
603 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900604 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100605 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900606 break;
607 case STKTABLE_TYPE_IPV6:
608 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100609 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900610 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900611 case STKTABLE_TYPE_INTEGER:
612 {
613 char *endptr;
614 unsigned long val;
615 errno = 0;
616 val = strtoul(args[4], &endptr, 10);
617 if ((errno == ERANGE && val == ULONG_MAX) ||
618 (errno != 0 && val == 0) || endptr == args[4] ||
619 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100620 appctx->ctx.cli.msg = "Invalid key\n";
621 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900622 return;
623 }
624 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100625 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900626 break;
627 }
628 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900629 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100630 static_table_key->key = args[4];
631 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900632 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900633 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200634 switch (action) {
635 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100636 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200637 break;
638 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100639 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200640 break;
641 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100642 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200643 break;
644 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100645 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900646 return;
647 }
648
649 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200650 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100651 appctx->ctx.cli.msg = stats_permission_denied_msg;
652 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900653 return;
654 }
655
Willy Tarreau07115412012-10-29 21:56:59 +0100656 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900657
Willy Tarreaudec98142012-06-06 23:37:08 +0200658 switch (action) {
659 case STAT_CLI_O_TAB:
660 if (!ts)
661 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100662 chunk_reset(&trash);
663 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900664 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900666 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200667
668 case STAT_CLI_O_CLR:
669 if (!ts)
670 return;
671 if (ts->ref_cnt) {
672 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100673 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
674 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200675 return;
676 }
677 stksess_kill(&px->table, ts);
678 break;
Simon Horman17bce342011-06-15 15:18:47 +0900679
Willy Tarreau654694e2012-06-07 01:03:16 +0200680 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200681 if (ts)
682 stktable_touch(&px->table, ts, 1);
683 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100684 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200685 if (!ts) {
686 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100687 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
688 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200689 return;
690 }
691 stktable_store(&px->table, ts, 1);
692 }
693
Willy Tarreau47060b62013-08-01 21:11:42 +0200694 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
695 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
697 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200698 return;
699 }
700
701 data_type = stktable_get_data_type(args[cur_arg] + 5);
702 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100703 appctx->ctx.cli.msg = "Unknown data type\n";
704 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200705 return;
706 }
707
708 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100709 appctx->ctx.cli.msg = "Data type not stored in this table\n";
710 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200711 return;
712 }
713
714 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
716 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200717 return;
718 }
719
720 ptr = stktable_data_ptr(&px->table, ts, data_type);
721
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 set both the current and previous values. That way
734 * the reported frequency is stable during all the period
735 * then slowly fades out. This allows external tools to
736 * push measures without having to update them too often.
737 */
738 frqp = &stktable_data_cast(ptr, std_t_frqp);
739 frqp->curr_tick = now_ms;
740 frqp->prev_ctr = 0;
741 frqp->curr_ctr = value;
742 break;
743 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200744 }
745 break;
746
Willy Tarreaudec98142012-06-06 23:37:08 +0200747 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100748 appctx->ctx.cli.msg = "Unknown action\n";
749 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200750 break;
Simon Horman121f3052011-06-15 15:18:46 +0900751 }
Simon Horman121f3052011-06-15 15:18:46 +0900752}
753
Willy Tarreau654694e2012-06-07 01:03:16 +0200754static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900755{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100756 struct appctx *appctx = __objt_appctx(si->end);
757
Willy Tarreau04b3a192013-04-13 09:41:37 +0200758 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100759 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
760 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200761 return;
762 }
763
Simon Hormand5b9fd92011-06-15 15:18:48 +0900764 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100765 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
766 if (appctx->ctx.table.data_type < 0) {
767 appctx->ctx.cli.msg = "Unknown data type\n";
768 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900769 return;
770 }
771
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100772 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
773 appctx->ctx.cli.msg = "Data type not stored in this table\n";
774 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900775 return;
776 }
777
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100778 appctx->ctx.table.data_op = get_std_op(args[4]);
779 if (appctx->ctx.table.data_op < 0) {
780 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
781 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900782 return;
783 }
784
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100785 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
786 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
787 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900788 return;
789 }
790}
791
Willy Tarreaudec98142012-06-06 23:37:08 +0200792static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900793{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100794 struct appctx *appctx = __objt_appctx(si->end);
795
796 appctx->ctx.table.data_type = -1;
797 appctx->st2 = STAT_ST_INIT;
798 appctx->ctx.table.target = NULL;
799 appctx->ctx.table.proxy = NULL;
800 appctx->ctx.table.entry = NULL;
801 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900802
803 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100804 appctx->ctx.table.target = find_stktable(args[2]);
805 if (!appctx->ctx.table.target) {
806 appctx->ctx.cli.msg = "No such table\n";
807 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900808 return;
809 }
810 }
811 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200812 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900813 goto err_args;
814 return;
815 }
816
817 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200818 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900819 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200820 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900821 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900822 goto err_args;
823
824 return;
825
826err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200827 switch (action) {
828 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100829 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200830 break;
831 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100832 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200833 break;
834 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200836 break;
837 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900839}
840
Willy Tarreau532a4502011-09-07 22:37:44 +0200841/* Expects to find a frontend named <arg> and returns it, otherwise displays various
842 * adequate error messages and returns NULL. This function also expects the session
843 * level to be admin.
844 */
845static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
846{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100847 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200848 struct proxy *px;
849
Willy Tarreau290e63a2012-09-20 18:07:14 +0200850 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 appctx->ctx.cli.msg = stats_permission_denied_msg;
852 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200853 return NULL;
854 }
855
856 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 appctx->ctx.cli.msg = "A frontend name is expected.\n";
858 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200859 return NULL;
860 }
861
862 px = findproxy(arg, PR_CAP_FE);
863 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 appctx->ctx.cli.msg = "No such frontend.\n";
865 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200866 return NULL;
867 }
868 return px;
869}
870
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200871/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
872 * and returns the pointer to the server. Otherwise, display adequate error messages
873 * and returns NULL. This function also expects the session level to be admin. Note:
874 * the <arg> is modified to remove the '/'.
875 */
876static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
877{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100878 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200879 struct proxy *px;
880 struct server *sv;
881 char *line;
882
Willy Tarreau290e63a2012-09-20 18:07:14 +0200883 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100884 appctx->ctx.cli.msg = stats_permission_denied_msg;
885 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200886 return NULL;
887 }
888
889 /* split "backend/server" and make <line> point to server */
890 for (line = arg; *line; line++)
891 if (*line == '/') {
892 *line++ = '\0';
893 break;
894 }
895
896 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
898 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200899 return NULL;
900 }
901
902 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200905 return NULL;
906 }
907
908 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100909 appctx->ctx.cli.msg = "Proxy is disabled.\n";
910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200911 return NULL;
912 }
913
914 return sv;
915}
916
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200917/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200918 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100919 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200920 * designating the function which will have to process the request, which can
921 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900923static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200924{
Willy Tarreau306f8302013-07-08 15:53:06 +0200925 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200927 char *args[MAX_STATS_ARGS + 1];
928 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100929 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200930
931 while (isspace((unsigned char)*line))
932 line++;
933
934 arg = 0;
935 args[arg] = line;
936
937 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100938 if (*line == '\\') {
939 line++;
940 if (*line == '\0')
941 break;
942 }
943 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200944 *line++ = '\0';
945
946 while (isspace((unsigned char)*line))
947 line++;
948
949 args[++arg] = line;
950 continue;
951 }
952
953 line++;
954 }
955
956 while (++arg <= MAX_STATS_ARGS)
957 args[arg] = line;
958
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100959 /* remove \ */
960 arg = 0;
961 while (*args[arg] != '\0') {
962 j = 0;
963 for (i=0; args[arg][i] != '\0'; i++) {
964 if (args[arg][i] == '\\')
965 continue;
966 args[arg][j] = args[arg][i];
967 j++;
968 }
969 args[arg][j] = '\0';
970 arg++;
971 }
972
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100973 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200974 if (strcmp(args[0], "show") == 0) {
975 if (strcmp(args[1], "stat") == 0) {
976 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100977 appctx->ctx.stats.flags |= STAT_BOUND;
978 appctx->ctx.stats.iid = atoi(args[2]);
979 appctx->ctx.stats.type = atoi(args[3]);
980 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200981 }
982
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100983 appctx->st2 = STAT_ST_INIT;
984 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200985 }
986 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100987 appctx->st2 = STAT_ST_INIT;
988 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200989 }
990 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100991 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200992 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100993 appctx->ctx.cli.msg = stats_permission_denied_msg;
994 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200995 return 1;
996 }
Willy Tarreau76153662012-11-26 01:16:39 +0100997 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100998 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +0100999 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001000 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001001 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001002 appctx->ctx.sess.target = NULL;
1003 appctx->ctx.sess.section = 0; /* start with session status */
1004 appctx->ctx.sess.pos = 0;
1005 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001006 }
1007 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001008 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001009 appctx->ctx.cli.msg = stats_permission_denied_msg;
1010 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001011 return 1;
1012 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001013 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001014 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001015 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001016 appctx->ctx.errors.iid = -1;
1017 appctx->ctx.errors.px = NULL;
1018 appctx->st2 = STAT_ST_INIT;
1019 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001020 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001021 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001022 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001023 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001024 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001025 return 0;
1026 }
1027 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001028 else if (strcmp(args[0], "clear") == 0) {
1029 if (strcmp(args[1], "counters") == 0) {
1030 struct proxy *px;
1031 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001032 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001033 int clrall = 0;
1034
1035 if (strcmp(args[2], "all") == 0)
1036 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001037
Willy Tarreau6162db22009-10-10 17:13:00 +02001038 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001039 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1040 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001041 appctx->ctx.cli.msg = stats_permission_denied_msg;
1042 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001043 return 1;
1044 }
1045
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001046 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001047 if (clrall) {
1048 memset(&px->be_counters, 0, sizeof(px->be_counters));
1049 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1050 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001051 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001052 px->be_counters.conn_max = 0;
1053 px->be_counters.p.http.rps_max = 0;
1054 px->be_counters.sps_max = 0;
1055 px->be_counters.cps_max = 0;
1056 px->be_counters.nbpend_max = 0;
1057
1058 px->fe_counters.conn_max = 0;
1059 px->fe_counters.p.http.rps_max = 0;
1060 px->fe_counters.sps_max = 0;
1061 px->fe_counters.cps_max = 0;
1062 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001063 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001064
1065 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001066 if (clrall)
1067 memset(&sv->counters, 0, sizeof(sv->counters));
1068 else {
1069 sv->counters.cur_sess_max = 0;
1070 sv->counters.nbpend_max = 0;
1071 sv->counters.sps_max = 0;
1072 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001073
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001074 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001075 if (li->counters) {
1076 if (clrall)
1077 memset(li->counters, 0, sizeof(*li->counters));
1078 else
1079 li->counters->conn_max = 0;
1080 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001081 }
1082
Willy Tarreau81c25d02011-09-07 15:17:21 +02001083 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001084 return 1;
1085 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001086 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001087 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001088 /* end of processing */
1089 return 1;
1090 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001091 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001092 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001093 return 0;
1094 }
1095 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001096 else if (strcmp(args[0], "get") == 0) {
1097 if (strcmp(args[1], "weight") == 0) {
1098 struct proxy *px;
1099 struct server *sv;
1100
1101 /* split "backend/server" and make <line> point to server */
1102 for (line = args[2]; *line; line++)
1103 if (*line == '/') {
1104 *line++ = '\0';
1105 break;
1106 }
1107
1108 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001109 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1110 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001111 return 1;
1112 }
1113
1114 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1116 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001117 return 1;
1118 }
1119
1120 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001121 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1122 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001123 return 1;
1124 }
1125 else { /* not "get weight" */
1126 return 0;
1127 }
1128 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001129 else if (strcmp(args[0], "set") == 0) {
1130 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001131 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001132 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001133
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001134 sv = expect_server_admin(s, si, args[2]);
1135 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001136 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001137
Simon Horman7d09b9a2013-02-12 10:45:51 +09001138 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001139 /*
1140 * The user-weight may now be zero and thus
1141 * the server considered to be draining.
1142 * Update the server's drain state as necessary.
1143 */
1144 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001145 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001146 appctx->ctx.cli.msg = warning;
1147 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001148 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001149 return 1;
1150 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001151 else if (strcmp(args[1], "timeout") == 0) {
1152 if (strcmp(args[2], "cli") == 0) {
1153 unsigned timeout;
1154 const char *res;
1155
1156 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001157 appctx->ctx.cli.msg = "Expects an integer value.\n";
1158 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001159 return 1;
1160 }
1161
1162 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1163 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001164 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1165 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001166 return 1;
1167 }
1168
1169 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1170 return 1;
1171 }
1172 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001173 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1174 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001175 return 1;
1176 }
1177 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001178 else if (strcmp(args[1], "maxconn") == 0) {
1179 if (strcmp(args[2], "frontend") == 0) {
1180 struct proxy *px;
1181 struct listener *l;
1182 int v;
1183
Willy Tarreau532a4502011-09-07 22:37:44 +02001184 px = expect_frontend_admin(s, si, args[3]);
1185 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001186 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001187
1188 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001189 appctx->ctx.cli.msg = "Integer value expected.\n";
1190 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001191 return 1;
1192 }
1193
1194 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001195 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001196 appctx->ctx.cli.msg = "Value out of range.\n";
1197 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001198 return 1;
1199 }
1200
1201 /* OK, the value is fine, so we assign it to the proxy and to all of
1202 * its listeners. The blocked ones will be dequeued.
1203 */
1204 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001205 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001206 l->maxconn = v;
1207 if (l->state == LI_FULL)
1208 resume_listener(l);
1209 }
1210
1211 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1212 dequeue_all_listeners(&s->fe->listener_queue);
1213
1214 return 1;
1215 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001216 else if (strcmp(args[2], "global") == 0) {
1217 int v;
1218
Willy Tarreau290e63a2012-09-20 18:07:14 +02001219 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001220 appctx->ctx.cli.msg = stats_permission_denied_msg;
1221 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001222 return 1;
1223 }
1224
1225 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001226 appctx->ctx.cli.msg = "Expects an integer value.\n";
1227 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001228 return 1;
1229 }
1230
1231 v = atoi(args[3]);
1232 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001233 appctx->ctx.cli.msg = "Value out of range.\n";
1234 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001235 return 1;
1236 }
1237
1238 /* check for unlimited values */
1239 if (v <= 0)
1240 v = global.hardmaxconn;
1241
1242 global.maxconn = 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 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001250 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001251 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1252 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001253 return 1;
1254 }
1255 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001256 else if (strcmp(args[1], "rate-limit") == 0) {
1257 if (strcmp(args[2], "connections") == 0) {
1258 if (strcmp(args[3], "global") == 0) {
1259 int v;
1260
Willy Tarreau290e63a2012-09-20 18:07:14 +02001261 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001262 appctx->ctx.cli.msg = stats_permission_denied_msg;
1263 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001264 return 1;
1265 }
1266
1267 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001268 appctx->ctx.cli.msg = "Expects an integer value.\n";
1269 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001270 return 1;
1271 }
1272
1273 v = atoi(args[4]);
1274 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001275 appctx->ctx.cli.msg = "Value out of range.\n";
1276 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001277 return 1;
1278 }
1279
1280 global.cps_lim = v;
1281
1282 /* Dequeues all of the listeners waiting for a resource */
1283 if (!LIST_ISEMPTY(&global_listener_queue))
1284 dequeue_all_listeners(&global_listener_queue);
1285
1286 return 1;
1287 }
1288 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001289 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1290 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001291 return 1;
1292 }
1293 }
William Lallemandd85f9172012-11-09 17:05:39 +01001294 else if (strcmp(args[2], "http-compression") == 0) {
1295 if (strcmp(args[3], "global") == 0) {
1296 int v;
1297
Willy Tarreau85d47f92012-11-21 00:29:50 +01001298 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001299 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1300 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001301 return 1;
1302 }
1303
William Lallemandd85f9172012-11-09 17:05:39 +01001304 v = atoi(args[4]);
1305 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1306 }
1307 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001308 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1309 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001310 return 1;
1311 }
1312 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001313 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001314 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
1315 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001316 return 1;
1317 }
1318 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001319 else if (strcmp(args[1], "table") == 0) {
1320 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1321 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001322 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001323 return 0;
1324 }
1325 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001326 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001327 if (strcmp(args[1], "agent") == 0) {
1328 struct server *sv;
1329
1330 sv = expect_server_admin(s, si, args[2]);
1331 if (!sv)
1332 return 1;
1333
1334 sv->agent.state &= ~CHK_STATE_DISABLED;
1335
1336 return 1;
1337 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001338 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001339 struct server *sv;
1340
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001341 sv = expect_server_admin(s, si, args[2]);
1342 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001343 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001344
Cyril Bontécd19e512010-01-31 22:34:03 +01001345 if (sv->state & SRV_MAINTAIN) {
1346 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001347 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001348 /* If this server tracks the status of another one,
1349 * we must restore the good status.
1350 */
Willy Tarreau44267702011-10-28 15:35:33 +02001351 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001352 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001353 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001354 } else {
1355 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001356 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001357 }
1358 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001359 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001360 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001361 }
1362 }
1363
Willy Tarreau532a4502011-09-07 22:37:44 +02001364 return 1;
1365 }
1366 else if (strcmp(args[1], "frontend") == 0) {
1367 struct proxy *px;
1368
1369 px = expect_frontend_admin(s, si, args[2]);
1370 if (!px)
1371 return 1;
1372
1373 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001374 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001376 return 1;
1377 }
1378
1379 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001380 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1381 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001382 return 1;
1383 }
1384
1385 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001386 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1387 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001388 return 1;
1389 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001390 return 1;
1391 }
1392 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001393 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1394 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001395 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001396 }
1397 }
1398 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001399 if (strcmp(args[1], "agent") == 0) {
1400 struct server *sv;
1401
1402 sv = expect_server_admin(s, si, args[2]);
1403 if (!sv)
1404 return 1;
1405
1406 sv->agent.state |= CHK_STATE_DISABLED;
1407
1408 return 1;
1409 }
1410 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001411 struct server *sv;
1412
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001413 sv = expect_server_admin(s, si, args[2]);
1414 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001415 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001416
Cyril Bontécd19e512010-01-31 22:34:03 +01001417 if (! (sv->state & SRV_MAINTAIN)) {
1418 /* Not already in maintenance, we can change the server state */
1419 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001420 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001421 }
1422
Willy Tarreau532a4502011-09-07 22:37:44 +02001423 return 1;
1424 }
1425 else if (strcmp(args[1], "frontend") == 0) {
1426 struct proxy *px;
1427
1428 px = expect_frontend_admin(s, si, args[2]);
1429 if (!px)
1430 return 1;
1431
1432 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001433 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1434 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001435 return 1;
1436 }
1437
1438 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1440 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001441 return 1;
1442 }
1443
1444 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001445 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1446 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001447 return 1;
1448 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001449 return 1;
1450 }
1451 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001452 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1453 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001454 return 1;
1455 }
1456 }
1457 else if (strcmp(args[0], "shutdown") == 0) {
1458 if (strcmp(args[1], "frontend") == 0) {
1459 struct proxy *px;
1460
1461 px = expect_frontend_admin(s, si, args[2]);
1462 if (!px)
1463 return 1;
1464
1465 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001466 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1467 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001468 return 1;
1469 }
1470
1471 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1472 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1473 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1474 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1475 stop_proxy(px);
1476 return 1;
1477 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001478 else if (strcmp(args[1], "session") == 0) {
1479 struct session *sess, *ptr;
1480
Willy Tarreau290e63a2012-09-20 18:07:14 +02001481 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001482 appctx->ctx.cli.msg = stats_permission_denied_msg;
1483 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001484 return 1;
1485 }
1486
1487 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001488 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1489 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001490 return 1;
1491 }
1492
1493 ptr = (void *)strtoul(args[2], NULL, 0);
1494
1495 /* first, look for the requested session in the session table */
1496 list_for_each_entry(sess, &sessions, list) {
1497 if (sess == ptr)
1498 break;
1499 }
1500
1501 /* do we have the session ? */
1502 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001503 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1504 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001505 return 1;
1506 }
1507
1508 session_shutdown(sess, SN_ERR_KILLED);
1509 return 1;
1510 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001511 else if (strcmp(args[1], "sessions") == 0) {
1512 if (strcmp(args[2], "server") == 0) {
1513 struct server *sv;
1514 struct session *sess, *sess_bck;
1515
1516 sv = expect_server_admin(s, si, args[3]);
1517 if (!sv)
1518 return 1;
1519
1520 /* kill all the session that are on this server */
1521 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1522 if (sess->srv_conn == sv)
1523 session_shutdown(sess, SN_ERR_KILLED);
1524
1525 return 1;
1526 }
1527 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001528 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1529 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001530 return 1;
1531 }
1532 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001533 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001534 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1535 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001536 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001537 }
1538 }
1539 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540 return 0;
1541 }
1542 return 1;
1543}
1544
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001546 * used to processes I/O from/to the stats unix socket. The system relies on a
1547 * state machine handling requests and various responses. We read a request,
1548 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001549 * Then we can read again. The state is stored in appctx->st0 and is one of the
1550 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001551 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001552 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001553static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001554{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001556 struct channel *req = si->ob;
1557 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001558 int reql;
1559 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001560
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001561 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1562 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001563
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001564 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001565 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001566 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001567 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1568 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001569 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001570 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001571 /* Let's close for real now. We just close the request
1572 * side, the conditions below will complete if needed.
1573 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001574 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001575 break;
1576 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001577 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001578 /* ensure we have some output room left in the event we
1579 * would want to return some info right after parsing.
1580 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001581 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001582 break;
1583
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001584 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001585 if (reql <= 0) { /* closed or EOL not found */
1586 if (reql == 0)
1587 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001589 continue;
1590 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001591
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001592 /* seek for a possible semi-colon. If we find one, we
1593 * replace it with an LF and skip only this part.
1594 */
1595 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001596 if (trash.str[len] == ';') {
1597 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001598 reql = len + 1;
1599 break;
1600 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001601
Willy Tarreau816fc222009-10-04 07:36:58 +02001602 /* now it is time to check that we have a full line,
1603 * remove the trailing \n and possibly \r, then cut the
1604 * line.
1605 */
1606 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001607 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001608 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001610 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001611
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001612 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001613 len--;
1614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001615 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001618 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001619 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001620 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 continue;
1622 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001623 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001624 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001625 else if (strcmp(trash.str, "help") == 0 ||
1626 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001627 appctx->ctx.cli.msg = stats_sock_usage_msg;
1628 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001629 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001630 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001631 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001632 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001633 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001634 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001635 /* if prompt is disabled, print help on empty lines,
1636 * so that the user at least knows how to enable
1637 * prompt and find help.
1638 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001639 appctx->ctx.cli.msg = stats_sock_usage_msg;
1640 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001641 }
1642
1643 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001644 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001645 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001646 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001647 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001648 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001649 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001650 continue;
1651 }
1652
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001653 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001654 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001655 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
1656 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001657 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001658 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001659 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001660 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001661 break;
1662 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001663 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001664 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001665 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001666 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001667 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001668 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001669 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001670 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001671 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001672 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001673 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001674 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001675 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001676 if (stats_table_request(si, appctx->st0))
1677 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001678 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001679 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001680 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001681 break;
1682 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001683
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001684 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 if (appctx->st0 == STAT_CLI_PROMPT) {
1686 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
1687 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001688 }
1689
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001690 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001691 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001692 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001693
1694 /* Now we close the output if one of the writers did so,
1695 * or if we're not in interactive mode and the request
1696 * buffer is empty. This still allows pipelined requests
1697 * to be sent in non-interactive mode.
1698 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001699 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
1700 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001701 continue;
1702 }
1703
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001704 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001705 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001706 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001707 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001708
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001709 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001710 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1711 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001712 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001713 * and nothing more to consume. This is comparable to a broken pipe, so
1714 * we forward the close to the request side so that it flows upstream to
1715 * the client.
1716 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001717 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001718 }
1719
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001720 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001721 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1722 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1723 /* We have no more processing to do, and nothing more to send, and
1724 * the client side has closed. So we'll forward this state downstream
1725 * on the response buffer.
1726 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001727 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001728 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001729 }
1730
1731 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001732 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001733
1734 /* we don't want to expire timeouts while we're processing requests */
1735 si->ib->rex = TICK_ETERNITY;
1736 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001737
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001738 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001739 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 +02001740 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001741 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 +02001742
1743 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1744 /* check that we have released everything then unregister */
1745 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001746 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001747}
1748
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001749/* This function dumps information onto the stream interface's read buffer.
1750 * It returns 0 as long as it does not complete, non-zero upon completion.
1751 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001752 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001753static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001754{
1755 unsigned int up = (now.tv_sec - start_date.tv_sec);
1756
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001757 chunk_printf(&trash,
1758 "Name: " PRODUCT_NAME "\n"
1759 "Version: " HAPROXY_VERSION "\n"
1760 "Release_date: " HAPROXY_DATE "\n"
1761 "Nbproc: %d\n"
1762 "Process_num: %d\n"
1763 "Pid: %d\n"
1764 "Uptime: %dd %dh%02dm%02ds\n"
1765 "Uptime_sec: %d\n"
1766 "Memmax_MB: %d\n"
1767 "Ulimit-n: %d\n"
1768 "Maxsock: %d\n"
1769 "Maxconn: %d\n"
1770 "Hard_maxconn: %d\n"
1771 "Maxpipes: %d\n"
1772 "CurrConns: %d\n"
1773 "PipesUsed: %d\n"
1774 "PipesFree: %d\n"
1775 "ConnRate: %d\n"
1776 "ConnRateLimit: %d\n"
1777 "MaxConnRate: %d\n"
1778 "CompressBpsIn: %u\n"
1779 "CompressBpsOut: %u\n"
1780 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001781#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001782 "ZlibMemUsage: %ld\n"
1783 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001784#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001785 "Tasks: %d\n"
1786 "Run_queue: %d\n"
1787 "Idle_pct: %d\n"
1788 "node: %s\n"
1789 "description: %s\n"
1790 "",
1791 global.nbproc,
1792 relative_pid,
1793 pid,
1794 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1795 up,
1796 global.rlimit_memmax,
1797 global.rlimit_nofile,
1798 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1799 actconn, pipes_used, pipes_free,
1800 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1801 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1802 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001803#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001804 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001805#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001806 nb_tasks_cur, run_queue_cur, idle_pct,
1807 global.node, global.desc ? global.desc : ""
1808 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001809
1810 if (bi_putchk(si->ib, &trash) == -1)
1811 return 0;
1812
1813 return 1;
1814}
1815
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001816/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1817 * the state from stream interface <si>. The caller is responsible for clearing
1818 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001819 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001820static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001821{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001822 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001823 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001824
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001825 if (!(px->cap & PR_CAP_FE))
1826 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001827
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001828 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001829 return 0;
1830
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001831 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001832 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001833 /* name, queue */
1834 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001835
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001836 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001837 /* Column sub-heading for Enable or Disable server */
1838 chunk_appendf(&trash, "<td></td>");
1839 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001841 chunk_appendf(&trash,
1842 "<td class=ac>"
1843 "<a name=\"%s/Frontend\"></a>"
1844 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1845 "<td colspan=3></td>"
1846 "",
1847 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001848
Willy Tarreau466c9b52012-12-23 02:25:03 +01001849 chunk_appendf(&trash,
1850 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001851 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001852 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1853 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1854 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001855 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1856 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1857 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001858
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001859 if (px->mode == PR_MODE_HTTP)
1860 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001861 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001862 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001863
1864 chunk_appendf(&trash,
1865 "</table></div></u></td>"
1866 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001867 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001868 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1869 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1870 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001871 U2H(px->fe_counters.sps_max),
1872 U2H(px->fe_counters.cps_max),
1873 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001874
1875 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001876 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001877 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001878 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001879
1880 chunk_appendf(&trash,
1881 "</table></div></u></td>"
1882 /* sessions rate : limit */
1883 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001884 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001885
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001886 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001887 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001888 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001889 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001890 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1891 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001892 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001893 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1894 U2H(px->fe_counters.cum_sess),
1895 U2H(px->fe_counters.cum_conn),
1896 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001897
Willy Tarreau466c9b52012-12-23 02:25:03 +01001898 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001899 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001900 chunk_appendf(&trash,
1901 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1902 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1903 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1904 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1905 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1906 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1907 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1908 "<tr><th>- other responses:</th><td>%s</td></tr>"
1909 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1910 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001911 U2H(px->fe_counters.p.http.cum_req),
1912 U2H(px->fe_counters.p.http.rsp[1]),
1913 U2H(px->fe_counters.p.http.rsp[2]),
1914 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001915 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001916 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001917 U2H(px->fe_counters.p.http.rsp[3]),
1918 U2H(px->fe_counters.p.http.rsp[4]),
1919 U2H(px->fe_counters.p.http.rsp[5]),
1920 U2H(px->fe_counters.p.http.rsp[0]),
1921 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001922 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001923
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001924 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001925 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001926 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001927 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001928 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001929 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001930 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001931 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001932
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001933 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001934 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001935 "<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 +01001936 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001937 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001938 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1939 px->fe_counters.comp_in ?
1940 (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 +01001941 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001942
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001943 chunk_appendf(&trash,
1944 /* denied: req, resp */
1945 "<td>%s</td><td>%s</td>"
1946 /* errors : request, connect, response */
1947 "<td>%s</td><td></td><td></td>"
1948 /* warnings: retries, redispatches */
1949 "<td></td><td></td>"
1950 /* server status : reflect frontend status */
1951 "<td class=ac>%s</td>"
1952 /* rest of server: nothing */
1953 "<td class=ac colspan=8></td></tr>"
1954 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001955 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1956 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001957 px->state == PR_STREADY ? "OPEN" :
1958 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001959 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001960 else { /* CSV mode */
1961 chunk_appendf(&trash,
1962 /* pxid, name, queue cur, queue max, */
1963 "%s,FRONTEND,,,"
1964 /* sessions : current, max, limit, total */
1965 "%d,%d,%d,%lld,"
1966 /* bytes : in, out */
1967 "%lld,%lld,"
1968 /* denied: req, resp */
1969 "%lld,%lld,"
1970 /* errors : request, connect, response */
1971 "%lld,,,"
1972 /* warnings: retries, redispatches */
1973 ",,"
1974 /* server status : reflect frontend status */
1975 "%s,"
1976 /* rest of server: nothing */
1977 ",,,,,,,,"
1978 /* pid, iid, sid, throttle, lbtot, tracked, type */
1979 "%d,%d,0,,,,%d,"
1980 /* rate, rate_lim, rate_max */
1981 "%u,%u,%u,"
1982 /* check_status, check_code, check_duration */
1983 ",,,",
1984 px->id,
1985 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1986 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1987 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1988 px->fe_counters.failed_req,
1989 px->state == PR_STREADY ? "OPEN" :
1990 px->state == PR_STFULL ? "FULL" : "STOP",
1991 relative_pid, px->uuid, STATS_TYPE_FE,
1992 read_freq_ctr(&px->fe_sess_per_sec),
1993 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001994
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001995 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1996 if (px->mode == PR_MODE_HTTP) {
1997 for (i=1; i<6; i++)
1998 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1999 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2000 }
2001 else
2002 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002003
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002004 /* failed health analyses */
2005 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002006
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002007 /* requests : req_rate, req_rate_max, req_tot, */
2008 chunk_appendf(&trash, "%u,%u,%lld,",
2009 read_freq_ctr(&px->fe_req_per_sec),
2010 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2011
2012 /* errors: cli_aborts, srv_aborts */
2013 chunk_appendf(&trash, ",,");
2014
2015 /* compression: in, out, bypassed */
2016 chunk_appendf(&trash, "%lld,%lld,%lld,",
2017 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2018
2019 /* compression: comp_rsp */
2020 chunk_appendf(&trash, "%lld,",
2021 px->fe_counters.p.http.comp_rsp);
2022
2023 /* finish with EOL */
2024 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002025 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002026 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002027}
2028
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002029/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2030 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2031 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2032 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002033 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002034static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002035{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002036 struct appctx *appctx = __objt_appctx(si->end);
2037
2038 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002039 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002041 /* Column sub-heading for Enable or Disable server */
2042 chunk_appendf(&trash, "<td></td>");
2043 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002044 chunk_appendf(&trash,
2045 /* frontend name, listener name */
2046 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2047 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2048 "",
2049 px->id, l->name,
2050 (flags & ST_SHLGNDS)?"<u>":"",
2051 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002052
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002053 if (flags & ST_SHLGNDS) {
2054 char str[INET6_ADDRSTRLEN];
2055 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002056
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002057 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002058
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002059 port = get_host_port(&l->addr);
2060 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2061 case AF_INET:
2062 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2063 break;
2064 case AF_INET6:
2065 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2066 break;
2067 case AF_UNIX:
2068 chunk_appendf(&trash, "unix, ");
2069 break;
2070 case -1:
2071 chunk_appendf(&trash, "(%s), ", strerror(errno));
2072 break;
2073 }
Willy Tarreau91861262007-10-17 17:06:05 +02002074
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002075 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002076 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002077 }
Willy Tarreau91861262007-10-17 17:06:05 +02002078
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002079 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002080 /* queue */
2081 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002082 /* sessions rate: current, max, limit */
2083 "<td colspan=3>&nbsp;</td>"
2084 /* sessions: current, max, limit, total, lbtot */
2085 "<td>%s</td><td>%s</td><td>%s</td>"
2086 "<td>%s</td><td>&nbsp;</td>"
2087 /* bytes: in, out */
2088 "<td>%s</td><td>%s</td>"
2089 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002090 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002091 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2092 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002093
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002094 chunk_appendf(&trash,
2095 /* denied: req, resp */
2096 "<td>%s</td><td>%s</td>"
2097 /* errors: request, connect, response */
2098 "<td>%s</td><td></td><td></td>"
2099 /* warnings: retries, redispatches */
2100 "<td></td><td></td>"
2101 /* server status: reflect listener status */
2102 "<td class=ac>%s</td>"
2103 /* rest of server: nothing */
2104 "<td class=ac colspan=8></td></tr>"
2105 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002106 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2107 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002108 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2109 }
2110 else { /* CSV mode */
2111 chunk_appendf(&trash,
2112 /* pxid, name, queue cur, queue max, */
2113 "%s,%s,,,"
2114 /* sessions: current, max, limit, total */
2115 "%d,%d,%d,%lld,"
2116 /* bytes: in, out */
2117 "%lld,%lld,"
2118 /* denied: req, resp */
2119 "%lld,%lld,"
2120 /* errors: request, connect, response */
2121 "%lld,,,"
2122 /* warnings: retries, redispatches */
2123 ",,"
2124 /* server status: reflect listener status */
2125 "%s,"
2126 /* rest of server: nothing */
2127 ",,,,,,,,"
2128 /* pid, iid, sid, throttle, lbtot, tracked, type */
2129 "%d,%d,%d,,,,%d,"
2130 /* rate, rate_lim, rate_max */
2131 ",,,"
2132 /* check_status, check_code, check_duration */
2133 ",,,"
2134 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2135 ",,,,,,"
2136 /* failed health analyses */
2137 ","
2138 /* requests : req_rate, req_rate_max, req_tot, */
2139 ",,,"
2140 /* errors: cli_aborts, srv_aborts */
2141 ",,"
2142 /* compression: in, out, bypassed, comp_rsp */
2143 ",,,,"
2144 "\n",
2145 px->id, l->name,
2146 l->nbconn, l->counters->conn_max,
2147 l->maxconn, l->counters->cum_conn,
2148 l->counters->bytes_in, l->counters->bytes_out,
2149 l->counters->denied_req, l->counters->denied_resp,
2150 l->counters->failed_req,
2151 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2152 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2153 }
2154 return 1;
2155}
Willy Tarreau91861262007-10-17 17:06:05 +02002156
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002157/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2158 * from stream interface <si>, stats flags <flags>, and server state <state>.
2159 * The caller is responsible for clearing the trash if needed. Returns non-zero
2160 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002161 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2162 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002163 */
2164static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2165{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002166 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002167 struct server *ref = sv->track ? sv->track : sv;
2168 char str[INET6_ADDRSTRLEN];
2169 struct chunk src;
2170 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002171
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002172 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002173 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002174 "DOWN",
2175 "DN %d/%d &uarr;",
2176 "UP %d/%d &darr;",
2177 "UP",
2178 "NOLB %d/%d &darr;",
2179 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002180 "DRAIN %d/%d &darr;",
2181 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002182 "<i>no check</i>"
2183 };
Willy Tarreau91861262007-10-17 17:06:05 +02002184
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002185 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2186 chunk_appendf(&trash, "<tr class=\"maintain\">");
2187 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002188 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002189 "<tr class=\"%s%d\">",
2190 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002191
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002192 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002193 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002194 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2195 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002196
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002197 chunk_appendf(&trash,
2198 "<td class=ac><a name=\"%s/%s\"></a>%s"
2199 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2200 "",
2201 px->id, sv->id,
2202 (flags & ST_SHLGNDS) ? "<u>" : "",
2203 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002205 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002206 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002207
2208 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2209 case AF_INET:
2210 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2211 break;
2212 case AF_INET6:
2213 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2214 break;
2215 case AF_UNIX:
2216 chunk_appendf(&trash, "unix, ");
2217 break;
2218 case -1:
2219 chunk_appendf(&trash, "(%s), ", strerror(errno));
2220 break;
2221 default: /* address family not supported */
2222 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002223 }
Willy Tarreau91861262007-10-17 17:06:05 +02002224
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002225 /* id */
2226 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002227
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002228 /* cookie */
2229 if (sv->cookie) {
2230 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002231
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002232 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2233 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002234
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002235 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002236 }
2237
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002238 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002239 }
Willy Tarreau91861262007-10-17 17:06:05 +02002240
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002241 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002242 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002243 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002244 /* sessions rate : current, max, limit */
2245 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002246 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002247 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002248 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2249 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002250
Willy Tarreau466c9b52012-12-23 02:25:03 +01002251
2252 chunk_appendf(&trash,
2253 /* sessions: current, max, limit, total */
2254 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002255 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002256 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2257 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002258 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2259 U2H(sv->counters.cum_sess),
2260 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002261
Willy Tarreau466c9b52012-12-23 02:25:03 +01002262 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2263 if (px->mode == PR_MODE_HTTP) {
2264 unsigned long long tot;
2265 for (tot = i = 0; i < 6; i++)
2266 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002267
Willy Tarreau466c9b52012-12-23 02:25:03 +01002268 chunk_appendf(&trash,
2269 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2270 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2271 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2272 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2273 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2274 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2275 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2276 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002277 U2H(tot),
2278 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2279 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2280 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2281 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2282 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2283 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 +02002284 }
Willy Tarreau91861262007-10-17 17:06:05 +02002285
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002286 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002287 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002288 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002289 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002290 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002291
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002292 chunk_appendf(&trash,
2293 /* bytes : in, out */
2294 "<td>%s</td><td>%s</td>"
2295 /* denied: req, resp */
2296 "<td></td><td>%s</td>"
2297 /* errors : request, connect */
2298 "<td></td><td>%s</td>"
2299 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002300 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002301 /* warnings: retries, redispatches */
2302 "<td>%lld</td><td>%lld</td>"
2303 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002304 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2305 U2H(sv->counters.failed_secu),
2306 U2H(sv->counters.failed_conns),
2307 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002308 sv->counters.cli_aborts,
2309 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002310 sv->counters.retries, sv->counters.redispatches);
2311
2312 /* status, lest check */
2313 chunk_appendf(&trash, "<td class=ac>");
2314
2315 if (sv->state & SRV_MAINTAIN) {
2316 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2317 chunk_appendf(&trash, "MAINT");
2318 }
2319 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2320 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2321 chunk_appendf(&trash, "MAINT(via)");
2322 }
2323 else if (ref->state & SRV_CHECKED) {
2324 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2325 chunk_appendf(&trash,
2326 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002327 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2328 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002329 }
Willy Tarreau91861262007-10-17 17:06:05 +02002330
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002331 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002332 chunk_appendf(&trash,
2333 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002334 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002335 get_check_status_info(sv->check.status));
2336
2337 if (sv->check.status >= HCHK_STATUS_L57DATA)
2338 chunk_appendf(&trash, "/%d", sv->check.code);
2339
2340 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002341 chunk_appendf(&trash, " in %lums", sv->check.duration);
2342
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002343 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002344 get_check_status_description(sv->check.status));
2345 if (*sv->check.desc) {
2346 chunk_appendf(&trash, ": ");
2347 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2348 chunk_htmlencode(&trash, &src);
2349 }
2350 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002351 }
2352 else
2353 chunk_appendf(&trash, "</td><td>");
2354
2355 chunk_appendf(&trash,
2356 /* weight */
2357 "</td><td class=ac>%d</td>"
2358 /* act, bck */
2359 "<td class=ac>%s</td><td class=ac>%s</td>"
2360 "",
2361 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2362 (sv->state & SRV_BACKUP) ? "-" : "Y",
2363 (sv->state & SRV_BACKUP) ? "Y" : "-");
2364
2365 /* check failures: unique, fatal, down time */
2366 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002367 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002368
2369 if (ref->observe)
2370 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2371
2372 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002373 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002374 "<td>%lld</td><td>%s</td>"
2375 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002376 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002377 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2378 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002379 else if (sv != ref) {
2380 if (sv->state & SRV_MAINTAIN)
2381 chunk_appendf(&trash,
2382 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2383 ref->proxy->id, ref->id);
2384 else
2385 chunk_appendf(&trash,
2386 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2387 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2388 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002389 else
2390 chunk_appendf(&trash, "<td colspan=3></td>");
2391
2392 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002393 if (sv->state & SRV_WARMINGUP)
2394 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002395 else
2396 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2397 }
2398 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002399 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002400 "DOWN,",
2401 "DOWN %d/%d,",
2402 "UP %d/%d,",
2403 "UP,",
2404 "NOLB %d/%d,",
2405 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002406 "DRAIN %d/%d,",
2407 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002408 "no check,"
2409 };
2410
2411 chunk_appendf(&trash,
2412 /* pxid, name */
2413 "%s,%s,"
2414 /* queue : current, max */
2415 "%d,%d,"
2416 /* sessions : current, max, limit, total */
2417 "%d,%d,%s,%lld,"
2418 /* bytes : in, out */
2419 "%lld,%lld,"
2420 /* denied: req, resp */
2421 ",%lld,"
2422 /* errors : request, connect, response */
2423 ",%lld,%lld,"
2424 /* warnings: retries, redispatches */
2425 "%lld,%lld,"
2426 "",
2427 px->id, sv->id,
2428 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002429 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002430 sv->counters.bytes_in, sv->counters.bytes_out,
2431 sv->counters.failed_secu,
2432 sv->counters.failed_conns, sv->counters.failed_resp,
2433 sv->counters.retries, sv->counters.redispatches);
2434
2435 /* status */
2436 if (sv->state & SRV_MAINTAIN)
2437 chunk_appendf(&trash, "MAINT,");
2438 else if (ref != sv && ref->state & SRV_MAINTAIN)
2439 chunk_appendf(&trash, "MAINT(via),");
2440 else
2441 chunk_appendf(&trash,
2442 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002443 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2444 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002445
2446 chunk_appendf(&trash,
2447 /* weight, active, backup */
2448 "%d,%d,%d,"
2449 "",
2450 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2451 (sv->state & SRV_BACKUP) ? 0 : 1,
2452 (sv->state & SRV_BACKUP) ? 1 : 0);
2453
2454 /* check failures: unique, fatal; last change, total downtime */
2455 if (sv->state & SRV_CHECKED)
2456 chunk_appendf(&trash,
2457 "%lld,%lld,%d,%d,",
2458 sv->counters.failed_checks, sv->counters.down_trans,
2459 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2460 else
2461 chunk_appendf(&trash, ",,,,");
2462
2463 /* queue limit, pid, iid, sid, */
2464 chunk_appendf(&trash,
2465 "%s,"
2466 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002467 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002468 relative_pid, px->uuid, sv->puid);
2469
2470 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002471 if (sv->state & SRV_WARMINGUP)
2472 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002473
2474 /* sessions: lbtot */
2475 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2476
2477 /* tracked */
2478 if (sv->track)
2479 chunk_appendf(&trash, "%s/%s,",
2480 sv->track->proxy->id, sv->track->id);
2481 else
2482 chunk_appendf(&trash, ",");
2483
2484 /* type */
2485 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2486
2487 /* rate */
2488 chunk_appendf(&trash, "%u,,%u,",
2489 read_freq_ctr(&sv->sess_per_sec),
2490 sv->counters.sps_max);
2491
2492 if (sv->state & SRV_CHECKED) {
2493 /* check_status */
2494 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2495
2496 /* check_code */
2497 if (sv->check.status >= HCHK_STATUS_L57DATA)
2498 chunk_appendf(&trash, "%u,", sv->check.code);
2499 else
2500 chunk_appendf(&trash, ",");
2501
2502 /* check_duration */
2503 if (sv->check.status >= HCHK_STATUS_CHECKED)
2504 chunk_appendf(&trash, "%lu,", sv->check.duration);
2505 else
2506 chunk_appendf(&trash, ",");
2507
2508 }
2509 else
2510 chunk_appendf(&trash, ",,,");
2511
2512 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2513 if (px->mode == PR_MODE_HTTP) {
2514 for (i=1; i<6; i++)
2515 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2516
2517 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2518 }
2519 else
2520 chunk_appendf(&trash, ",,,,,,");
2521
2522 /* failed health analyses */
2523 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2524
2525 /* requests : req_rate, req_rate_max, req_tot, */
2526 chunk_appendf(&trash, ",,,");
2527
2528 /* errors: cli_aborts, srv_aborts */
2529 chunk_appendf(&trash, "%lld,%lld,",
2530 sv->counters.cli_aborts, sv->counters.srv_aborts);
2531
2532 /* compression: in, out, bypassed, comp_rsp */
2533 chunk_appendf(&trash, ",,,,");
2534
2535 /* finish with EOL */
2536 chunk_appendf(&trash, "\n");
2537 }
2538 return 1;
2539}
2540
2541/* Dumps a line for backend <px> to the trash for and uses the state from stream
2542 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2543 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2544 */
2545static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2546{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002547 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548 struct chunk src;
2549 int i;
2550
2551 if (!(px->cap & PR_CAP_BE))
2552 return 0;
2553
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002554 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555 return 0;
2556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002557 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002558 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002559 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002560 /* Column sub-heading for Enable or Disable server */
2561 chunk_appendf(&trash, "<td></td>");
2562 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002563 chunk_appendf(&trash,
2564 "<td class=ac>"
2565 /* name */
2566 "%s<a name=\"%s/Backend\"></a>"
2567 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2568 "",
2569 (flags & ST_SHLGNDS)?"<u>":"",
2570 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571
2572 if (flags & ST_SHLGNDS) {
2573 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002574 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2576
2577 /* cookie */
2578 if (px->cookie_name) {
2579 chunk_appendf(&trash, ", cookie: '");
2580 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2581 chunk_htmlencode(&trash, &src);
2582 chunk_appendf(&trash, "'");
2583 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002584 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002585 }
2586
2587 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002588 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 /* queue : current, max */
2590 "<td>%s</td><td>%s</td><td></td>"
2591 /* sessions rate : current, max, limit */
2592 "<td>%s</td><td>%s</td><td></td>"
2593 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002595 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2596 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597
2598 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002599 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002600 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002601 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002602 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002603 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002604 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2605 U2H(px->be_counters.cum_conn),
2606 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607
Willy Tarreau466c9b52012-12-23 02:25:03 +01002608 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002609 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002610 chunk_appendf(&trash,
2611 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2612 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2613 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2614 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2615 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2616 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2617 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2618 "<tr><th>- other responses:</th><td>%s</td></tr>"
2619 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2620 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002621 U2H(px->be_counters.p.http.cum_req),
2622 U2H(px->be_counters.p.http.rsp[1]),
2623 U2H(px->be_counters.p.http.rsp[2]),
2624 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002625 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002626 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002627 U2H(px->be_counters.p.http.rsp[3]),
2628 U2H(px->be_counters.p.http.rsp[4]),
2629 U2H(px->be_counters.p.http.rsp[5]),
2630 U2H(px->be_counters.p.http.rsp[0]),
2631 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002632 }
2633
2634 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002635 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002636 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002637 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002638 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002639 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002640 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002641 U2H(px->be_counters.cum_lbconn),
2642 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002643
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002644 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002645 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002646 "<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 +01002647 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002648 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002649 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2650 px->be_counters.comp_in ?
2651 (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 +01002652 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2653
2654 chunk_appendf(&trash,
2655 /* denied: req, resp */
2656 "<td>%s</td><td>%s</td>"
2657 /* errors : request, connect */
2658 "<td></td><td>%s</td>"
2659 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002660 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002661 /* warnings: retries, redispatches */
2662 "<td>%lld</td><td>%lld</td>"
2663 /* backend status: reflect backend status (up/down): we display UP
2664 * if the backend has known working servers or if it has no server at
2665 * all (eg: for stats). Then we display the total weight, number of
2666 * active and backups. */
2667 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2668 "<td class=ac>%d</td><td class=ac>%d</td>"
2669 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002670 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2671 U2H(px->be_counters.failed_conns),
2672 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002673 px->be_counters.cli_aborts,
2674 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002675 px->be_counters.retries, px->be_counters.redispatches,
2676 human_time(now.tv_sec - px->last_change, 1),
2677 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2678 "<font color=\"red\"><b>DOWN</b></font>",
2679 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2680 px->srv_act, px->srv_bck);
2681
2682 chunk_appendf(&trash,
2683 /* rest of backend: nothing, down transitions, total downtime, throttle */
2684 "<td class=ac>&nbsp;</td><td>%d</td>"
2685 "<td>%s</td>"
2686 "<td></td>"
2687 "</tr>",
2688 px->down_trans,
2689 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2690 }
2691 else { /* CSV mode */
2692 chunk_appendf(&trash,
2693 /* pxid, name */
2694 "%s,BACKEND,"
2695 /* queue : current, max */
2696 "%d,%d,"
2697 /* sessions : current, max, limit, total */
2698 "%d,%d,%d,%lld,"
2699 /* bytes : in, out */
2700 "%lld,%lld,"
2701 /* denied: req, resp */
2702 "%lld,%lld,"
2703 /* errors : request, connect, response */
2704 ",%lld,%lld,"
2705 /* warnings: retries, redispatches */
2706 "%lld,%lld,"
2707 /* backend status: reflect backend status (up/down): we display UP
2708 * if the backend has known working servers or if it has no server at
2709 * all (eg: for stats). Then we display the total weight, number of
2710 * active and backups. */
2711 "%s,"
2712 "%d,%d,%d,"
2713 /* rest of backend: nothing, down transitions, last change, total downtime */
2714 ",%d,%d,%d,,"
2715 /* pid, iid, sid, throttle, lbtot, tracked, type */
2716 "%d,%d,0,,%lld,,%d,"
2717 /* rate, rate_lim, rate_max, */
2718 "%u,,%u,"
2719 /* check_status, check_code, check_duration */
2720 ",,,",
2721 px->id,
2722 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2723 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2724 px->be_counters.bytes_in, px->be_counters.bytes_out,
2725 px->be_counters.denied_req, px->be_counters.denied_resp,
2726 px->be_counters.failed_conns, px->be_counters.failed_resp,
2727 px->be_counters.retries, px->be_counters.redispatches,
2728 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2729 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2730 px->srv_act, px->srv_bck,
2731 px->down_trans, (int)(now.tv_sec - px->last_change),
2732 px->srv?be_downtime(px):0,
2733 relative_pid, px->uuid,
2734 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2735 read_freq_ctr(&px->be_sess_per_sec),
2736 px->be_counters.sps_max);
2737
2738 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2739 if (px->mode == PR_MODE_HTTP) {
2740 for (i=1; i<6; i++)
2741 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2742 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2743 }
2744 else
2745 chunk_appendf(&trash, ",,,,,,");
2746
2747 /* failed health analyses */
2748 chunk_appendf(&trash, ",");
2749
2750 /* requests : req_rate, req_rate_max, req_tot, */
2751 chunk_appendf(&trash, ",,,");
2752
2753 /* errors: cli_aborts, srv_aborts */
2754 chunk_appendf(&trash, "%lld,%lld,",
2755 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2756
2757 /* compression: in, out, bypassed */
2758 chunk_appendf(&trash, "%lld,%lld,%lld,",
2759 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2760
2761 /* compression: comp_rsp */
2762 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2763
2764 /* finish with EOL */
2765 chunk_appendf(&trash, "\n");
2766 }
2767 return 1;
2768}
2769
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002770/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002771 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002772 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002773 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002774static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002776 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002777 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2778
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002779 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002780 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002781
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002782 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002783 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002784 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002785 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002786 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2787 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002788 }
2789
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002791 "<form action=\"%s%s%s%s\" method=\"post\">",
2792 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002793 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2794 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002795 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796 }
2797
2798 /* print a new table */
2799 chunk_appendf(&trash,
2800 "<table class=\"tbl\" width=\"100%%\">\n"
2801 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002802 "<th class=\"pxname\" width=\"10%%\">");
2803
2804 chunk_appendf(&trash,
2805 "<a name=\"%s\"></a>%s"
2806 "<a class=px href=\"#%s\">%s</a>",
2807 px->id,
2808 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2809 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810
2811 if (uri->flags & ST_SHLGNDS) {
2812 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002813 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002814 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2815 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002816 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 }
2818
2819 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002820 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002821 "<th class=\"%s\" width=\"90%%\">%s</th>"
2822 "</tr>\n"
2823 "</table>\n"
2824 "<table class=\"tbl\" width=\"100%%\">\n"
2825 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002826 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2827 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2828
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002829 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002830 /* Column heading for Enable or Disable server */
2831 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002832 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002833
2834 chunk_appendf(&trash,
2835 "<th rowspan=2></th>"
2836 "<th colspan=3>Queue</th>"
2837 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2838 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2839 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2840 "<th colspan=9>Server</th>"
2841 "</tr>\n"
2842 "<tr class=\"titre\">"
2843 "<th>Cur</th><th>Max</th><th>Limit</th>"
2844 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2845 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2846 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2847 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2848 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2849 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2850 "<th>Thrtle</th>\n"
2851 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002852}
2853
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002854/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002855 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002856 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002857static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002859 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002860 chunk_appendf(&trash, "</table>");
2861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002862 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002863 /* close the form used to enable/disable this proxy servers */
2864 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002865 "Choose the action to perform on the checked servers : "
2866 "<select name=action>"
2867 "<option value=\"\"></option>"
2868 "<option value=\"disable\">Disable</option>"
2869 "<option value=\"enable\">Enable</option>"
2870 "<option value=\"stop\">Soft Stop</option>"
2871 "<option value=\"start\">Soft Start</option>"
2872 "<option value=\"shutdown\">Kill Sessions</option>"
2873 "</select>"
2874 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2875 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2876 "</form>",
2877 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002878 }
2879
2880 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002881}
Willy Tarreau91861262007-10-17 17:06:05 +02002882
2883/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002884 * Dumps statistics for a proxy. The output is sent to the stream interface's
2885 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2886 * buffer space, or non-zero if everything completed. This function is used
2887 * both by the CLI and the HTTP entry points, and is able to dump the output
2888 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002889 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002890static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002891{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002892 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02002893 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002894 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002895 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002896 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002897
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002898 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002899
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002900 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002902 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002903 if (uri && uri->scope) {
2904 /* we have a limited scope, we have to check the proxy name */
2905 struct stat_scope *scope;
2906 int len;
2907
2908 len = strlen(px->id);
2909 scope = uri->scope;
2910
2911 while (scope) {
2912 /* match exact proxy name */
2913 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2914 break;
2915
2916 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002917 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002918 break;
2919 scope = scope->next;
2920 }
2921
2922 /* proxy name not found : don't dump anything */
2923 if (scope == NULL)
2924 return 1;
2925 }
2926
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002927 /* if the user has requested a limited output and the proxy
2928 * name does not match, skip it.
2929 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002930 if (appctx->ctx.stats.scope_len &&
2931 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002932 return 1;
2933
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002934 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2935 (appctx->ctx.stats.iid != -1) &&
2936 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002937 return 1;
2938
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002939 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002940 /* fall through */
2941
Willy Tarreau295a8372011-03-10 11:25:07 +01002942 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002943 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002944 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002945 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002946 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002947 }
Willy Tarreau91861262007-10-17 17:06:05 +02002948
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002949 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002950 /* fall through */
2951
Willy Tarreau295a8372011-03-10 11:25:07 +01002952 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002953 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002954 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002955 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002956 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002957
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 appctx->ctx.stats.l = px->conf.listeners.n;
2959 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002960 /* fall through */
2961
Willy Tarreau295a8372011-03-10 11:25:07 +01002962 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002963 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002964 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002965 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002966 return 0;
2967
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002968 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002969 if (!l->counters)
2970 continue;
2971
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002972 if (appctx->ctx.stats.flags & STAT_BOUND) {
2973 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002974 break;
2975
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002976 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002977 continue;
2978 }
2979
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 /* print the frontend */
2981 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2982 if (bi_putchk(rep, &trash) == -1)
2983 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002984 }
2985
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002986 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2987 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002988 /* fall through */
2989
Willy Tarreau295a8372011-03-10 11:25:07 +01002990 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002991 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002992 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002993 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 +02002994
Willy Tarreau9b28e032012-10-12 23:49:43 +02002995 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002996 return 0;
2997
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002998 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002999
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003000 if (appctx->ctx.stats.flags & STAT_BOUND) {
3001 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003002 break;
3003
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003004 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003005 continue;
3006 }
3007
Willy Tarreau44267702011-10-28 15:35:33 +02003008 if (sv->track)
3009 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003010 else
3011 svs = sv;
3012
Willy Tarreau91861262007-10-17 17:06:05 +02003013 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003014 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003015 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003016 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003017 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003018 sv_state = 3; /* UP */
3019 else
3020 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003021
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003022 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003023 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003024 else if (svs->state & SRV_GOINGDOWN)
3025 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003026 }
Willy Tarreau91861262007-10-17 17:06:05 +02003027 else
Simon Horman125d0992013-02-24 17:23:38 +09003028 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003029 sv_state = 1; /* going up */
3030 else
3031 sv_state = 0; /* DOWN */
3032
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003033 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003034 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003036 continue;
3037 }
3038
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003039 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3040 if (bi_putchk(rep, &trash) == -1)
3041 return 0;
3042 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003043
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003044 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003046
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003047 case STAT_PX_ST_BE:
3048 /* print the backend */
3049 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3050 if (bi_putchk(rep, &trash) == -1)
3051 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003052
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003053 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003055
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003056 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003057 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003058 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003059 if (bi_putchk(rep, &trash) == -1)
3060 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003061 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003062
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003063 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003064 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003065
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003066 case STAT_PX_ST_FIN:
3067 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003068
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003069 default:
3070 /* unknown state, we should put an abort() here ! */
3071 return 1;
3072 }
3073}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003074
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003075/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3076 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003077 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003078static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003079{
3080 /* WARNING! This must fit in the first buffer !!! */
3081 chunk_appendf(&trash,
3082 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3083 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3084 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3085 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3086 "<style type=\"text/css\"><!--\n"
3087 "body {"
3088 " font-family: arial, helvetica, sans-serif;"
3089 " font-size: 12px;"
3090 " font-weight: normal;"
3091 " color: black;"
3092 " background: white;"
3093 "}\n"
3094 "th,td {"
3095 " font-size: 10px;"
3096 "}\n"
3097 "h1 {"
3098 " font-size: x-large;"
3099 " margin-bottom: 0.5em;"
3100 "}\n"
3101 "h2 {"
3102 " font-family: helvetica, arial;"
3103 " font-size: x-large;"
3104 " font-weight: bold;"
3105 " font-style: italic;"
3106 " color: #6020a0;"
3107 " margin-top: 0em;"
3108 " margin-bottom: 0em;"
3109 "}\n"
3110 "h3 {"
3111 " font-family: helvetica, arial;"
3112 " font-size: 16px;"
3113 " font-weight: bold;"
3114 " color: #b00040;"
3115 " background: #e8e8d0;"
3116 " margin-top: 0em;"
3117 " margin-bottom: 0em;"
3118 "}\n"
3119 "li {"
3120 " margin-top: 0.25em;"
3121 " margin-right: 2em;"
3122 "}\n"
3123 ".hr {margin-top: 0.25em;"
3124 " border-color: black;"
3125 " border-bottom-style: solid;"
3126 "}\n"
3127 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3128 ".total {background: #20D0D0;color: #ffff80;}\n"
3129 ".frontend {background: #e8e8d0;}\n"
3130 ".socket {background: #d0d0d0;}\n"
3131 ".backend {background: #e8e8d0;}\n"
3132 ".active0 {background: #ff9090;}\n"
3133 ".active1 {background: #ffd020;}\n"
3134 ".active2 {background: #ffffa0;}\n"
3135 ".active3 {background: #c0ffc0;}\n"
3136 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003137 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3138 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3139 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003140 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141 ".backup0 {background: #ff9090;}\n"
3142 ".backup1 {background: #ff80ff;}\n"
3143 ".backup2 {background: #c060ff;}\n"
3144 ".backup3 {background: #b0d0ff;}\n"
3145 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3146 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003147 ".backup6 {background: #c060ff;}\n"
3148 ".backup7 {background: #cc9900;}\n"
3149 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003150 ".maintain {background: #c07820;}\n"
3151 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3152 "\n"
3153 "a.px:link {color: #ffff40; text-decoration: none;}"
3154 "a.px:visited {color: #ffff40; text-decoration: none;}"
3155 "a.px:hover {color: #ffffff; text-decoration: none;}"
3156 "a.lfsb:link {color: #000000; text-decoration: none;}"
3157 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3158 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3159 "\n"
3160 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3161 "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"
3162 "table.tbl td.ac { text-align: center;}\n"
3163 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3164 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3165 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3166 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3167 "\n"
3168 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3169 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3170 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003171 "table.det { border-collapse: collapse; border-style: none; }\n"
3172 "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"
3173 "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 +01003174 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003175 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003176 " display:block;\n"
3177 " visibility:hidden;\n"
3178 " z-index:2147483647;\n"
3179 " position:absolute;\n"
3180 " padding:2px 4px 3px;\n"
3181 " background:#f0f060; color:#000000;\n"
3182 " border:1px solid #7040c0;\n"
3183 " white-space:nowrap;\n"
3184 " font-style:normal;font-size:11px;font-weight:normal;\n"
3185 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3186 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3187 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003188 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 "-->\n"
3190 "</style></head>\n",
3191 (uri->flags & ST_SHNODE) ? " on " : "",
3192 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3193 );
3194}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003195
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003196/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003197 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003198 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003199 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003200static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003201{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003202 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003203 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003204 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003205
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 /* WARNING! this has to fit the first packet too.
3207 * We are around 3.5 kB, add adding entries will
3208 * become tricky if we want to support 4kB buffers !
3209 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003210 chunk_appendf(&trash,
3211 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3212 PRODUCT_NAME "%s</a></h1>\n"
3213 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3214 "<hr width=\"100%%\" class=\"hr\">\n"
3215 "<h3>&gt; General process information</h3>\n"
3216 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3217 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3218 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3219 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3220 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3221 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3222 "Running tasks: %d/%d; idle = %d %%<br>\n"
3223 "</td><td align=\"center\" nowrap>\n"
3224 "<table class=\"lgd\"><tr>\n"
3225 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3226 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3227 "</tr><tr>\n"
3228 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3229 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3230 "</tr><tr>\n"
3231 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3232 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3233 "</tr><tr>\n"
3234 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003235 "</tr><tr>\n"
3236 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003237 "</tr><tr>\n"
3238 "<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 -07003239 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003240 "<td class=\"active7\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003241 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003242 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003243 "</td>"
3244 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3245 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3246 "",
3247 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3248 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3249 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3250 (uri->flags & ST_SHDESC) ? ": " : "",
3251 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3252 pid, relative_pid, global.nbproc,
3253 up / 86400, (up % 86400) / 3600,
3254 (up % 3600) / 60, (up % 60),
3255 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3256 global.rlimit_memmax ? " MB" : "",
3257 global.rlimit_nofile,
3258 global.maxsock, global.maxconn, global.maxpipes,
3259 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3260 run_queue_cur, nb_tasks_cur, idle_pct
3261 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003262
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3264 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3265 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003266
3267 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003268 "<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 +02003269 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003270 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3271 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3272 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003273 STAT_SCOPE_TXT_MAXLEN);
3274
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003276 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003277 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003278 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003279 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3280 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003281 }
3282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003283 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003284 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003285 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003286 uri->uri_prefix,
3287 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003288 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003289 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003290 else
3291 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003292 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003293 uri->uri_prefix,
3294 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003295 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003296 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003297
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003298 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003299 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003300 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003301 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003302 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003303 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003304 "",
3305 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003306 else
3307 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003308 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003310 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003311 ";norefresh",
3312 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003313 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003314
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003315 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003316 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3319 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003320 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003321
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003323 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003324 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003325 (uri->refresh > 0) ? ";norefresh" : "",
3326 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003327
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003328 chunk_appendf(&trash,
3329 "</ul></td>"
3330 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3331 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3332 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3333 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3334 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3335 "</ul>"
3336 "</td>"
3337 "</tr></table>\n"
3338 ""
3339 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003341 if (appctx->ctx.stats.st_code) {
3342 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 case STAT_STATUS_DONE:
3344 chunk_appendf(&trash,
3345 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003346 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003348 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003349 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3350 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003351 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 break;
3353 case STAT_STATUS_NONE:
3354 chunk_appendf(&trash,
3355 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003356 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003358 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003359 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3360 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003361 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362 break;
3363 case STAT_STATUS_PART:
3364 chunk_appendf(&trash,
3365 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003366 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003367 "Action partially processed.<br>"
3368 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003369 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003370 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3371 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003372 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 break;
3374 case STAT_STATUS_ERRP:
3375 chunk_appendf(&trash,
3376 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003377 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 "Action not processed because of invalid parameters."
3379 "<ul>"
3380 "<li>The action is maybe unknown.</li>"
3381 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3382 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3383 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003384 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003385 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3386 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003387 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388 break;
3389 case STAT_STATUS_EXCD:
3390 chunk_appendf(&trash,
3391 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003392 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3394 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003395 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003396 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3397 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003398 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 break;
3400 case STAT_STATUS_DENY:
3401 chunk_appendf(&trash,
3402 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003403 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003405 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003406 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3407 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003408 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 break;
3410 default:
3411 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003412 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003413 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003415 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003416 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3417 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003418 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 }
3420 chunk_appendf(&trash, "<p>\n");
3421 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003423
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003424/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3425 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003427static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003429 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003431
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003432/* This function dumps statistics onto the stream interface's read buffer in
3433 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003434 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3435 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3436 * and the session must be closed, or -1 in case of any error. This function is
3437 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003439static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003441 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003442 struct channel *rep = si->ib;
3443 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003446
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003447 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003449 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003451
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003453 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003454 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003455 else
3456 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003457
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003458 if (bi_putchk(rep, &trash) == -1)
3459 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003460
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003461 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003463
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003464 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003465 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003466 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003467 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003468 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003469 }
Willy Tarreau91861262007-10-17 17:06:05 +02003470
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003471 appctx->ctx.stats.px = proxy;
3472 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3473 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003474 /* fall through */
3475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 case STAT_ST_LIST:
3477 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003478 while (appctx->ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003479 if (buffer_almost_full(rep->buf))
3480 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003481
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003482 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003483 /* skip the disabled proxies, global frontend and non-networked ones */
3484 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003485 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003486 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003487
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003488 appctx->ctx.stats.px = px->next;
3489 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003490 }
3491 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003493 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003495
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003496 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003497 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003498 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003499 if (bi_putchk(rep, &trash) == -1)
3500 return 0;
3501 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003503 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003505
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003506 case STAT_ST_FIN:
3507 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003508
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 default:
3510 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003511 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003512 return -1;
3513 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003514}
Willy Tarreauae526782010-03-04 20:34:23 +01003515
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003516/* We reached the stats page through a POST request. The appctx is
3517 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01003518 * Parse the posted data and enable/disable servers if necessary.
3519 * Returns 1 if request was parsed or zero if it needs more data.
3520 */
3521static int stats_process_http_post(struct stream_interface *si)
3522{
3523 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003524 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003525
3526 struct proxy *px = NULL;
3527 struct server *sv = NULL;
3528
3529 char key[LINESIZE];
3530 int action = ST_ADM_ACTION_NONE;
3531 int reprocess = 0;
3532
3533 int total_servers = 0;
3534 int altered_servers = 0;
3535
3536 char *first_param, *cur_param, *next_param, *end_params;
3537 char *st_cur_param = NULL;
3538 char *st_next_param = NULL;
3539
3540 struct chunk *temp;
3541 int reql;
3542
3543 temp = get_trash_chunk();
3544 if (temp->size < s->txn.req.body_len) {
3545 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003546 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003547 goto out;
3548 }
3549
3550 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3551 if (reql <= 0) {
3552 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003553 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003554 return 0;
3555 }
3556
3557 first_param = temp->str;
3558 end_params = temp->str + reql;
3559 cur_param = next_param = end_params;
3560 *end_params = '\0';
3561
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003562 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003563
3564 /*
3565 * Parse the parameters in reverse order to only store the last value.
3566 * From the html form, the backend and the action are at the end.
3567 */
3568 while (cur_param > first_param) {
3569 char *value;
3570 int poffset, plen;
3571
3572 cur_param--;
3573
3574 if ((*cur_param == '&') || (cur_param == first_param)) {
3575 reprocess_servers:
3576 /* Parse the key */
3577 poffset = (cur_param != first_param ? 1 : 0);
3578 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3579 if ((plen > 0) && (plen <= sizeof(key))) {
3580 strncpy(key, cur_param + poffset, plen);
3581 key[plen - 1] = '\0';
3582 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003583 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003584 goto out;
3585 }
3586
3587 /* Parse the value */
3588 value = key;
3589 while (*value != '\0' && *value != '=') {
3590 value++;
3591 }
3592 if (*value == '=') {
3593 /* Ok, a value is found, we can mark the end of the key */
3594 *value++ = '\0';
3595 }
3596 if (url_decode(key) < 0 || url_decode(value) < 0)
3597 break;
3598
3599 /* Now we can check the key to see what to do */
3600 if (!px && (strcmp(key, "b") == 0)) {
3601 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
3602 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003603 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003604 goto out;
3605 }
3606 }
3607 else if (!action && (strcmp(key, "action") == 0)) {
3608 if (strcmp(value, "disable") == 0) {
3609 action = ST_ADM_ACTION_DISABLE;
3610 }
3611 else if (strcmp(value, "enable") == 0) {
3612 action = ST_ADM_ACTION_ENABLE;
3613 }
3614 else if (strcmp(value, "stop") == 0) {
3615 action = ST_ADM_ACTION_STOP;
3616 }
3617 else if (strcmp(value, "start") == 0) {
3618 action = ST_ADM_ACTION_START;
3619 }
3620 else if (strcmp(value, "shutdown") == 0) {
3621 action = ST_ADM_ACTION_SHUTDOWN;
3622 }
3623 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003624 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003625 goto out;
3626 }
3627 }
3628 else if (strcmp(key, "s") == 0) {
3629 if (!(px && action)) {
3630 /*
3631 * Indicates that we'll need to reprocess the parameters
3632 * as soon as backend and action are known
3633 */
3634 if (!reprocess) {
3635 st_cur_param = cur_param;
3636 st_next_param = next_param;
3637 }
3638 reprocess = 1;
3639 }
3640 else if ((sv = findserver(px, value)) != NULL) {
3641 switch (action) {
3642 case ST_ADM_ACTION_DISABLE:
3643 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
3644 /* Not already in maintenance, we can change the server state */
3645 sv->state |= SRV_MAINTAIN;
3646 set_server_down(&sv->check);
3647 altered_servers++;
3648 total_servers++;
3649 }
3650 break;
3651 case ST_ADM_ACTION_ENABLE:
3652 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
3653 /* Already in maintenance, we can change the server state */
3654 set_server_up(&sv->check);
3655 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
3656 altered_servers++;
3657 total_servers++;
3658 }
3659 break;
3660 case ST_ADM_ACTION_STOP:
3661 case ST_ADM_ACTION_START:
3662 if (action == ST_ADM_ACTION_START)
3663 sv->uweight = sv->iweight;
3664 else
3665 sv->uweight = 0;
3666
3667 server_recalc_eweight(sv);
3668 set_server_drain_state(sv);
3669
3670 altered_servers++;
3671 total_servers++;
3672 break;
3673 case ST_ADM_ACTION_SHUTDOWN:
3674 if (px->state != PR_STSTOPPED) {
3675 struct session *sess, *sess_bck;
3676
3677 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3678 if (sess->srv_conn == sv)
3679 session_shutdown(sess, SN_ERR_KILLED);
3680
3681 altered_servers++;
3682 total_servers++;
3683 }
3684 break;
3685 }
3686 } else {
3687 /* the server name is unknown or ambiguous (duplicate names) */
3688 total_servers++;
3689 }
3690 }
3691 if (reprocess && px && action) {
3692 /* Now, we know the backend and the action chosen by the user.
3693 * We can safely restart from the first server parameter
3694 * to reprocess them
3695 */
3696 cur_param = st_cur_param;
3697 next_param = st_next_param;
3698 reprocess = 0;
3699 goto reprocess_servers;
3700 }
3701
3702 next_param = cur_param;
3703 }
3704 }
3705
3706 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003707 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003708 }
3709 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003710 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003711 }
3712 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003713 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003714 }
3715 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003716 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003717 }
3718 out:
3719 return 1;
3720}
3721
3722
3723static int stats_send_http_headers(struct stream_interface *si)
3724{
3725 struct session *s = session_from_task(si->owner);
3726 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003727 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003728
3729 chunk_printf(&trash,
3730 "HTTP/1.0 200 OK\r\n"
3731 "Cache-Control: no-cache\r\n"
3732 "Connection: close\r\n"
3733 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003734 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01003735
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003736 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01003737 chunk_appendf(&trash, "Refresh: %d\r\n",
3738 uri->refresh);
3739
3740 chunk_appendf(&trash, "\r\n");
3741
3742 s->txn.status = 200;
3743 s->logs.tv_request = now;
3744
3745 if (bi_putchk(si->ib, &trash) == -1)
3746 return 0;
3747
3748 return 1;
3749}
3750
3751static int stats_send_http_redirect(struct stream_interface *si)
3752{
3753 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3754 struct session *s = session_from_task(si->owner);
3755 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003756 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003757
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003758 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003759 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003760 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003761 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003762 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3763 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003764 }
3765
3766 /* We don't want to land on the posted stats page because a refresh will
3767 * repost the data. We don't want this to happen on accident so we redirect
3768 * the browse to the stats page with a GET.
3769 */
3770 chunk_printf(&trash,
3771 "HTTP/1.1 303 See Other\r\n"
3772 "Cache-Control: no-cache\r\n"
3773 "Content-Type: text/plain\r\n"
3774 "Connection: close\r\n"
3775 "Location: %s;st=%s%s%s%s\r\n"
3776 "\r\n",
3777 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003778 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3779 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3780 stat_status_codes[appctx->ctx.stats.st_code]) ?
3781 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01003782 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003783 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3784 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01003785 scope_txt);
3786
3787 s->txn.status = 303;
3788 s->logs.tv_request = now;
3789
3790 if (bi_putchk(si->ib, &trash) == -1)
3791 return 0;
3792
3793 return 1;
3794}
3795
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003796/* This I/O handler runs as an applet embedded in a stream interface. It is
3797 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003798 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003799 * automatically unregisters itself once transfer is complete.
3800 */
3801static void http_stats_io_handler(struct stream_interface *si)
3802{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003803 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003804 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003805 struct channel *req = si->ob;
3806 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003807
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3809 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003810
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003811 /* check that the output is not closed */
3812 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003813 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003814
Willy Tarreau347a35d2013-11-22 17:51:09 +01003815 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003816 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003817 if (stats_send_http_headers(si)) {
3818 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003819 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003820 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003821 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003822 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003823 }
3824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003825 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003826 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003827 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003830 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003831 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003832 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003833 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003834 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003835 }
3836
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003837 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003838 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003839 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003840 }
3841
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003842 if (appctx->st0 == STAT_HTTP_DONE)
Willy Tarreau347a35d2013-11-22 17:51:09 +01003843 si_shutw(si);
3844
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003845 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3846 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003847
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003848 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01003849 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3850 si_shutr(si);
3851 res->flags |= CF_READ_NULL;
3852 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003853 }
Willy Tarreau91861262007-10-17 17:06:05 +02003854
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003855 /* update all other flags and resync with the other side */
3856 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003858 /* we don't want to expire timeouts while we're processing requests */
3859 si->ib->rex = TICK_ETERNITY;
3860 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003861
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003862 out:
3863 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3864 /* check that we have released everything then unregister */
3865 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003866 }
3867}
3868
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869
Willy Tarreau909d5172012-11-26 03:04:41 +01003870static inline const char *get_conn_ctrl_name(const struct connection *conn)
3871{
Willy Tarreauf79c8172013-10-21 16:30:56 +02003872 if (!(conn->flags & CO_FL_CTRL_READY) || !conn->ctrl)
Willy Tarreau909d5172012-11-26 03:04:41 +01003873 return "NONE";
3874 return conn->ctrl->name;
3875}
3876
3877static inline const char *get_conn_xprt_name(const struct connection *conn)
3878{
3879 static char ptr[17];
3880
Willy Tarreauf79c8172013-10-21 16:30:56 +02003881 if (!(conn->flags & CO_FL_XPRT_READY) || !conn->xprt)
Willy Tarreau909d5172012-11-26 03:04:41 +01003882 return "NONE";
3883
3884 if (conn->xprt == &raw_sock)
3885 return "RAW";
3886
3887#ifdef USE_OPENSSL
3888 if (conn->xprt == &ssl_sock)
3889 return "SSL";
3890#endif
3891 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3892 return ptr;
3893}
3894
3895static inline const char *get_conn_data_name(const struct connection *conn)
3896{
3897 static char ptr[17];
3898
3899 if (!conn->data)
3900 return "NONE";
3901
3902 if (conn->data == &sess_conn_cb)
3903 return "SESS";
3904
3905 if (conn->data == &si_conn_cb)
3906 return "STRM";
3907
3908 if (conn->data == &check_conn_cb)
3909 return "CHCK";
3910
3911 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3912 return ptr;
3913}
3914
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003915/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003916 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003917 * 0 if the output buffer is full and it needs to be called again, otherwise
3918 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003919 */
Willy Tarreau76153662012-11-26 01:16:39 +01003920static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003921{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003922 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003923 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003924 extern const char *monthname[12];
3925 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003926 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01003927 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003929 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003930
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003931 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003932 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003933 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3934 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003935 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003936 appctx->ctx.sess.uid = 0;
3937 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003938 return 1;
3939 }
3940
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003941 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003942 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003943 appctx->ctx.sess.uid = sess->uniq_id;
3944 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003945 /* fall through */
3946
3947 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003948 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003949 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003950 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003951 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003952 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3953 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003954 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003955 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003956
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003957 conn = objt_conn(sess->si[0].end);
3958 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003959 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003960 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003961 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003962 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003963 break;
3964 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003965 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003966 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003967 default:
3968 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003969 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003970 break;
3971 }
3972
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003973 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003974 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003975 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003976
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003977 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003978 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003979 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3980 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3981 sess->listener ? sess->listener->luid : 0);
3982
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003983 if (conn)
3984 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003985
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003986 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003987 case AF_INET:
3988 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003989 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003990 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003991 break;
3992 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003993 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003994 break;
3995 default:
3996 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003997 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003998 break;
3999 }
4000
Willy Tarreau50943332011-09-02 17:33:05 +02004001 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004002 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004003 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004004 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004005 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4006 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004007 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004008
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004009 conn = objt_conn(sess->si[1].end);
4010 if (conn)
4011 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004012
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004013 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004014 case AF_INET:
4015 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004016 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004017 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004018 break;
4019 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004020 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004021 break;
4022 default:
4023 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004024 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004025 break;
4026 }
4027
4028 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004029 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004030 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004031 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4032 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004033 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004034 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004035
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004036 if (conn)
4037 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004038
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004039 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004040 case AF_INET:
4041 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004042 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004043 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004044 break;
4045 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004046 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004047 break;
4048 default:
4049 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004050 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004051 break;
4052 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004053
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004054 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004055 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004056 sess->task,
4057 sess->task->state,
4058 sess->task->nice, sess->task->calls,
4059 sess->task->expire ?
4060 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4061 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4062 TICKS_TO_MS(1000)) : "<NEVER>",
4063 task_in_rq(sess->task) ? ", running" : "");
4064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004065 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004066 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004067 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4068
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004069 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004070 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4071 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4072 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4073
4074 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004075 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004076 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004077 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004078 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004079 obj_type_name(sess->si[0].end),
4080 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004081 sess->si[0].exp ?
4082 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4083 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4084 TICKS_TO_MS(1000)) : "<NEVER>",
4085 sess->si[0].err_type);
4086
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004087 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004088 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004089 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004090 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004091 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004092 obj_type_name(sess->si[1].end),
4093 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004094 sess->si[1].exp ?
4095 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4096 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4097 TICKS_TO_MS(1000)) : "<NEVER>",
4098 sess->si[1].err_type);
4099
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004100 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004101 chunk_appendf(&trash,
4102 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004103 conn,
4104 get_conn_ctrl_name(conn),
4105 get_conn_xprt_name(conn),
4106 get_conn_data_name(conn),
4107 obj_type_name(conn->target),
4108 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004109
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004110 chunk_appendf(&trash,
4111 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004112 conn->flags,
4113 conn->t.sock.fd,
4114 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4115 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4116 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004117 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004118 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4119 chunk_appendf(&trash,
4120 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4121 tmpctx,
4122 tmpctx->st0,
4123 tmpctx->st1,
4124 tmpctx->st2,
4125 tmpctx->applet->name);
4126 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004127
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004128 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004129 chunk_appendf(&trash,
4130 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004131 conn,
4132 get_conn_ctrl_name(conn),
4133 get_conn_xprt_name(conn),
4134 get_conn_data_name(conn),
4135 obj_type_name(conn->target),
4136 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004137
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004138 chunk_appendf(&trash,
4139 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004140 conn->flags,
4141 conn->t.sock.fd,
4142 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4143 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4144 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004145 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004146 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4147 chunk_appendf(&trash,
4148 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4149 tmpctx,
4150 tmpctx->st0,
4151 tmpctx->st1,
4152 tmpctx->st2,
4153 tmpctx->applet->name);
4154 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004155
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004156 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004157 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004158 " an_exp=%s",
4159 sess->req,
4160 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004161 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004162 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004163 sess->req->analyse_exp ?
4164 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4165 TICKS_TO_MS(1000)) : "<NEVER>");
4166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004167 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004168 " rex=%s",
4169 sess->req->rex ?
4170 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4171 TICKS_TO_MS(1000)) : "<NEVER>");
4172
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004173 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004174 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004175 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004176 sess->req->wex ?
4177 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4178 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004179 sess->req->buf,
4180 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004181 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004182 sess->txn.req.next, sess->req->buf->i,
4183 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004184
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004185 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004186 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004187 " an_exp=%s",
4188 sess->rep,
4189 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004190 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004191 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004192 sess->rep->analyse_exp ?
4193 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4194 TICKS_TO_MS(1000)) : "<NEVER>");
4195
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004196 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004197 " rex=%s",
4198 sess->rep->rex ?
4199 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4200 TICKS_TO_MS(1000)) : "<NEVER>");
4201
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004202 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004203 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004204 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004205 sess->rep->wex ?
4206 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4207 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004208 sess->rep->buf,
4209 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004210 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004211 sess->txn.rsp.next, sess->rep->buf->i,
4212 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004214 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004215 return 0;
4216
4217 /* use other states to dump the contents */
4218 }
4219 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004220 appctx->ctx.sess.uid = 0;
4221 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004222 return 1;
4223}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004224
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004225/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004226 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004227 * to be called again, otherwise non-zero. It is designed to be called
4228 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004229 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004230static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004231{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004232 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004233 struct connection *conn;
4234
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004235 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004236 /* If we're forced to shut down, we might have to remove our
4237 * reference to the last session being dumped.
4238 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004239 if (appctx->st2 == STAT_ST_LIST) {
4240 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4241 LIST_DEL(&appctx->ctx.sess.bref.users);
4242 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004243 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004244 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004245 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004246 }
4247
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004248 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004249
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004250 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004251 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004252 /* the function had not been called yet, let's prepare the
4253 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004254 * pointer to the first in the global list. When a target
4255 * session is being destroyed, it is responsible for updating
4256 * this pointer. We know we have reached the end when this
4257 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004258 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004259 LIST_INIT(&appctx->ctx.sess.bref.users);
4260 appctx->ctx.sess.bref.ref = sessions.n;
4261 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004262 /* fall through */
4263
Willy Tarreau295a8372011-03-10 11:25:07 +01004264 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004265 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004266 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4267 LIST_DEL(&appctx->ctx.sess.bref.users);
4268 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004269 }
4270
4271 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004272 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004273 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004274 struct session *curr_sess;
4275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004276 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004277
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004278 if (appctx->ctx.sess.target) {
4279 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004280 goto next_sess;
4281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004282 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004283 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01004284 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004285 return 0;
4286
4287 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004288 LIST_DEL(&appctx->ctx.sess.bref.users);
4289 LIST_INIT(&appctx->ctx.sess.bref.users);
4290 if (appctx->ctx.sess.target != (void *)-1) {
4291 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01004292 break;
4293 }
4294 else
4295 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004296 }
4297
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004298 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004299 "%p: proto=%s",
4300 curr_sess,
4301 curr_sess->listener->proto->name);
4302
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004303
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004304 conn = objt_conn(curr_sess->si[0].end);
4305 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02004306 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004307 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004308 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004309 " src=%s:%d fe=%s be=%s srv=%s",
4310 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004311 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004312 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004313 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004314 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004315 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004316 break;
4317 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004318 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02004319 " src=unix:%d fe=%s be=%s srv=%s",
4320 curr_sess->listener->luid,
4321 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004322 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004323 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02004324 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004325 break;
4326 }
4327
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004328 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02004329 " ts=%02x age=%s calls=%d",
4330 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01004331 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
4332 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004333
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004334 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004335 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004336 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004337 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004338 curr_sess->req->analysers,
4339 curr_sess->req->rex ?
4340 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
4341 TICKS_TO_MS(1000)) : "");
4342
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004343 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004344 ",wx=%s",
4345 curr_sess->req->wex ?
4346 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
4347 TICKS_TO_MS(1000)) : "");
4348
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004349 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004350 ",ax=%s]",
4351 curr_sess->req->analyse_exp ?
4352 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
4353 TICKS_TO_MS(1000)) : "");
4354
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004355 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004356 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004357 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004358 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004359 curr_sess->rep->analysers,
4360 curr_sess->rep->rex ?
4361 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
4362 TICKS_TO_MS(1000)) : "");
4363
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004364 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004365 ",wx=%s",
4366 curr_sess->rep->wex ?
4367 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
4368 TICKS_TO_MS(1000)) : "");
4369
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004370 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004371 ",ax=%s]",
4372 curr_sess->rep->analyse_exp ?
4373 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
4374 TICKS_TO_MS(1000)) : "");
4375
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004376 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004377 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004378 " s0=[%d,%1xh,fd=%d,ex=%s]",
4379 curr_sess->si[0].state,
4380 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004381 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004382 curr_sess->si[0].exp ?
4383 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4384 TICKS_TO_MS(1000)) : "");
4385
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004386 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004387 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004388 " s1=[%d,%1xh,fd=%d,ex=%s]",
4389 curr_sess->si[1].state,
4390 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004391 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004392 curr_sess->si[1].exp ?
4393 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4394 TICKS_TO_MS(1000)) : "");
4395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004396 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004397 " exp=%s",
4398 curr_sess->task->expire ?
4399 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4400 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004401 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004402 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004403
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004404 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004405
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004406 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004407 /* let's try again later from this session. We add ourselves into
4408 * this session's users so that it can remove us upon termination.
4409 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004410 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004411 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004412 }
4413
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004414 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004415 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004416 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004417
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004418 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004419 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004420 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004421 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004422 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004423 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004424
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004425 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004426 return 0;
4427
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004428 appctx->ctx.sess.target = NULL;
4429 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004430 return 1;
4431 }
4432
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004433 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004434 /* fall through */
4435
4436 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004437 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004438 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004439 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004440}
4441
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004442/* This is called when the stream interface is closed. For instance, upon an
4443 * external abort, we won't call the i/o handler anymore so we may need to
4444 * remove back references to the session currently being dumped.
4445 */
Simon Horman74d88312013-02-12 10:45:50 +09004446static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004447{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004448 struct appctx *appctx = __objt_appctx(si->end);
4449
4450 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
4451 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
4452 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004453 }
4454}
4455
Willy Tarreau20e99322013-04-13 09:22:25 +02004456/* This function is used to either dump tables states (when action is set
4457 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02004458 * It returns 0 if the output buffer is full and it needs to be called
4459 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004460 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004461static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004462{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004463 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004464 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004465 struct ebmb_node *eb;
4466 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004467 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004468 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004469
4470 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004471 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01004472 * - STAT_ST_INIT : the first call
4473 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004474 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004475 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004476 * and the entry pointer points to the next entry to be dumped,
4477 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004478 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004479 * data though.
4480 */
4481
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004482 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004483 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 if (appctx->st2 == STAT_ST_LIST) {
4485 appctx->ctx.table.entry->ref_cnt--;
4486 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004487 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004488 return 1;
4489 }
4490
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004491 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004493 while (appctx->st2 != STAT_ST_FIN) {
4494 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004495 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004496 appctx->ctx.table.proxy = appctx->ctx.table.target;
4497 if (!appctx->ctx.table.proxy)
4498 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004499
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004500 appctx->ctx.table.entry = NULL;
4501 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004502 break;
4503
Willy Tarreau295a8372011-03-10 11:25:07 +01004504 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004505 if (!appctx->ctx.table.proxy ||
4506 (appctx->ctx.table.target &&
4507 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
4508 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004509 break;
4510 }
4511
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004512 if (appctx->ctx.table.proxy->table.size) {
4513 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
4514 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004515 return 0;
4516
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004517 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004518 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004519 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004521 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004522 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4523 appctx->ctx.table.entry->ref_cnt++;
4524 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004525 break;
4526 }
4527 }
4528 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004529 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004530 break;
4531
Willy Tarreau295a8372011-03-10 11:25:07 +01004532 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004533 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004534
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004535 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004536 /* we're filtering on some data contents */
4537 void *ptr;
4538 long long data;
4539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004540 dt = appctx->ctx.table.data_type;
4541 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
4542 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004543 dt);
4544
4545 data = 0;
4546 switch (stktable_data_types[dt].std_type) {
4547 case STD_T_SINT:
4548 data = stktable_data_cast(ptr, std_t_sint);
4549 break;
4550 case STD_T_UINT:
4551 data = stktable_data_cast(ptr, std_t_uint);
4552 break;
4553 case STD_T_ULL:
4554 data = stktable_data_cast(ptr, std_t_ull);
4555 break;
4556 case STD_T_FRQP:
4557 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004558 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004559 break;
4560 }
4561
4562 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004563 if ((data < appctx->ctx.table.value &&
4564 (appctx->ctx.table.data_op == STD_OP_EQ ||
4565 appctx->ctx.table.data_op == STD_OP_GT ||
4566 appctx->ctx.table.data_op == STD_OP_GE)) ||
4567 (data == appctx->ctx.table.value &&
4568 (appctx->ctx.table.data_op == STD_OP_NE ||
4569 appctx->ctx.table.data_op == STD_OP_GT ||
4570 appctx->ctx.table.data_op == STD_OP_LT)) ||
4571 (data > appctx->ctx.table.value &&
4572 (appctx->ctx.table.data_op == STD_OP_EQ ||
4573 appctx->ctx.table.data_op == STD_OP_LT ||
4574 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004575 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004576 }
4577
Simon Hormanc88b8872011-06-15 15:18:49 +09004578 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004579 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
4580 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09004581 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004583 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004585 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004586 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004587 struct stksess *old = appctx->ctx.table.entry;
4588 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004589 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004590 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
4591 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
4592 stksess_kill(&appctx->ctx.table.proxy->table, old);
4593 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004594 break;
4595 }
4596
Simon Hormanc88b8872011-06-15 15:18:49 +09004597
4598 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004599 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
4600 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
4601 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09004602
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004603 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
4604 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004605 break;
4606
Willy Tarreau295a8372011-03-10 11:25:07 +01004607 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004608 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004609 break;
4610 }
4611 }
4612 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004613}
4614
Willy Tarreaud426a182010-03-05 14:58:26 +01004615/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004616 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4617 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4618 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4619 * lines are respected within the limit of 70 output chars. Lines that are
4620 * continuation of a previous truncated line begin with "+" instead of " "
4621 * after the offset. The new pointer is returned.
4622 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004623static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4624 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004625{
4626 int end;
4627 unsigned char c;
4628
4629 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004630 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004631 return ptr;
4632
Willy Tarreau77804732012-10-29 16:14:26 +01004633 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004634
Willy Tarreaud426a182010-03-05 14:58:26 +01004635 while (ptr < len && ptr < bsize) {
4636 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004637 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004638 if (out->len > end - 2)
4639 break;
4640 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004641 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004642 if (out->len > end - 3)
4643 break;
4644 out->str[out->len++] = '\\';
4645 switch (c) {
4646 case '\t': c = 't'; break;
4647 case '\n': c = 'n'; break;
4648 case '\r': c = 'r'; break;
4649 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004650 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004651 }
4652 out->str[out->len++] = c;
4653 } else {
4654 if (out->len > end - 5)
4655 break;
4656 out->str[out->len++] = '\\';
4657 out->str[out->len++] = 'x';
4658 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4659 out->str[out->len++] = hextab[c & 0xF];
4660 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004661 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004662 /* we had a line break, let's return now */
4663 out->str[out->len++] = '\n';
4664 *line = ptr;
4665 return ptr;
4666 }
4667 }
4668 /* we have an incomplete line, we return it as-is */
4669 out->str[out->len++] = '\n';
4670 return ptr;
4671}
4672
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004673/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004674 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004675 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004676 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004677static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004678{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004679 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004680 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004681
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004682 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004683 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004684
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004685 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004686
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004687 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004688 /* the function had not been called yet, let's prepare the
4689 * buffer for a response.
4690 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004691 struct tm tm;
4692
4693 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004694 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004695 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4696 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4697 error_snapshot_id);
4698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004699 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004700 /* Socket buffer full. Let's try again later from the same point */
4701 return 0;
4702 }
4703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004704 appctx->ctx.errors.px = proxy;
4705 appctx->ctx.errors.buf = 0;
4706 appctx->ctx.errors.bol = 0;
4707 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004708 }
4709
4710 /* we have two inner loops here, one for the proxy, the other one for
4711 * the buffer.
4712 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004713 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004714 struct error_snapshot *es;
4715
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004716 if (appctx->ctx.errors.buf == 0)
4717 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004718 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004719 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004720
4721 if (!es->when.tv_sec)
4722 goto next;
4723
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004724 if (appctx->ctx.errors.iid >= 0 &&
4725 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
4726 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004727 goto next;
4728
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004729 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004730 /* just print headers now */
4731
4732 char pn[INET6_ADDRSTRLEN];
4733 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004734 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004735
4736 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004737 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004738 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004739 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004740
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004741 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4742 case AF_INET:
4743 case AF_INET6:
4744 port = get_host_port(&es->src);
4745 break;
4746 default:
4747 port = 0;
4748 }
4749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004750 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004751 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004752 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004753 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004754 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004755 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004756 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4757 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004758 break;
4759 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004760 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004761 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004762 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004763 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004764 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004765 break;
4766 }
4767
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004768 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004769 ", server %s (#%d), event #%u\n"
4770 " src %s:%d, session #%d, session flags 0x%08x\n"
4771 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4772 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4773 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4774 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4775 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4776 es->ev_id,
4777 pn, port, es->sid, es->s_flags,
4778 es->state, es->m_flags, es->t_flags,
4779 es->m_clen, es->m_blen,
4780 es->b_flags, es->b_out, es->b_tot,
4781 es->len, es->b_wrap, es->pos);
4782
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004783 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004784 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004785 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004786 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004787 appctx->ctx.errors.ptr = 0;
4788 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004789 }
4790
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004791 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004792 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004793 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004794 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004795 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004796 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004797 goto next;
4798 }
4799
4800 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004801 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004802 int newptr;
4803 int newline;
4804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004805 newline = appctx->ctx.errors.bol;
4806 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
4807 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004808 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004809
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004810 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004811 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004812 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004813 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004814 appctx->ctx.errors.ptr = newptr;
4815 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004816 };
4817 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004818 appctx->ctx.errors.bol = 0;
4819 appctx->ctx.errors.ptr = -1;
4820 appctx->ctx.errors.buf++;
4821 if (appctx->ctx.errors.buf > 1) {
4822 appctx->ctx.errors.buf = 0;
4823 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004824 }
4825 }
4826
4827 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004828 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004829}
4830
Willy Tarreaud5781202012-09-22 19:32:35 +02004831/* parse the "level" argument on the bind lines */
4832static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4833{
4834 if (!*args[cur_arg + 1]) {
4835 memprintf(err, "'%s' : missing level", args[cur_arg]);
4836 return ERR_ALERT | ERR_FATAL;
4837 }
4838
4839 if (!strcmp(args[cur_arg+1], "user"))
4840 conf->level = ACCESS_LVL_USER;
4841 else if (!strcmp(args[cur_arg+1], "operator"))
4842 conf->level = ACCESS_LVL_OPER;
4843 else if (!strcmp(args[cur_arg+1], "admin"))
4844 conf->level = ACCESS_LVL_ADMIN;
4845 else {
4846 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4847 args[cur_arg], args[cur_arg+1]);
4848 return ERR_ALERT | ERR_FATAL;
4849 }
4850
4851 return 0;
4852}
4853
Willy Tarreaub24281b2011-02-13 13:16:36 +01004854struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004855 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004856 .name = "<STATS>", /* used for logging */
4857 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004858 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004859};
4860
Simon Horman9bd2c732011-06-15 15:18:44 +09004861static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004862 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004863 .name = "<CLI>", /* used for logging */
4864 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004865 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004866};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004867
Willy Tarreaudc13c112013-06-21 23:16:39 +02004868static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004869 { CFG_GLOBAL, "stats", stats_parse_global },
4870 { 0, NULL, NULL },
4871}};
4872
Willy Tarreaud5781202012-09-22 19:32:35 +02004873static struct bind_kw_list bind_kws = { "STAT", { }, {
4874 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4875 { NULL, NULL, 0 },
4876}};
4877
Willy Tarreau10522fd2008-07-09 20:12:41 +02004878__attribute__((constructor))
4879static void __dumpstats_module_init(void)
4880{
4881 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004882 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004883}
4884
Willy Tarreau91861262007-10-17 17:06:05 +02004885/*
4886 * Local variables:
4887 * c-indent-level: 8
4888 * c-basic-offset: 8
4889 * End:
4890 */