blob: 629bf96b708984a3f8a469aa178c76dfe6d8253d [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010051#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010064static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010065static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090066static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010068static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010069static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Simon Horman9bd2c732011-06-15 15:18:44 +090071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010072/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010073 * cli_io_handler()
74 * -> stats_dump_sess_to_buffer() // "show sess"
75 * -> stats_dump_errors_to_buffer() // "show errors"
76 * -> stats_dump_info_to_buffer() // "show info"
77 * -> stats_dump_stat_to_buffer() // "show stat"
78 * -> stats_dump_csv_header()
79 * -> stats_dump_proxy_to_buffer()
80 * -> stats_dump_fe_stats()
81 * -> stats_dump_li_stats()
82 * -> stats_dump_sv_stats()
83 * -> stats_dump_be_stats()
84 *
85 * http_stats_io_handler()
86 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
87 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
88 * -> stats_dump_html_head() // emits the HTML headers
89 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
90 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
91 * -> stats_dump_html_px_hdr()
92 * -> stats_dump_fe_stats()
93 * -> stats_dump_li_stats()
94 * -> stats_dump_sv_stats()
95 * -> stats_dump_be_stats()
96 * -> stats_dump_html_px_end()
97 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010098 */
99
Simon Horman9bd2c732011-06-15 15:18:44 +0900100static struct si_applet cli_applet;
101
102static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200103 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200104 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200105 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200106 " help : this message\n"
107 " prompt : toggle interactive mode with prompt\n"
108 " quit : disconnect\n"
109 " show info : report information about the running process\n"
110 " show stat : report counters for each proxy and server\n"
111 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100112 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200113 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200114 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200115 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200116 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100117 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200118 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200119 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200120 " disable : put a server or frontend in maintenance mode\n"
121 " enable : re-enable a server or frontend which is in maintenance mode\n"
122 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200123 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200126 "Permission denied\n"
127 "";
128
Willy Tarreau295a8372011-03-10 11:25:07 +0100129/* data transmission states for the stats responses */
130enum {
131 STAT_ST_INIT = 0,
132 STAT_ST_HEAD,
133 STAT_ST_INFO,
134 STAT_ST_LIST,
135 STAT_ST_END,
136 STAT_ST_FIN,
137};
138
139/* data transmission states for the stats responses inside a proxy */
140enum {
141 STAT_PX_ST_INIT = 0,
142 STAT_PX_ST_TH,
143 STAT_PX_ST_FE,
144 STAT_PX_ST_LI,
145 STAT_PX_ST_SV,
146 STAT_PX_ST_BE,
147 STAT_PX_ST_END,
148 STAT_PX_ST_FIN,
149};
150
Cyril Bonté19979e12012-04-04 12:57:21 +0200151extern const char *stat_status_codes[];
152
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200153/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the connection
155 * needs to be closed and ignored, or a negative value upon critical failure.
156 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900157static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200158{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200159 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100160 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreaucf644ed2013-09-29 17:19:56 +0200161 s->target = &cli_applet.obj_type; // for logging only
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100162 s->si[1].appctx.st1 = 0;
163 s->si[1].appctx.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200164
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200165 tv_zero(&s->logs.tv_request);
166 s->logs.t_queue = 0;
167 s->logs.t_connect = 0;
168 s->logs.t_data = 0;
169 s->logs.t_close = 0;
170 s->logs.bytes_in = s->logs.bytes_out = 0;
171 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
172 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200173
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200174 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200175
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200176 if (s->listener->timeout) {
177 s->req->rto = *s->listener->timeout;
178 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200181}
182
Willy Tarreau07e9e642010-08-17 21:48:17 +0200183/* allocate a new stats frontend named <name>, and return it
184 * (or NULL in case of lack of memory).
185 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200186static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200187{
188 struct proxy *fe;
189
190 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
191 if (!fe)
192 return NULL;
193
Willy Tarreau237250c2011-07-29 01:49:03 +0200194 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200195 fe->next = proxy;
196 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200197 fe->last_change = now.tv_sec;
198 fe->id = strdup("GLOBAL");
199 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200200 fe->maxconn = 10; /* default to 10 concurrent connections */
201 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200202 fe->conf.file = strdup(file);
203 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200204 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200205
206 /* the stats frontend is the only one able to assign ID #0 */
207 fe->conf.id.key = fe->uuid = 0;
208 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200209 return fe;
210}
211
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200213 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
214 * error message into the <err> buffer which will be preallocated. The trailing
215 * '\n' must not be written. The function must be called with <args> pointing to
216 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200217 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200218static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200219 struct proxy *defpx, const char *file, int line,
220 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200221{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200222 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200223 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200224
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226 int cur_arg;
227
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200228 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200229 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 +0200230 return -1;
231 }
232
Willy Tarreau89a63132009-08-16 17:41:45 +0200233 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200234 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200235 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200236 return -1;
237 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200238 }
239
Willy Tarreau4348fad2012-09-20 16:48:07 +0200240 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200241 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200242
Willy Tarreauc53d4222012-09-20 20:19:28 +0200243 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
244 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
245 file, line, args[0], args[1], err && *err ? *err : "error");
246 return -1;
247 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200249 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200250 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200251 static int bind_dumped;
252 struct bind_kw *kw;
253
254 kw = bind_find_kw(args[cur_arg]);
255 if (kw) {
256 if (!kw->parse) {
257 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
258 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200259 return -1;
260 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200261
262 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
263 if (err && *err)
264 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
265 else
266 memprintf(err, "'%s %s' : error encountered while processing '%s'",
267 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200268 return -1;
269 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200270
271 cur_arg += 1 + kw->skip;
272 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200273 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200274
275 if (!bind_dumped) {
276 bind_dump_kws(err);
277 indent_msg(err, 4);
278 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200280
281 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
282 args[0], args[1], args[cur_arg],
283 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
284 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200285 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100286
Willy Tarreauc53d4222012-09-20 20:19:28 +0200287 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
288 l->maxconn = global.stats_fe->maxconn;
289 l->backlog = global.stats_fe->backlog;
290 l->timeout = &global.stats_fe->timeout.client;
291 l->accept = session_accept;
292 l->handler = process_session;
293 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
294 l->nice = -64; /* we want to boost priority for local stats */
295 global.maxsock += l->maxconn;
296 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200298 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100299 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100301
302 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304 return -1;
305 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100307 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200308 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 return -1;
310 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200311 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200312 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200313 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200314 return -1;
315 }
316 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200317 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200319 else if (!strcmp(args[1], "maxconn")) {
320 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321
322 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200323 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 return -1;
325 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200326
327 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200328 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200329 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200330 return -1;
331 }
332 }
333 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200334 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200335 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
336 int cur_arg = 2;
337 unsigned int set = 0;
338
Willy Tarreau91319572013-04-20 09:48:50 +0200339 if (!global.stats_fe) {
340 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
341 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
342 return -1;
343 }
344 }
345
Willy Tarreau35b7b162012-10-22 23:17:18 +0200346 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100347 unsigned int low, high;
348
Willy Tarreau35b7b162012-10-22 23:17:18 +0200349 if (strcmp(args[cur_arg], "all") == 0) {
350 set = 0;
351 break;
352 }
353 else if (strcmp(args[cur_arg], "odd") == 0) {
354 set |= 0x55555555;
355 }
356 else if (strcmp(args[cur_arg], "even") == 0) {
357 set |= 0xAAAAAAAA;
358 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100359 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100360 char *dash = strchr(args[cur_arg], '-');
361
362 low = high = str2uic(args[cur_arg]);
363 if (dash)
364 high = str2uic(dash + 1);
365
366 if (high < low) {
367 unsigned int swap = low;
368 low = high;
369 high = swap;
370 }
371
372 if (low < 1 || high > 32) {
373 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
374 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200375 return -1;
376 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100377
378 while (low <= high)
379 set |= 1 << (low++ - 1);
380 }
381 else {
382 memprintf(err,
383 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
384 args[0], args[1]);
385 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200386 }
387 cur_arg++;
388 }
389 global.stats_fe->bind_proc = set;
390 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200391 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200392 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200393 return -1;
394 }
395 return 0;
396}
397
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100398/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
399 * for clearing it if needed.
400 */
401static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100402{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100403 chunk_appendf(&trash,
404 "# pxname,svname,"
405 "qcur,qmax,"
406 "scur,smax,slim,stot,"
407 "bin,bout,"
408 "dreq,dresp,"
409 "ereq,econ,eresp,"
410 "wretr,wredis,"
411 "status,weight,act,bck,"
412 "chkfail,chkdown,lastchg,downtime,qlimit,"
413 "pid,iid,sid,throttle,lbtot,tracked,type,"
414 "rate,rate_lim,rate_max,"
415 "check_status,check_code,check_duration,"
416 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
417 "req_rate,req_rate_max,req_tot,"
418 "cli_abrt,srv_abrt,"
419 "comp_in,comp_out,comp_byp,comp_rsp,"
420 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100421}
422
Simon Hormand9366582011-06-15 15:18:45 +0900423/* print a string of text buffer to <out>. The format is :
424 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
425 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
426 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
427 */
428static int dump_text(struct chunk *out, const char *buf, int bsize)
429{
430 unsigned char c;
431 int ptr = 0;
432
433 while (buf[ptr] && ptr < bsize) {
434 c = buf[ptr];
435 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
436 if (out->len > out->size - 1)
437 break;
438 out->str[out->len++] = c;
439 }
440 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
441 if (out->len > out->size - 2)
442 break;
443 out->str[out->len++] = '\\';
444 switch (c) {
445 case ' ': c = ' '; break;
446 case '\t': c = 't'; break;
447 case '\n': c = 'n'; break;
448 case '\r': c = 'r'; break;
449 case '\e': c = 'e'; break;
450 case '\\': c = '\\'; break;
451 }
452 out->str[out->len++] = c;
453 }
454 else {
455 if (out->len > out->size - 4)
456 break;
457 out->str[out->len++] = '\\';
458 out->str[out->len++] = 'x';
459 out->str[out->len++] = hextab[(c >> 4) & 0xF];
460 out->str[out->len++] = hextab[c & 0xF];
461 }
462 ptr++;
463 }
464
465 return ptr;
466}
467
468/* print a buffer in hexa.
469 * Print stopped if <bsize> is reached, or if no more place in the chunk.
470 */
471static int dump_binary(struct chunk *out, const char *buf, int bsize)
472{
473 unsigned char c;
474 int ptr = 0;
475
476 while (ptr < bsize) {
477 c = buf[ptr];
478
479 if (out->len > out->size - 2)
480 break;
481 out->str[out->len++] = hextab[(c >> 4) & 0xF];
482 out->str[out->len++] = hextab[c & 0xF];
483
484 ptr++;
485 }
486 return ptr;
487}
488
489/* Dump the status of a table to a stream interface's
490 * read buffer. It returns 0 if the output buffer is full
491 * and needs to be called again, otherwise non-zero.
492 */
493static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
494 struct proxy *proxy, struct proxy *target)
495{
Willy Tarreau306f8302013-07-08 15:53:06 +0200496 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900497
Willy Tarreau77804732012-10-29 16:14:26 +0100498 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900499 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
500
501 /* any other information should be dumped here */
502
Willy Tarreau290e63a2012-09-20 18:07:14 +0200503 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100504 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900505
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200506 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900507 return 0;
508
509 return 1;
510}
511
512/* Dump the a table entry to a stream interface's
513 * read buffer. It returns 0 if the output buffer is full
514 * and needs to be called again, otherwise non-zero.
515 */
516static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
517 struct proxy *proxy, struct stksess *entry)
518{
519 int dt;
520
Willy Tarreau77804732012-10-29 16:14:26 +0100521 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900522
523 if (proxy->table.type == STKTABLE_TYPE_IP) {
524 char addr[INET_ADDRSTRLEN];
525 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100526 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900527 }
528 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
529 char addr[INET6_ADDRSTRLEN];
530 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900532 }
533 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900535 }
536 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900538 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
539 }
540 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100541 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900542 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
543 }
544
Willy Tarreau77804732012-10-29 16:14:26 +0100545 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900546
547 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
548 void *ptr;
549
550 if (proxy->table.data_ofs[dt] == 0)
551 continue;
552 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100553 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900554 else
Willy Tarreau77804732012-10-29 16:14:26 +0100555 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900556
557 ptr = stktable_data_ptr(&proxy->table, entry, dt);
558 switch (stktable_data_types[dt].std_type) {
559 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900561 break;
562 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900564 break;
565 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900567 break;
568 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100569 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900570 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
571 proxy->table.data_arg[dt].u));
572 break;
573 }
574 }
Willy Tarreau77804732012-10-29 16:14:26 +0100575 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900576
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200577 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900578 return 0;
579
580 return 1;
581}
582
Willy Tarreaudec98142012-06-06 23:37:08 +0200583static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900584{
Willy Tarreau306f8302013-07-08 15:53:06 +0200585 struct session *s = session_from_task(si->owner);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100586 struct proxy *px = si->appctx.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900587 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900588 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900589 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200590 long long value;
591 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200592 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200593 void *ptr;
594 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900595
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100596 si->appctx.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900597
598 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100599 si->appctx.ctx.cli.msg = "Key value expected\n";
600 si->appctx.st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900601 return;
602 }
603
Simon Hormanc5b89f62011-06-15 15:18:50 +0900604 switch (px->table.type) {
605 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900606 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100607 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900608 break;
609 case STKTABLE_TYPE_IPV6:
610 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100611 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900612 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900613 case STKTABLE_TYPE_INTEGER:
614 {
615 char *endptr;
616 unsigned long val;
617 errno = 0;
618 val = strtoul(args[4], &endptr, 10);
619 if ((errno == ERANGE && val == ULONG_MAX) ||
620 (errno != 0 && val == 0) || endptr == args[4] ||
621 val > 0xffffffff) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100622 si->appctx.ctx.cli.msg = "Invalid key\n";
623 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900624 return;
625 }
626 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100627 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900628 break;
629 }
630 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900631 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100632 static_table_key->key = args[4];
633 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900634 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900635 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200636 switch (action) {
637 case STAT_CLI_O_TAB:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100638 si->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 +0200639 break;
640 case STAT_CLI_O_CLR:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100641 si->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 +0200642 break;
643 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100644 si->appctx.ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200645 break;
646 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100647 si->appctx.st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900648 return;
649 }
650
651 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200652 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100653 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
654 si->appctx.st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900655 return;
656 }
657
Willy Tarreau07115412012-10-29 21:56:59 +0100658 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900659
Willy Tarreaudec98142012-06-06 23:37:08 +0200660 switch (action) {
661 case STAT_CLI_O_TAB:
662 if (!ts)
663 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100664 chunk_reset(&trash);
665 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900666 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100667 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900668 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200669
670 case STAT_CLI_O_CLR:
671 if (!ts)
672 return;
673 if (ts->ref_cnt) {
674 /* don't delete an entry which is currently referenced */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100675 si->appctx.ctx.cli.msg = "Entry currently in use, cannot remove\n";
676 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200677 return;
678 }
679 stksess_kill(&px->table, ts);
680 break;
Simon Horman17bce342011-06-15 15:18:47 +0900681
Willy Tarreau654694e2012-06-07 01:03:16 +0200682 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200683 if (ts)
684 stktable_touch(&px->table, ts, 1);
685 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100686 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200687 if (!ts) {
688 /* don't delete an entry which is currently referenced */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100689 si->appctx.ctx.cli.msg = "Unable to allocate a new entry\n";
690 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200691 return;
692 }
693 stktable_store(&px->table, ts, 1);
694 }
695
Willy Tarreau47060b62013-08-01 21:11:42 +0200696 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
697 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100698 si->appctx.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
699 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200700 return;
701 }
702
703 data_type = stktable_get_data_type(args[cur_arg] + 5);
704 if (data_type < 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100705 si->appctx.ctx.cli.msg = "Unknown data type\n";
706 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200707 return;
708 }
709
710 if (!px->table.data_ofs[data_type]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100711 si->appctx.ctx.cli.msg = "Data type not stored in this table\n";
712 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200713 return;
714 }
715
716 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100717 si->appctx.ctx.cli.msg = "Require a valid integer value to store\n";
718 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200719 return;
720 }
721
722 ptr = stktable_data_ptr(&px->table, ts, data_type);
723
724 switch (stktable_data_types[data_type].std_type) {
725 case STD_T_SINT:
726 stktable_data_cast(ptr, std_t_sint) = value;
727 break;
728 case STD_T_UINT:
729 stktable_data_cast(ptr, std_t_uint) = value;
730 break;
731 case STD_T_ULL:
732 stktable_data_cast(ptr, std_t_ull) = value;
733 break;
734 case STD_T_FRQP:
735 /* We set both the current and previous values. That way
736 * the reported frequency is stable during all the period
737 * then slowly fades out. This allows external tools to
738 * push measures without having to update them too often.
739 */
740 frqp = &stktable_data_cast(ptr, std_t_frqp);
741 frqp->curr_tick = now_ms;
742 frqp->prev_ctr = 0;
743 frqp->curr_ctr = value;
744 break;
745 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200746 }
747 break;
748
Willy Tarreaudec98142012-06-06 23:37:08 +0200749 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100750 si->appctx.ctx.cli.msg = "Unknown action\n";
751 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200752 break;
Simon Horman121f3052011-06-15 15:18:46 +0900753 }
Simon Horman121f3052011-06-15 15:18:46 +0900754}
755
Willy Tarreau654694e2012-06-07 01:03:16 +0200756static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900757{
Willy Tarreau04b3a192013-04-13 09:41:37 +0200758 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100759 si->appctx.ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
760 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200761 return;
762 }
763
Simon Hormand5b9fd92011-06-15 15:18:48 +0900764 /* condition on stored data value */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100765 si->appctx.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
766 if (si->appctx.ctx.table.data_type < 0) {
767 si->appctx.ctx.cli.msg = "Unknown data type\n";
768 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900769 return;
770 }
771
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100772 if (!((struct proxy *)si->appctx.ctx.table.target)->table.data_ofs[si->appctx.ctx.table.data_type]) {
773 si->appctx.ctx.cli.msg = "Data type not stored in this table\n";
774 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900775 return;
776 }
777
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100778 si->appctx.ctx.table.data_op = get_std_op(args[4]);
779 if (si->appctx.ctx.table.data_op < 0) {
780 si->appctx.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
781 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900782 return;
783 }
784
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100785 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->appctx.ctx.table.value) != 0) {
786 si->appctx.ctx.cli.msg = "Require a valid integer value to compare against\n";
787 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900788 return;
789 }
790}
791
Willy Tarreaudec98142012-06-06 23:37:08 +0200792static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900793{
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100794 si->appctx.ctx.table.data_type = -1;
795 si->appctx.st2 = STAT_ST_INIT;
796 si->appctx.ctx.table.target = NULL;
797 si->appctx.ctx.table.proxy = NULL;
798 si->appctx.ctx.table.entry = NULL;
799 si->appctx.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900800
801 if (*args[2]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100802 si->appctx.ctx.table.target = find_stktable(args[2]);
803 if (!si->appctx.ctx.table.target) {
804 si->appctx.ctx.cli.msg = "No such table\n";
805 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900806 return;
807 }
808 }
809 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200810 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811 goto err_args;
812 return;
813 }
814
815 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200816 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900817 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200818 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900819 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820 goto err_args;
821
822 return;
823
824err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200825 switch (action) {
826 case STAT_CLI_O_TAB:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100827 si->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 +0200828 break;
829 case STAT_CLI_O_CLR:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100830 si->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 +0200831 break;
832 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100833 si->appctx.ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200834 break;
835 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100836 si->appctx.st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900837}
838
Willy Tarreau532a4502011-09-07 22:37:44 +0200839/* Expects to find a frontend named <arg> and returns it, otherwise displays various
840 * adequate error messages and returns NULL. This function also expects the session
841 * level to be admin.
842 */
843static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
844{
845 struct proxy *px;
846
Willy Tarreau290e63a2012-09-20 18:07:14 +0200847 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100848 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
849 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200850 return NULL;
851 }
852
853 if (!*arg) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100854 si->appctx.ctx.cli.msg = "A frontend name is expected.\n";
855 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200856 return NULL;
857 }
858
859 px = findproxy(arg, PR_CAP_FE);
860 if (!px) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100861 si->appctx.ctx.cli.msg = "No such frontend.\n";
862 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200863 return NULL;
864 }
865 return px;
866}
867
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200868/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
869 * and returns the pointer to the server. Otherwise, display adequate error messages
870 * and returns NULL. This function also expects the session level to be admin. Note:
871 * the <arg> is modified to remove the '/'.
872 */
873static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
874{
875 struct proxy *px;
876 struct server *sv;
877 char *line;
878
Willy Tarreau290e63a2012-09-20 18:07:14 +0200879 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100880 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
881 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200882 return NULL;
883 }
884
885 /* split "backend/server" and make <line> point to server */
886 for (line = arg; *line; line++)
887 if (*line == '/') {
888 *line++ = '\0';
889 break;
890 }
891
892 if (!*line || !*arg) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100893 si->appctx.ctx.cli.msg = "Require 'backend/server'.\n";
894 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200895 return NULL;
896 }
897
898 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100899 si->appctx.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
900 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200901 return NULL;
902 }
903
904 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100905 si->appctx.ctx.cli.msg = "Proxy is disabled.\n";
906 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200907 return NULL;
908 }
909
910 return sv;
911}
912
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200913/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200914 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100915 * if the request was understood, otherwise zero. It sets si->appctx.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200916 * designating the function which will have to process the request, which can
917 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200918 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900919static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200920{
Willy Tarreau306f8302013-07-08 15:53:06 +0200921 struct session *s = session_from_task(si->owner);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 char *args[MAX_STATS_ARGS + 1];
923 int arg;
924
925 while (isspace((unsigned char)*line))
926 line++;
927
928 arg = 0;
929 args[arg] = line;
930
931 while (*line && arg < MAX_STATS_ARGS) {
932 if (isspace((unsigned char)*line)) {
933 *line++ = '\0';
934
935 while (isspace((unsigned char)*line))
936 line++;
937
938 args[++arg] = line;
939 continue;
940 }
941
942 line++;
943 }
944
945 while (++arg <= MAX_STATS_ARGS)
946 args[arg] = line;
947
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100948 si->appctx.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200949 if (strcmp(args[0], "show") == 0) {
950 if (strcmp(args[1], "stat") == 0) {
951 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100952 si->appctx.ctx.stats.flags |= STAT_BOUND;
953 si->appctx.ctx.stats.iid = atoi(args[2]);
954 si->appctx.ctx.stats.type = atoi(args[3]);
955 si->appctx.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200956 }
957
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100958 si->appctx.st2 = STAT_ST_INIT;
959 si->appctx.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200960 }
961 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100962 si->appctx.st2 = STAT_ST_INIT;
963 si->appctx.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200964 }
965 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100966 si->appctx.st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200967 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100968 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
969 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200970 return 1;
971 }
Willy Tarreau76153662012-11-26 01:16:39 +0100972 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100973 si->appctx.ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +0100974 else if (*args[2])
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100975 si->appctx.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100976 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100977 si->appctx.ctx.sess.target = NULL;
978 si->appctx.ctx.sess.section = 0; /* start with session status */
979 si->appctx.ctx.sess.pos = 0;
980 si->appctx.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200981 }
982 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200983 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100984 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
985 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200986 return 1;
987 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200988 if (*args[2])
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100989 si->appctx.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200990 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +0100991 si->appctx.ctx.errors.iid = -1;
992 si->appctx.ctx.errors.px = NULL;
993 si->appctx.st2 = STAT_ST_INIT;
994 si->appctx.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200995 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200996 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200997 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200998 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700999 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000 return 0;
1001 }
1002 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001003 else if (strcmp(args[0], "clear") == 0) {
1004 if (strcmp(args[1], "counters") == 0) {
1005 struct proxy *px;
1006 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001007 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001008 int clrall = 0;
1009
1010 if (strcmp(args[2], "all") == 0)
1011 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001012
Willy Tarreau6162db22009-10-10 17:13:00 +02001013 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001014 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1015 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001016 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1017 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001018 return 1;
1019 }
1020
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001021 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001022 if (clrall) {
1023 memset(&px->be_counters, 0, sizeof(px->be_counters));
1024 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1025 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001026 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001027 px->be_counters.conn_max = 0;
1028 px->be_counters.p.http.rps_max = 0;
1029 px->be_counters.sps_max = 0;
1030 px->be_counters.cps_max = 0;
1031 px->be_counters.nbpend_max = 0;
1032
1033 px->fe_counters.conn_max = 0;
1034 px->fe_counters.p.http.rps_max = 0;
1035 px->fe_counters.sps_max = 0;
1036 px->fe_counters.cps_max = 0;
1037 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001038 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001039
1040 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001041 if (clrall)
1042 memset(&sv->counters, 0, sizeof(sv->counters));
1043 else {
1044 sv->counters.cur_sess_max = 0;
1045 sv->counters.nbpend_max = 0;
1046 sv->counters.sps_max = 0;
1047 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001048
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001049 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001050 if (li->counters) {
1051 if (clrall)
1052 memset(li->counters, 0, sizeof(*li->counters));
1053 else
1054 li->counters->conn_max = 0;
1055 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001056 }
1057
Willy Tarreau81c25d02011-09-07 15:17:21 +02001058 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001059 return 1;
1060 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001061 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001062 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001063 /* end of processing */
1064 return 1;
1065 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001066 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001067 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001068 return 0;
1069 }
1070 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001071 else if (strcmp(args[0], "get") == 0) {
1072 if (strcmp(args[1], "weight") == 0) {
1073 struct proxy *px;
1074 struct server *sv;
1075
1076 /* split "backend/server" and make <line> point to server */
1077 for (line = args[2]; *line; line++)
1078 if (*line == '/') {
1079 *line++ = '\0';
1080 break;
1081 }
1082
1083 if (!*line) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001084 si->appctx.ctx.cli.msg = "Require 'backend/server'.\n";
1085 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001086 return 1;
1087 }
1088
1089 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001090 si->appctx.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1091 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001092 return 1;
1093 }
1094
1095 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001096 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1097 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001098 return 1;
1099 }
1100 else { /* not "get weight" */
1101 return 0;
1102 }
1103 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001104 else if (strcmp(args[0], "set") == 0) {
1105 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001106 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001107 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001108
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001109 sv = expect_server_admin(s, si, args[2]);
1110 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001111 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001112
Simon Horman7d09b9a2013-02-12 10:45:51 +09001113 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001114 /*
1115 * The user-weight may now be zero and thus
1116 * the server considered to be draining.
1117 * Update the server's drain state as necessary.
1118 */
1119 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001120 if (warning) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001121 si->appctx.ctx.cli.msg = warning;
1122 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001123 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001124 return 1;
1125 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001126 else if (strcmp(args[1], "timeout") == 0) {
1127 if (strcmp(args[2], "cli") == 0) {
1128 unsigned timeout;
1129 const char *res;
1130
1131 if (!*args[3]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001132 si->appctx.ctx.cli.msg = "Expects an integer value.\n";
1133 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001134 return 1;
1135 }
1136
1137 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1138 if (res || timeout < 1) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001139 si->appctx.ctx.cli.msg = "Invalid timeout value.\n";
1140 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001141 return 1;
1142 }
1143
1144 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1145 return 1;
1146 }
1147 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001148 si->appctx.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1149 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001150 return 1;
1151 }
1152 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001153 else if (strcmp(args[1], "maxconn") == 0) {
1154 if (strcmp(args[2], "frontend") == 0) {
1155 struct proxy *px;
1156 struct listener *l;
1157 int v;
1158
Willy Tarreau532a4502011-09-07 22:37:44 +02001159 px = expect_frontend_admin(s, si, args[3]);
1160 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001161 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001162
1163 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001164 si->appctx.ctx.cli.msg = "Integer value expected.\n";
1165 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001166 return 1;
1167 }
1168
1169 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001170 if (v < 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001171 si->appctx.ctx.cli.msg = "Value out of range.\n";
1172 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001173 return 1;
1174 }
1175
1176 /* OK, the value is fine, so we assign it to the proxy and to all of
1177 * its listeners. The blocked ones will be dequeued.
1178 */
1179 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001180 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001181 l->maxconn = v;
1182 if (l->state == LI_FULL)
1183 resume_listener(l);
1184 }
1185
1186 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1187 dequeue_all_listeners(&s->fe->listener_queue);
1188
1189 return 1;
1190 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001191 else if (strcmp(args[2], "global") == 0) {
1192 int v;
1193
Willy Tarreau290e63a2012-09-20 18:07:14 +02001194 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001195 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1196 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001197 return 1;
1198 }
1199
1200 if (!*args[3]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001201 si->appctx.ctx.cli.msg = "Expects an integer value.\n";
1202 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001203 return 1;
1204 }
1205
1206 v = atoi(args[3]);
1207 if (v > global.hardmaxconn) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001208 si->appctx.ctx.cli.msg = "Value out of range.\n";
1209 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001210 return 1;
1211 }
1212
1213 /* check for unlimited values */
1214 if (v <= 0)
1215 v = global.hardmaxconn;
1216
1217 global.maxconn = v;
1218
1219 /* Dequeues all of the listeners waiting for a resource */
1220 if (!LIST_ISEMPTY(&global_listener_queue))
1221 dequeue_all_listeners(&global_listener_queue);
1222
1223 return 1;
1224 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001225 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001226 si->appctx.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1227 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001228 return 1;
1229 }
1230 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001231 else if (strcmp(args[1], "rate-limit") == 0) {
1232 if (strcmp(args[2], "connections") == 0) {
1233 if (strcmp(args[3], "global") == 0) {
1234 int v;
1235
Willy Tarreau290e63a2012-09-20 18:07:14 +02001236 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001237 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1238 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001239 return 1;
1240 }
1241
1242 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001243 si->appctx.ctx.cli.msg = "Expects an integer value.\n";
1244 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001245 return 1;
1246 }
1247
1248 v = atoi(args[4]);
1249 if (v < 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001250 si->appctx.ctx.cli.msg = "Value out of range.\n";
1251 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001252 return 1;
1253 }
1254
1255 global.cps_lim = v;
1256
1257 /* Dequeues all of the listeners waiting for a resource */
1258 if (!LIST_ISEMPTY(&global_listener_queue))
1259 dequeue_all_listeners(&global_listener_queue);
1260
1261 return 1;
1262 }
1263 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001264 si->appctx.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1265 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001266 return 1;
1267 }
1268 }
William Lallemandd85f9172012-11-09 17:05:39 +01001269 else if (strcmp(args[2], "http-compression") == 0) {
1270 if (strcmp(args[3], "global") == 0) {
1271 int v;
1272
Willy Tarreau85d47f92012-11-21 00:29:50 +01001273 if (!*args[4]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001274 si->appctx.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1275 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001276 return 1;
1277 }
1278
William Lallemandd85f9172012-11-09 17:05:39 +01001279 v = atoi(args[4]);
1280 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1281 }
1282 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001283 si->appctx.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1284 si->appctx.st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001285 return 1;
1286 }
1287 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001288 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001289 si->appctx.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
1290 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001291 return 1;
1292 }
1293 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001294 else if (strcmp(args[1], "table") == 0) {
1295 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1296 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001297 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001298 return 0;
1299 }
1300 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001301 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001302 if (strcmp(args[1], "agent") == 0) {
1303 struct server *sv;
1304
1305 sv = expect_server_admin(s, si, args[2]);
1306 if (!sv)
1307 return 1;
1308
1309 sv->agent.state &= ~CHK_STATE_DISABLED;
1310
1311 return 1;
1312 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001313 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001314 struct server *sv;
1315
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001316 sv = expect_server_admin(s, si, args[2]);
1317 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001318 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001319
Cyril Bontécd19e512010-01-31 22:34:03 +01001320 if (sv->state & SRV_MAINTAIN) {
1321 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001322 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001323 /* If this server tracks the status of another one,
1324 * we must restore the good status.
1325 */
Willy Tarreau44267702011-10-28 15:35:33 +02001326 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001327 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001328 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001329 } else {
1330 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001331 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001332 }
1333 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001334 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001335 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001336 }
1337 }
1338
Willy Tarreau532a4502011-09-07 22:37:44 +02001339 return 1;
1340 }
1341 else if (strcmp(args[1], "frontend") == 0) {
1342 struct proxy *px;
1343
1344 px = expect_frontend_admin(s, si, args[2]);
1345 if (!px)
1346 return 1;
1347
1348 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001349 si->appctx.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1350 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001351 return 1;
1352 }
1353
1354 if (px->state != PR_STPAUSED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001355 si->appctx.ctx.cli.msg = "Frontend is already enabled.\n";
1356 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001357 return 1;
1358 }
1359
1360 if (!resume_proxy(px)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001361 si->appctx.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1362 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001363 return 1;
1364 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001365 return 1;
1366 }
1367 else { /* unknown "enable" parameter */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001368 si->appctx.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1369 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001370 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001371 }
1372 }
1373 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001374 if (strcmp(args[1], "agent") == 0) {
1375 struct server *sv;
1376
1377 sv = expect_server_admin(s, si, args[2]);
1378 if (!sv)
1379 return 1;
1380
1381 sv->agent.state |= CHK_STATE_DISABLED;
1382
1383 return 1;
1384 }
1385 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001386 struct server *sv;
1387
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001388 sv = expect_server_admin(s, si, args[2]);
1389 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001390 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001391
Cyril Bontécd19e512010-01-31 22:34:03 +01001392 if (! (sv->state & SRV_MAINTAIN)) {
1393 /* Not already in maintenance, we can change the server state */
1394 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001395 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001396 }
1397
Willy Tarreau532a4502011-09-07 22:37:44 +02001398 return 1;
1399 }
1400 else if (strcmp(args[1], "frontend") == 0) {
1401 struct proxy *px;
1402
1403 px = expect_frontend_admin(s, si, args[2]);
1404 if (!px)
1405 return 1;
1406
1407 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001408 si->appctx.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1409 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001410 return 1;
1411 }
1412
1413 if (px->state == PR_STPAUSED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001414 si->appctx.ctx.cli.msg = "Frontend is already disabled.\n";
1415 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001416 return 1;
1417 }
1418
1419 if (!pause_proxy(px)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001420 si->appctx.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1421 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001422 return 1;
1423 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001424 return 1;
1425 }
1426 else { /* unknown "disable" parameter */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001427 si->appctx.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1428 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001429 return 1;
1430 }
1431 }
1432 else if (strcmp(args[0], "shutdown") == 0) {
1433 if (strcmp(args[1], "frontend") == 0) {
1434 struct proxy *px;
1435
1436 px = expect_frontend_admin(s, si, args[2]);
1437 if (!px)
1438 return 1;
1439
1440 if (px->state == PR_STSTOPPED) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001441 si->appctx.ctx.cli.msg = "Frontend was already shut down.\n";
1442 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001443 return 1;
1444 }
1445
1446 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1447 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1448 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1449 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1450 stop_proxy(px);
1451 return 1;
1452 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001453 else if (strcmp(args[1], "session") == 0) {
1454 struct session *sess, *ptr;
1455
Willy Tarreau290e63a2012-09-20 18:07:14 +02001456 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001457 si->appctx.ctx.cli.msg = stats_permission_denied_msg;
1458 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001459 return 1;
1460 }
1461
1462 if (!*args[2]) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001463 si->appctx.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1464 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001465 return 1;
1466 }
1467
1468 ptr = (void *)strtoul(args[2], NULL, 0);
1469
1470 /* first, look for the requested session in the session table */
1471 list_for_each_entry(sess, &sessions, list) {
1472 if (sess == ptr)
1473 break;
1474 }
1475
1476 /* do we have the session ? */
1477 if (sess != ptr) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001478 si->appctx.ctx.cli.msg = "No such session (use 'show sess').\n";
1479 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001480 return 1;
1481 }
1482
1483 session_shutdown(sess, SN_ERR_KILLED);
1484 return 1;
1485 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001486 else if (strcmp(args[1], "sessions") == 0) {
1487 if (strcmp(args[2], "server") == 0) {
1488 struct server *sv;
1489 struct session *sess, *sess_bck;
1490
1491 sv = expect_server_admin(s, si, args[3]);
1492 if (!sv)
1493 return 1;
1494
1495 /* kill all the session that are on this server */
1496 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1497 if (sess->srv_conn == sv)
1498 session_shutdown(sess, SN_ERR_KILLED);
1499
1500 return 1;
1501 }
1502 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001503 si->appctx.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1504 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001505 return 1;
1506 }
1507 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001508 else { /* unknown "disable" parameter */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001509 si->appctx.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1510 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001511 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001512 }
1513 }
1514 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001515 return 0;
1516 }
1517 return 1;
1518}
1519
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001520/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001521 * used to processes I/O from/to the stats unix socket. The system relies on a
1522 * state machine handling requests and various responses. We read a request,
1523 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001524 * Then we can read again. The state is stored in si->appctx.st0 and is one of the
1525 * STAT_CLI_* constants. si->appctx.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001526 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001527 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001528static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001529{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001530 struct channel *req = si->ob;
1531 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532 int reql;
1533 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001534
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001535 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1536 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001537
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 while (1) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001539 if (si->appctx.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001540 /* Stats output not initialized yet */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001541 memset(&si->appctx.ctx.stats, 0, sizeof(si->appctx.ctx.stats));
1542 si->appctx.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001543 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001544 else if (si->appctx.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001545 /* Let's close for real now. We just close the request
1546 * side, the conditions below will complete if needed.
1547 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001548 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001549 break;
1550 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001551 else if (si->appctx.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001552 /* ensure we have some output room left in the event we
1553 * would want to return some info right after parsing.
1554 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001555 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001556 break;
1557
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001558 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001559 if (reql <= 0) { /* closed or EOL not found */
1560 if (reql == 0)
1561 break;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001562 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001563 continue;
1564 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001565
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001566 /* seek for a possible semi-colon. If we find one, we
1567 * replace it with an LF and skip only this part.
1568 */
1569 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001570 if (trash.str[len] == ';') {
1571 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 reql = len + 1;
1573 break;
1574 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001575
Willy Tarreau816fc222009-10-04 07:36:58 +02001576 /* now it is time to check that we have a full line,
1577 * remove the trailing \n and possibly \r, then cut the
1578 * line.
1579 */
1580 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001581 if (trash.str[len] != '\n') {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001582 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001583 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001584 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001586 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001587 len--;
1588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001589 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001590
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001591 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001592 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001593 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001594 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001595 continue;
1596 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001597 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001598 si->appctx.st1 = !si->appctx.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001599 else if (strcmp(trash.str, "help") == 0 ||
1600 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001601 si->appctx.ctx.cli.msg = stats_sock_usage_msg;
1602 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001603 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001604 /* NB: stats_sock_parse_request() may have put
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001605 * another STAT_CLI_O_* into si->appctx.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001606 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001607 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001608 else if (!si->appctx.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609 /* if prompt is disabled, print help on empty lines,
1610 * so that the user at least knows how to enable
1611 * prompt and find help.
1612 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001613 si->appctx.ctx.cli.msg = stats_sock_usage_msg;
1614 si->appctx.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001615 }
1616
1617 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001618 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001619 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001620 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001621 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001622 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001623 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001624 continue;
1625 }
1626
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001627 switch (si->appctx.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001628 case STAT_CLI_PRINT:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001629 if (bi_putstr(si->ib, si->appctx.ctx.cli.msg) != -1)
1630 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001631 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001632 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001633 if (stats_dump_info_to_buffer(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001634 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001635 break;
1636 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001637 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001638 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001639 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001640 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001641 if (stats_dump_sess_to_buffer(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001642 si->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_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001645 if (stats_dump_errors_to_buffer(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001646 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001647 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001648 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001649 case STAT_CLI_O_CLR:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001650 if (stats_table_request(si, si->appctx.st0))
1651 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001652 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001653 default: /* abnormal state */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001654 si->appctx.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001655 break;
1656 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001657
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001658 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001659 if (si->appctx.st0 == STAT_CLI_PROMPT) {
1660 if (bi_putstr(si->ib, si->appctx.st1 ? "\n> " : "\n") != -1)
1661 si->appctx.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001662 }
1663
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001664 /* If the output functions are still there, it means they require more room. */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001665 if (si->appctx.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001666 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667
1668 /* Now we close the output if one of the writers did so,
1669 * or if we're not in interactive mode and the request
1670 * buffer is empty. This still allows pipelined requests
1671 * to be sent in non-interactive mode.
1672 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001673 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->appctx.st1 && !req->buf->o)) {
1674 si->appctx.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001675 continue;
1676 }
1677
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001678 /* switch state back to GETREQ to read next requests */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001679 si->appctx.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001680 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001681 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001682
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001683 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->appctx.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001684 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1685 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001686 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001687 * and nothing more to consume. This is comparable to a broken pipe, so
1688 * we forward the close to the request side so that it flows upstream to
1689 * the client.
1690 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001691 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001692 }
1693
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001694 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->appctx.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001695 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1696 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1697 /* We have no more processing to do, and nothing more to send, and
1698 * the client side has closed. So we'll forward this state downstream
1699 * on the response buffer.
1700 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001701 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001702 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001703 }
1704
1705 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001706 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001707
1708 /* we don't want to expire timeouts while we're processing requests */
1709 si->ib->rex = TICK_ETERNITY;
1710 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001711
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001712 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001713 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 +02001714 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001715 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 +02001716
1717 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1718 /* check that we have released everything then unregister */
1719 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001720 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001721}
1722
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001723/* This function dumps information onto the stream interface's read buffer.
1724 * It returns 0 as long as it does not complete, non-zero upon completion.
1725 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001726 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001727static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001728{
1729 unsigned int up = (now.tv_sec - start_date.tv_sec);
1730
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001731 chunk_printf(&trash,
1732 "Name: " PRODUCT_NAME "\n"
1733 "Version: " HAPROXY_VERSION "\n"
1734 "Release_date: " HAPROXY_DATE "\n"
1735 "Nbproc: %d\n"
1736 "Process_num: %d\n"
1737 "Pid: %d\n"
1738 "Uptime: %dd %dh%02dm%02ds\n"
1739 "Uptime_sec: %d\n"
1740 "Memmax_MB: %d\n"
1741 "Ulimit-n: %d\n"
1742 "Maxsock: %d\n"
1743 "Maxconn: %d\n"
1744 "Hard_maxconn: %d\n"
1745 "Maxpipes: %d\n"
1746 "CurrConns: %d\n"
1747 "PipesUsed: %d\n"
1748 "PipesFree: %d\n"
1749 "ConnRate: %d\n"
1750 "ConnRateLimit: %d\n"
1751 "MaxConnRate: %d\n"
1752 "CompressBpsIn: %u\n"
1753 "CompressBpsOut: %u\n"
1754 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001755#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001756 "ZlibMemUsage: %ld\n"
1757 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001758#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001759 "Tasks: %d\n"
1760 "Run_queue: %d\n"
1761 "Idle_pct: %d\n"
1762 "node: %s\n"
1763 "description: %s\n"
1764 "",
1765 global.nbproc,
1766 relative_pid,
1767 pid,
1768 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1769 up,
1770 global.rlimit_memmax,
1771 global.rlimit_nofile,
1772 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1773 actconn, pipes_used, pipes_free,
1774 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1775 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1776 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001777#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001778 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001779#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001780 nb_tasks_cur, run_queue_cur, idle_pct,
1781 global.node, global.desc ? global.desc : ""
1782 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001783
1784 if (bi_putchk(si->ib, &trash) == -1)
1785 return 0;
1786
1787 return 1;
1788}
1789
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001790/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1791 * the state from stream interface <si>. The caller is responsible for clearing
1792 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001793 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001794static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001795{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001796 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001797
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001798 if (!(px->cap & PR_CAP_FE))
1799 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001800
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001801 if ((si->appctx.ctx.stats.flags & STAT_BOUND) && !(si->appctx.ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001802 return 0;
1803
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001804 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001805 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001806 /* name, queue */
1807 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001808
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01001809 if (px->cap & PR_CAP_BE && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001810 /* Column sub-heading for Enable or Disable server */
1811 chunk_appendf(&trash, "<td></td>");
1812 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001813
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001814 chunk_appendf(&trash,
1815 "<td class=ac>"
1816 "<a name=\"%s/Frontend\"></a>"
1817 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1818 "<td colspan=3></td>"
1819 "",
1820 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001821
Willy Tarreau466c9b52012-12-23 02:25:03 +01001822 chunk_appendf(&trash,
1823 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001824 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001825 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1826 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1827 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001828 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1829 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1830 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001831
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001832 if (px->mode == PR_MODE_HTTP)
1833 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001834 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001835 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001836
1837 chunk_appendf(&trash,
1838 "</table></div></u></td>"
1839 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001840 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001841 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1842 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1843 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001844 U2H(px->fe_counters.sps_max),
1845 U2H(px->fe_counters.cps_max),
1846 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001847
1848 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001849 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001850 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001851 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001852
1853 chunk_appendf(&trash,
1854 "</table></div></u></td>"
1855 /* sessions rate : limit */
1856 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001857 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001858
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001859 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001860 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001861 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001862 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001863 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1864 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001865 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001866 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1867 U2H(px->fe_counters.cum_sess),
1868 U2H(px->fe_counters.cum_conn),
1869 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001870
Willy Tarreau466c9b52012-12-23 02:25:03 +01001871 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001872 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001873 chunk_appendf(&trash,
1874 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1875 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1876 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1877 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1878 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1879 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1880 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1881 "<tr><th>- other responses:</th><td>%s</td></tr>"
1882 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1883 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001884 U2H(px->fe_counters.p.http.cum_req),
1885 U2H(px->fe_counters.p.http.rsp[1]),
1886 U2H(px->fe_counters.p.http.rsp[2]),
1887 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001888 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001889 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001890 U2H(px->fe_counters.p.http.rsp[3]),
1891 U2H(px->fe_counters.p.http.rsp[4]),
1892 U2H(px->fe_counters.p.http.rsp[5]),
1893 U2H(px->fe_counters.p.http.rsp[0]),
1894 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001895 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001896
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001897 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001898 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001899 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001900 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001901 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001902 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001903 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001904 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001905
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001906 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001907 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001908 "<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 +01001909 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001910 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001911 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1912 px->fe_counters.comp_in ?
1913 (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 +01001914 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001915
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001916 chunk_appendf(&trash,
1917 /* denied: req, resp */
1918 "<td>%s</td><td>%s</td>"
1919 /* errors : request, connect, response */
1920 "<td>%s</td><td></td><td></td>"
1921 /* warnings: retries, redispatches */
1922 "<td></td><td></td>"
1923 /* server status : reflect frontend status */
1924 "<td class=ac>%s</td>"
1925 /* rest of server: nothing */
1926 "<td class=ac colspan=8></td></tr>"
1927 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001928 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1929 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001930 px->state == PR_STREADY ? "OPEN" :
1931 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001932 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001933 else { /* CSV mode */
1934 chunk_appendf(&trash,
1935 /* pxid, name, queue cur, queue max, */
1936 "%s,FRONTEND,,,"
1937 /* sessions : current, max, limit, total */
1938 "%d,%d,%d,%lld,"
1939 /* bytes : in, out */
1940 "%lld,%lld,"
1941 /* denied: req, resp */
1942 "%lld,%lld,"
1943 /* errors : request, connect, response */
1944 "%lld,,,"
1945 /* warnings: retries, redispatches */
1946 ",,"
1947 /* server status : reflect frontend status */
1948 "%s,"
1949 /* rest of server: nothing */
1950 ",,,,,,,,"
1951 /* pid, iid, sid, throttle, lbtot, tracked, type */
1952 "%d,%d,0,,,,%d,"
1953 /* rate, rate_lim, rate_max */
1954 "%u,%u,%u,"
1955 /* check_status, check_code, check_duration */
1956 ",,,",
1957 px->id,
1958 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1959 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1960 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1961 px->fe_counters.failed_req,
1962 px->state == PR_STREADY ? "OPEN" :
1963 px->state == PR_STFULL ? "FULL" : "STOP",
1964 relative_pid, px->uuid, STATS_TYPE_FE,
1965 read_freq_ctr(&px->fe_sess_per_sec),
1966 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001967
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001968 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1969 if (px->mode == PR_MODE_HTTP) {
1970 for (i=1; i<6; i++)
1971 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1972 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1973 }
1974 else
1975 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001976
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001977 /* failed health analyses */
1978 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001979
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001980 /* requests : req_rate, req_rate_max, req_tot, */
1981 chunk_appendf(&trash, "%u,%u,%lld,",
1982 read_freq_ctr(&px->fe_req_per_sec),
1983 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1984
1985 /* errors: cli_aborts, srv_aborts */
1986 chunk_appendf(&trash, ",,");
1987
1988 /* compression: in, out, bypassed */
1989 chunk_appendf(&trash, "%lld,%lld,%lld,",
1990 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1991
1992 /* compression: comp_rsp */
1993 chunk_appendf(&trash, "%lld,",
1994 px->fe_counters.p.http.comp_rsp);
1995
1996 /* finish with EOL */
1997 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001998 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001999 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002000}
2001
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002002/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2003 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2004 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2005 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002006 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002007static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002008{
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002009 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002010 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002011 if (px->cap & PR_CAP_BE && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002012 /* Column sub-heading for Enable or Disable server */
2013 chunk_appendf(&trash, "<td></td>");
2014 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002015 chunk_appendf(&trash,
2016 /* frontend name, listener name */
2017 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2018 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2019 "",
2020 px->id, l->name,
2021 (flags & ST_SHLGNDS)?"<u>":"",
2022 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002023
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002024 if (flags & ST_SHLGNDS) {
2025 char str[INET6_ADDRSTRLEN];
2026 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002027
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002028 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002029
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002030 port = get_host_port(&l->addr);
2031 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2032 case AF_INET:
2033 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2034 break;
2035 case AF_INET6:
2036 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2037 break;
2038 case AF_UNIX:
2039 chunk_appendf(&trash, "unix, ");
2040 break;
2041 case -1:
2042 chunk_appendf(&trash, "(%s), ", strerror(errno));
2043 break;
2044 }
Willy Tarreau91861262007-10-17 17:06:05 +02002045
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002046 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002047 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002048 }
Willy Tarreau91861262007-10-17 17:06:05 +02002049
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002050 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002051 /* queue */
2052 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002053 /* sessions rate: current, max, limit */
2054 "<td colspan=3>&nbsp;</td>"
2055 /* sessions: current, max, limit, total, lbtot */
2056 "<td>%s</td><td>%s</td><td>%s</td>"
2057 "<td>%s</td><td>&nbsp;</td>"
2058 /* bytes: in, out */
2059 "<td>%s</td><td>%s</td>"
2060 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002061 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002062 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2063 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002064
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002065 chunk_appendf(&trash,
2066 /* denied: req, resp */
2067 "<td>%s</td><td>%s</td>"
2068 /* errors: request, connect, response */
2069 "<td>%s</td><td></td><td></td>"
2070 /* warnings: retries, redispatches */
2071 "<td></td><td></td>"
2072 /* server status: reflect listener status */
2073 "<td class=ac>%s</td>"
2074 /* rest of server: nothing */
2075 "<td class=ac colspan=8></td></tr>"
2076 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002077 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2078 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002079 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2080 }
2081 else { /* CSV mode */
2082 chunk_appendf(&trash,
2083 /* pxid, name, queue cur, queue max, */
2084 "%s,%s,,,"
2085 /* sessions: current, max, limit, total */
2086 "%d,%d,%d,%lld,"
2087 /* bytes: in, out */
2088 "%lld,%lld,"
2089 /* denied: req, resp */
2090 "%lld,%lld,"
2091 /* errors: request, connect, response */
2092 "%lld,,,"
2093 /* warnings: retries, redispatches */
2094 ",,"
2095 /* server status: reflect listener status */
2096 "%s,"
2097 /* rest of server: nothing */
2098 ",,,,,,,,"
2099 /* pid, iid, sid, throttle, lbtot, tracked, type */
2100 "%d,%d,%d,,,,%d,"
2101 /* rate, rate_lim, rate_max */
2102 ",,,"
2103 /* check_status, check_code, check_duration */
2104 ",,,"
2105 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2106 ",,,,,,"
2107 /* failed health analyses */
2108 ","
2109 /* requests : req_rate, req_rate_max, req_tot, */
2110 ",,,"
2111 /* errors: cli_aborts, srv_aborts */
2112 ",,"
2113 /* compression: in, out, bypassed, comp_rsp */
2114 ",,,,"
2115 "\n",
2116 px->id, l->name,
2117 l->nbconn, l->counters->conn_max,
2118 l->maxconn, l->counters->cum_conn,
2119 l->counters->bytes_in, l->counters->bytes_out,
2120 l->counters->denied_req, l->counters->denied_resp,
2121 l->counters->failed_req,
2122 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2123 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2124 }
2125 return 1;
2126}
Willy Tarreau91861262007-10-17 17:06:05 +02002127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002128/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2129 * from stream interface <si>, stats flags <flags>, and server state <state>.
2130 * The caller is responsible for clearing the trash if needed. Returns non-zero
2131 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002132 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2133 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002134 */
2135static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2136{
2137 struct server *ref = sv->track ? sv->track : sv;
2138 char str[INET6_ADDRSTRLEN];
2139 struct chunk src;
2140 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002141
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002142 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002143 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002144 "DOWN",
2145 "DN %d/%d &uarr;",
2146 "UP %d/%d &darr;",
2147 "UP",
2148 "NOLB %d/%d &darr;",
2149 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002150 "DRAIN %d/%d &darr;",
2151 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002152 "<i>no check</i>"
2153 };
Willy Tarreau91861262007-10-17 17:06:05 +02002154
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002155 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2156 chunk_appendf(&trash, "<tr class=\"maintain\">");
2157 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002158 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002159 "<tr class=\"%s%d\">",
2160 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002161
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002162 if ((px->cap & PR_CAP_BE) && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002163 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002164 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2165 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002166
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002167 chunk_appendf(&trash,
2168 "<td class=ac><a name=\"%s/%s\"></a>%s"
2169 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2170 "",
2171 px->id, sv->id,
2172 (flags & ST_SHLGNDS) ? "<u>" : "",
2173 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002175 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002176 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002177
2178 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2179 case AF_INET:
2180 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2181 break;
2182 case AF_INET6:
2183 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2184 break;
2185 case AF_UNIX:
2186 chunk_appendf(&trash, "unix, ");
2187 break;
2188 case -1:
2189 chunk_appendf(&trash, "(%s), ", strerror(errno));
2190 break;
2191 default: /* address family not supported */
2192 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002193 }
Willy Tarreau91861262007-10-17 17:06:05 +02002194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002195 /* id */
2196 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002197
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002198 /* cookie */
2199 if (sv->cookie) {
2200 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002201
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002202 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2203 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002205 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002206 }
2207
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002208 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002209 }
Willy Tarreau91861262007-10-17 17:06:05 +02002210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002211 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002212 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002213 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002214 /* sessions rate : current, max, limit */
2215 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002216 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002217 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002218 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2219 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002220
Willy Tarreau466c9b52012-12-23 02:25:03 +01002221
2222 chunk_appendf(&trash,
2223 /* sessions: current, max, limit, total */
2224 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002225 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002226 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2227 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002228 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2229 U2H(sv->counters.cum_sess),
2230 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002231
Willy Tarreau466c9b52012-12-23 02:25:03 +01002232 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2233 if (px->mode == PR_MODE_HTTP) {
2234 unsigned long long tot;
2235 for (tot = i = 0; i < 6; i++)
2236 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002237
Willy Tarreau466c9b52012-12-23 02:25:03 +01002238 chunk_appendf(&trash,
2239 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2240 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2241 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2242 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2243 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2244 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2245 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2246 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002247 U2H(tot),
2248 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2249 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2250 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2251 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2252 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2253 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 +02002254 }
Willy Tarreau91861262007-10-17 17:06:05 +02002255
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002256 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002257 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002258 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002259 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002260 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002261
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002262 chunk_appendf(&trash,
2263 /* bytes : in, out */
2264 "<td>%s</td><td>%s</td>"
2265 /* denied: req, resp */
2266 "<td></td><td>%s</td>"
2267 /* errors : request, connect */
2268 "<td></td><td>%s</td>"
2269 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002270 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002271 /* warnings: retries, redispatches */
2272 "<td>%lld</td><td>%lld</td>"
2273 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002274 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2275 U2H(sv->counters.failed_secu),
2276 U2H(sv->counters.failed_conns),
2277 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002278 sv->counters.cli_aborts,
2279 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002280 sv->counters.retries, sv->counters.redispatches);
2281
2282 /* status, lest check */
2283 chunk_appendf(&trash, "<td class=ac>");
2284
2285 if (sv->state & SRV_MAINTAIN) {
2286 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2287 chunk_appendf(&trash, "MAINT");
2288 }
2289 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2290 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2291 chunk_appendf(&trash, "MAINT(via)");
2292 }
2293 else if (ref->state & SRV_CHECKED) {
2294 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2295 chunk_appendf(&trash,
2296 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002297 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2298 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002299 }
Willy Tarreau91861262007-10-17 17:06:05 +02002300
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002301 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002302 chunk_appendf(&trash,
2303 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002304 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002305 get_check_status_info(sv->check.status));
2306
2307 if (sv->check.status >= HCHK_STATUS_L57DATA)
2308 chunk_appendf(&trash, "/%d", sv->check.code);
2309
2310 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002311 chunk_appendf(&trash, " in %lums", sv->check.duration);
2312
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002313 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002314 get_check_status_description(sv->check.status));
2315 if (*sv->check.desc) {
2316 chunk_appendf(&trash, ": ");
2317 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2318 chunk_htmlencode(&trash, &src);
2319 }
2320 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002321 }
2322 else
2323 chunk_appendf(&trash, "</td><td>");
2324
2325 chunk_appendf(&trash,
2326 /* weight */
2327 "</td><td class=ac>%d</td>"
2328 /* act, bck */
2329 "<td class=ac>%s</td><td class=ac>%s</td>"
2330 "",
2331 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2332 (sv->state & SRV_BACKUP) ? "-" : "Y",
2333 (sv->state & SRV_BACKUP) ? "Y" : "-");
2334
2335 /* check failures: unique, fatal, down time */
2336 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002337 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002338
2339 if (ref->observe)
2340 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2341
2342 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002343 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002344 "<td>%lld</td><td>%s</td>"
2345 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002346 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002347 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2348 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002349 else if (sv != ref) {
2350 if (sv->state & SRV_MAINTAIN)
2351 chunk_appendf(&trash,
2352 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2353 ref->proxy->id, ref->id);
2354 else
2355 chunk_appendf(&trash,
2356 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2357 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2358 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359 else
2360 chunk_appendf(&trash, "<td colspan=3></td>");
2361
2362 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002363 if (sv->state & SRV_WARMINGUP)
2364 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002365 else
2366 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2367 }
2368 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002369 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002370 "DOWN,",
2371 "DOWN %d/%d,",
2372 "UP %d/%d,",
2373 "UP,",
2374 "NOLB %d/%d,",
2375 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002376 "DRAIN %d/%d,",
2377 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002378 "no check,"
2379 };
2380
2381 chunk_appendf(&trash,
2382 /* pxid, name */
2383 "%s,%s,"
2384 /* queue : current, max */
2385 "%d,%d,"
2386 /* sessions : current, max, limit, total */
2387 "%d,%d,%s,%lld,"
2388 /* bytes : in, out */
2389 "%lld,%lld,"
2390 /* denied: req, resp */
2391 ",%lld,"
2392 /* errors : request, connect, response */
2393 ",%lld,%lld,"
2394 /* warnings: retries, redispatches */
2395 "%lld,%lld,"
2396 "",
2397 px->id, sv->id,
2398 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002399 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002400 sv->counters.bytes_in, sv->counters.bytes_out,
2401 sv->counters.failed_secu,
2402 sv->counters.failed_conns, sv->counters.failed_resp,
2403 sv->counters.retries, sv->counters.redispatches);
2404
2405 /* status */
2406 if (sv->state & SRV_MAINTAIN)
2407 chunk_appendf(&trash, "MAINT,");
2408 else if (ref != sv && ref->state & SRV_MAINTAIN)
2409 chunk_appendf(&trash, "MAINT(via),");
2410 else
2411 chunk_appendf(&trash,
2412 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002413 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2414 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002415
2416 chunk_appendf(&trash,
2417 /* weight, active, backup */
2418 "%d,%d,%d,"
2419 "",
2420 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2421 (sv->state & SRV_BACKUP) ? 0 : 1,
2422 (sv->state & SRV_BACKUP) ? 1 : 0);
2423
2424 /* check failures: unique, fatal; last change, total downtime */
2425 if (sv->state & SRV_CHECKED)
2426 chunk_appendf(&trash,
2427 "%lld,%lld,%d,%d,",
2428 sv->counters.failed_checks, sv->counters.down_trans,
2429 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2430 else
2431 chunk_appendf(&trash, ",,,,");
2432
2433 /* queue limit, pid, iid, sid, */
2434 chunk_appendf(&trash,
2435 "%s,"
2436 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002437 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002438 relative_pid, px->uuid, sv->puid);
2439
2440 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002441 if (sv->state & SRV_WARMINGUP)
2442 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002443
2444 /* sessions: lbtot */
2445 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2446
2447 /* tracked */
2448 if (sv->track)
2449 chunk_appendf(&trash, "%s/%s,",
2450 sv->track->proxy->id, sv->track->id);
2451 else
2452 chunk_appendf(&trash, ",");
2453
2454 /* type */
2455 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2456
2457 /* rate */
2458 chunk_appendf(&trash, "%u,,%u,",
2459 read_freq_ctr(&sv->sess_per_sec),
2460 sv->counters.sps_max);
2461
2462 if (sv->state & SRV_CHECKED) {
2463 /* check_status */
2464 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2465
2466 /* check_code */
2467 if (sv->check.status >= HCHK_STATUS_L57DATA)
2468 chunk_appendf(&trash, "%u,", sv->check.code);
2469 else
2470 chunk_appendf(&trash, ",");
2471
2472 /* check_duration */
2473 if (sv->check.status >= HCHK_STATUS_CHECKED)
2474 chunk_appendf(&trash, "%lu,", sv->check.duration);
2475 else
2476 chunk_appendf(&trash, ",");
2477
2478 }
2479 else
2480 chunk_appendf(&trash, ",,,");
2481
2482 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2483 if (px->mode == PR_MODE_HTTP) {
2484 for (i=1; i<6; i++)
2485 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2486
2487 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2488 }
2489 else
2490 chunk_appendf(&trash, ",,,,,,");
2491
2492 /* failed health analyses */
2493 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2494
2495 /* requests : req_rate, req_rate_max, req_tot, */
2496 chunk_appendf(&trash, ",,,");
2497
2498 /* errors: cli_aborts, srv_aborts */
2499 chunk_appendf(&trash, "%lld,%lld,",
2500 sv->counters.cli_aborts, sv->counters.srv_aborts);
2501
2502 /* compression: in, out, bypassed, comp_rsp */
2503 chunk_appendf(&trash, ",,,,");
2504
2505 /* finish with EOL */
2506 chunk_appendf(&trash, "\n");
2507 }
2508 return 1;
2509}
2510
2511/* Dumps a line for backend <px> to the trash for and uses the state from stream
2512 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2513 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2514 */
2515static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2516{
2517 struct chunk src;
2518 int i;
2519
2520 if (!(px->cap & PR_CAP_BE))
2521 return 0;
2522
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002523 if ((si->appctx.ctx.stats.flags & STAT_BOUND) && !(si->appctx.ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002524 return 0;
2525
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002526 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002527 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002528 if (px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002529 /* Column sub-heading for Enable or Disable server */
2530 chunk_appendf(&trash, "<td></td>");
2531 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002532 chunk_appendf(&trash,
2533 "<td class=ac>"
2534 /* name */
2535 "%s<a name=\"%s/Backend\"></a>"
2536 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2537 "",
2538 (flags & ST_SHLGNDS)?"<u>":"",
2539 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002540
2541 if (flags & ST_SHLGNDS) {
2542 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002543 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002544 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2545
2546 /* cookie */
2547 if (px->cookie_name) {
2548 chunk_appendf(&trash, ", cookie: '");
2549 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2550 chunk_htmlencode(&trash, &src);
2551 chunk_appendf(&trash, "'");
2552 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002553 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002554 }
2555
2556 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002557 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002558 /* queue : current, max */
2559 "<td>%s</td><td>%s</td><td></td>"
2560 /* sessions rate : current, max, limit */
2561 "<td>%s</td><td>%s</td><td></td>"
2562 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002564 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2565 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002566
2567 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002568 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002570 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002571 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002573 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2574 U2H(px->be_counters.cum_conn),
2575 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002576
Willy Tarreau466c9b52012-12-23 02:25:03 +01002577 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002579 chunk_appendf(&trash,
2580 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2581 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2582 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2583 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2584 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2585 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2586 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2587 "<tr><th>- other responses:</th><td>%s</td></tr>"
2588 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2589 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002590 U2H(px->be_counters.p.http.cum_req),
2591 U2H(px->be_counters.p.http.rsp[1]),
2592 U2H(px->be_counters.p.http.rsp[2]),
2593 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002595 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002596 U2H(px->be_counters.p.http.rsp[3]),
2597 U2H(px->be_counters.p.http.rsp[4]),
2598 U2H(px->be_counters.p.http.rsp[5]),
2599 U2H(px->be_counters.p.http.rsp[0]),
2600 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002601 }
2602
2603 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002604 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002605 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002606 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002608 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002609 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002610 U2H(px->be_counters.cum_lbconn),
2611 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002613 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002614 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002615 "<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 +01002616 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002617 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002618 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2619 px->be_counters.comp_in ?
2620 (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 +01002621 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2622
2623 chunk_appendf(&trash,
2624 /* denied: req, resp */
2625 "<td>%s</td><td>%s</td>"
2626 /* errors : request, connect */
2627 "<td></td><td>%s</td>"
2628 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002629 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002630 /* warnings: retries, redispatches */
2631 "<td>%lld</td><td>%lld</td>"
2632 /* backend status: reflect backend status (up/down): we display UP
2633 * if the backend has known working servers or if it has no server at
2634 * all (eg: for stats). Then we display the total weight, number of
2635 * active and backups. */
2636 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2637 "<td class=ac>%d</td><td class=ac>%d</td>"
2638 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002639 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2640 U2H(px->be_counters.failed_conns),
2641 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002642 px->be_counters.cli_aborts,
2643 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002644 px->be_counters.retries, px->be_counters.redispatches,
2645 human_time(now.tv_sec - px->last_change, 1),
2646 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2647 "<font color=\"red\"><b>DOWN</b></font>",
2648 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2649 px->srv_act, px->srv_bck);
2650
2651 chunk_appendf(&trash,
2652 /* rest of backend: nothing, down transitions, total downtime, throttle */
2653 "<td class=ac>&nbsp;</td><td>%d</td>"
2654 "<td>%s</td>"
2655 "<td></td>"
2656 "</tr>",
2657 px->down_trans,
2658 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2659 }
2660 else { /* CSV mode */
2661 chunk_appendf(&trash,
2662 /* pxid, name */
2663 "%s,BACKEND,"
2664 /* queue : current, max */
2665 "%d,%d,"
2666 /* sessions : current, max, limit, total */
2667 "%d,%d,%d,%lld,"
2668 /* bytes : in, out */
2669 "%lld,%lld,"
2670 /* denied: req, resp */
2671 "%lld,%lld,"
2672 /* errors : request, connect, response */
2673 ",%lld,%lld,"
2674 /* warnings: retries, redispatches */
2675 "%lld,%lld,"
2676 /* backend status: reflect backend status (up/down): we display UP
2677 * if the backend has known working servers or if it has no server at
2678 * all (eg: for stats). Then we display the total weight, number of
2679 * active and backups. */
2680 "%s,"
2681 "%d,%d,%d,"
2682 /* rest of backend: nothing, down transitions, last change, total downtime */
2683 ",%d,%d,%d,,"
2684 /* pid, iid, sid, throttle, lbtot, tracked, type */
2685 "%d,%d,0,,%lld,,%d,"
2686 /* rate, rate_lim, rate_max, */
2687 "%u,,%u,"
2688 /* check_status, check_code, check_duration */
2689 ",,,",
2690 px->id,
2691 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2692 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2693 px->be_counters.bytes_in, px->be_counters.bytes_out,
2694 px->be_counters.denied_req, px->be_counters.denied_resp,
2695 px->be_counters.failed_conns, px->be_counters.failed_resp,
2696 px->be_counters.retries, px->be_counters.redispatches,
2697 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2698 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2699 px->srv_act, px->srv_bck,
2700 px->down_trans, (int)(now.tv_sec - px->last_change),
2701 px->srv?be_downtime(px):0,
2702 relative_pid, px->uuid,
2703 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2704 read_freq_ctr(&px->be_sess_per_sec),
2705 px->be_counters.sps_max);
2706
2707 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2708 if (px->mode == PR_MODE_HTTP) {
2709 for (i=1; i<6; i++)
2710 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2711 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2712 }
2713 else
2714 chunk_appendf(&trash, ",,,,,,");
2715
2716 /* failed health analyses */
2717 chunk_appendf(&trash, ",");
2718
2719 /* requests : req_rate, req_rate_max, req_tot, */
2720 chunk_appendf(&trash, ",,,");
2721
2722 /* errors: cli_aborts, srv_aborts */
2723 chunk_appendf(&trash, "%lld,%lld,",
2724 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2725
2726 /* compression: in, out, bypassed */
2727 chunk_appendf(&trash, "%lld,%lld,%lld,",
2728 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2729
2730 /* compression: comp_rsp */
2731 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2732
2733 /* finish with EOL */
2734 chunk_appendf(&trash, "\n");
2735 }
2736 return 1;
2737}
2738
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002739/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002740 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002741 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002742 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002743static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744{
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002745 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2746
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002747 if (px->cap & PR_CAP_BE && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002748 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002749
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002750 /* scope_txt = search pattern + search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002751 scope_txt[0] = 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002752 if (si->appctx.ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002753 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002754 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
2755 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->appctx.ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002756 }
2757
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002758 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002759 "<form action=\"%s%s%s%s\" method=\"post\">",
2760 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002761 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2762 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002763 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 }
2765
2766 /* print a new table */
2767 chunk_appendf(&trash,
2768 "<table class=\"tbl\" width=\"100%%\">\n"
2769 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002770 "<th class=\"pxname\" width=\"10%%\">");
2771
2772 chunk_appendf(&trash,
2773 "<a name=\"%s\"></a>%s"
2774 "<a class=px href=\"#%s\">%s</a>",
2775 px->id,
2776 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2777 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778
2779 if (uri->flags & ST_SHLGNDS) {
2780 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002781 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2783 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002784 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 }
2786
2787 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002788 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 "<th class=\"%s\" width=\"90%%\">%s</th>"
2790 "</tr>\n"
2791 "</table>\n"
2792 "<table class=\"tbl\" width=\"100%%\">\n"
2793 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2795 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2796
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002797 if ((px->cap & PR_CAP_BE) && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798 /* Column heading for Enable or Disable server */
2799 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002800 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801
2802 chunk_appendf(&trash,
2803 "<th rowspan=2></th>"
2804 "<th colspan=3>Queue</th>"
2805 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2806 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2807 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2808 "<th colspan=9>Server</th>"
2809 "</tr>\n"
2810 "<tr class=\"titre\">"
2811 "<th>Cur</th><th>Max</th><th>Limit</th>"
2812 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2813 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2814 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2815 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2816 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2817 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2818 "<th>Thrtle</th>\n"
2819 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002820}
2821
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002822/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002823 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002824 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002825static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002826{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 chunk_appendf(&trash, "</table>");
2828
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002829 if ((px->cap & PR_CAP_BE) && px->srv && (si->appctx.ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002830 /* close the form used to enable/disable this proxy servers */
2831 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002832 "Choose the action to perform on the checked servers : "
2833 "<select name=action>"
2834 "<option value=\"\"></option>"
2835 "<option value=\"disable\">Disable</option>"
2836 "<option value=\"enable\">Enable</option>"
2837 "<option value=\"stop\">Soft Stop</option>"
2838 "<option value=\"start\">Soft Start</option>"
2839 "<option value=\"shutdown\">Kill Sessions</option>"
2840 "</select>"
2841 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2842 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2843 "</form>",
2844 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002845 }
2846
2847 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848}
Willy Tarreau91861262007-10-17 17:06:05 +02002849
2850/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002851 * Dumps statistics for a proxy. The output is sent to the stream interface's
2852 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2853 * buffer space, or non-zero if everything completed. This function is used
2854 * both by the CLI and the HTTP entry points, and is able to dump the output
2855 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002856 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002857static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002858{
Willy Tarreau306f8302013-07-08 15:53:06 +02002859 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002860 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002861 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002862 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002864 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002865
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002866 switch (si->appctx.ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002867 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002868 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002869 if (uri && uri->scope) {
2870 /* we have a limited scope, we have to check the proxy name */
2871 struct stat_scope *scope;
2872 int len;
2873
2874 len = strlen(px->id);
2875 scope = uri->scope;
2876
2877 while (scope) {
2878 /* match exact proxy name */
2879 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2880 break;
2881
2882 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002883 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002884 break;
2885 scope = scope->next;
2886 }
2887
2888 /* proxy name not found : don't dump anything */
2889 if (scope == NULL)
2890 return 1;
2891 }
2892
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002893 /* if the user has requested a limited output and the proxy
2894 * name does not match, skip it.
2895 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002896 if (si->appctx.ctx.stats.scope_len &&
2897 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002898 return 1;
2899
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002900 if ((si->appctx.ctx.stats.flags & STAT_BOUND) &&
2901 (si->appctx.ctx.stats.iid != -1) &&
2902 (px->uuid != si->appctx.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002903 return 1;
2904
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002905 si->appctx.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002906 /* fall through */
2907
Willy Tarreau295a8372011-03-10 11:25:07 +01002908 case STAT_PX_ST_TH:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002909 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002910 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002911 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002912 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002913 }
Willy Tarreau91861262007-10-17 17:06:05 +02002914
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002915 si->appctx.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002916 /* fall through */
2917
Willy Tarreau295a8372011-03-10 11:25:07 +01002918 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002919 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002920 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002921 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002922 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002923
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002924 si->appctx.ctx.stats.l = px->conf.listeners.n;
2925 si->appctx.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002926 /* fall through */
2927
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002929 /* stats.l has been initialized above */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002930 for (; si->appctx.ctx.stats.l != &px->conf.listeners; si->appctx.ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002931 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002932 return 0;
2933
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002934 l = LIST_ELEM(si->appctx.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002935 if (!l->counters)
2936 continue;
2937
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002938 if (si->appctx.ctx.stats.flags & STAT_BOUND) {
2939 if (!(si->appctx.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002940 break;
2941
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002942 if (si->appctx.ctx.stats.sid != -1 && l->luid != si->appctx.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002943 continue;
2944 }
2945
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946 /* print the frontend */
2947 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2948 if (bi_putchk(rep, &trash) == -1)
2949 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002950 }
2951
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002952 si->appctx.ctx.stats.sv = px->srv; /* may be NULL */
2953 si->appctx.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002954 /* fall through */
2955
Willy Tarreau295a8372011-03-10 11:25:07 +01002956 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002957 /* stats.sv has been initialized above */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002958 for (; si->appctx.ctx.stats.sv != NULL; si->appctx.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002959 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 +02002960
Willy Tarreau9b28e032012-10-12 23:49:43 +02002961 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002962 return 0;
2963
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002964 sv = si->appctx.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002965
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002966 if (si->appctx.ctx.stats.flags & STAT_BOUND) {
2967 if (!(si->appctx.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002968 break;
2969
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002970 if (si->appctx.ctx.stats.sid != -1 && sv->puid != si->appctx.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002971 continue;
2972 }
2973
Willy Tarreau44267702011-10-28 15:35:33 +02002974 if (sv->track)
2975 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002976 else
2977 svs = sv;
2978
Willy Tarreau91861262007-10-17 17:06:05 +02002979 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002980 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09002981 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002982 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09002983 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002984 sv_state = 3; /* UP */
2985 else
2986 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002987
Willy Tarreau6b7764a2013-12-04 00:43:21 +01002988 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09002989 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01002990 else if (svs->state & SRV_GOINGDOWN)
2991 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01002992 }
Willy Tarreau91861262007-10-17 17:06:05 +02002993 else
Simon Horman125d0992013-02-24 17:23:38 +09002994 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02002995 sv_state = 1; /* going up */
2996 else
2997 sv_state = 0; /* DOWN */
2998
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01002999 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003000 /* do not report servers which are DOWN */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003001 si->appctx.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003002 continue;
3003 }
3004
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3006 if (bi_putchk(rep, &trash) == -1)
3007 return 0;
3008 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003009
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003010 si->appctx.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 case STAT_PX_ST_BE:
3014 /* print the backend */
3015 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3016 if (bi_putchk(rep, &trash) == -1)
3017 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003018
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003019 si->appctx.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003020 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003021
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003022 case STAT_PX_ST_END:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003023 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003024 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 if (bi_putchk(rep, &trash) == -1)
3026 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003027 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003028
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003029 si->appctx.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003031
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003032 case STAT_PX_ST_FIN:
3033 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035 default:
3036 /* unknown state, we should put an abort() here ! */
3037 return 1;
3038 }
3039}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003040
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003041/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3042 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003043 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003044static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045{
3046 /* WARNING! This must fit in the first buffer !!! */
3047 chunk_appendf(&trash,
3048 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3049 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3050 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3051 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3052 "<style type=\"text/css\"><!--\n"
3053 "body {"
3054 " font-family: arial, helvetica, sans-serif;"
3055 " font-size: 12px;"
3056 " font-weight: normal;"
3057 " color: black;"
3058 " background: white;"
3059 "}\n"
3060 "th,td {"
3061 " font-size: 10px;"
3062 "}\n"
3063 "h1 {"
3064 " font-size: x-large;"
3065 " margin-bottom: 0.5em;"
3066 "}\n"
3067 "h2 {"
3068 " font-family: helvetica, arial;"
3069 " font-size: x-large;"
3070 " font-weight: bold;"
3071 " font-style: italic;"
3072 " color: #6020a0;"
3073 " margin-top: 0em;"
3074 " margin-bottom: 0em;"
3075 "}\n"
3076 "h3 {"
3077 " font-family: helvetica, arial;"
3078 " font-size: 16px;"
3079 " font-weight: bold;"
3080 " color: #b00040;"
3081 " background: #e8e8d0;"
3082 " margin-top: 0em;"
3083 " margin-bottom: 0em;"
3084 "}\n"
3085 "li {"
3086 " margin-top: 0.25em;"
3087 " margin-right: 2em;"
3088 "}\n"
3089 ".hr {margin-top: 0.25em;"
3090 " border-color: black;"
3091 " border-bottom-style: solid;"
3092 "}\n"
3093 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3094 ".total {background: #20D0D0;color: #ffff80;}\n"
3095 ".frontend {background: #e8e8d0;}\n"
3096 ".socket {background: #d0d0d0;}\n"
3097 ".backend {background: #e8e8d0;}\n"
3098 ".active0 {background: #ff9090;}\n"
3099 ".active1 {background: #ffd020;}\n"
3100 ".active2 {background: #ffffa0;}\n"
3101 ".active3 {background: #c0ffc0;}\n"
3102 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003103 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3104 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3105 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003106 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107 ".backup0 {background: #ff9090;}\n"
3108 ".backup1 {background: #ff80ff;}\n"
3109 ".backup2 {background: #c060ff;}\n"
3110 ".backup3 {background: #b0d0ff;}\n"
3111 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3112 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003113 ".backup6 {background: #c060ff;}\n"
3114 ".backup7 {background: #cc9900;}\n"
3115 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003116 ".maintain {background: #c07820;}\n"
3117 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3118 "\n"
3119 "a.px:link {color: #ffff40; text-decoration: none;}"
3120 "a.px:visited {color: #ffff40; text-decoration: none;}"
3121 "a.px:hover {color: #ffffff; text-decoration: none;}"
3122 "a.lfsb:link {color: #000000; text-decoration: none;}"
3123 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3124 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3125 "\n"
3126 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3127 "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"
3128 "table.tbl td.ac { text-align: center;}\n"
3129 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3130 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3131 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3132 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3133 "\n"
3134 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3135 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3136 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003137 "table.det { border-collapse: collapse; border-style: none; }\n"
3138 "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"
3139 "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 +01003140 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003141 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003142 " display:block;\n"
3143 " visibility:hidden;\n"
3144 " z-index:2147483647;\n"
3145 " position:absolute;\n"
3146 " padding:2px 4px 3px;\n"
3147 " background:#f0f060; color:#000000;\n"
3148 " border:1px solid #7040c0;\n"
3149 " white-space:nowrap;\n"
3150 " font-style:normal;font-size:11px;font-weight:normal;\n"
3151 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3152 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3153 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003154 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003155 "-->\n"
3156 "</style></head>\n",
3157 (uri->flags & ST_SHNODE) ? " on " : "",
3158 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3159 );
3160}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003161
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003162/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003163 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003164 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003165 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003166static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167{
3168 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003169 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171 /* WARNING! this has to fit the first packet too.
3172 * We are around 3.5 kB, add adding entries will
3173 * become tricky if we want to support 4kB buffers !
3174 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 chunk_appendf(&trash,
3176 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3177 PRODUCT_NAME "%s</a></h1>\n"
3178 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3179 "<hr width=\"100%%\" class=\"hr\">\n"
3180 "<h3>&gt; General process information</h3>\n"
3181 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3182 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3183 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3184 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3185 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3186 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3187 "Running tasks: %d/%d; idle = %d %%<br>\n"
3188 "</td><td align=\"center\" nowrap>\n"
3189 "<table class=\"lgd\"><tr>\n"
3190 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3191 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3192 "</tr><tr>\n"
3193 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3194 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3195 "</tr><tr>\n"
3196 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3197 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3198 "</tr><tr>\n"
3199 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003200 "</tr><tr>\n"
3201 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 "</tr><tr>\n"
3203 "<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 -07003204 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003205 "<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 +01003206 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003207 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003208 "</td>"
3209 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3210 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3211 "",
3212 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3213 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3214 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3215 (uri->flags & ST_SHDESC) ? ": " : "",
3216 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3217 pid, relative_pid, global.nbproc,
3218 up / 86400, (up % 86400) / 3600,
3219 (up % 3600) / 60, (up % 60),
3220 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3221 global.rlimit_memmax ? " MB" : "",
3222 global.rlimit_nofile,
3223 global.maxsock, global.maxconn, global.maxpipes,
3224 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3225 run_queue_cur, nb_tasks_cur, idle_pct
3226 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003227
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003228 /* scope_txt = search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3229 memcpy(scope_txt, bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
3230 scope_txt[si->appctx.ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003231
3232 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003233 "<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 +02003234 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003235 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3236 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3237 (si->appctx.ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003238 STAT_SCOPE_TXT_MAXLEN);
3239
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003240 /* scope_txt = search pattern + search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003241 scope_txt[0] = 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003242 if (si->appctx.ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003243 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003244 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
3245 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->appctx.ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003246 }
3247
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003248 if (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003249 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003250 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 uri->uri_prefix,
3252 "",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003253 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003254 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003255 else
3256 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003257 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003258 uri->uri_prefix,
3259 ";up",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003260 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003261 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003262
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263 if (uri->refresh > 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003264 if (si->appctx.ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003266 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003268 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003269 "",
3270 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003271 else
3272 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003273 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003275 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003276 ";norefresh",
3277 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003279
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003280 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003281 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003283 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3284 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003285 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003286
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003288 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003289 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003290 (uri->refresh > 0) ? ";norefresh" : "",
3291 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003292
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003293 chunk_appendf(&trash,
3294 "</ul></td>"
3295 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3296 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3297 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3298 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3299 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3300 "</ul>"
3301 "</td>"
3302 "</tr></table>\n"
3303 ""
3304 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003305
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003306 if (si->appctx.ctx.stats.st_code) {
3307 switch (si->appctx.ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308 case STAT_STATUS_DONE:
3309 chunk_appendf(&trash,
3310 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003311 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003313 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003314 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3315 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003316 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 break;
3318 case STAT_STATUS_NONE:
3319 chunk_appendf(&trash,
3320 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003321 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003323 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003324 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3325 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003326 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 break;
3328 case STAT_STATUS_PART:
3329 chunk_appendf(&trash,
3330 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003331 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 "Action partially processed.<br>"
3333 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003334 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003335 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3336 (si->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_ERRP:
3340 chunk_appendf(&trash,
3341 "<p><div class=active0>"
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 "Action not processed because of invalid parameters."
3344 "<ul>"
3345 "<li>The action is maybe unknown.</li>"
3346 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3347 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3348 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003349 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003350 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3351 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003352 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 break;
3354 case STAT_STATUS_EXCD:
3355 chunk_appendf(&trash,
3356 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003357 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3359 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003360 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003361 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3362 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003363 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 break;
3365 case STAT_STATUS_DENY:
3366 chunk_appendf(&trash,
3367 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003368 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003370 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003371 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3372 (si->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 default:
3376 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003377 "<p><div class=active8>"
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 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003380 "</div>\n", uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003381 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3382 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003383 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 }
3385 chunk_appendf(&trash, "<p>\n");
3386 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003387}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003388
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003389/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3390 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003392static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003394 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003396
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003397/* This function dumps statistics onto the stream interface's read buffer in
3398 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003399 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3400 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3401 * and the session must be closed, or -1 in case of any error. This function is
3402 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003404static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003406 struct channel *rep = si->ib;
3407 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003408
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003410
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003411 switch (si->appctx.st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 case STAT_ST_INIT:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003413 si->appctx.st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003415
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 case STAT_ST_HEAD:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003417 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003418 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003419 else
3420 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003421
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 if (bi_putchk(rep, &trash) == -1)
3423 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003424
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003425 si->appctx.st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 case STAT_ST_INFO:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003429 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003430 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003431 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003432 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 }
Willy Tarreau91861262007-10-17 17:06:05 +02003434
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003435 si->appctx.ctx.stats.px = proxy;
3436 si->appctx.ctx.stats.px_st = STAT_PX_ST_INIT;
3437 si->appctx.st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003438 /* fall through */
3439
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440 case STAT_ST_LIST:
3441 /* dump proxies */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003442 while (si->appctx.ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 if (buffer_almost_full(rep->buf))
3444 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003445
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003446 px = si->appctx.ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003447 /* skip the disabled proxies, global frontend and non-networked ones */
3448 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003449 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003451
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003452 si->appctx.ctx.stats.px = px->next;
3453 si->appctx.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 }
3455 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003456
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003457 si->appctx.st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003458 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003459
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003460 case STAT_ST_END:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003461 if (si->appctx.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003462 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 if (bi_putchk(rep, &trash) == -1)
3464 return 0;
3465 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003466
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003467 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003468 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003469
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 case STAT_ST_FIN:
3471 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 default:
3474 /* unknown state ! */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003475 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 return -1;
3477 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478}
Willy Tarreauae526782010-03-04 20:34:23 +01003479
Willy Tarreau347a35d2013-11-22 17:51:09 +01003480/* We reached the stats page through a POST request.
3481 * Parse the posted data and enable/disable servers if necessary.
3482 * Returns 1 if request was parsed or zero if it needs more data.
3483 */
3484static int stats_process_http_post(struct stream_interface *si)
3485{
3486 struct session *s = session_from_task(si->owner);
3487
3488 struct proxy *px = NULL;
3489 struct server *sv = NULL;
3490
3491 char key[LINESIZE];
3492 int action = ST_ADM_ACTION_NONE;
3493 int reprocess = 0;
3494
3495 int total_servers = 0;
3496 int altered_servers = 0;
3497
3498 char *first_param, *cur_param, *next_param, *end_params;
3499 char *st_cur_param = NULL;
3500 char *st_next_param = NULL;
3501
3502 struct chunk *temp;
3503 int reql;
3504
3505 temp = get_trash_chunk();
3506 if (temp->size < s->txn.req.body_len) {
3507 /* too large request */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003508 si->appctx.ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003509 goto out;
3510 }
3511
3512 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3513 if (reql <= 0) {
3514 /* we need more data */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003515 si->appctx.ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003516 return 0;
3517 }
3518
3519 first_param = temp->str;
3520 end_params = temp->str + reql;
3521 cur_param = next_param = end_params;
3522 *end_params = '\0';
3523
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003524 si->appctx.ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003525
3526 /*
3527 * Parse the parameters in reverse order to only store the last value.
3528 * From the html form, the backend and the action are at the end.
3529 */
3530 while (cur_param > first_param) {
3531 char *value;
3532 int poffset, plen;
3533
3534 cur_param--;
3535
3536 if ((*cur_param == '&') || (cur_param == first_param)) {
3537 reprocess_servers:
3538 /* Parse the key */
3539 poffset = (cur_param != first_param ? 1 : 0);
3540 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3541 if ((plen > 0) && (plen <= sizeof(key))) {
3542 strncpy(key, cur_param + poffset, plen);
3543 key[plen - 1] = '\0';
3544 } else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003545 si->appctx.ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003546 goto out;
3547 }
3548
3549 /* Parse the value */
3550 value = key;
3551 while (*value != '\0' && *value != '=') {
3552 value++;
3553 }
3554 if (*value == '=') {
3555 /* Ok, a value is found, we can mark the end of the key */
3556 *value++ = '\0';
3557 }
3558 if (url_decode(key) < 0 || url_decode(value) < 0)
3559 break;
3560
3561 /* Now we can check the key to see what to do */
3562 if (!px && (strcmp(key, "b") == 0)) {
3563 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
3564 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003565 si->appctx.ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003566 goto out;
3567 }
3568 }
3569 else if (!action && (strcmp(key, "action") == 0)) {
3570 if (strcmp(value, "disable") == 0) {
3571 action = ST_ADM_ACTION_DISABLE;
3572 }
3573 else if (strcmp(value, "enable") == 0) {
3574 action = ST_ADM_ACTION_ENABLE;
3575 }
3576 else if (strcmp(value, "stop") == 0) {
3577 action = ST_ADM_ACTION_STOP;
3578 }
3579 else if (strcmp(value, "start") == 0) {
3580 action = ST_ADM_ACTION_START;
3581 }
3582 else if (strcmp(value, "shutdown") == 0) {
3583 action = ST_ADM_ACTION_SHUTDOWN;
3584 }
3585 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003586 si->appctx.ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003587 goto out;
3588 }
3589 }
3590 else if (strcmp(key, "s") == 0) {
3591 if (!(px && action)) {
3592 /*
3593 * Indicates that we'll need to reprocess the parameters
3594 * as soon as backend and action are known
3595 */
3596 if (!reprocess) {
3597 st_cur_param = cur_param;
3598 st_next_param = next_param;
3599 }
3600 reprocess = 1;
3601 }
3602 else if ((sv = findserver(px, value)) != NULL) {
3603 switch (action) {
3604 case ST_ADM_ACTION_DISABLE:
3605 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
3606 /* Not already in maintenance, we can change the server state */
3607 sv->state |= SRV_MAINTAIN;
3608 set_server_down(&sv->check);
3609 altered_servers++;
3610 total_servers++;
3611 }
3612 break;
3613 case ST_ADM_ACTION_ENABLE:
3614 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
3615 /* Already in maintenance, we can change the server state */
3616 set_server_up(&sv->check);
3617 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
3618 altered_servers++;
3619 total_servers++;
3620 }
3621 break;
3622 case ST_ADM_ACTION_STOP:
3623 case ST_ADM_ACTION_START:
3624 if (action == ST_ADM_ACTION_START)
3625 sv->uweight = sv->iweight;
3626 else
3627 sv->uweight = 0;
3628
3629 server_recalc_eweight(sv);
3630 set_server_drain_state(sv);
3631
3632 altered_servers++;
3633 total_servers++;
3634 break;
3635 case ST_ADM_ACTION_SHUTDOWN:
3636 if (px->state != PR_STSTOPPED) {
3637 struct session *sess, *sess_bck;
3638
3639 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3640 if (sess->srv_conn == sv)
3641 session_shutdown(sess, SN_ERR_KILLED);
3642
3643 altered_servers++;
3644 total_servers++;
3645 }
3646 break;
3647 }
3648 } else {
3649 /* the server name is unknown or ambiguous (duplicate names) */
3650 total_servers++;
3651 }
3652 }
3653 if (reprocess && px && action) {
3654 /* Now, we know the backend and the action chosen by the user.
3655 * We can safely restart from the first server parameter
3656 * to reprocess them
3657 */
3658 cur_param = st_cur_param;
3659 next_param = st_next_param;
3660 reprocess = 0;
3661 goto reprocess_servers;
3662 }
3663
3664 next_param = cur_param;
3665 }
3666 }
3667
3668 if (total_servers == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003669 si->appctx.ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003670 }
3671 else if (altered_servers == 0) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003672 si->appctx.ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003673 }
3674 else if (altered_servers == total_servers) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003675 si->appctx.ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003676 }
3677 else {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003678 si->appctx.ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003679 }
3680 out:
3681 return 1;
3682}
3683
3684
3685static int stats_send_http_headers(struct stream_interface *si)
3686{
3687 struct session *s = session_from_task(si->owner);
3688 struct uri_auth *uri = s->be->uri_auth;
3689
3690 chunk_printf(&trash,
3691 "HTTP/1.0 200 OK\r\n"
3692 "Cache-Control: no-cache\r\n"
3693 "Connection: close\r\n"
3694 "Content-Type: %s\r\n",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003695 (si->appctx.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01003696
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003697 if (uri->refresh > 0 && !(si->appctx.ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01003698 chunk_appendf(&trash, "Refresh: %d\r\n",
3699 uri->refresh);
3700
3701 chunk_appendf(&trash, "\r\n");
3702
3703 s->txn.status = 200;
3704 s->logs.tv_request = now;
3705
3706 if (bi_putchk(si->ib, &trash) == -1)
3707 return 0;
3708
3709 return 1;
3710}
3711
3712static int stats_send_http_redirect(struct stream_interface *si)
3713{
3714 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3715 struct session *s = session_from_task(si->owner);
3716 struct uri_auth *uri = s->be->uri_auth;
3717
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003718 /* scope_txt = search pattern + search query, si->appctx.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003719 scope_txt[0] = 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003720 if (si->appctx.ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003721 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003722 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->appctx.ctx.stats.scope_str, si->appctx.ctx.stats.scope_len);
3723 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->appctx.ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003724 }
3725
3726 /* We don't want to land on the posted stats page because a refresh will
3727 * repost the data. We don't want this to happen on accident so we redirect
3728 * the browse to the stats page with a GET.
3729 */
3730 chunk_printf(&trash,
3731 "HTTP/1.1 303 See Other\r\n"
3732 "Cache-Control: no-cache\r\n"
3733 "Content-Type: text/plain\r\n"
3734 "Connection: close\r\n"
3735 "Location: %s;st=%s%s%s%s\r\n"
3736 "\r\n",
3737 uri->uri_prefix,
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003738 ((si->appctx.ctx.stats.st_code > STAT_STATUS_INIT) &&
3739 (si->appctx.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3740 stat_status_codes[si->appctx.ctx.stats.st_code]) ?
3741 stat_status_codes[si->appctx.ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01003742 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003743 (si->appctx.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3744 (si->appctx.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01003745 scope_txt);
3746
3747 s->txn.status = 303;
3748 s->logs.tv_request = now;
3749
3750 if (bi_putchk(si->ib, &trash) == -1)
3751 return 0;
3752
3753 return 1;
3754}
3755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756/* This I/O handler runs as an applet embedded in a stream interface. It is
3757 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003758 * si->appctx.st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003759 * automatically unregisters itself once transfer is complete.
3760 */
3761static void http_stats_io_handler(struct stream_interface *si)
3762{
Willy Tarreau306f8302013-07-08 15:53:06 +02003763 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003764 struct channel *req = si->ob;
3765 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003766
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003767 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3768 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003769
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003770 /* check that the output is not closed */
3771 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003772 si->appctx.st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003773
Willy Tarreau347a35d2013-11-22 17:51:09 +01003774 /* all states are processed in sequence */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003775 if (si->appctx.st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003776 if (stats_send_http_headers(si)) {
3777 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003778 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003779 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003780 si->appctx.st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003781 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003782 }
3783
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003784 if (si->appctx.st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003785 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003786 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003787 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003788
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003789 if (si->appctx.st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003790 if (stats_process_http_post(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003791 si->appctx.st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003792 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003793 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003794 }
3795
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003796 if (si->appctx.st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003797 if (stats_send_http_redirect(si))
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003798 si->appctx.st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003799 }
3800
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003801 if (si->appctx.st0 == STAT_HTTP_DONE)
Willy Tarreau347a35d2013-11-22 17:51:09 +01003802 si_shutw(si);
3803
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3805 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003806
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003807 if (si->appctx.st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01003808 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3809 si_shutr(si);
3810 res->flags |= CF_READ_NULL;
3811 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003812 }
Willy Tarreau91861262007-10-17 17:06:05 +02003813
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003814 /* update all other flags and resync with the other side */
3815 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003816
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 /* we don't want to expire timeouts while we're processing requests */
3818 si->ib->rex = TICK_ETERNITY;
3819 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003820
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003821 out:
3822 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3823 /* check that we have released everything then unregister */
3824 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003825 }
3826}
3827
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828
Willy Tarreau909d5172012-11-26 03:04:41 +01003829static inline const char *get_conn_ctrl_name(const struct connection *conn)
3830{
3831 if (!conn->ctrl)
3832 return "NONE";
3833 return conn->ctrl->name;
3834}
3835
3836static inline const char *get_conn_xprt_name(const struct connection *conn)
3837{
3838 static char ptr[17];
3839
3840 if (!conn->xprt)
3841 return "NONE";
3842
3843 if (conn->xprt == &raw_sock)
3844 return "RAW";
3845
3846#ifdef USE_OPENSSL
3847 if (conn->xprt == &ssl_sock)
3848 return "SSL";
3849#endif
3850 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3851 return ptr;
3852}
3853
3854static inline const char *get_conn_data_name(const struct connection *conn)
3855{
3856 static char ptr[17];
3857
3858 if (!conn->data)
3859 return "NONE";
3860
3861 if (conn->data == &sess_conn_cb)
3862 return "SESS";
3863
3864 if (conn->data == &si_conn_cb)
3865 return "STRM";
3866
3867 if (conn->data == &check_conn_cb)
3868 return "CHCK";
3869
3870 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3871 return ptr;
3872}
3873
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003874/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003875 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003876 * 0 if the output buffer is full and it needs to be called again, otherwise
3877 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003878 */
Willy Tarreau76153662012-11-26 01:16:39 +01003879static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003880{
3881 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003882 extern const char *monthname[12];
3883 char pn[INET6_ADDRSTRLEN];
3884
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003885 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003886
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003887 if (si->appctx.ctx.sess.section > 0 && si->appctx.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003888 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003889 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3890 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003891 return 0;
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003892 si->appctx.ctx.sess.uid = 0;
3893 si->appctx.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003894 return 1;
3895 }
3896
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003897 switch (si->appctx.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003898 case 0: /* main status of the session */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01003899 si->appctx.ctx.sess.uid = sess->uniq_id;
3900 si->appctx.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003901 /* fall through */
3902
3903 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003904 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003905 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003906 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003907 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003908 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3909 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003910 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003911 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003912
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003913 switch ((obj_type(sess->si[0].end) == OBJ_TYPE_CONN) ?
3914 addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003915 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003916 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003917 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003918 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003919 break;
3920 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003921 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003922 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003923 default:
3924 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003925 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003926 break;
3927 }
3928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003929 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003930 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003931 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003932
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003933 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003934 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003935 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3936 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3937 sess->listener ? sess->listener->luid : 0);
3938
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003939 if (obj_type(sess->si[0].end) == OBJ_TYPE_CONN)
3940 conn_get_to_addr(sess->si[0].conn);
3941
3942 switch ((obj_type(sess->si[0].end) == OBJ_TYPE_CONN) ?
3943 addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003944 case AF_INET:
3945 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003946 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003947 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003948 break;
3949 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003950 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003951 break;
3952 default:
3953 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003954 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003955 break;
3956 }
3957
Willy Tarreau50943332011-09-02 17:33:05 +02003958 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003959 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003960 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003961 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003962 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3963 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003964 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003965
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003966 if (obj_type(sess->si[1].end) == OBJ_TYPE_CONN)
3967 conn_get_from_addr(sess->si[1].conn);
3968
3969 switch ((obj_type(sess->si[1].end) == OBJ_TYPE_CONN) ?
3970 addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003971 case AF_INET:
3972 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003973 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003974 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003975 break;
3976 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003977 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003978 break;
3979 default:
3980 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003981 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003982 break;
3983 }
3984
3985 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003986 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003987 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003988 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3989 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003990 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003992
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003993 if (obj_type(sess->si[1].end) == OBJ_TYPE_CONN)
3994 conn_get_to_addr(sess->si[1].conn);
3995
3996 switch ((obj_type(sess->si[1].end) == OBJ_TYPE_CONN) ?
3997 addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003998 case AF_INET:
3999 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004000 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004001 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004002 break;
4003 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004004 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004005 break;
4006 default:
4007 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004008 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004009 break;
4010 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004011
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004012 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004013 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004014 sess->task,
4015 sess->task->state,
4016 sess->task->nice, sess->task->calls,
4017 sess->task->expire ?
4018 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4019 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4020 TICKS_TO_MS(1000)) : "<NEVER>",
4021 task_in_rq(sess->task) ? ", running" : "");
4022
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004023 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004024 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004025 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4026
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004027 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004028 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4029 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4030 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4031
4032 chunk_appendf(&trash,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004033 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004034 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004035 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004036 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004037 obj_type_name(sess->si[0].end),
4038 obj_base_ptr(sess->si[0].end),
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004039 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004040 sess->si[0].exp ?
4041 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4042 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4043 TICKS_TO_MS(1000)) : "<NEVER>",
4044 sess->si[0].err_type);
4045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004046 chunk_appendf(&trash,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004047 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004048 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004049 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004050 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004051 obj_type_name(sess->si[1].end),
4052 obj_base_ptr(sess->si[1].end),
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004053 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004054 sess->si[1].exp ?
4055 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4056 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4057 TICKS_TO_MS(1000)) : "<NEVER>",
4058 sess->si[1].err_type);
4059
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004060 if (obj_type(sess->si[0].end) == OBJ_TYPE_CONN) {
4061 chunk_appendf(&trash,
4062 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
4063 sess->si[0].conn,
4064 get_conn_ctrl_name(sess->si[0].conn),
4065 get_conn_xprt_name(sess->si[0].conn),
4066 get_conn_data_name(sess->si[0].conn),
4067 obj_type_name(sess->si[0].conn->target),
4068 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004069
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004070 chunk_appendf(&trash,
4071 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
4072 sess->si[0].conn->flags,
4073 sess->si[0].conn->t.sock.fd,
4074 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
4075 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
4076 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].updated : 0);
4077 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004078
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004079 if (obj_type(sess->si[1].end) == OBJ_TYPE_CONN) {
4080 chunk_appendf(&trash,
4081 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
4082 sess->si[1].conn,
4083 get_conn_ctrl_name(sess->si[1].conn),
4084 get_conn_xprt_name(sess->si[1].conn),
4085 get_conn_data_name(sess->si[1].conn),
4086 obj_type_name(sess->si[1].conn->target),
4087 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004088
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004089 chunk_appendf(&trash,
4090 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
4091 sess->si[1].conn->flags,
4092 sess->si[1].conn->t.sock.fd,
4093 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
4094 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
4095 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].updated : 0);
4096 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004097
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004098 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004099 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004100 " an_exp=%s",
4101 sess->req,
4102 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004103 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004104 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004105 sess->req->analyse_exp ?
4106 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4107 TICKS_TO_MS(1000)) : "<NEVER>");
4108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004109 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004110 " rex=%s",
4111 sess->req->rex ?
4112 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4113 TICKS_TO_MS(1000)) : "<NEVER>");
4114
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004115 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004116 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004117 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004118 sess->req->wex ?
4119 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4120 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004121 sess->req->buf,
4122 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004123 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004124 sess->txn.req.next, sess->req->buf->i,
4125 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004127 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004128 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004129 " an_exp=%s",
4130 sess->rep,
4131 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004132 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004133 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004134 sess->rep->analyse_exp ?
4135 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4136 TICKS_TO_MS(1000)) : "<NEVER>");
4137
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004138 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004139 " rex=%s",
4140 sess->rep->rex ?
4141 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4142 TICKS_TO_MS(1000)) : "<NEVER>");
4143
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004144 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004145 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004146 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004147 sess->rep->wex ?
4148 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4149 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004150 sess->rep->buf,
4151 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004152 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004153 sess->txn.rsp.next, sess->rep->buf->i,
4154 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004155
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004156 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004157 return 0;
4158
4159 /* use other states to dump the contents */
4160 }
4161 /* end of dump */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004162 si->appctx.ctx.sess.uid = 0;
4163 si->appctx.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004164 return 1;
4165}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004166
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004167/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004168 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004169 * to be called again, otherwise non-zero. It is designed to be called
4170 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004171 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004172static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004173{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004174 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004175 /* If we're forced to shut down, we might have to remove our
4176 * reference to the last session being dumped.
4177 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004178 if (si->appctx.st2 == STAT_ST_LIST) {
4179 if (!LIST_ISEMPTY(&si->appctx.ctx.sess.bref.users)) {
4180 LIST_DEL(&si->appctx.ctx.sess.bref.users);
4181 LIST_INIT(&si->appctx.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004182 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004183 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004184 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004185 }
4186
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004187 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004188
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004189 switch (si->appctx.st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004190 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004191 /* the function had not been called yet, let's prepare the
4192 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004193 * pointer to the first in the global list. When a target
4194 * session is being destroyed, it is responsible for updating
4195 * this pointer. We know we have reached the end when this
4196 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004197 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004198 LIST_INIT(&si->appctx.ctx.sess.bref.users);
4199 si->appctx.ctx.sess.bref.ref = sessions.n;
4200 si->appctx.st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004201 /* fall through */
4202
Willy Tarreau295a8372011-03-10 11:25:07 +01004203 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004204 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004205 if (!LIST_ISEMPTY(&si->appctx.ctx.sess.bref.users)) {
4206 LIST_DEL(&si->appctx.ctx.sess.bref.users);
4207 LIST_INIT(&si->appctx.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004208 }
4209
4210 /* and start from where we stopped */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004211 while (si->appctx.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004212 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004213 struct session *curr_sess;
4214
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004215 curr_sess = LIST_ELEM(si->appctx.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004216
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004217 if (si->appctx.ctx.sess.target) {
4218 if (si->appctx.ctx.sess.target != (void *)-1 && si->appctx.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004219 goto next_sess;
4220
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004221 LIST_ADDQ(&curr_sess->back_refs, &si->appctx.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004222 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01004223 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004224 return 0;
4225
4226 /* session dump complete */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004227 LIST_DEL(&si->appctx.ctx.sess.bref.users);
4228 LIST_INIT(&si->appctx.ctx.sess.bref.users);
4229 if (si->appctx.ctx.sess.target != (void *)-1) {
4230 si->appctx.ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01004231 break;
4232 }
4233 else
4234 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004235 }
4236
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004237 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004238 "%p: proto=%s",
4239 curr_sess,
4240 curr_sess->listener->proto->name);
4241
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004242
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004243 switch ((obj_type(curr_sess->si[0].end) == OBJ_TYPE_CONN) ?
4244 addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02004245 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004246 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004247 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004248 " src=%s:%d fe=%s be=%s srv=%s",
4249 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004250 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004251 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004252 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004253 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004254 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004255 break;
4256 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004257 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02004258 " src=unix:%d fe=%s be=%s srv=%s",
4259 curr_sess->listener->luid,
4260 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004261 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004262 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02004263 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004264 break;
4265 }
4266
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004267 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02004268 " ts=%02x age=%s calls=%d",
4269 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01004270 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
4271 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004272
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004273 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004274 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004275 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004276 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004277 curr_sess->req->analysers,
4278 curr_sess->req->rex ?
4279 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
4280 TICKS_TO_MS(1000)) : "");
4281
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004282 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004283 ",wx=%s",
4284 curr_sess->req->wex ?
4285 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
4286 TICKS_TO_MS(1000)) : "");
4287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004288 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004289 ",ax=%s]",
4290 curr_sess->req->analyse_exp ?
4291 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
4292 TICKS_TO_MS(1000)) : "");
4293
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004294 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004295 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004296 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004297 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004298 curr_sess->rep->analysers,
4299 curr_sess->rep->rex ?
4300 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
4301 TICKS_TO_MS(1000)) : "");
4302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004303 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004304 ",wx=%s",
4305 curr_sess->rep->wex ?
4306 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
4307 TICKS_TO_MS(1000)) : "");
4308
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004309 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004310 ",ax=%s]",
4311 curr_sess->rep->analyse_exp ?
4312 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
4313 TICKS_TO_MS(1000)) : "");
4314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004315 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004316 " s0=[%d,%1xh,fd=%d,ex=%s]",
4317 curr_sess->si[0].state,
4318 curr_sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004319 (obj_type(curr_sess->si[0].end) == OBJ_TYPE_CONN) ?
4320 curr_sess->si[0].conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004321 curr_sess->si[0].exp ?
4322 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4323 TICKS_TO_MS(1000)) : "");
4324
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004325 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004326 " s1=[%d,%1xh,fd=%d,ex=%s]",
4327 curr_sess->si[1].state,
4328 curr_sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004329 (obj_type(curr_sess->si[1].end) == OBJ_TYPE_CONN) ?
4330 curr_sess->si[1].conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004331 curr_sess->si[1].exp ?
4332 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4333 TICKS_TO_MS(1000)) : "");
4334
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004335 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004336 " exp=%s",
4337 curr_sess->task->expire ?
4338 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4339 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004340 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004341 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004342
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004343 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004344
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004345 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004346 /* let's try again later from this session. We add ourselves into
4347 * this session's users so that it can remove us upon termination.
4348 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004349 LIST_ADDQ(&curr_sess->back_refs, &si->appctx.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004350 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004351 }
4352
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004353 next_sess:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004354 si->appctx.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004355 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004356
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004357 if (si->appctx.ctx.sess.target && si->appctx.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004358 /* specified session not found */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004359 if (si->appctx.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004360 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004361 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004362 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004363
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004364 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004365 return 0;
4366
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004367 si->appctx.ctx.sess.target = NULL;
4368 si->appctx.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004369 return 1;
4370 }
4371
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004372 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004373 /* fall through */
4374
4375 default:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004376 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004377 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004378 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004379}
4380
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004381/* This is called when the stream interface is closed. For instance, upon an
4382 * external abort, we won't call the i/o handler anymore so we may need to
4383 * remove back references to the session currently being dumped.
4384 */
Simon Horman74d88312013-02-12 10:45:50 +09004385static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004386{
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004387 if (si->appctx.st0 == STAT_CLI_O_SESS && si->appctx.st2 == STAT_ST_LIST) {
4388 if (!LIST_ISEMPTY(&si->appctx.ctx.sess.bref.users))
4389 LIST_DEL(&si->appctx.ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004390 }
4391}
4392
Willy Tarreau20e99322013-04-13 09:22:25 +02004393/* This function is used to either dump tables states (when action is set
4394 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02004395 * It returns 0 if the output buffer is full and it needs to be called
4396 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004397 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004398static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004399{
Willy Tarreau306f8302013-07-08 15:53:06 +02004400 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004401 struct ebmb_node *eb;
4402 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004403 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004404 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004405
4406 /*
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004407 * We have 3 possible states in si->appctx.st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01004408 * - STAT_ST_INIT : the first call
4409 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004410 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004411 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004412 * and the entry pointer points to the next entry to be dumped,
4413 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004414 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004415 * data though.
4416 */
4417
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004418 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004419 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004420 if (si->appctx.st2 == STAT_ST_LIST) {
4421 si->appctx.ctx.table.entry->ref_cnt--;
4422 stksess_kill_if_expired(&si->appctx.ctx.table.proxy->table, si->appctx.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004423 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004424 return 1;
4425 }
4426
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004427 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004428
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004429 while (si->appctx.st2 != STAT_ST_FIN) {
4430 switch (si->appctx.st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004431 case STAT_ST_INIT:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004432 si->appctx.ctx.table.proxy = si->appctx.ctx.table.target;
4433 if (!si->appctx.ctx.table.proxy)
4434 si->appctx.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004435
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004436 si->appctx.ctx.table.entry = NULL;
4437 si->appctx.st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004438 break;
4439
Willy Tarreau295a8372011-03-10 11:25:07 +01004440 case STAT_ST_INFO:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004441 if (!si->appctx.ctx.table.proxy ||
4442 (si->appctx.ctx.table.target &&
4443 si->appctx.ctx.table.proxy != si->appctx.ctx.table.target)) {
4444 si->appctx.st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004445 break;
4446 }
4447
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004448 if (si->appctx.ctx.table.proxy->table.size) {
4449 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->appctx.ctx.table.proxy,
4450 si->appctx.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004451 return 0;
4452
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004453 if (si->appctx.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004454 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004455 /* dump entries only if table explicitly requested */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004456 eb = ebmb_first(&si->appctx.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004457 if (eb) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004458 si->appctx.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4459 si->appctx.ctx.table.entry->ref_cnt++;
4460 si->appctx.st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004461 break;
4462 }
4463 }
4464 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004465 si->appctx.ctx.table.proxy = si->appctx.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004466 break;
4467
Willy Tarreau295a8372011-03-10 11:25:07 +01004468 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004469 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004470
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004471 if (si->appctx.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004472 /* we're filtering on some data contents */
4473 void *ptr;
4474 long long data;
4475
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004476 dt = si->appctx.ctx.table.data_type;
4477 ptr = stktable_data_ptr(&si->appctx.ctx.table.proxy->table,
4478 si->appctx.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004479 dt);
4480
4481 data = 0;
4482 switch (stktable_data_types[dt].std_type) {
4483 case STD_T_SINT:
4484 data = stktable_data_cast(ptr, std_t_sint);
4485 break;
4486 case STD_T_UINT:
4487 data = stktable_data_cast(ptr, std_t_uint);
4488 break;
4489 case STD_T_ULL:
4490 data = stktable_data_cast(ptr, std_t_ull);
4491 break;
4492 case STD_T_FRQP:
4493 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004494 si->appctx.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004495 break;
4496 }
4497
4498 /* skip the entry if the data does not match the test and the value */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004499 if ((data < si->appctx.ctx.table.value &&
4500 (si->appctx.ctx.table.data_op == STD_OP_EQ ||
4501 si->appctx.ctx.table.data_op == STD_OP_GT ||
4502 si->appctx.ctx.table.data_op == STD_OP_GE)) ||
4503 (data == si->appctx.ctx.table.value &&
4504 (si->appctx.ctx.table.data_op == STD_OP_NE ||
4505 si->appctx.ctx.table.data_op == STD_OP_GT ||
4506 si->appctx.ctx.table.data_op == STD_OP_LT)) ||
4507 (data > si->appctx.ctx.table.value &&
4508 (si->appctx.ctx.table.data_op == STD_OP_EQ ||
4509 si->appctx.ctx.table.data_op == STD_OP_LT ||
4510 si->appctx.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004511 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004512 }
4513
Simon Hormanc88b8872011-06-15 15:18:49 +09004514 if (show && !skip_entry &&
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004515 !stats_dump_table_entry_to_buffer(&trash, si, si->appctx.ctx.table.proxy,
4516 si->appctx.ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09004517 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004518
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004519 si->appctx.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004520
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004521 eb = ebmb_next(&si->appctx.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004522 if (eb) {
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004523 struct stksess *old = si->appctx.ctx.table.entry;
4524 si->appctx.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004525 if (show)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004526 stksess_kill_if_expired(&si->appctx.ctx.table.proxy->table, old);
4527 else if (!skip_entry && !si->appctx.ctx.table.entry->ref_cnt)
4528 stksess_kill(&si->appctx.ctx.table.proxy->table, old);
4529 si->appctx.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004530 break;
4531 }
4532
Simon Hormanc88b8872011-06-15 15:18:49 +09004533
4534 if (show)
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004535 stksess_kill_if_expired(&si->appctx.ctx.table.proxy->table, si->appctx.ctx.table.entry);
4536 else if (!skip_entry && !si->appctx.ctx.table.entry->ref_cnt)
4537 stksess_kill(&si->appctx.ctx.table.proxy->table, si->appctx.ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09004538
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004539 si->appctx.ctx.table.proxy = si->appctx.ctx.table.proxy->next;
4540 si->appctx.st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004541 break;
4542
Willy Tarreau295a8372011-03-10 11:25:07 +01004543 case STAT_ST_END:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004544 si->appctx.st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004545 break;
4546 }
4547 }
4548 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004549}
4550
Willy Tarreaud426a182010-03-05 14:58:26 +01004551/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004552 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4553 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4554 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4555 * lines are respected within the limit of 70 output chars. Lines that are
4556 * continuation of a previous truncated line begin with "+" instead of " "
4557 * after the offset. The new pointer is returned.
4558 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004559static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4560 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004561{
4562 int end;
4563 unsigned char c;
4564
4565 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004566 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004567 return ptr;
4568
Willy Tarreau77804732012-10-29 16:14:26 +01004569 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004570
Willy Tarreaud426a182010-03-05 14:58:26 +01004571 while (ptr < len && ptr < bsize) {
4572 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004573 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004574 if (out->len > end - 2)
4575 break;
4576 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004577 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004578 if (out->len > end - 3)
4579 break;
4580 out->str[out->len++] = '\\';
4581 switch (c) {
4582 case '\t': c = 't'; break;
4583 case '\n': c = 'n'; break;
4584 case '\r': c = 'r'; break;
4585 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004586 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004587 }
4588 out->str[out->len++] = c;
4589 } else {
4590 if (out->len > end - 5)
4591 break;
4592 out->str[out->len++] = '\\';
4593 out->str[out->len++] = 'x';
4594 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4595 out->str[out->len++] = hextab[c & 0xF];
4596 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004597 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004598 /* we had a line break, let's return now */
4599 out->str[out->len++] = '\n';
4600 *line = ptr;
4601 return ptr;
4602 }
4603 }
4604 /* we have an incomplete line, we return it as-is */
4605 out->str[out->len++] = '\n';
4606 return ptr;
4607}
4608
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004609/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004610 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004611 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004612 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004613static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004614{
4615 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004616
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004617 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004618 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004619
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004620 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004621
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004622 if (!si->appctx.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004623 /* the function had not been called yet, let's prepare the
4624 * buffer for a response.
4625 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004626 struct tm tm;
4627
4628 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004630 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4631 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4632 error_snapshot_id);
4633
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004634 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004635 /* Socket buffer full. Let's try again later from the same point */
4636 return 0;
4637 }
4638
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004639 si->appctx.ctx.errors.px = proxy;
4640 si->appctx.ctx.errors.buf = 0;
4641 si->appctx.ctx.errors.bol = 0;
4642 si->appctx.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004643 }
4644
4645 /* we have two inner loops here, one for the proxy, the other one for
4646 * the buffer.
4647 */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004648 while (si->appctx.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004649 struct error_snapshot *es;
4650
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004651 if (si->appctx.ctx.errors.buf == 0)
4652 es = &si->appctx.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004653 else
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004654 es = &si->appctx.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004655
4656 if (!es->when.tv_sec)
4657 goto next;
4658
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004659 if (si->appctx.ctx.errors.iid >= 0 &&
4660 si->appctx.ctx.errors.px->uuid != si->appctx.ctx.errors.iid &&
4661 es->oe->uuid != si->appctx.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004662 goto next;
4663
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004664 if (si->appctx.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004665 /* just print headers now */
4666
4667 char pn[INET6_ADDRSTRLEN];
4668 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004669 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004670
4671 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004672 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004673 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004674 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004675
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004676 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4677 case AF_INET:
4678 case AF_INET6:
4679 port = get_host_port(&es->src);
4680 break;
4681 default:
4682 port = 0;
4683 }
4684
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004685 switch (si->appctx.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004686 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004687 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004688 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004689 " backend %s (#%d)",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004690 si->appctx.ctx.errors.px->id, si->appctx.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004691 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4692 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004693 break;
4694 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004695 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004696 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004697 " frontend %s (#%d)",
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004698 si->appctx.ctx.errors.px->id, si->appctx.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004699 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004700 break;
4701 }
4702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004704 ", server %s (#%d), event #%u\n"
4705 " src %s:%d, session #%d, session flags 0x%08x\n"
4706 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4707 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4708 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4709 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4710 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4711 es->ev_id,
4712 pn, port, es->sid, es->s_flags,
4713 es->state, es->m_flags, es->t_flags,
4714 es->m_clen, es->m_blen,
4715 es->b_flags, es->b_out, es->b_tot,
4716 es->len, es->b_wrap, es->pos);
4717
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004718 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004719 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004720 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004721 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004722 si->appctx.ctx.errors.ptr = 0;
4723 si->appctx.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004724 }
4725
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004726 if (si->appctx.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004727 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004728 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004729 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004730 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004731 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004732 goto next;
4733 }
4734
4735 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004736 while (si->appctx.ctx.errors.ptr < es->len && si->appctx.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004737 int newptr;
4738 int newline;
4739
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004740 newline = si->appctx.ctx.errors.bol;
4741 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, si->appctx.ctx.errors.ptr);
4742 if (newptr == si->appctx.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004743 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004744
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004745 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004746 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004747 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004748 }
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004749 si->appctx.ctx.errors.ptr = newptr;
4750 si->appctx.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004751 };
4752 next:
Willy Tarreau9b6c2c72013-11-24 09:38:33 +01004753 si->appctx.ctx.errors.bol = 0;
4754 si->appctx.ctx.errors.ptr = -1;
4755 si->appctx.ctx.errors.buf++;
4756 if (si->appctx.ctx.errors.buf > 1) {
4757 si->appctx.ctx.errors.buf = 0;
4758 si->appctx.ctx.errors.px = si->appctx.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004759 }
4760 }
4761
4762 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004763 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004764}
4765
Willy Tarreaud5781202012-09-22 19:32:35 +02004766/* parse the "level" argument on the bind lines */
4767static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4768{
4769 if (!*args[cur_arg + 1]) {
4770 memprintf(err, "'%s' : missing level", args[cur_arg]);
4771 return ERR_ALERT | ERR_FATAL;
4772 }
4773
4774 if (!strcmp(args[cur_arg+1], "user"))
4775 conf->level = ACCESS_LVL_USER;
4776 else if (!strcmp(args[cur_arg+1], "operator"))
4777 conf->level = ACCESS_LVL_OPER;
4778 else if (!strcmp(args[cur_arg+1], "admin"))
4779 conf->level = ACCESS_LVL_ADMIN;
4780 else {
4781 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4782 args[cur_arg], args[cur_arg+1]);
4783 return ERR_ALERT | ERR_FATAL;
4784 }
4785
4786 return 0;
4787}
4788
Willy Tarreaub24281b2011-02-13 13:16:36 +01004789struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004790 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004791 .name = "<STATS>", /* used for logging */
4792 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004793 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004794};
4795
Simon Horman9bd2c732011-06-15 15:18:44 +09004796static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004797 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004798 .name = "<CLI>", /* used for logging */
4799 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004800 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004801};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004802
Willy Tarreaudc13c112013-06-21 23:16:39 +02004803static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004804 { CFG_GLOBAL, "stats", stats_parse_global },
4805 { 0, NULL, NULL },
4806}};
4807
Willy Tarreaud5781202012-09-22 19:32:35 +02004808static struct bind_kw_list bind_kws = { "STAT", { }, {
4809 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4810 { NULL, NULL, 0 },
4811}};
4812
Willy Tarreau10522fd2008-07-09 20:12:41 +02004813__attribute__((constructor))
4814static void __dumpstats_module_init(void)
4815{
4816 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004817 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004818}
4819
Willy Tarreau91861262007-10-17 17:06:05 +02004820/*
4821 * Local variables:
4822 * c-indent-level: 8
4823 * c-basic-offset: 8
4824 * End:
4825 */