blob: 1b6fe3d343a4d9ca3ccc56af964675a23bddc88b [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 struct appctx *appctx;
160
161 s->target = &cli_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +0100162 appctx = stream_int_register_handler(&s->si[1], objt_applet(s->target));
163 if (!appctx)
164 return -1;
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100165 appctx->st1 = 0;
166 appctx->st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200167
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200168 tv_zero(&s->logs.tv_request);
169 s->logs.t_queue = 0;
170 s->logs.t_connect = 0;
171 s->logs.t_data = 0;
172 s->logs.t_close = 0;
173 s->logs.bytes_in = s->logs.bytes_out = 0;
174 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
175 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200176
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200177 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200178
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200179 if (s->listener->timeout) {
180 s->req->rto = *s->listener->timeout;
181 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200182 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200183 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200184}
185
Willy Tarreau07e9e642010-08-17 21:48:17 +0200186/* allocate a new stats frontend named <name>, and return it
187 * (or NULL in case of lack of memory).
188 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200189static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200190{
191 struct proxy *fe;
192
193 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
194 if (!fe)
195 return NULL;
196
Willy Tarreau237250c2011-07-29 01:49:03 +0200197 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200198 fe->next = proxy;
199 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200200 fe->last_change = now.tv_sec;
201 fe->id = strdup("GLOBAL");
202 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200203 fe->maxconn = 10; /* default to 10 concurrent connections */
204 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200205 fe->conf.file = strdup(file);
206 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200207 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200208
209 /* the stats frontend is the only one able to assign ID #0 */
210 fe->conf.id.key = fe->uuid = 0;
211 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200212 return fe;
213}
214
Willy Tarreaufbee7132007-10-18 13:53:22 +0200215/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200216 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
217 * error message into the <err> buffer which will be preallocated. The trailing
218 * '\n' must not be written. The function must be called with <args> pointing to
219 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200220 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200221static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200222 struct proxy *defpx, const char *file, int line,
223 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200224{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200225 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200226 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200227
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200228 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200229 int cur_arg;
230
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200231 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200232 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 +0200233 return -1;
234 }
235
Willy Tarreau89a63132009-08-16 17:41:45 +0200236 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200237 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200238 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200239 return -1;
240 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200241 }
242
Willy Tarreau4348fad2012-09-20 16:48:07 +0200243 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200244 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200245
Willy Tarreauc53d4222012-09-20 20:19:28 +0200246 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
247 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
248 file, line, args[0], args[1], err && *err ? *err : "error");
249 return -1;
250 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200251
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200252 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200254 static int bind_dumped;
255 struct bind_kw *kw;
256
257 kw = bind_find_kw(args[cur_arg]);
258 if (kw) {
259 if (!kw->parse) {
260 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
261 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200262 return -1;
263 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200264
265 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
266 if (err && *err)
267 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
268 else
269 memprintf(err, "'%s %s' : error encountered while processing '%s'",
270 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200271 return -1;
272 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200273
274 cur_arg += 1 + kw->skip;
275 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200276 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200277
278 if (!bind_dumped) {
279 bind_dump_kws(err);
280 indent_msg(err, 4);
281 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200283
284 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
285 args[0], args[1], args[cur_arg],
286 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
287 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100289
Willy Tarreauc53d4222012-09-20 20:19:28 +0200290 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
291 l->maxconn = global.stats_fe->maxconn;
292 l->backlog = global.stats_fe->backlog;
293 l->timeout = &global.stats_fe->timeout.client;
294 l->accept = session_accept;
295 l->handler = process_session;
296 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
297 l->nice = -64; /* we want to boost priority for local stats */
298 global.maxsock += l->maxconn;
299 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100302 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304
305 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200306 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100307 return -1;
308 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100310 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200311 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200312 return -1;
313 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200314 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200315 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200316 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200317 return -1;
318 }
319 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200320 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200322 else if (!strcmp(args[1], "maxconn")) {
323 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324
325 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200326 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200327 return -1;
328 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200329
330 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200331 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200332 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200333 return -1;
334 }
335 }
336 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200337 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200338 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
339 int cur_arg = 2;
340 unsigned int set = 0;
341
Willy Tarreau91319572013-04-20 09:48:50 +0200342 if (!global.stats_fe) {
343 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
344 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
345 return -1;
346 }
347 }
348
Willy Tarreau35b7b162012-10-22 23:17:18 +0200349 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100350 unsigned int low, high;
351
Willy Tarreau35b7b162012-10-22 23:17:18 +0200352 if (strcmp(args[cur_arg], "all") == 0) {
353 set = 0;
354 break;
355 }
356 else if (strcmp(args[cur_arg], "odd") == 0) {
357 set |= 0x55555555;
358 }
359 else if (strcmp(args[cur_arg], "even") == 0) {
360 set |= 0xAAAAAAAA;
361 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100362 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100363 char *dash = strchr(args[cur_arg], '-');
364
365 low = high = str2uic(args[cur_arg]);
366 if (dash)
367 high = str2uic(dash + 1);
368
369 if (high < low) {
370 unsigned int swap = low;
371 low = high;
372 high = swap;
373 }
374
375 if (low < 1 || high > 32) {
376 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
377 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200378 return -1;
379 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100380
381 while (low <= high)
382 set |= 1 << (low++ - 1);
383 }
384 else {
385 memprintf(err,
386 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
387 args[0], args[1]);
388 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200389 }
390 cur_arg++;
391 }
392 global.stats_fe->bind_proc = set;
393 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200394 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200395 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200396 return -1;
397 }
398 return 0;
399}
400
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100401/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
402 * for clearing it if needed.
403 */
404static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100405{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100406 chunk_appendf(&trash,
407 "# pxname,svname,"
408 "qcur,qmax,"
409 "scur,smax,slim,stot,"
410 "bin,bout,"
411 "dreq,dresp,"
412 "ereq,econ,eresp,"
413 "wretr,wredis,"
414 "status,weight,act,bck,"
415 "chkfail,chkdown,lastchg,downtime,qlimit,"
416 "pid,iid,sid,throttle,lbtot,tracked,type,"
417 "rate,rate_lim,rate_max,"
418 "check_status,check_code,check_duration,"
419 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
420 "req_rate,req_rate_max,req_tot,"
421 "cli_abrt,srv_abrt,"
422 "comp_in,comp_out,comp_byp,comp_rsp,"
423 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100424}
425
Simon Hormand9366582011-06-15 15:18:45 +0900426/* print a string of text buffer to <out>. The format is :
427 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
428 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
429 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
430 */
431static int dump_text(struct chunk *out, const char *buf, int bsize)
432{
433 unsigned char c;
434 int ptr = 0;
435
436 while (buf[ptr] && ptr < bsize) {
437 c = buf[ptr];
438 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
439 if (out->len > out->size - 1)
440 break;
441 out->str[out->len++] = c;
442 }
443 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
444 if (out->len > out->size - 2)
445 break;
446 out->str[out->len++] = '\\';
447 switch (c) {
448 case ' ': c = ' '; break;
449 case '\t': c = 't'; break;
450 case '\n': c = 'n'; break;
451 case '\r': c = 'r'; break;
452 case '\e': c = 'e'; break;
453 case '\\': c = '\\'; break;
454 }
455 out->str[out->len++] = c;
456 }
457 else {
458 if (out->len > out->size - 4)
459 break;
460 out->str[out->len++] = '\\';
461 out->str[out->len++] = 'x';
462 out->str[out->len++] = hextab[(c >> 4) & 0xF];
463 out->str[out->len++] = hextab[c & 0xF];
464 }
465 ptr++;
466 }
467
468 return ptr;
469}
470
471/* print a buffer in hexa.
472 * Print stopped if <bsize> is reached, or if no more place in the chunk.
473 */
474static int dump_binary(struct chunk *out, const char *buf, int bsize)
475{
476 unsigned char c;
477 int ptr = 0;
478
479 while (ptr < bsize) {
480 c = buf[ptr];
481
482 if (out->len > out->size - 2)
483 break;
484 out->str[out->len++] = hextab[(c >> 4) & 0xF];
485 out->str[out->len++] = hextab[c & 0xF];
486
487 ptr++;
488 }
489 return ptr;
490}
491
492/* Dump the status of a table to a stream interface's
493 * read buffer. It returns 0 if the output buffer is full
494 * and needs to be called again, otherwise non-zero.
495 */
496static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
497 struct proxy *proxy, struct proxy *target)
498{
Willy Tarreau306f8302013-07-08 15:53:06 +0200499 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900500
Willy Tarreau77804732012-10-29 16:14:26 +0100501 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900502 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
503
504 /* any other information should be dumped here */
505
Willy Tarreau290e63a2012-09-20 18:07:14 +0200506 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100507 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900508
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200509 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900510 return 0;
511
512 return 1;
513}
514
515/* Dump the a table entry to a stream interface's
516 * read buffer. It returns 0 if the output buffer is full
517 * and needs to be called again, otherwise non-zero.
518 */
519static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
520 struct proxy *proxy, struct stksess *entry)
521{
522 int dt;
523
Willy Tarreau77804732012-10-29 16:14:26 +0100524 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900525
526 if (proxy->table.type == STKTABLE_TYPE_IP) {
527 char addr[INET_ADDRSTRLEN];
528 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100529 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900530 }
531 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
532 char addr[INET6_ADDRSTRLEN];
533 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900535 }
536 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900538 }
539 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100540 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900541 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
542 }
543 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100544 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900545 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
546 }
547
Willy Tarreau77804732012-10-29 16:14:26 +0100548 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900549
550 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
551 void *ptr;
552
553 if (proxy->table.data_ofs[dt] == 0)
554 continue;
555 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900557 else
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900559
560 ptr = stktable_data_ptr(&proxy->table, entry, dt);
561 switch (stktable_data_types[dt].std_type) {
562 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900564 break;
565 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900567 break;
568 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100569 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900570 break;
571 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100572 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900573 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
574 proxy->table.data_arg[dt].u));
575 break;
576 }
577 }
Willy Tarreau77804732012-10-29 16:14:26 +0100578 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900579
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200580 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900581 return 0;
582
583 return 1;
584}
585
Willy Tarreaudec98142012-06-06 23:37:08 +0200586static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900587{
Willy Tarreau306f8302013-07-08 15:53:06 +0200588 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100589 struct appctx *appctx = __objt_appctx(si->end);
590 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900591 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900592 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900593 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200594 long long value;
595 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200596 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200597 void *ptr;
598 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900599
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100600 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900601
602 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100603 appctx->ctx.cli.msg = "Key value expected\n";
604 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900605 return;
606 }
607
Simon Hormanc5b89f62011-06-15 15:18:50 +0900608 switch (px->table.type) {
609 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900610 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100611 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900612 break;
613 case STKTABLE_TYPE_IPV6:
614 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100615 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900616 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900617 case STKTABLE_TYPE_INTEGER:
618 {
619 char *endptr;
620 unsigned long val;
621 errno = 0;
622 val = strtoul(args[4], &endptr, 10);
623 if ((errno == ERANGE && val == ULONG_MAX) ||
624 (errno != 0 && val == 0) || endptr == args[4] ||
625 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100626 appctx->ctx.cli.msg = "Invalid key\n";
627 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900628 return;
629 }
630 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100631 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900632 break;
633 }
634 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900635 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100636 static_table_key->key = args[4];
637 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900638 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900639 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200640 switch (action) {
641 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100642 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 +0200643 break;
644 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100645 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 +0200646 break;
647 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100648 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200649 break;
650 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100651 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900652 return;
653 }
654
655 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200656 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100657 appctx->ctx.cli.msg = stats_permission_denied_msg;
658 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900659 return;
660 }
661
Willy Tarreau07115412012-10-29 21:56:59 +0100662 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900663
Willy Tarreaudec98142012-06-06 23:37:08 +0200664 switch (action) {
665 case STAT_CLI_O_TAB:
666 if (!ts)
667 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100668 chunk_reset(&trash);
669 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900670 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100671 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900672 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200673
674 case STAT_CLI_O_CLR:
675 if (!ts)
676 return;
677 if (ts->ref_cnt) {
678 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100679 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
680 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200681 return;
682 }
683 stksess_kill(&px->table, ts);
684 break;
Simon Horman17bce342011-06-15 15:18:47 +0900685
Willy Tarreau654694e2012-06-07 01:03:16 +0200686 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200687 if (ts)
688 stktable_touch(&px->table, ts, 1);
689 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100690 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200691 if (!ts) {
692 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100693 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
694 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200695 return;
696 }
697 stktable_store(&px->table, ts, 1);
698 }
699
Willy Tarreau47060b62013-08-01 21:11:42 +0200700 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
701 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100702 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
703 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200704 return;
705 }
706
707 data_type = stktable_get_data_type(args[cur_arg] + 5);
708 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100709 appctx->ctx.cli.msg = "Unknown data type\n";
710 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200711 return;
712 }
713
714 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 appctx->ctx.cli.msg = "Data type not stored in this table\n";
716 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200717 return;
718 }
719
720 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100721 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
722 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200723 return;
724 }
725
726 ptr = stktable_data_ptr(&px->table, ts, data_type);
727
728 switch (stktable_data_types[data_type].std_type) {
729 case STD_T_SINT:
730 stktable_data_cast(ptr, std_t_sint) = value;
731 break;
732 case STD_T_UINT:
733 stktable_data_cast(ptr, std_t_uint) = value;
734 break;
735 case STD_T_ULL:
736 stktable_data_cast(ptr, std_t_ull) = value;
737 break;
738 case STD_T_FRQP:
739 /* We set both the current and previous values. That way
740 * the reported frequency is stable during all the period
741 * then slowly fades out. This allows external tools to
742 * push measures without having to update them too often.
743 */
744 frqp = &stktable_data_cast(ptr, std_t_frqp);
745 frqp->curr_tick = now_ms;
746 frqp->prev_ctr = 0;
747 frqp->curr_ctr = value;
748 break;
749 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200750 }
751 break;
752
Willy Tarreaudec98142012-06-06 23:37:08 +0200753 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100754 appctx->ctx.cli.msg = "Unknown action\n";
755 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200756 break;
Simon Horman121f3052011-06-15 15:18:46 +0900757 }
Simon Horman121f3052011-06-15 15:18:46 +0900758}
759
Willy Tarreau654694e2012-06-07 01:03:16 +0200760static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900761{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100762 struct appctx *appctx = __objt_appctx(si->end);
763
Willy Tarreau04b3a192013-04-13 09:41:37 +0200764 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100765 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
766 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200767 return;
768 }
769
Simon Hormand5b9fd92011-06-15 15:18:48 +0900770 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100771 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
772 if (appctx->ctx.table.data_type < 0) {
773 appctx->ctx.cli.msg = "Unknown data type\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 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
779 appctx->ctx.cli.msg = "Data type not stored in this table\n";
780 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900781 return;
782 }
783
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100784 appctx->ctx.table.data_op = get_std_op(args[4]);
785 if (appctx->ctx.table.data_op < 0) {
786 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
787 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900788 return;
789 }
790
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100791 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
792 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
793 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900794 return;
795 }
796}
797
Willy Tarreaudec98142012-06-06 23:37:08 +0200798static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900799{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100800 struct appctx *appctx = __objt_appctx(si->end);
801
802 appctx->ctx.table.data_type = -1;
803 appctx->st2 = STAT_ST_INIT;
804 appctx->ctx.table.target = NULL;
805 appctx->ctx.table.proxy = NULL;
806 appctx->ctx.table.entry = NULL;
807 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900808
809 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100810 appctx->ctx.table.target = find_stktable(args[2]);
811 if (!appctx->ctx.table.target) {
812 appctx->ctx.cli.msg = "No such table\n";
813 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900814 return;
815 }
816 }
817 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200818 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900819 goto err_args;
820 return;
821 }
822
823 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200824 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900825 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200826 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900827 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900828 goto err_args;
829
830 return;
831
832err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200833 switch (action) {
834 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 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 +0200836 break;
837 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 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 +0200839 break;
840 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100841 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200842 break;
843 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100844 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900845}
846
Willy Tarreau532a4502011-09-07 22:37:44 +0200847/* Expects to find a frontend named <arg> and returns it, otherwise displays various
848 * adequate error messages and returns NULL. This function also expects the session
849 * level to be admin.
850 */
851static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
852{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100853 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200854 struct proxy *px;
855
Willy Tarreau290e63a2012-09-20 18:07:14 +0200856 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 appctx->ctx.cli.msg = stats_permission_denied_msg;
858 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200859 return NULL;
860 }
861
862 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100863 appctx->ctx.cli.msg = "A frontend name is expected.\n";
864 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200865 return NULL;
866 }
867
868 px = findproxy(arg, PR_CAP_FE);
869 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100870 appctx->ctx.cli.msg = "No such frontend.\n";
871 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200872 return NULL;
873 }
874 return px;
875}
876
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200877/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
878 * and returns the pointer to the server. Otherwise, display adequate error messages
879 * and returns NULL. This function also expects the session level to be admin. Note:
880 * the <arg> is modified to remove the '/'.
881 */
882static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
883{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100884 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200885 struct proxy *px;
886 struct server *sv;
887 char *line;
888
Willy Tarreau290e63a2012-09-20 18:07:14 +0200889 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100890 appctx->ctx.cli.msg = stats_permission_denied_msg;
891 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200892 return NULL;
893 }
894
895 /* split "backend/server" and make <line> point to server */
896 for (line = arg; *line; line++)
897 if (*line == '/') {
898 *line++ = '\0';
899 break;
900 }
901
902 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200905 return NULL;
906 }
907
908 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100909 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200911 return NULL;
912 }
913
914 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100915 appctx->ctx.cli.msg = "Proxy is disabled.\n";
916 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200917 return NULL;
918 }
919
920 return sv;
921}
922
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200923/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200924 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100925 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200926 * designating the function which will have to process the request, which can
927 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200928 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900929static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200930{
Willy Tarreau306f8302013-07-08 15:53:06 +0200931 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100932 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200933 char *args[MAX_STATS_ARGS + 1];
934 int arg;
935
936 while (isspace((unsigned char)*line))
937 line++;
938
939 arg = 0;
940 args[arg] = line;
941
942 while (*line && arg < MAX_STATS_ARGS) {
943 if (isspace((unsigned char)*line)) {
944 *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
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100959 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200960 if (strcmp(args[0], "show") == 0) {
961 if (strcmp(args[1], "stat") == 0) {
962 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100963 appctx->ctx.stats.flags |= STAT_BOUND;
964 appctx->ctx.stats.iid = atoi(args[2]);
965 appctx->ctx.stats.type = atoi(args[3]);
966 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200967 }
968
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100969 appctx->st2 = STAT_ST_INIT;
970 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200971 }
972 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100973 appctx->st2 = STAT_ST_INIT;
974 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200975 }
976 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100977 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200978 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100979 appctx->ctx.cli.msg = stats_permission_denied_msg;
980 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200981 return 1;
982 }
Willy Tarreau76153662012-11-26 01:16:39 +0100983 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100984 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +0100985 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100986 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100987 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100988 appctx->ctx.sess.target = NULL;
989 appctx->ctx.sess.section = 0; /* start with session status */
990 appctx->ctx.sess.pos = 0;
991 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200992 }
993 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200994 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100995 appctx->ctx.cli.msg = stats_permission_denied_msg;
996 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200997 return 1;
998 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200999 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001000 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001001 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001002 appctx->ctx.errors.iid = -1;
1003 appctx->ctx.errors.px = NULL;
1004 appctx->st2 = STAT_ST_INIT;
1005 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001006 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001007 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001008 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001009 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001010 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001011 return 0;
1012 }
1013 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001014 else if (strcmp(args[0], "clear") == 0) {
1015 if (strcmp(args[1], "counters") == 0) {
1016 struct proxy *px;
1017 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001018 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001019 int clrall = 0;
1020
1021 if (strcmp(args[2], "all") == 0)
1022 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001023
Willy Tarreau6162db22009-10-10 17:13:00 +02001024 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001025 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1026 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001027 appctx->ctx.cli.msg = stats_permission_denied_msg;
1028 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001029 return 1;
1030 }
1031
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001032 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001033 if (clrall) {
1034 memset(&px->be_counters, 0, sizeof(px->be_counters));
1035 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1036 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001037 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001038 px->be_counters.conn_max = 0;
1039 px->be_counters.p.http.rps_max = 0;
1040 px->be_counters.sps_max = 0;
1041 px->be_counters.cps_max = 0;
1042 px->be_counters.nbpend_max = 0;
1043
1044 px->fe_counters.conn_max = 0;
1045 px->fe_counters.p.http.rps_max = 0;
1046 px->fe_counters.sps_max = 0;
1047 px->fe_counters.cps_max = 0;
1048 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001049 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001050
1051 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001052 if (clrall)
1053 memset(&sv->counters, 0, sizeof(sv->counters));
1054 else {
1055 sv->counters.cur_sess_max = 0;
1056 sv->counters.nbpend_max = 0;
1057 sv->counters.sps_max = 0;
1058 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001059
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001060 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001061 if (li->counters) {
1062 if (clrall)
1063 memset(li->counters, 0, sizeof(*li->counters));
1064 else
1065 li->counters->conn_max = 0;
1066 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001067 }
1068
Willy Tarreau81c25d02011-09-07 15:17:21 +02001069 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001070 return 1;
1071 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001072 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001073 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001074 /* end of processing */
1075 return 1;
1076 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001077 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001078 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001079 return 0;
1080 }
1081 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001082 else if (strcmp(args[0], "get") == 0) {
1083 if (strcmp(args[1], "weight") == 0) {
1084 struct proxy *px;
1085 struct server *sv;
1086
1087 /* split "backend/server" and make <line> point to server */
1088 for (line = args[2]; *line; line++)
1089 if (*line == '/') {
1090 *line++ = '\0';
1091 break;
1092 }
1093
1094 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001095 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1096 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001097 return 1;
1098 }
1099
1100 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001101 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1102 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001103 return 1;
1104 }
1105
1106 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001107 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1108 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001109 return 1;
1110 }
1111 else { /* not "get weight" */
1112 return 0;
1113 }
1114 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001115 else if (strcmp(args[0], "set") == 0) {
1116 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001117 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001118 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001119
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001120 sv = expect_server_admin(s, si, args[2]);
1121 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001122 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001123
Simon Horman7d09b9a2013-02-12 10:45:51 +09001124 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001125 /*
1126 * The user-weight may now be zero and thus
1127 * the server considered to be draining.
1128 * Update the server's drain state as necessary.
1129 */
1130 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001131 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001132 appctx->ctx.cli.msg = warning;
1133 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001134 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001135 return 1;
1136 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001137 else if (strcmp(args[1], "timeout") == 0) {
1138 if (strcmp(args[2], "cli") == 0) {
1139 unsigned timeout;
1140 const char *res;
1141
1142 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001143 appctx->ctx.cli.msg = "Expects an integer value.\n";
1144 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001145 return 1;
1146 }
1147
1148 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1149 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001150 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1151 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001152 return 1;
1153 }
1154
1155 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1156 return 1;
1157 }
1158 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001159 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1160 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001161 return 1;
1162 }
1163 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001164 else if (strcmp(args[1], "maxconn") == 0) {
1165 if (strcmp(args[2], "frontend") == 0) {
1166 struct proxy *px;
1167 struct listener *l;
1168 int v;
1169
Willy Tarreau532a4502011-09-07 22:37:44 +02001170 px = expect_frontend_admin(s, si, args[3]);
1171 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001172 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001173
1174 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001175 appctx->ctx.cli.msg = "Integer value expected.\n";
1176 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001177 return 1;
1178 }
1179
1180 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001181 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001182 appctx->ctx.cli.msg = "Value out of range.\n";
1183 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001184 return 1;
1185 }
1186
1187 /* OK, the value is fine, so we assign it to the proxy and to all of
1188 * its listeners. The blocked ones will be dequeued.
1189 */
1190 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001191 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001192 l->maxconn = v;
1193 if (l->state == LI_FULL)
1194 resume_listener(l);
1195 }
1196
1197 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1198 dequeue_all_listeners(&s->fe->listener_queue);
1199
1200 return 1;
1201 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001202 else if (strcmp(args[2], "global") == 0) {
1203 int v;
1204
Willy Tarreau290e63a2012-09-20 18:07:14 +02001205 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001206 appctx->ctx.cli.msg = stats_permission_denied_msg;
1207 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001208 return 1;
1209 }
1210
1211 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001212 appctx->ctx.cli.msg = "Expects an integer value.\n";
1213 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001214 return 1;
1215 }
1216
1217 v = atoi(args[3]);
1218 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001219 appctx->ctx.cli.msg = "Value out of range.\n";
1220 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001221 return 1;
1222 }
1223
1224 /* check for unlimited values */
1225 if (v <= 0)
1226 v = global.hardmaxconn;
1227
1228 global.maxconn = v;
1229
1230 /* Dequeues all of the listeners waiting for a resource */
1231 if (!LIST_ISEMPTY(&global_listener_queue))
1232 dequeue_all_listeners(&global_listener_queue);
1233
1234 return 1;
1235 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001236 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001237 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1238 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001239 return 1;
1240 }
1241 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001242 else if (strcmp(args[1], "rate-limit") == 0) {
1243 if (strcmp(args[2], "connections") == 0) {
1244 if (strcmp(args[3], "global") == 0) {
1245 int v;
1246
Willy Tarreau290e63a2012-09-20 18:07:14 +02001247 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001248 appctx->ctx.cli.msg = stats_permission_denied_msg;
1249 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001250 return 1;
1251 }
1252
1253 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001254 appctx->ctx.cli.msg = "Expects an integer value.\n";
1255 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001256 return 1;
1257 }
1258
1259 v = atoi(args[4]);
1260 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001261 appctx->ctx.cli.msg = "Value out of range.\n";
1262 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001263 return 1;
1264 }
1265
1266 global.cps_lim = v;
1267
1268 /* Dequeues all of the listeners waiting for a resource */
1269 if (!LIST_ISEMPTY(&global_listener_queue))
1270 dequeue_all_listeners(&global_listener_queue);
1271
1272 return 1;
1273 }
1274 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001275 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1276 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001277 return 1;
1278 }
1279 }
William Lallemandd85f9172012-11-09 17:05:39 +01001280 else if (strcmp(args[2], "http-compression") == 0) {
1281 if (strcmp(args[3], "global") == 0) {
1282 int v;
1283
Willy Tarreau85d47f92012-11-21 00:29:50 +01001284 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001285 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1286 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001287 return 1;
1288 }
1289
William Lallemandd85f9172012-11-09 17:05:39 +01001290 v = atoi(args[4]);
1291 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1292 }
1293 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001294 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1295 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001296 return 1;
1297 }
1298 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001299 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001300 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
1301 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001302 return 1;
1303 }
1304 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001305 else if (strcmp(args[1], "table") == 0) {
1306 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1307 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001308 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001309 return 0;
1310 }
1311 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001312 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001313 if (strcmp(args[1], "agent") == 0) {
1314 struct server *sv;
1315
1316 sv = expect_server_admin(s, si, args[2]);
1317 if (!sv)
1318 return 1;
1319
1320 sv->agent.state &= ~CHK_STATE_DISABLED;
1321
1322 return 1;
1323 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001324 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001325 struct server *sv;
1326
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001327 sv = expect_server_admin(s, si, args[2]);
1328 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001329 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001330
Cyril Bontécd19e512010-01-31 22:34:03 +01001331 if (sv->state & SRV_MAINTAIN) {
1332 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001333 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001334 /* If this server tracks the status of another one,
1335 * we must restore the good status.
1336 */
Willy Tarreau44267702011-10-28 15:35:33 +02001337 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001338 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001339 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001340 } else {
1341 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001342 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001343 }
1344 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001345 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001346 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001347 }
1348 }
1349
Willy Tarreau532a4502011-09-07 22:37:44 +02001350 return 1;
1351 }
1352 else if (strcmp(args[1], "frontend") == 0) {
1353 struct proxy *px;
1354
1355 px = expect_frontend_admin(s, si, args[2]);
1356 if (!px)
1357 return 1;
1358
1359 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001360 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1361 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001362 return 1;
1363 }
1364
1365 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001366 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1367 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001368 return 1;
1369 }
1370
1371 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001372 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1373 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001374 return 1;
1375 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001376 return 1;
1377 }
1378 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001379 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1380 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001381 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001382 }
1383 }
1384 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001385 if (strcmp(args[1], "agent") == 0) {
1386 struct server *sv;
1387
1388 sv = expect_server_admin(s, si, args[2]);
1389 if (!sv)
1390 return 1;
1391
1392 sv->agent.state |= CHK_STATE_DISABLED;
1393
1394 return 1;
1395 }
1396 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001397 struct server *sv;
1398
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001399 sv = expect_server_admin(s, si, args[2]);
1400 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001401 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001402
Cyril Bontécd19e512010-01-31 22:34:03 +01001403 if (! (sv->state & SRV_MAINTAIN)) {
1404 /* Not already in maintenance, we can change the server state */
1405 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001406 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001407 }
1408
Willy Tarreau532a4502011-09-07 22:37:44 +02001409 return 1;
1410 }
1411 else if (strcmp(args[1], "frontend") == 0) {
1412 struct proxy *px;
1413
1414 px = expect_frontend_admin(s, si, args[2]);
1415 if (!px)
1416 return 1;
1417
1418 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001419 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1420 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001421 return 1;
1422 }
1423
1424 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1426 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001427 return 1;
1428 }
1429
1430 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001431 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1432 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001433 return 1;
1434 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001435 return 1;
1436 }
1437 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001438 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1439 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001440 return 1;
1441 }
1442 }
1443 else if (strcmp(args[0], "shutdown") == 0) {
1444 if (strcmp(args[1], "frontend") == 0) {
1445 struct proxy *px;
1446
1447 px = expect_frontend_admin(s, si, args[2]);
1448 if (!px)
1449 return 1;
1450
1451 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001452 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1453 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001454 return 1;
1455 }
1456
1457 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1458 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1459 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1460 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1461 stop_proxy(px);
1462 return 1;
1463 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001464 else if (strcmp(args[1], "session") == 0) {
1465 struct session *sess, *ptr;
1466
Willy Tarreau290e63a2012-09-20 18:07:14 +02001467 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001468 appctx->ctx.cli.msg = stats_permission_denied_msg;
1469 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001470 return 1;
1471 }
1472
1473 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001474 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1475 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001476 return 1;
1477 }
1478
1479 ptr = (void *)strtoul(args[2], NULL, 0);
1480
1481 /* first, look for the requested session in the session table */
1482 list_for_each_entry(sess, &sessions, list) {
1483 if (sess == ptr)
1484 break;
1485 }
1486
1487 /* do we have the session ? */
1488 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001489 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1490 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001491 return 1;
1492 }
1493
1494 session_shutdown(sess, SN_ERR_KILLED);
1495 return 1;
1496 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001497 else if (strcmp(args[1], "sessions") == 0) {
1498 if (strcmp(args[2], "server") == 0) {
1499 struct server *sv;
1500 struct session *sess, *sess_bck;
1501
1502 sv = expect_server_admin(s, si, args[3]);
1503 if (!sv)
1504 return 1;
1505
1506 /* kill all the session that are on this server */
1507 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1508 if (sess->srv_conn == sv)
1509 session_shutdown(sess, SN_ERR_KILLED);
1510
1511 return 1;
1512 }
1513 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001514 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1515 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001516 return 1;
1517 }
1518 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001519 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001520 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1521 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001522 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001523 }
1524 }
1525 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001526 return 0;
1527 }
1528 return 1;
1529}
1530
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001531/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001532 * used to processes I/O from/to the stats unix socket. The system relies on a
1533 * state machine handling requests and various responses. We read a request,
1534 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001535 * Then we can read again. The state is stored in appctx->st0 and is one of the
1536 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001537 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001538 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001539static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001541 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001542 struct channel *req = si->ob;
1543 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001544 int reql;
1545 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001546
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001547 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1548 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001550 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001551 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001552 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1554 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001555 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001557 /* Let's close for real now. We just close the request
1558 * side, the conditions below will complete if needed.
1559 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001560 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001561 break;
1562 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001564 /* ensure we have some output room left in the event we
1565 * would want to return some info right after parsing.
1566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001567 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001568 break;
1569
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001570 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001571 if (reql <= 0) { /* closed or EOL not found */
1572 if (reql == 0)
1573 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001574 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001575 continue;
1576 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001577
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001578 /* seek for a possible semi-colon. If we find one, we
1579 * replace it with an LF and skip only this part.
1580 */
1581 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001582 if (trash.str[len] == ';') {
1583 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001584 reql = len + 1;
1585 break;
1586 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001587
Willy Tarreau816fc222009-10-04 07:36:58 +02001588 /* now it is time to check that we have a full line,
1589 * remove the trailing \n and possibly \r, then cut the
1590 * line.
1591 */
1592 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001593 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001595 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001596 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001598 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001599 len--;
1600
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001601 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001602
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001603 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001604 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001605 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001606 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001607 continue;
1608 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001609 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001610 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001611 else if (strcmp(trash.str, "help") == 0 ||
1612 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001613 appctx->ctx.cli.msg = stats_sock_usage_msg;
1614 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001615 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001616 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001618 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001619 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001620 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 /* if prompt is disabled, print help on empty lines,
1622 * so that the user at least knows how to enable
1623 * prompt and find help.
1624 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001625 appctx->ctx.cli.msg = stats_sock_usage_msg;
1626 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001627 }
1628
1629 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001630 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001631 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001632 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001633 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001634 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001635 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001636 continue;
1637 }
1638
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001639 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001640 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001641 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
1642 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001643 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001644 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001645 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001646 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001647 break;
1648 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001649 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001650 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001651 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001652 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001653 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001654 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001655 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001656 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001657 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001658 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001659 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001660 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001661 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001662 if (stats_table_request(si, appctx->st0))
1663 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001664 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001665 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001666 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667 break;
1668 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001669
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001670 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001671 if (appctx->st0 == STAT_CLI_PROMPT) {
1672 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
1673 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001674 }
1675
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001676 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001677 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001678 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001679
1680 /* Now we close the output if one of the writers did so,
1681 * or if we're not in interactive mode and the request
1682 * buffer is empty. This still allows pipelined requests
1683 * to be sent in non-interactive mode.
1684 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
1686 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001687 continue;
1688 }
1689
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001690 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001691 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001692 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001693 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001694
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001695 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001696 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1697 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001698 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001699 * and nothing more to consume. This is comparable to a broken pipe, so
1700 * we forward the close to the request side so that it flows upstream to
1701 * the client.
1702 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001703 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001704 }
1705
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001706 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001707 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1708 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1709 /* We have no more processing to do, and nothing more to send, and
1710 * the client side has closed. So we'll forward this state downstream
1711 * on the response buffer.
1712 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001713 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001714 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001715 }
1716
1717 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001718 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001719
1720 /* we don't want to expire timeouts while we're processing requests */
1721 si->ib->rex = TICK_ETERNITY;
1722 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001723
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001724 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001725 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001726 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001727 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001728
1729 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1730 /* check that we have released everything then unregister */
1731 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001732 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001733}
1734
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001735/* This function dumps information onto the stream interface's read buffer.
1736 * It returns 0 as long as it does not complete, non-zero upon completion.
1737 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001738 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001739static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001740{
1741 unsigned int up = (now.tv_sec - start_date.tv_sec);
1742
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001743 chunk_printf(&trash,
1744 "Name: " PRODUCT_NAME "\n"
1745 "Version: " HAPROXY_VERSION "\n"
1746 "Release_date: " HAPROXY_DATE "\n"
1747 "Nbproc: %d\n"
1748 "Process_num: %d\n"
1749 "Pid: %d\n"
1750 "Uptime: %dd %dh%02dm%02ds\n"
1751 "Uptime_sec: %d\n"
1752 "Memmax_MB: %d\n"
1753 "Ulimit-n: %d\n"
1754 "Maxsock: %d\n"
1755 "Maxconn: %d\n"
1756 "Hard_maxconn: %d\n"
1757 "Maxpipes: %d\n"
1758 "CurrConns: %d\n"
1759 "PipesUsed: %d\n"
1760 "PipesFree: %d\n"
1761 "ConnRate: %d\n"
1762 "ConnRateLimit: %d\n"
1763 "MaxConnRate: %d\n"
1764 "CompressBpsIn: %u\n"
1765 "CompressBpsOut: %u\n"
1766 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001767#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001768 "ZlibMemUsage: %ld\n"
1769 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001770#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001771 "Tasks: %d\n"
1772 "Run_queue: %d\n"
1773 "Idle_pct: %d\n"
1774 "node: %s\n"
1775 "description: %s\n"
1776 "",
1777 global.nbproc,
1778 relative_pid,
1779 pid,
1780 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1781 up,
1782 global.rlimit_memmax,
1783 global.rlimit_nofile,
1784 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1785 actconn, pipes_used, pipes_free,
1786 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1787 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1788 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001789#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001790 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001791#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001792 nb_tasks_cur, run_queue_cur, idle_pct,
1793 global.node, global.desc ? global.desc : ""
1794 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001795
1796 if (bi_putchk(si->ib, &trash) == -1)
1797 return 0;
1798
1799 return 1;
1800}
1801
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001802/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1803 * the state from stream interface <si>. The caller is responsible for clearing
1804 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001805 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001806static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001807{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001808 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001809 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001810
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001811 if (!(px->cap & PR_CAP_FE))
1812 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001813
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001814 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001815 return 0;
1816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001817 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001818 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001819 /* name, queue */
1820 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001822 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001823 /* Column sub-heading for Enable or Disable server */
1824 chunk_appendf(&trash, "<td></td>");
1825 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001826
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001827 chunk_appendf(&trash,
1828 "<td class=ac>"
1829 "<a name=\"%s/Frontend\"></a>"
1830 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1831 "<td colspan=3></td>"
1832 "",
1833 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001834
Willy Tarreau466c9b52012-12-23 02:25:03 +01001835 chunk_appendf(&trash,
1836 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001837 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001838 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1839 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1840 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001841 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1842 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1843 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001844
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001845 if (px->mode == PR_MODE_HTTP)
1846 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001847 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001848 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001849
1850 chunk_appendf(&trash,
1851 "</table></div></u></td>"
1852 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001853 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001854 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1855 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1856 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001857 U2H(px->fe_counters.sps_max),
1858 U2H(px->fe_counters.cps_max),
1859 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001860
1861 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001862 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001863 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001864 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001865
1866 chunk_appendf(&trash,
1867 "</table></div></u></td>"
1868 /* sessions rate : limit */
1869 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001870 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001871
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001872 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001873 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001874 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001875 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001876 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1877 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001878 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001879 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1880 U2H(px->fe_counters.cum_sess),
1881 U2H(px->fe_counters.cum_conn),
1882 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001883
Willy Tarreau466c9b52012-12-23 02:25:03 +01001884 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001885 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001886 chunk_appendf(&trash,
1887 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1888 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1889 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1890 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1891 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1892 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1893 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1894 "<tr><th>- other responses:</th><td>%s</td></tr>"
1895 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1896 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001897 U2H(px->fe_counters.p.http.cum_req),
1898 U2H(px->fe_counters.p.http.rsp[1]),
1899 U2H(px->fe_counters.p.http.rsp[2]),
1900 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001901 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001902 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001903 U2H(px->fe_counters.p.http.rsp[3]),
1904 U2H(px->fe_counters.p.http.rsp[4]),
1905 U2H(px->fe_counters.p.http.rsp[5]),
1906 U2H(px->fe_counters.p.http.rsp[0]),
1907 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001908 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001910 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001911 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001912 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001913 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001914 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001915 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001916 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001917 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001918
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001919 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001920 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001921 "<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 +01001922 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001923 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001924 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1925 px->fe_counters.comp_in ?
1926 (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 +01001927 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001928
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001929 chunk_appendf(&trash,
1930 /* denied: req, resp */
1931 "<td>%s</td><td>%s</td>"
1932 /* errors : request, connect, response */
1933 "<td>%s</td><td></td><td></td>"
1934 /* warnings: retries, redispatches */
1935 "<td></td><td></td>"
1936 /* server status : reflect frontend status */
1937 "<td class=ac>%s</td>"
1938 /* rest of server: nothing */
1939 "<td class=ac colspan=8></td></tr>"
1940 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001941 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1942 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001943 px->state == PR_STREADY ? "OPEN" :
1944 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001945 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001946 else { /* CSV mode */
1947 chunk_appendf(&trash,
1948 /* pxid, name, queue cur, queue max, */
1949 "%s,FRONTEND,,,"
1950 /* sessions : current, max, limit, total */
1951 "%d,%d,%d,%lld,"
1952 /* bytes : in, out */
1953 "%lld,%lld,"
1954 /* denied: req, resp */
1955 "%lld,%lld,"
1956 /* errors : request, connect, response */
1957 "%lld,,,"
1958 /* warnings: retries, redispatches */
1959 ",,"
1960 /* server status : reflect frontend status */
1961 "%s,"
1962 /* rest of server: nothing */
1963 ",,,,,,,,"
1964 /* pid, iid, sid, throttle, lbtot, tracked, type */
1965 "%d,%d,0,,,,%d,"
1966 /* rate, rate_lim, rate_max */
1967 "%u,%u,%u,"
1968 /* check_status, check_code, check_duration */
1969 ",,,",
1970 px->id,
1971 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1972 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1973 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1974 px->fe_counters.failed_req,
1975 px->state == PR_STREADY ? "OPEN" :
1976 px->state == PR_STFULL ? "FULL" : "STOP",
1977 relative_pid, px->uuid, STATS_TYPE_FE,
1978 read_freq_ctr(&px->fe_sess_per_sec),
1979 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001980
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001981 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1982 if (px->mode == PR_MODE_HTTP) {
1983 for (i=1; i<6; i++)
1984 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1985 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1986 }
1987 else
1988 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001989
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001990 /* failed health analyses */
1991 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001992
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001993 /* requests : req_rate, req_rate_max, req_tot, */
1994 chunk_appendf(&trash, "%u,%u,%lld,",
1995 read_freq_ctr(&px->fe_req_per_sec),
1996 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1997
1998 /* errors: cli_aborts, srv_aborts */
1999 chunk_appendf(&trash, ",,");
2000
2001 /* compression: in, out, bypassed */
2002 chunk_appendf(&trash, "%lld,%lld,%lld,",
2003 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2004
2005 /* compression: comp_rsp */
2006 chunk_appendf(&trash, "%lld,",
2007 px->fe_counters.p.http.comp_rsp);
2008
2009 /* finish with EOL */
2010 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002011 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002012 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002013}
2014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002015/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2016 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2017 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2018 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002019 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002020static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002021{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002022 struct appctx *appctx = __objt_appctx(si->end);
2023
2024 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002025 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002026 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002027 /* Column sub-heading for Enable or Disable server */
2028 chunk_appendf(&trash, "<td></td>");
2029 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002030 chunk_appendf(&trash,
2031 /* frontend name, listener name */
2032 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2033 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2034 "",
2035 px->id, l->name,
2036 (flags & ST_SHLGNDS)?"<u>":"",
2037 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002038
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002039 if (flags & ST_SHLGNDS) {
2040 char str[INET6_ADDRSTRLEN];
2041 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002042
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002043 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002044
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002045 port = get_host_port(&l->addr);
2046 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2047 case AF_INET:
2048 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2049 break;
2050 case AF_INET6:
2051 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2052 break;
2053 case AF_UNIX:
2054 chunk_appendf(&trash, "unix, ");
2055 break;
2056 case -1:
2057 chunk_appendf(&trash, "(%s), ", strerror(errno));
2058 break;
2059 }
Willy Tarreau91861262007-10-17 17:06:05 +02002060
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002061 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002062 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002063 }
Willy Tarreau91861262007-10-17 17:06:05 +02002064
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002065 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002066 /* queue */
2067 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002068 /* sessions rate: current, max, limit */
2069 "<td colspan=3>&nbsp;</td>"
2070 /* sessions: current, max, limit, total, lbtot */
2071 "<td>%s</td><td>%s</td><td>%s</td>"
2072 "<td>%s</td><td>&nbsp;</td>"
2073 /* bytes: in, out */
2074 "<td>%s</td><td>%s</td>"
2075 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002076 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002077 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2078 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002079
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002080 chunk_appendf(&trash,
2081 /* denied: req, resp */
2082 "<td>%s</td><td>%s</td>"
2083 /* errors: request, connect, response */
2084 "<td>%s</td><td></td><td></td>"
2085 /* warnings: retries, redispatches */
2086 "<td></td><td></td>"
2087 /* server status: reflect listener status */
2088 "<td class=ac>%s</td>"
2089 /* rest of server: nothing */
2090 "<td class=ac colspan=8></td></tr>"
2091 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002092 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2093 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002094 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2095 }
2096 else { /* CSV mode */
2097 chunk_appendf(&trash,
2098 /* pxid, name, queue cur, queue max, */
2099 "%s,%s,,,"
2100 /* sessions: current, max, limit, total */
2101 "%d,%d,%d,%lld,"
2102 /* bytes: in, out */
2103 "%lld,%lld,"
2104 /* denied: req, resp */
2105 "%lld,%lld,"
2106 /* errors: request, connect, response */
2107 "%lld,,,"
2108 /* warnings: retries, redispatches */
2109 ",,"
2110 /* server status: reflect listener status */
2111 "%s,"
2112 /* rest of server: nothing */
2113 ",,,,,,,,"
2114 /* pid, iid, sid, throttle, lbtot, tracked, type */
2115 "%d,%d,%d,,,,%d,"
2116 /* rate, rate_lim, rate_max */
2117 ",,,"
2118 /* check_status, check_code, check_duration */
2119 ",,,"
2120 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2121 ",,,,,,"
2122 /* failed health analyses */
2123 ","
2124 /* requests : req_rate, req_rate_max, req_tot, */
2125 ",,,"
2126 /* errors: cli_aborts, srv_aborts */
2127 ",,"
2128 /* compression: in, out, bypassed, comp_rsp */
2129 ",,,,"
2130 "\n",
2131 px->id, l->name,
2132 l->nbconn, l->counters->conn_max,
2133 l->maxconn, l->counters->cum_conn,
2134 l->counters->bytes_in, l->counters->bytes_out,
2135 l->counters->denied_req, l->counters->denied_resp,
2136 l->counters->failed_req,
2137 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2138 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2139 }
2140 return 1;
2141}
Willy Tarreau91861262007-10-17 17:06:05 +02002142
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002143/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2144 * from stream interface <si>, stats flags <flags>, and server state <state>.
2145 * The caller is responsible for clearing the trash if needed. Returns non-zero
2146 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002147 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2148 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002149 */
2150static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2151{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002153 struct server *ref = sv->track ? sv->track : sv;
2154 char str[INET6_ADDRSTRLEN];
2155 struct chunk src;
2156 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002157
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002158 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002159 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002160 "DOWN",
2161 "DN %d/%d &uarr;",
2162 "UP %d/%d &darr;",
2163 "UP",
2164 "NOLB %d/%d &darr;",
2165 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002166 "DRAIN %d/%d &darr;",
2167 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002168 "<i>no check</i>"
2169 };
Willy Tarreau91861262007-10-17 17:06:05 +02002170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002171 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2172 chunk_appendf(&trash, "<tr class=\"maintain\">");
2173 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002174 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002175 "<tr class=\"%s%d\">",
2176 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002177
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002178 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002179 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002180 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2181 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002182
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002183 chunk_appendf(&trash,
2184 "<td class=ac><a name=\"%s/%s\"></a>%s"
2185 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2186 "",
2187 px->id, sv->id,
2188 (flags & ST_SHLGNDS) ? "<u>" : "",
2189 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002190
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002191 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002192 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002193
2194 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2195 case AF_INET:
2196 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2197 break;
2198 case AF_INET6:
2199 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2200 break;
2201 case AF_UNIX:
2202 chunk_appendf(&trash, "unix, ");
2203 break;
2204 case -1:
2205 chunk_appendf(&trash, "(%s), ", strerror(errno));
2206 break;
2207 default: /* address family not supported */
2208 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002209 }
Willy Tarreau91861262007-10-17 17:06:05 +02002210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002211 /* id */
2212 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002213
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002214 /* cookie */
2215 if (sv->cookie) {
2216 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002217
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002218 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2219 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002220
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002221 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002222 }
2223
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002224 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002225 }
Willy Tarreau91861262007-10-17 17:06:05 +02002226
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002227 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002228 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002229 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002230 /* sessions rate : current, max, limit */
2231 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002232 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002233 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002234 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2235 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002236
Willy Tarreau466c9b52012-12-23 02:25:03 +01002237
2238 chunk_appendf(&trash,
2239 /* sessions: current, max, limit, total */
2240 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002241 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002242 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2243 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002244 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2245 U2H(sv->counters.cum_sess),
2246 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002247
Willy Tarreau466c9b52012-12-23 02:25:03 +01002248 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2249 if (px->mode == PR_MODE_HTTP) {
2250 unsigned long long tot;
2251 for (tot = i = 0; i < 6; i++)
2252 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002253
Willy Tarreau466c9b52012-12-23 02:25:03 +01002254 chunk_appendf(&trash,
2255 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2256 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2257 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2258 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2259 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2260 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2261 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2262 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002263 U2H(tot),
2264 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2265 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2266 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2267 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2268 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2269 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 +02002270 }
Willy Tarreau91861262007-10-17 17:06:05 +02002271
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002272 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002273 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002274 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002275 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002276 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002277
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002278 chunk_appendf(&trash,
2279 /* bytes : in, out */
2280 "<td>%s</td><td>%s</td>"
2281 /* denied: req, resp */
2282 "<td></td><td>%s</td>"
2283 /* errors : request, connect */
2284 "<td></td><td>%s</td>"
2285 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002286 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002287 /* warnings: retries, redispatches */
2288 "<td>%lld</td><td>%lld</td>"
2289 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002290 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2291 U2H(sv->counters.failed_secu),
2292 U2H(sv->counters.failed_conns),
2293 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002294 sv->counters.cli_aborts,
2295 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002296 sv->counters.retries, sv->counters.redispatches);
2297
2298 /* status, lest check */
2299 chunk_appendf(&trash, "<td class=ac>");
2300
2301 if (sv->state & SRV_MAINTAIN) {
2302 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2303 chunk_appendf(&trash, "MAINT");
2304 }
2305 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2306 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2307 chunk_appendf(&trash, "MAINT(via)");
2308 }
2309 else if (ref->state & SRV_CHECKED) {
2310 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2311 chunk_appendf(&trash,
2312 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002313 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2314 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002315 }
Willy Tarreau91861262007-10-17 17:06:05 +02002316
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002317 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002318 chunk_appendf(&trash,
2319 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002320 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002321 get_check_status_info(sv->check.status));
2322
2323 if (sv->check.status >= HCHK_STATUS_L57DATA)
2324 chunk_appendf(&trash, "/%d", sv->check.code);
2325
2326 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002327 chunk_appendf(&trash, " in %lums", sv->check.duration);
2328
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002329 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002330 get_check_status_description(sv->check.status));
2331 if (*sv->check.desc) {
2332 chunk_appendf(&trash, ": ");
2333 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2334 chunk_htmlencode(&trash, &src);
2335 }
2336 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002337 }
2338 else
2339 chunk_appendf(&trash, "</td><td>");
2340
2341 chunk_appendf(&trash,
2342 /* weight */
2343 "</td><td class=ac>%d</td>"
2344 /* act, bck */
2345 "<td class=ac>%s</td><td class=ac>%s</td>"
2346 "",
2347 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2348 (sv->state & SRV_BACKUP) ? "-" : "Y",
2349 (sv->state & SRV_BACKUP) ? "Y" : "-");
2350
2351 /* check failures: unique, fatal, down time */
2352 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002353 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002354
2355 if (ref->observe)
2356 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2357
2358 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002359 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002360 "<td>%lld</td><td>%s</td>"
2361 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002362 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002363 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2364 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002365 else if (sv != ref) {
2366 if (sv->state & SRV_MAINTAIN)
2367 chunk_appendf(&trash,
2368 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2369 ref->proxy->id, ref->id);
2370 else
2371 chunk_appendf(&trash,
2372 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2373 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2374 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375 else
2376 chunk_appendf(&trash, "<td colspan=3></td>");
2377
2378 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002379 if (sv->state & SRV_WARMINGUP)
2380 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002381 else
2382 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2383 }
2384 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002385 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002386 "DOWN,",
2387 "DOWN %d/%d,",
2388 "UP %d/%d,",
2389 "UP,",
2390 "NOLB %d/%d,",
2391 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002392 "DRAIN %d/%d,",
2393 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002394 "no check,"
2395 };
2396
2397 chunk_appendf(&trash,
2398 /* pxid, name */
2399 "%s,%s,"
2400 /* queue : current, max */
2401 "%d,%d,"
2402 /* sessions : current, max, limit, total */
2403 "%d,%d,%s,%lld,"
2404 /* bytes : in, out */
2405 "%lld,%lld,"
2406 /* denied: req, resp */
2407 ",%lld,"
2408 /* errors : request, connect, response */
2409 ",%lld,%lld,"
2410 /* warnings: retries, redispatches */
2411 "%lld,%lld,"
2412 "",
2413 px->id, sv->id,
2414 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002415 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002416 sv->counters.bytes_in, sv->counters.bytes_out,
2417 sv->counters.failed_secu,
2418 sv->counters.failed_conns, sv->counters.failed_resp,
2419 sv->counters.retries, sv->counters.redispatches);
2420
2421 /* status */
2422 if (sv->state & SRV_MAINTAIN)
2423 chunk_appendf(&trash, "MAINT,");
2424 else if (ref != sv && ref->state & SRV_MAINTAIN)
2425 chunk_appendf(&trash, "MAINT(via),");
2426 else
2427 chunk_appendf(&trash,
2428 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002429 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2430 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002431
2432 chunk_appendf(&trash,
2433 /* weight, active, backup */
2434 "%d,%d,%d,"
2435 "",
2436 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2437 (sv->state & SRV_BACKUP) ? 0 : 1,
2438 (sv->state & SRV_BACKUP) ? 1 : 0);
2439
2440 /* check failures: unique, fatal; last change, total downtime */
2441 if (sv->state & SRV_CHECKED)
2442 chunk_appendf(&trash,
2443 "%lld,%lld,%d,%d,",
2444 sv->counters.failed_checks, sv->counters.down_trans,
2445 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2446 else
2447 chunk_appendf(&trash, ",,,,");
2448
2449 /* queue limit, pid, iid, sid, */
2450 chunk_appendf(&trash,
2451 "%s,"
2452 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002453 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002454 relative_pid, px->uuid, sv->puid);
2455
2456 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002457 if (sv->state & SRV_WARMINGUP)
2458 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002459
2460 /* sessions: lbtot */
2461 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2462
2463 /* tracked */
2464 if (sv->track)
2465 chunk_appendf(&trash, "%s/%s,",
2466 sv->track->proxy->id, sv->track->id);
2467 else
2468 chunk_appendf(&trash, ",");
2469
2470 /* type */
2471 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2472
2473 /* rate */
2474 chunk_appendf(&trash, "%u,,%u,",
2475 read_freq_ctr(&sv->sess_per_sec),
2476 sv->counters.sps_max);
2477
2478 if (sv->state & SRV_CHECKED) {
2479 /* check_status */
2480 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2481
2482 /* check_code */
2483 if (sv->check.status >= HCHK_STATUS_L57DATA)
2484 chunk_appendf(&trash, "%u,", sv->check.code);
2485 else
2486 chunk_appendf(&trash, ",");
2487
2488 /* check_duration */
2489 if (sv->check.status >= HCHK_STATUS_CHECKED)
2490 chunk_appendf(&trash, "%lu,", sv->check.duration);
2491 else
2492 chunk_appendf(&trash, ",");
2493
2494 }
2495 else
2496 chunk_appendf(&trash, ",,,");
2497
2498 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2499 if (px->mode == PR_MODE_HTTP) {
2500 for (i=1; i<6; i++)
2501 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2502
2503 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2504 }
2505 else
2506 chunk_appendf(&trash, ",,,,,,");
2507
2508 /* failed health analyses */
2509 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2510
2511 /* requests : req_rate, req_rate_max, req_tot, */
2512 chunk_appendf(&trash, ",,,");
2513
2514 /* errors: cli_aborts, srv_aborts */
2515 chunk_appendf(&trash, "%lld,%lld,",
2516 sv->counters.cli_aborts, sv->counters.srv_aborts);
2517
2518 /* compression: in, out, bypassed, comp_rsp */
2519 chunk_appendf(&trash, ",,,,");
2520
2521 /* finish with EOL */
2522 chunk_appendf(&trash, "\n");
2523 }
2524 return 1;
2525}
2526
2527/* Dumps a line for backend <px> to the trash for and uses the state from stream
2528 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2529 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2530 */
2531static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2532{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002533 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 struct chunk src;
2535 int i;
2536
2537 if (!(px->cap & PR_CAP_BE))
2538 return 0;
2539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002540 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002541 return 0;
2542
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002543 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002544 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002545 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002546 /* Column sub-heading for Enable or Disable server */
2547 chunk_appendf(&trash, "<td></td>");
2548 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002549 chunk_appendf(&trash,
2550 "<td class=ac>"
2551 /* name */
2552 "%s<a name=\"%s/Backend\"></a>"
2553 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2554 "",
2555 (flags & ST_SHLGNDS)?"<u>":"",
2556 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002557
2558 if (flags & ST_SHLGNDS) {
2559 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002560 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002561 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2562
2563 /* cookie */
2564 if (px->cookie_name) {
2565 chunk_appendf(&trash, ", cookie: '");
2566 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2567 chunk_htmlencode(&trash, &src);
2568 chunk_appendf(&trash, "'");
2569 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002570 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 }
2572
2573 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002574 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 /* queue : current, max */
2576 "<td>%s</td><td>%s</td><td></td>"
2577 /* sessions rate : current, max, limit */
2578 "<td>%s</td><td>%s</td><td></td>"
2579 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002581 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2582 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583
2584 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002585 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002587 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002588 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002590 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2591 U2H(px->be_counters.cum_conn),
2592 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002593
Willy Tarreau466c9b52012-12-23 02:25:03 +01002594 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002595 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002596 chunk_appendf(&trash,
2597 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2598 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2599 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2600 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2601 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2602 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2603 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2604 "<tr><th>- other responses:</th><td>%s</td></tr>"
2605 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2606 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002607 U2H(px->be_counters.p.http.cum_req),
2608 U2H(px->be_counters.p.http.rsp[1]),
2609 U2H(px->be_counters.p.http.rsp[2]),
2610 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002611 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002612 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002613 U2H(px->be_counters.p.http.rsp[3]),
2614 U2H(px->be_counters.p.http.rsp[4]),
2615 U2H(px->be_counters.p.http.rsp[5]),
2616 U2H(px->be_counters.p.http.rsp[0]),
2617 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002618 }
2619
2620 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002621 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002622 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002623 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002625 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002627 U2H(px->be_counters.cum_lbconn),
2628 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002629
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002630 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002631 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002632 "<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 +01002633 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002634 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002635 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2636 px->be_counters.comp_in ?
2637 (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 +01002638 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2639
2640 chunk_appendf(&trash,
2641 /* denied: req, resp */
2642 "<td>%s</td><td>%s</td>"
2643 /* errors : request, connect */
2644 "<td></td><td>%s</td>"
2645 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002646 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 /* warnings: retries, redispatches */
2648 "<td>%lld</td><td>%lld</td>"
2649 /* backend status: reflect backend status (up/down): we display UP
2650 * if the backend has known working servers or if it has no server at
2651 * all (eg: for stats). Then we display the total weight, number of
2652 * active and backups. */
2653 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2654 "<td class=ac>%d</td><td class=ac>%d</td>"
2655 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002656 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2657 U2H(px->be_counters.failed_conns),
2658 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002659 px->be_counters.cli_aborts,
2660 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002661 px->be_counters.retries, px->be_counters.redispatches,
2662 human_time(now.tv_sec - px->last_change, 1),
2663 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2664 "<font color=\"red\"><b>DOWN</b></font>",
2665 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2666 px->srv_act, px->srv_bck);
2667
2668 chunk_appendf(&trash,
2669 /* rest of backend: nothing, down transitions, total downtime, throttle */
2670 "<td class=ac>&nbsp;</td><td>%d</td>"
2671 "<td>%s</td>"
2672 "<td></td>"
2673 "</tr>",
2674 px->down_trans,
2675 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2676 }
2677 else { /* CSV mode */
2678 chunk_appendf(&trash,
2679 /* pxid, name */
2680 "%s,BACKEND,"
2681 /* queue : current, max */
2682 "%d,%d,"
2683 /* sessions : current, max, limit, total */
2684 "%d,%d,%d,%lld,"
2685 /* bytes : in, out */
2686 "%lld,%lld,"
2687 /* denied: req, resp */
2688 "%lld,%lld,"
2689 /* errors : request, connect, response */
2690 ",%lld,%lld,"
2691 /* warnings: retries, redispatches */
2692 "%lld,%lld,"
2693 /* backend status: reflect backend status (up/down): we display UP
2694 * if the backend has known working servers or if it has no server at
2695 * all (eg: for stats). Then we display the total weight, number of
2696 * active and backups. */
2697 "%s,"
2698 "%d,%d,%d,"
2699 /* rest of backend: nothing, down transitions, last change, total downtime */
2700 ",%d,%d,%d,,"
2701 /* pid, iid, sid, throttle, lbtot, tracked, type */
2702 "%d,%d,0,,%lld,,%d,"
2703 /* rate, rate_lim, rate_max, */
2704 "%u,,%u,"
2705 /* check_status, check_code, check_duration */
2706 ",,,",
2707 px->id,
2708 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2709 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2710 px->be_counters.bytes_in, px->be_counters.bytes_out,
2711 px->be_counters.denied_req, px->be_counters.denied_resp,
2712 px->be_counters.failed_conns, px->be_counters.failed_resp,
2713 px->be_counters.retries, px->be_counters.redispatches,
2714 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2715 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2716 px->srv_act, px->srv_bck,
2717 px->down_trans, (int)(now.tv_sec - px->last_change),
2718 px->srv?be_downtime(px):0,
2719 relative_pid, px->uuid,
2720 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2721 read_freq_ctr(&px->be_sess_per_sec),
2722 px->be_counters.sps_max);
2723
2724 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2725 if (px->mode == PR_MODE_HTTP) {
2726 for (i=1; i<6; i++)
2727 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2728 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2729 }
2730 else
2731 chunk_appendf(&trash, ",,,,,,");
2732
2733 /* failed health analyses */
2734 chunk_appendf(&trash, ",");
2735
2736 /* requests : req_rate, req_rate_max, req_tot, */
2737 chunk_appendf(&trash, ",,,");
2738
2739 /* errors: cli_aborts, srv_aborts */
2740 chunk_appendf(&trash, "%lld,%lld,",
2741 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2742
2743 /* compression: in, out, bypassed */
2744 chunk_appendf(&trash, "%lld,%lld,%lld,",
2745 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2746
2747 /* compression: comp_rsp */
2748 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2749
2750 /* finish with EOL */
2751 chunk_appendf(&trash, "\n");
2752 }
2753 return 1;
2754}
2755
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002756/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002757 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002758 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002759 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002760static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002761{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002762 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002763 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2764
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002765 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002766 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002767
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002768 /* 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 +02002769 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002770 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002771 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002772 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2773 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002774 }
2775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002777 "<form action=\"%s%s%s%s\" method=\"post\">",
2778 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002779 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2780 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002781 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 }
2783
2784 /* print a new table */
2785 chunk_appendf(&trash,
2786 "<table class=\"tbl\" width=\"100%%\">\n"
2787 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002788 "<th class=\"pxname\" width=\"10%%\">");
2789
2790 chunk_appendf(&trash,
2791 "<a name=\"%s\"></a>%s"
2792 "<a class=px href=\"#%s\">%s</a>",
2793 px->id,
2794 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2795 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796
2797 if (uri->flags & ST_SHLGNDS) {
2798 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002799 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002800 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2801 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002802 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803 }
2804
2805 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002806 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002807 "<th class=\"%s\" width=\"90%%\">%s</th>"
2808 "</tr>\n"
2809 "</table>\n"
2810 "<table class=\"tbl\" width=\"100%%\">\n"
2811 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002812 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2813 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2814
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002815 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 /* Column heading for Enable or Disable server */
2817 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002818 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002819
2820 chunk_appendf(&trash,
2821 "<th rowspan=2></th>"
2822 "<th colspan=3>Queue</th>"
2823 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2824 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2825 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2826 "<th colspan=9>Server</th>"
2827 "</tr>\n"
2828 "<tr class=\"titre\">"
2829 "<th>Cur</th><th>Max</th><th>Limit</th>"
2830 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2831 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2832 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2833 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2834 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2835 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2836 "<th>Thrtle</th>\n"
2837 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002838}
2839
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002840/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002843static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002844{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002845 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002846 chunk_appendf(&trash, "</table>");
2847
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002848 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 /* close the form used to enable/disable this proxy servers */
2850 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002851 "Choose the action to perform on the checked servers : "
2852 "<select name=action>"
2853 "<option value=\"\"></option>"
2854 "<option value=\"disable\">Disable</option>"
2855 "<option value=\"enable\">Enable</option>"
2856 "<option value=\"stop\">Soft Stop</option>"
2857 "<option value=\"start\">Soft Start</option>"
2858 "<option value=\"shutdown\">Kill Sessions</option>"
2859 "</select>"
2860 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2861 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2862 "</form>",
2863 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002864 }
2865
2866 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002867}
Willy Tarreau91861262007-10-17 17:06:05 +02002868
2869/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002870 * Dumps statistics for a proxy. The output is sent to the stream interface's
2871 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2872 * buffer space, or non-zero if everything completed. This function is used
2873 * both by the CLI and the HTTP entry points, and is able to dump the output
2874 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002875 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002876static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002877{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002878 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02002879 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002880 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002881 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002882 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002883
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002884 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002885
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002886 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002887 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002888 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002889 if (uri && uri->scope) {
2890 /* we have a limited scope, we have to check the proxy name */
2891 struct stat_scope *scope;
2892 int len;
2893
2894 len = strlen(px->id);
2895 scope = uri->scope;
2896
2897 while (scope) {
2898 /* match exact proxy name */
2899 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2900 break;
2901
2902 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002903 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002904 break;
2905 scope = scope->next;
2906 }
2907
2908 /* proxy name not found : don't dump anything */
2909 if (scope == NULL)
2910 return 1;
2911 }
2912
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002913 /* if the user has requested a limited output and the proxy
2914 * name does not match, skip it.
2915 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002916 if (appctx->ctx.stats.scope_len &&
2917 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 +02002918 return 1;
2919
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002920 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2921 (appctx->ctx.stats.iid != -1) &&
2922 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002923 return 1;
2924
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002925 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002926 /* fall through */
2927
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002929 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002930 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002931 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002932 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002933 }
Willy Tarreau91861262007-10-17 17:06:05 +02002934
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002935 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002936 /* fall through */
2937
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002939 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002940 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002941 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002942 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002943
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 appctx->ctx.stats.l = px->conf.listeners.n;
2945 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002946 /* fall through */
2947
Willy Tarreau295a8372011-03-10 11:25:07 +01002948 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002949 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002950 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002951 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002952 return 0;
2953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002954 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002955 if (!l->counters)
2956 continue;
2957
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 if (appctx->ctx.stats.flags & STAT_BOUND) {
2959 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002960 break;
2961
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002962 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002963 continue;
2964 }
2965
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002966 /* print the frontend */
2967 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2968 if (bi_putchk(rep, &trash) == -1)
2969 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002970 }
2971
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002972 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2973 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002974 /* fall through */
2975
Willy Tarreau295a8372011-03-10 11:25:07 +01002976 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002977 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002978 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002979 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 +02002980
Willy Tarreau9b28e032012-10-12 23:49:43 +02002981 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002982 return 0;
2983
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002984 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002985
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002986 if (appctx->ctx.stats.flags & STAT_BOUND) {
2987 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002988 break;
2989
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002990 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002991 continue;
2992 }
2993
Willy Tarreau44267702011-10-28 15:35:33 +02002994 if (sv->track)
2995 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002996 else
2997 svs = sv;
2998
Willy Tarreau91861262007-10-17 17:06:05 +02002999 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003000 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003001 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003002 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003003 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003004 sv_state = 3; /* UP */
3005 else
3006 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003007
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003008 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003009 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003010 else if (svs->state & SRV_GOINGDOWN)
3011 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003012 }
Willy Tarreau91861262007-10-17 17:06:05 +02003013 else
Simon Horman125d0992013-02-24 17:23:38 +09003014 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003015 sv_state = 1; /* going up */
3016 else
3017 sv_state = 0; /* DOWN */
3018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003019 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003020 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003021 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003022 continue;
3023 }
3024
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3026 if (bi_putchk(rep, &trash) == -1)
3027 return 0;
3028 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003029
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003030 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003031 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003032
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003033 case STAT_PX_ST_BE:
3034 /* print the backend */
3035 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3036 if (bi_putchk(rep, &trash) == -1)
3037 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003039 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003040 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003043 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003044 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 if (bi_putchk(rep, &trash) == -1)
3046 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003047 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003048
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003049 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003050 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003051
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003052 case STAT_PX_ST_FIN:
3053 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003054
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003055 default:
3056 /* unknown state, we should put an abort() here ! */
3057 return 1;
3058 }
3059}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003060
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003061/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3062 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003063 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003064static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003065{
3066 /* WARNING! This must fit in the first buffer !!! */
3067 chunk_appendf(&trash,
3068 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3069 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3070 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3071 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3072 "<style type=\"text/css\"><!--\n"
3073 "body {"
3074 " font-family: arial, helvetica, sans-serif;"
3075 " font-size: 12px;"
3076 " font-weight: normal;"
3077 " color: black;"
3078 " background: white;"
3079 "}\n"
3080 "th,td {"
3081 " font-size: 10px;"
3082 "}\n"
3083 "h1 {"
3084 " font-size: x-large;"
3085 " margin-bottom: 0.5em;"
3086 "}\n"
3087 "h2 {"
3088 " font-family: helvetica, arial;"
3089 " font-size: x-large;"
3090 " font-weight: bold;"
3091 " font-style: italic;"
3092 " color: #6020a0;"
3093 " margin-top: 0em;"
3094 " margin-bottom: 0em;"
3095 "}\n"
3096 "h3 {"
3097 " font-family: helvetica, arial;"
3098 " font-size: 16px;"
3099 " font-weight: bold;"
3100 " color: #b00040;"
3101 " background: #e8e8d0;"
3102 " margin-top: 0em;"
3103 " margin-bottom: 0em;"
3104 "}\n"
3105 "li {"
3106 " margin-top: 0.25em;"
3107 " margin-right: 2em;"
3108 "}\n"
3109 ".hr {margin-top: 0.25em;"
3110 " border-color: black;"
3111 " border-bottom-style: solid;"
3112 "}\n"
3113 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3114 ".total {background: #20D0D0;color: #ffff80;}\n"
3115 ".frontend {background: #e8e8d0;}\n"
3116 ".socket {background: #d0d0d0;}\n"
3117 ".backend {background: #e8e8d0;}\n"
3118 ".active0 {background: #ff9090;}\n"
3119 ".active1 {background: #ffd020;}\n"
3120 ".active2 {background: #ffffa0;}\n"
3121 ".active3 {background: #c0ffc0;}\n"
3122 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003123 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3124 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3125 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003126 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003127 ".backup0 {background: #ff9090;}\n"
3128 ".backup1 {background: #ff80ff;}\n"
3129 ".backup2 {background: #c060ff;}\n"
3130 ".backup3 {background: #b0d0ff;}\n"
3131 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3132 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003133 ".backup6 {background: #c060ff;}\n"
3134 ".backup7 {background: #cc9900;}\n"
3135 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003136 ".maintain {background: #c07820;}\n"
3137 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3138 "\n"
3139 "a.px:link {color: #ffff40; text-decoration: none;}"
3140 "a.px:visited {color: #ffff40; text-decoration: none;}"
3141 "a.px:hover {color: #ffffff; text-decoration: none;}"
3142 "a.lfsb:link {color: #000000; text-decoration: none;}"
3143 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3144 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3145 "\n"
3146 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3147 "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"
3148 "table.tbl td.ac { text-align: center;}\n"
3149 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3150 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3151 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3152 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3153 "\n"
3154 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3155 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3156 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003157 "table.det { border-collapse: collapse; border-style: none; }\n"
3158 "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"
3159 "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 +01003160 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003161 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003162 " display:block;\n"
3163 " visibility:hidden;\n"
3164 " z-index:2147483647;\n"
3165 " position:absolute;\n"
3166 " padding:2px 4px 3px;\n"
3167 " background:#f0f060; color:#000000;\n"
3168 " border:1px solid #7040c0;\n"
3169 " white-space:nowrap;\n"
3170 " font-style:normal;font-size:11px;font-weight:normal;\n"
3171 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3172 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3173 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003174 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 "-->\n"
3176 "</style></head>\n",
3177 (uri->flags & ST_SHNODE) ? " on " : "",
3178 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3179 );
3180}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003181
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003182/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003183 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003184 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003185 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003186static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003187{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003188 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003190 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003191
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003192 /* WARNING! this has to fit the first packet too.
3193 * We are around 3.5 kB, add adding entries will
3194 * become tricky if we want to support 4kB buffers !
3195 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003196 chunk_appendf(&trash,
3197 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3198 PRODUCT_NAME "%s</a></h1>\n"
3199 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3200 "<hr width=\"100%%\" class=\"hr\">\n"
3201 "<h3>&gt; General process information</h3>\n"
3202 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3203 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3204 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3205 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3206 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3207 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3208 "Running tasks: %d/%d; idle = %d %%<br>\n"
3209 "</td><td align=\"center\" nowrap>\n"
3210 "<table class=\"lgd\"><tr>\n"
3211 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3212 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3213 "</tr><tr>\n"
3214 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3215 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3216 "</tr><tr>\n"
3217 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3218 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3219 "</tr><tr>\n"
3220 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003221 "</tr><tr>\n"
3222 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 "</tr><tr>\n"
3224 "<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 -07003225 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003226 "<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 +01003227 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003228 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 "</td>"
3230 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3231 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3232 "",
3233 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3234 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3235 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3236 (uri->flags & ST_SHDESC) ? ": " : "",
3237 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3238 pid, relative_pid, global.nbproc,
3239 up / 86400, (up % 86400) / 3600,
3240 (up % 3600) / 60, (up % 60),
3241 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3242 global.rlimit_memmax ? " MB" : "",
3243 global.rlimit_nofile,
3244 global.maxsock, global.maxconn, global.maxpipes,
3245 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3246 run_queue_cur, nb_tasks_cur, idle_pct
3247 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003248
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003249 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3250 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3251 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003252
3253 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003254 "<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 +02003255 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003256 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3257 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3258 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003259 STAT_SCOPE_TXT_MAXLEN);
3260
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003261 /* 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 +02003262 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003264 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3266 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003267 }
3268
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003269 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003271 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 uri->uri_prefix,
3273 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003274 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003275 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003276 else
3277 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003278 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279 uri->uri_prefix,
3280 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003281 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003282 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003283
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003284 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003286 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003287 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003288 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003289 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003290 "",
3291 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292 else
3293 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003294 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003295 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003296 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003297 ";norefresh",
3298 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003299 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003300
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003302 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003304 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3305 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003306 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003307
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003309 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003311 (uri->refresh > 0) ? ";norefresh" : "",
3312 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003313
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314 chunk_appendf(&trash,
3315 "</ul></td>"
3316 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3317 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3318 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3319 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3320 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3321 "</ul>"
3322 "</td>"
3323 "</tr></table>\n"
3324 ""
3325 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003326
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003327 if (appctx->ctx.stats.st_code) {
3328 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 case STAT_STATUS_DONE:
3330 chunk_appendf(&trash,
3331 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003332 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003334 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003335 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3336 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003337 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003338 break;
3339 case STAT_STATUS_NONE:
3340 chunk_appendf(&trash,
3341 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003342 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003344 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3346 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003347 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003348 break;
3349 case STAT_STATUS_PART:
3350 chunk_appendf(&trash,
3351 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003352 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 "Action partially processed.<br>"
3354 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003355 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003356 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3357 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003358 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003359 break;
3360 case STAT_STATUS_ERRP:
3361 chunk_appendf(&trash,
3362 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003363 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 "Action not processed because of invalid parameters."
3365 "<ul>"
3366 "<li>The action is maybe unknown.</li>"
3367 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3368 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3369 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003370 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003371 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3372 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003373 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 break;
3375 case STAT_STATUS_EXCD:
3376 chunk_appendf(&trash,
3377 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003378 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3380 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003381 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3383 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003384 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 break;
3386 case STAT_STATUS_DENY:
3387 chunk_appendf(&trash,
3388 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003389 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003390 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003391 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3393 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003394 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 break;
3396 default:
3397 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003398 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003399 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003401 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003402 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3403 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003404 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 }
3406 chunk_appendf(&trash, "<p>\n");
3407 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003409
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003410/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3411 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003413static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003417
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003418/* This function dumps statistics onto the stream interface's read buffer in
3419 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003420 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3421 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3422 * and the session must be closed, or -1 in case of any error. This function is
3423 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003425static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003427 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 struct channel *rep = si->ib;
3429 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003430
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003432
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003433 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003434 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003435 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003436 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003439 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003440 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003441 else
3442 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003443
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 if (bi_putchk(rep, &trash) == -1)
3445 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003446
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003447 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003449
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003451 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003452 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003453 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003454 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 }
Willy Tarreau91861262007-10-17 17:06:05 +02003456
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003457 appctx->ctx.stats.px = proxy;
3458 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3459 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003460 /* fall through */
3461
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 case STAT_ST_LIST:
3463 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003464 while (appctx->ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003465 if (buffer_almost_full(rep->buf))
3466 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003467
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003468 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003469 /* skip the disabled proxies, global frontend and non-networked ones */
3470 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003471 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003472 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003473
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003474 appctx->ctx.stats.px = px->next;
3475 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 }
3477 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003479 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003480 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003481
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003482 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003483 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003484 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 if (bi_putchk(rep, &trash) == -1)
3486 return 0;
3487 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003488
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003489 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003490 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 case STAT_ST_FIN:
3493 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003494
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003495 default:
3496 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003497 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003498 return -1;
3499 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003500}
Willy Tarreauae526782010-03-04 20:34:23 +01003501
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003502/* We reached the stats page through a POST request. The appctx is
3503 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01003504 * Parse the posted data and enable/disable servers if necessary.
3505 * Returns 1 if request was parsed or zero if it needs more data.
3506 */
3507static int stats_process_http_post(struct stream_interface *si)
3508{
3509 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003510 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003511
3512 struct proxy *px = NULL;
3513 struct server *sv = NULL;
3514
3515 char key[LINESIZE];
3516 int action = ST_ADM_ACTION_NONE;
3517 int reprocess = 0;
3518
3519 int total_servers = 0;
3520 int altered_servers = 0;
3521
3522 char *first_param, *cur_param, *next_param, *end_params;
3523 char *st_cur_param = NULL;
3524 char *st_next_param = NULL;
3525
3526 struct chunk *temp;
3527 int reql;
3528
3529 temp = get_trash_chunk();
3530 if (temp->size < s->txn.req.body_len) {
3531 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003532 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003533 goto out;
3534 }
3535
3536 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3537 if (reql <= 0) {
3538 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003539 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003540 return 0;
3541 }
3542
3543 first_param = temp->str;
3544 end_params = temp->str + reql;
3545 cur_param = next_param = end_params;
3546 *end_params = '\0';
3547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003548 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003549
3550 /*
3551 * Parse the parameters in reverse order to only store the last value.
3552 * From the html form, the backend and the action are at the end.
3553 */
3554 while (cur_param > first_param) {
3555 char *value;
3556 int poffset, plen;
3557
3558 cur_param--;
3559
3560 if ((*cur_param == '&') || (cur_param == first_param)) {
3561 reprocess_servers:
3562 /* Parse the key */
3563 poffset = (cur_param != first_param ? 1 : 0);
3564 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3565 if ((plen > 0) && (plen <= sizeof(key))) {
3566 strncpy(key, cur_param + poffset, plen);
3567 key[plen - 1] = '\0';
3568 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003569 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003570 goto out;
3571 }
3572
3573 /* Parse the value */
3574 value = key;
3575 while (*value != '\0' && *value != '=') {
3576 value++;
3577 }
3578 if (*value == '=') {
3579 /* Ok, a value is found, we can mark the end of the key */
3580 *value++ = '\0';
3581 }
3582 if (url_decode(key) < 0 || url_decode(value) < 0)
3583 break;
3584
3585 /* Now we can check the key to see what to do */
3586 if (!px && (strcmp(key, "b") == 0)) {
3587 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
3588 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003589 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003590 goto out;
3591 }
3592 }
3593 else if (!action && (strcmp(key, "action") == 0)) {
3594 if (strcmp(value, "disable") == 0) {
3595 action = ST_ADM_ACTION_DISABLE;
3596 }
3597 else if (strcmp(value, "enable") == 0) {
3598 action = ST_ADM_ACTION_ENABLE;
3599 }
3600 else if (strcmp(value, "stop") == 0) {
3601 action = ST_ADM_ACTION_STOP;
3602 }
3603 else if (strcmp(value, "start") == 0) {
3604 action = ST_ADM_ACTION_START;
3605 }
3606 else if (strcmp(value, "shutdown") == 0) {
3607 action = ST_ADM_ACTION_SHUTDOWN;
3608 }
3609 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003610 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003611 goto out;
3612 }
3613 }
3614 else if (strcmp(key, "s") == 0) {
3615 if (!(px && action)) {
3616 /*
3617 * Indicates that we'll need to reprocess the parameters
3618 * as soon as backend and action are known
3619 */
3620 if (!reprocess) {
3621 st_cur_param = cur_param;
3622 st_next_param = next_param;
3623 }
3624 reprocess = 1;
3625 }
3626 else if ((sv = findserver(px, value)) != NULL) {
3627 switch (action) {
3628 case ST_ADM_ACTION_DISABLE:
3629 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
3630 /* Not already in maintenance, we can change the server state */
3631 sv->state |= SRV_MAINTAIN;
3632 set_server_down(&sv->check);
3633 altered_servers++;
3634 total_servers++;
3635 }
3636 break;
3637 case ST_ADM_ACTION_ENABLE:
3638 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
3639 /* Already in maintenance, we can change the server state */
3640 set_server_up(&sv->check);
3641 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
3642 altered_servers++;
3643 total_servers++;
3644 }
3645 break;
3646 case ST_ADM_ACTION_STOP:
3647 case ST_ADM_ACTION_START:
3648 if (action == ST_ADM_ACTION_START)
3649 sv->uweight = sv->iweight;
3650 else
3651 sv->uweight = 0;
3652
3653 server_recalc_eweight(sv);
3654 set_server_drain_state(sv);
3655
3656 altered_servers++;
3657 total_servers++;
3658 break;
3659 case ST_ADM_ACTION_SHUTDOWN:
3660 if (px->state != PR_STSTOPPED) {
3661 struct session *sess, *sess_bck;
3662
3663 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3664 if (sess->srv_conn == sv)
3665 session_shutdown(sess, SN_ERR_KILLED);
3666
3667 altered_servers++;
3668 total_servers++;
3669 }
3670 break;
3671 }
3672 } else {
3673 /* the server name is unknown or ambiguous (duplicate names) */
3674 total_servers++;
3675 }
3676 }
3677 if (reprocess && px && action) {
3678 /* Now, we know the backend and the action chosen by the user.
3679 * We can safely restart from the first server parameter
3680 * to reprocess them
3681 */
3682 cur_param = st_cur_param;
3683 next_param = st_next_param;
3684 reprocess = 0;
3685 goto reprocess_servers;
3686 }
3687
3688 next_param = cur_param;
3689 }
3690 }
3691
3692 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003693 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003694 }
3695 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003696 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003697 }
3698 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003699 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003700 }
3701 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003702 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003703 }
3704 out:
3705 return 1;
3706}
3707
3708
3709static int stats_send_http_headers(struct stream_interface *si)
3710{
3711 struct session *s = session_from_task(si->owner);
3712 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003713 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003714
3715 chunk_printf(&trash,
3716 "HTTP/1.0 200 OK\r\n"
3717 "Cache-Control: no-cache\r\n"
3718 "Connection: close\r\n"
3719 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003720 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01003721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003722 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01003723 chunk_appendf(&trash, "Refresh: %d\r\n",
3724 uri->refresh);
3725
3726 chunk_appendf(&trash, "\r\n");
3727
3728 s->txn.status = 200;
3729 s->logs.tv_request = now;
3730
3731 if (bi_putchk(si->ib, &trash) == -1)
3732 return 0;
3733
3734 return 1;
3735}
3736
3737static int stats_send_http_redirect(struct stream_interface *si)
3738{
3739 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3740 struct session *s = session_from_task(si->owner);
3741 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003742 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003743
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003744 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003745 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003746 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003747 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003748 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3749 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003750 }
3751
3752 /* We don't want to land on the posted stats page because a refresh will
3753 * repost the data. We don't want this to happen on accident so we redirect
3754 * the browse to the stats page with a GET.
3755 */
3756 chunk_printf(&trash,
3757 "HTTP/1.1 303 See Other\r\n"
3758 "Cache-Control: no-cache\r\n"
3759 "Content-Type: text/plain\r\n"
3760 "Connection: close\r\n"
3761 "Location: %s;st=%s%s%s%s\r\n"
3762 "\r\n",
3763 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003764 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3765 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3766 stat_status_codes[appctx->ctx.stats.st_code]) ?
3767 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01003768 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003769 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3770 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01003771 scope_txt);
3772
3773 s->txn.status = 303;
3774 s->logs.tv_request = now;
3775
3776 if (bi_putchk(si->ib, &trash) == -1)
3777 return 0;
3778
3779 return 1;
3780}
3781
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003782/* This I/O handler runs as an applet embedded in a stream interface. It is
3783 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003784 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 * automatically unregisters itself once transfer is complete.
3786 */
3787static void http_stats_io_handler(struct stream_interface *si)
3788{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003789 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003790 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003791 struct channel *req = si->ob;
3792 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003793
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003794 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3795 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003796
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 /* check that the output is not closed */
3798 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003799 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003800
Willy Tarreau347a35d2013-11-22 17:51:09 +01003801 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003802 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003803 if (stats_send_http_headers(si)) {
3804 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003805 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003806 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003807 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003809 }
3810
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003811 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003812 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003813 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003814 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003816 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003817 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003818 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003819 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003820 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003821 }
3822
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003823 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003824 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003825 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003826 }
3827
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003828 if (appctx->st0 == STAT_HTTP_DONE)
Willy Tarreau347a35d2013-11-22 17:51:09 +01003829 si_shutw(si);
3830
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003831 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3832 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003833
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003834 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01003835 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3836 si_shutr(si);
3837 res->flags |= CF_READ_NULL;
3838 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003839 }
Willy Tarreau91861262007-10-17 17:06:05 +02003840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003841 /* update all other flags and resync with the other side */
3842 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003843
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003844 /* we don't want to expire timeouts while we're processing requests */
3845 si->ib->rex = TICK_ETERNITY;
3846 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003848 out:
3849 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3850 /* check that we have released everything then unregister */
3851 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003852 }
3853}
3854
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003855
Willy Tarreau909d5172012-11-26 03:04:41 +01003856static inline const char *get_conn_ctrl_name(const struct connection *conn)
3857{
Willy Tarreauf79c8172013-10-21 16:30:56 +02003858 if (!(conn->flags & CO_FL_CTRL_READY) || !conn->ctrl)
Willy Tarreau909d5172012-11-26 03:04:41 +01003859 return "NONE";
3860 return conn->ctrl->name;
3861}
3862
3863static inline const char *get_conn_xprt_name(const struct connection *conn)
3864{
3865 static char ptr[17];
3866
Willy Tarreauf79c8172013-10-21 16:30:56 +02003867 if (!(conn->flags & CO_FL_XPRT_READY) || !conn->xprt)
Willy Tarreau909d5172012-11-26 03:04:41 +01003868 return "NONE";
3869
3870 if (conn->xprt == &raw_sock)
3871 return "RAW";
3872
3873#ifdef USE_OPENSSL
3874 if (conn->xprt == &ssl_sock)
3875 return "SSL";
3876#endif
3877 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3878 return ptr;
3879}
3880
3881static inline const char *get_conn_data_name(const struct connection *conn)
3882{
3883 static char ptr[17];
3884
3885 if (!conn->data)
3886 return "NONE";
3887
3888 if (conn->data == &sess_conn_cb)
3889 return "SESS";
3890
3891 if (conn->data == &si_conn_cb)
3892 return "STRM";
3893
3894 if (conn->data == &check_conn_cb)
3895 return "CHCK";
3896
3897 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3898 return ptr;
3899}
3900
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003901/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003902 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003903 * 0 if the output buffer is full and it needs to be called again, otherwise
3904 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003905 */
Willy Tarreau76153662012-11-26 01:16:39 +01003906static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003907{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003908 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003909 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003910 extern const char *monthname[12];
3911 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003912 struct connection *conn;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003913
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003914 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003915
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003916 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003917 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003918 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3919 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003920 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003921 appctx->ctx.sess.uid = 0;
3922 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003923 return 1;
3924 }
3925
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003926 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003927 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003928 appctx->ctx.sess.uid = sess->uniq_id;
3929 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003930 /* fall through */
3931
3932 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003933 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003934 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003935 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003936 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003937 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3938 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003939 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003940 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003941
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003942 conn = objt_conn(sess->si[0].end);
3943 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003944 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003945 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003946 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003947 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003948 break;
3949 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003950 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003951 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003952 default:
3953 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003954 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003955 break;
3956 }
3957
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003958 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003959 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003960 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003961
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003963 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003964 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3965 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3966 sess->listener ? sess->listener->luid : 0);
3967
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003968 if (conn)
3969 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003970
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003971 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003972 case AF_INET:
3973 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003974 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003975 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003976 break;
3977 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003978 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003979 break;
3980 default:
3981 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003982 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003983 break;
3984 }
3985
Willy Tarreau50943332011-09-02 17:33:05 +02003986 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003987 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003988 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003989 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003990 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3991 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003992 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003993
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003994 conn = objt_conn(sess->si[1].end);
3995 if (conn)
3996 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003997
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003998 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003999 case AF_INET:
4000 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004001 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004002 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004003 break;
4004 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004005 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004006 break;
4007 default:
4008 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004009 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004010 break;
4011 }
4012
4013 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004014 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004015 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004016 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4017 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004018 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004019 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004020
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004021 if (conn)
4022 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004023
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004024 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004025 case AF_INET:
4026 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004027 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004028 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004029 break;
4030 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004031 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004032 break;
4033 default:
4034 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004035 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004036 break;
4037 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004038
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004039 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004040 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004041 sess->task,
4042 sess->task->state,
4043 sess->task->nice, sess->task->calls,
4044 sess->task->expire ?
4045 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4046 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4047 TICKS_TO_MS(1000)) : "<NEVER>",
4048 task_in_rq(sess->task) ? ", running" : "");
4049
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004050 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004051 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004052 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4053
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004054 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004055 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4056 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4057 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4058
4059 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004060 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004061 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004062 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004063 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004064 obj_type_name(sess->si[0].end),
4065 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004066 sess->si[0].exp ?
4067 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4068 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4069 TICKS_TO_MS(1000)) : "<NEVER>",
4070 sess->si[0].err_type);
4071
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004072 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004073 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004074 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004075 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004076 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004077 obj_type_name(sess->si[1].end),
4078 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004079 sess->si[1].exp ?
4080 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4081 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4082 TICKS_TO_MS(1000)) : "<NEVER>",
4083 sess->si[1].err_type);
4084
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004085 conn = objt_conn(sess->si[0].end);
4086 if (conn) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004087 chunk_appendf(&trash,
4088 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004089 conn,
4090 get_conn_ctrl_name(conn),
4091 get_conn_xprt_name(conn),
4092 get_conn_data_name(conn),
4093 obj_type_name(conn->target),
4094 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004095
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004096 chunk_appendf(&trash,
4097 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004098 conn->flags,
4099 conn->t.sock.fd,
4100 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4101 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4102 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004103 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004104
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004105 conn = objt_conn(sess->si[1].end);
4106 if (conn) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004107 chunk_appendf(&trash,
4108 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004109 conn,
4110 get_conn_ctrl_name(conn),
4111 get_conn_xprt_name(conn),
4112 get_conn_data_name(conn),
4113 obj_type_name(conn->target),
4114 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004115
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004116 chunk_appendf(&trash,
4117 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004118 conn->flags,
4119 conn->t.sock.fd,
4120 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4121 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4122 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004123 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004124
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004125 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004126 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004127 " an_exp=%s",
4128 sess->req,
4129 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004130 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004131 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004132 sess->req->analyse_exp ?
4133 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4134 TICKS_TO_MS(1000)) : "<NEVER>");
4135
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004136 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004137 " rex=%s",
4138 sess->req->rex ?
4139 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4140 TICKS_TO_MS(1000)) : "<NEVER>");
4141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004142 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004143 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004144 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004145 sess->req->wex ?
4146 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4147 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004148 sess->req->buf,
4149 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004150 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004151 sess->txn.req.next, sess->req->buf->i,
4152 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004153
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004154 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004155 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004156 " an_exp=%s",
4157 sess->rep,
4158 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004159 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004160 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004161 sess->rep->analyse_exp ?
4162 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4163 TICKS_TO_MS(1000)) : "<NEVER>");
4164
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004165 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004166 " rex=%s",
4167 sess->rep->rex ?
4168 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4169 TICKS_TO_MS(1000)) : "<NEVER>");
4170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004171 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004172 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004173 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004174 sess->rep->wex ?
4175 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4176 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004177 sess->rep->buf,
4178 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004179 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004180 sess->txn.rsp.next, sess->rep->buf->i,
4181 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004182
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004183 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004184 return 0;
4185
4186 /* use other states to dump the contents */
4187 }
4188 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004189 appctx->ctx.sess.uid = 0;
4190 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004191 return 1;
4192}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004193
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004194/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004195 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004196 * to be called again, otherwise non-zero. It is designed to be called
4197 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004198 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004199static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004200{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004201 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004202 struct connection *conn;
4203
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004204 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004205 /* If we're forced to shut down, we might have to remove our
4206 * reference to the last session being dumped.
4207 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004208 if (appctx->st2 == STAT_ST_LIST) {
4209 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4210 LIST_DEL(&appctx->ctx.sess.bref.users);
4211 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004212 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004213 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004214 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004215 }
4216
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004217 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004218
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004219 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004220 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004221 /* the function had not been called yet, let's prepare the
4222 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004223 * pointer to the first in the global list. When a target
4224 * session is being destroyed, it is responsible for updating
4225 * this pointer. We know we have reached the end when this
4226 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004227 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004228 LIST_INIT(&appctx->ctx.sess.bref.users);
4229 appctx->ctx.sess.bref.ref = sessions.n;
4230 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004231 /* fall through */
4232
Willy Tarreau295a8372011-03-10 11:25:07 +01004233 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004234 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004235 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4236 LIST_DEL(&appctx->ctx.sess.bref.users);
4237 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004238 }
4239
4240 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004241 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004242 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004243 struct session *curr_sess;
4244
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004245 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004246
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004247 if (appctx->ctx.sess.target) {
4248 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004249 goto next_sess;
4250
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004251 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004252 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01004253 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004254 return 0;
4255
4256 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004257 LIST_DEL(&appctx->ctx.sess.bref.users);
4258 LIST_INIT(&appctx->ctx.sess.bref.users);
4259 if (appctx->ctx.sess.target != (void *)-1) {
4260 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01004261 break;
4262 }
4263 else
4264 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004265 }
4266
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004267 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004268 "%p: proto=%s",
4269 curr_sess,
4270 curr_sess->listener->proto->name);
4271
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004272
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004273 conn = objt_conn(curr_sess->si[0].end);
4274 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02004275 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004276 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004277 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004278 " src=%s:%d fe=%s be=%s srv=%s",
4279 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004280 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004281 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004282 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004283 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004284 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004285 break;
4286 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004287 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02004288 " src=unix:%d fe=%s be=%s srv=%s",
4289 curr_sess->listener->luid,
4290 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004291 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004292 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02004293 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004294 break;
4295 }
4296
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004297 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02004298 " ts=%02x age=%s calls=%d",
4299 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01004300 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
4301 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004303 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004304 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004305 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004306 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004307 curr_sess->req->analysers,
4308 curr_sess->req->rex ?
4309 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
4310 TICKS_TO_MS(1000)) : "");
4311
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004312 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004313 ",wx=%s",
4314 curr_sess->req->wex ?
4315 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
4316 TICKS_TO_MS(1000)) : "");
4317
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004318 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004319 ",ax=%s]",
4320 curr_sess->req->analyse_exp ?
4321 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
4322 TICKS_TO_MS(1000)) : "");
4323
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004324 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004325 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004326 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004327 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004328 curr_sess->rep->analysers,
4329 curr_sess->rep->rex ?
4330 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
4331 TICKS_TO_MS(1000)) : "");
4332
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004333 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004334 ",wx=%s",
4335 curr_sess->rep->wex ?
4336 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
4337 TICKS_TO_MS(1000)) : "");
4338
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004339 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004340 ",ax=%s]",
4341 curr_sess->rep->analyse_exp ?
4342 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
4343 TICKS_TO_MS(1000)) : "");
4344
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004345 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004346 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004347 " s0=[%d,%1xh,fd=%d,ex=%s]",
4348 curr_sess->si[0].state,
4349 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004350 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004351 curr_sess->si[0].exp ?
4352 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4353 TICKS_TO_MS(1000)) : "");
4354
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004355 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004356 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004357 " s1=[%d,%1xh,fd=%d,ex=%s]",
4358 curr_sess->si[1].state,
4359 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004360 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004361 curr_sess->si[1].exp ?
4362 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4363 TICKS_TO_MS(1000)) : "");
4364
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004365 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004366 " exp=%s",
4367 curr_sess->task->expire ?
4368 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4369 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004370 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004371 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004372
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004374
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004375 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004376 /* let's try again later from this session. We add ourselves into
4377 * this session's users so that it can remove us upon termination.
4378 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004379 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004380 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004381 }
4382
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004383 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004384 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004385 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004386
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004388 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004389 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004390 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004391 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004392 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004393
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004394 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004395 return 0;
4396
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004397 appctx->ctx.sess.target = NULL;
4398 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004399 return 1;
4400 }
4401
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004402 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004403 /* fall through */
4404
4405 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004406 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004407 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004408 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004409}
4410
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004411/* This is called when the stream interface is closed. For instance, upon an
4412 * external abort, we won't call the i/o handler anymore so we may need to
4413 * remove back references to the session currently being dumped.
4414 */
Simon Horman74d88312013-02-12 10:45:50 +09004415static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004416{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004417 struct appctx *appctx = __objt_appctx(si->end);
4418
4419 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
4420 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
4421 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004422 }
4423}
4424
Willy Tarreau20e99322013-04-13 09:22:25 +02004425/* This function is used to either dump tables states (when action is set
4426 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02004427 * It returns 0 if the output buffer is full and it needs to be called
4428 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004429 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004430static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004431{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004432 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004433 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004434 struct ebmb_node *eb;
4435 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004436 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004437 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004438
4439 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004440 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01004441 * - STAT_ST_INIT : the first call
4442 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004443 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004444 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004445 * and the entry pointer points to the next entry to be dumped,
4446 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004447 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004448 * data though.
4449 */
4450
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004451 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004452 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 if (appctx->st2 == STAT_ST_LIST) {
4454 appctx->ctx.table.entry->ref_cnt--;
4455 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004456 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004457 return 1;
4458 }
4459
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004460 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004461
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004462 while (appctx->st2 != STAT_ST_FIN) {
4463 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004464 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004465 appctx->ctx.table.proxy = appctx->ctx.table.target;
4466 if (!appctx->ctx.table.proxy)
4467 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004468
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004469 appctx->ctx.table.entry = NULL;
4470 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004471 break;
4472
Willy Tarreau295a8372011-03-10 11:25:07 +01004473 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004474 if (!appctx->ctx.table.proxy ||
4475 (appctx->ctx.table.target &&
4476 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
4477 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004478 break;
4479 }
4480
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004481 if (appctx->ctx.table.proxy->table.size) {
4482 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
4483 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004484 return 0;
4485
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004486 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004487 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004488 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004489 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004490 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004491 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4492 appctx->ctx.table.entry->ref_cnt++;
4493 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004494 break;
4495 }
4496 }
4497 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004499 break;
4500
Willy Tarreau295a8372011-03-10 11:25:07 +01004501 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004502 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004503
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004504 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004505 /* we're filtering on some data contents */
4506 void *ptr;
4507 long long data;
4508
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004509 dt = appctx->ctx.table.data_type;
4510 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
4511 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004512 dt);
4513
4514 data = 0;
4515 switch (stktable_data_types[dt].std_type) {
4516 case STD_T_SINT:
4517 data = stktable_data_cast(ptr, std_t_sint);
4518 break;
4519 case STD_T_UINT:
4520 data = stktable_data_cast(ptr, std_t_uint);
4521 break;
4522 case STD_T_ULL:
4523 data = stktable_data_cast(ptr, std_t_ull);
4524 break;
4525 case STD_T_FRQP:
4526 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004527 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004528 break;
4529 }
4530
4531 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004532 if ((data < appctx->ctx.table.value &&
4533 (appctx->ctx.table.data_op == STD_OP_EQ ||
4534 appctx->ctx.table.data_op == STD_OP_GT ||
4535 appctx->ctx.table.data_op == STD_OP_GE)) ||
4536 (data == appctx->ctx.table.value &&
4537 (appctx->ctx.table.data_op == STD_OP_NE ||
4538 appctx->ctx.table.data_op == STD_OP_GT ||
4539 appctx->ctx.table.data_op == STD_OP_LT)) ||
4540 (data > appctx->ctx.table.value &&
4541 (appctx->ctx.table.data_op == STD_OP_EQ ||
4542 appctx->ctx.table.data_op == STD_OP_LT ||
4543 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004544 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004545 }
4546
Simon Hormanc88b8872011-06-15 15:18:49 +09004547 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004548 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
4549 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09004550 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004552 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004553
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004554 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004555 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004556 struct stksess *old = appctx->ctx.table.entry;
4557 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004558 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004559 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
4560 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
4561 stksess_kill(&appctx->ctx.table.proxy->table, old);
4562 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004563 break;
4564 }
4565
Simon Hormanc88b8872011-06-15 15:18:49 +09004566
4567 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004568 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
4569 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
4570 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09004571
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004572 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
4573 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004574 break;
4575
Willy Tarreau295a8372011-03-10 11:25:07 +01004576 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004578 break;
4579 }
4580 }
4581 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004582}
4583
Willy Tarreaud426a182010-03-05 14:58:26 +01004584/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004585 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4586 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4587 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4588 * lines are respected within the limit of 70 output chars. Lines that are
4589 * continuation of a previous truncated line begin with "+" instead of " "
4590 * after the offset. The new pointer is returned.
4591 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004592static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4593 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004594{
4595 int end;
4596 unsigned char c;
4597
4598 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004599 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004600 return ptr;
4601
Willy Tarreau77804732012-10-29 16:14:26 +01004602 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004603
Willy Tarreaud426a182010-03-05 14:58:26 +01004604 while (ptr < len && ptr < bsize) {
4605 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004606 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004607 if (out->len > end - 2)
4608 break;
4609 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004610 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004611 if (out->len > end - 3)
4612 break;
4613 out->str[out->len++] = '\\';
4614 switch (c) {
4615 case '\t': c = 't'; break;
4616 case '\n': c = 'n'; break;
4617 case '\r': c = 'r'; break;
4618 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004619 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004620 }
4621 out->str[out->len++] = c;
4622 } else {
4623 if (out->len > end - 5)
4624 break;
4625 out->str[out->len++] = '\\';
4626 out->str[out->len++] = 'x';
4627 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4628 out->str[out->len++] = hextab[c & 0xF];
4629 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004630 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004631 /* we had a line break, let's return now */
4632 out->str[out->len++] = '\n';
4633 *line = ptr;
4634 return ptr;
4635 }
4636 }
4637 /* we have an incomplete line, we return it as-is */
4638 out->str[out->len++] = '\n';
4639 return ptr;
4640}
4641
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004642/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004643 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004644 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004645 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004646static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004647{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004648 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004649 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004650
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004651 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004652 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004653
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004654 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004655
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004656 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004657 /* the function had not been called yet, let's prepare the
4658 * buffer for a response.
4659 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004660 struct tm tm;
4661
4662 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004663 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004664 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4665 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4666 error_snapshot_id);
4667
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004668 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004669 /* Socket buffer full. Let's try again later from the same point */
4670 return 0;
4671 }
4672
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004673 appctx->ctx.errors.px = proxy;
4674 appctx->ctx.errors.buf = 0;
4675 appctx->ctx.errors.bol = 0;
4676 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004677 }
4678
4679 /* we have two inner loops here, one for the proxy, the other one for
4680 * the buffer.
4681 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004682 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004683 struct error_snapshot *es;
4684
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004685 if (appctx->ctx.errors.buf == 0)
4686 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004687 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004688 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004689
4690 if (!es->when.tv_sec)
4691 goto next;
4692
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004693 if (appctx->ctx.errors.iid >= 0 &&
4694 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
4695 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004696 goto next;
4697
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004698 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004699 /* just print headers now */
4700
4701 char pn[INET6_ADDRSTRLEN];
4702 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004703 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004704
4705 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004706 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004707 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004708 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004709
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004710 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4711 case AF_INET:
4712 case AF_INET6:
4713 port = get_host_port(&es->src);
4714 break;
4715 default:
4716 port = 0;
4717 }
4718
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004719 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004720 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004721 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004722 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004723 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004724 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004725 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4726 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004727 break;
4728 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004729 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004730 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004731 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004732 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004733 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004734 break;
4735 }
4736
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004737 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004738 ", server %s (#%d), event #%u\n"
4739 " src %s:%d, session #%d, session flags 0x%08x\n"
4740 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4741 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4742 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4743 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4744 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4745 es->ev_id,
4746 pn, port, es->sid, es->s_flags,
4747 es->state, es->m_flags, es->t_flags,
4748 es->m_clen, es->m_blen,
4749 es->b_flags, es->b_out, es->b_tot,
4750 es->len, es->b_wrap, es->pos);
4751
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004752 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004753 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004754 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004755 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004756 appctx->ctx.errors.ptr = 0;
4757 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004758 }
4759
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004760 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004761 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004762 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004763 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004764 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004765 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004766 goto next;
4767 }
4768
4769 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004770 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004771 int newptr;
4772 int newline;
4773
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004774 newline = appctx->ctx.errors.bol;
4775 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
4776 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004777 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004778
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004779 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004780 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004781 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004782 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004783 appctx->ctx.errors.ptr = newptr;
4784 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004785 };
4786 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004787 appctx->ctx.errors.bol = 0;
4788 appctx->ctx.errors.ptr = -1;
4789 appctx->ctx.errors.buf++;
4790 if (appctx->ctx.errors.buf > 1) {
4791 appctx->ctx.errors.buf = 0;
4792 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004793 }
4794 }
4795
4796 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004797 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004798}
4799
Willy Tarreaud5781202012-09-22 19:32:35 +02004800/* parse the "level" argument on the bind lines */
4801static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4802{
4803 if (!*args[cur_arg + 1]) {
4804 memprintf(err, "'%s' : missing level", args[cur_arg]);
4805 return ERR_ALERT | ERR_FATAL;
4806 }
4807
4808 if (!strcmp(args[cur_arg+1], "user"))
4809 conf->level = ACCESS_LVL_USER;
4810 else if (!strcmp(args[cur_arg+1], "operator"))
4811 conf->level = ACCESS_LVL_OPER;
4812 else if (!strcmp(args[cur_arg+1], "admin"))
4813 conf->level = ACCESS_LVL_ADMIN;
4814 else {
4815 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4816 args[cur_arg], args[cur_arg+1]);
4817 return ERR_ALERT | ERR_FATAL;
4818 }
4819
4820 return 0;
4821}
4822
Willy Tarreaub24281b2011-02-13 13:16:36 +01004823struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004824 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004825 .name = "<STATS>", /* used for logging */
4826 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004827 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004828};
4829
Simon Horman9bd2c732011-06-15 15:18:44 +09004830static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004831 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004832 .name = "<CLI>", /* used for logging */
4833 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004834 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004835};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004836
Willy Tarreaudc13c112013-06-21 23:16:39 +02004837static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004838 { CFG_GLOBAL, "stats", stats_parse_global },
4839 { 0, NULL, NULL },
4840}};
4841
Willy Tarreaud5781202012-09-22 19:32:35 +02004842static struct bind_kw_list bind_kws = { "STAT", { }, {
4843 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4844 { NULL, NULL, 0 },
4845}};
4846
Willy Tarreau10522fd2008-07-09 20:12:41 +02004847__attribute__((constructor))
4848static void __dumpstats_module_init(void)
4849{
4850 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004851 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004852}
4853
Willy Tarreau91861262007-10-17 17:06:05 +02004854/*
4855 * Local variables:
4856 * c-indent-level: 8
4857 * c-basic-offset: 8
4858 * End:
4859 */