blob: d1518b8db2ef94b754b2055ae914e2d90a342946 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010051#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010064static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010065static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090066static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010068static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010069static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Simon Horman9bd2c732011-06-15 15:18:44 +090071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010072/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010073 * cli_io_handler()
74 * -> stats_dump_sess_to_buffer() // "show sess"
75 * -> stats_dump_errors_to_buffer() // "show errors"
76 * -> stats_dump_info_to_buffer() // "show info"
77 * -> stats_dump_stat_to_buffer() // "show stat"
78 * -> stats_dump_csv_header()
79 * -> stats_dump_proxy_to_buffer()
80 * -> stats_dump_fe_stats()
81 * -> stats_dump_li_stats()
82 * -> stats_dump_sv_stats()
83 * -> stats_dump_be_stats()
84 *
85 * http_stats_io_handler()
86 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
87 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
88 * -> stats_dump_html_head() // emits the HTML headers
89 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
90 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
91 * -> stats_dump_html_px_hdr()
92 * -> stats_dump_fe_stats()
93 * -> stats_dump_li_stats()
94 * -> stats_dump_sv_stats()
95 * -> stats_dump_be_stats()
96 * -> stats_dump_html_px_end()
97 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010098 */
99
Simon Horman9bd2c732011-06-15 15:18:44 +0900100static struct si_applet cli_applet;
101
102static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200103 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200104 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200105 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200106 " help : this message\n"
107 " prompt : toggle interactive mode with prompt\n"
108 " quit : disconnect\n"
109 " show info : report information about the running process\n"
110 " show stat : report counters for each proxy and server\n"
111 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100112 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200113 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200114 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200115 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200116 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100117 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200118 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200119 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200120 " disable : put a server or frontend in maintenance mode\n"
121 " enable : re-enable a server or frontend which is in maintenance mode\n"
122 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200123 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200126 "Permission denied\n"
127 "";
128
Willy Tarreau295a8372011-03-10 11:25:07 +0100129/* data transmission states for the stats responses */
130enum {
131 STAT_ST_INIT = 0,
132 STAT_ST_HEAD,
133 STAT_ST_INFO,
134 STAT_ST_LIST,
135 STAT_ST_END,
136 STAT_ST_FIN,
137};
138
139/* data transmission states for the stats responses inside a proxy */
140enum {
141 STAT_PX_ST_INIT = 0,
142 STAT_PX_ST_TH,
143 STAT_PX_ST_FE,
144 STAT_PX_ST_LI,
145 STAT_PX_ST_SV,
146 STAT_PX_ST_BE,
147 STAT_PX_ST_END,
148 STAT_PX_ST_FIN,
149};
150
Cyril Bonté19979e12012-04-04 12:57:21 +0200151extern const char *stat_status_codes[];
152
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200153/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200155 * needs to be closed and ignored, or a negative value upon critical failure.
156 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900157static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200158{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100159 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100160 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200161
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200162 tv_zero(&s->logs.tv_request);
163 s->logs.t_queue = 0;
164 s->logs.t_connect = 0;
165 s->logs.t_data = 0;
166 s->logs.t_close = 0;
167 s->logs.bytes_in = s->logs.bytes_out = 0;
168 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
169 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200170
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200171 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200172
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200173 if (s->listener->timeout) {
174 s->req->rto = *s->listener->timeout;
175 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200176 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200177 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200178}
179
Willy Tarreau07e9e642010-08-17 21:48:17 +0200180/* allocate a new stats frontend named <name>, and return it
181 * (or NULL in case of lack of memory).
182 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200183static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200184{
185 struct proxy *fe;
186
187 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
188 if (!fe)
189 return NULL;
190
Willy Tarreau237250c2011-07-29 01:49:03 +0200191 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200192 fe->next = proxy;
193 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200194 fe->last_change = now.tv_sec;
195 fe->id = strdup("GLOBAL");
196 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200197 fe->maxconn = 10; /* default to 10 concurrent connections */
198 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200199 fe->conf.file = strdup(file);
200 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200201 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200202
203 /* the stats frontend is the only one able to assign ID #0 */
204 fe->conf.id.key = fe->uuid = 0;
205 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200206 return fe;
207}
208
Willy Tarreaufbee7132007-10-18 13:53:22 +0200209/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200210 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
211 * error message into the <err> buffer which will be preallocated. The trailing
212 * '\n' must not be written. The function must be called with <args> pointing to
213 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200214 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200215static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200216 struct proxy *defpx, const char *file, int line,
217 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200218{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200219 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200220 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200221
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200222 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200223 int cur_arg;
224
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200226 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200227 return -1;
228 }
229
Willy Tarreau89a63132009-08-16 17:41:45 +0200230 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200231 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200232 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200233 return -1;
234 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200235 }
236
Willy Tarreau4348fad2012-09-20 16:48:07 +0200237 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200238 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200239
Willy Tarreauc53d4222012-09-20 20:19:28 +0200240 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
241 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
242 file, line, args[0], args[1], err && *err ? *err : "error");
243 return -1;
244 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200245
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200246 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200247 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200248 static int bind_dumped;
249 struct bind_kw *kw;
250
251 kw = bind_find_kw(args[cur_arg]);
252 if (kw) {
253 if (!kw->parse) {
254 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
255 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200256 return -1;
257 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200258
259 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
260 if (err && *err)
261 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
262 else
263 memprintf(err, "'%s %s' : error encountered while processing '%s'",
264 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200265 return -1;
266 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200267
268 cur_arg += 1 + kw->skip;
269 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200270 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200271
272 if (!bind_dumped) {
273 bind_dump_kws(err);
274 indent_msg(err, 4);
275 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200276 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200277
278 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
279 args[0], args[1], args[cur_arg],
280 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
281 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100283
Willy Tarreauc53d4222012-09-20 20:19:28 +0200284 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
285 l->maxconn = global.stats_fe->maxconn;
286 l->backlog = global.stats_fe->backlog;
287 l->timeout = &global.stats_fe->timeout.client;
288 l->accept = session_accept;
289 l->handler = process_session;
290 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
291 l->nice = -64; /* we want to boost priority for local stats */
292 global.maxsock += l->maxconn;
293 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200294 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200295 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100296 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100298
299 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100301 return -1;
302 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200303
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200305 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306 return -1;
307 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200308 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200309 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200310 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200311 return -1;
312 }
313 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200314 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200315 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200316 else if (!strcmp(args[1], "maxconn")) {
317 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318
319 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321 return -1;
322 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200323
324 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200325 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200326 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200327 return -1;
328 }
329 }
330 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200332 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
333 int cur_arg = 2;
334 unsigned int set = 0;
335
Willy Tarreau91319572013-04-20 09:48:50 +0200336 if (!global.stats_fe) {
337 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
338 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
339 return -1;
340 }
341 }
342
Willy Tarreau35b7b162012-10-22 23:17:18 +0200343 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100344 unsigned int low, high;
345
Willy Tarreau35b7b162012-10-22 23:17:18 +0200346 if (strcmp(args[cur_arg], "all") == 0) {
347 set = 0;
348 break;
349 }
350 else if (strcmp(args[cur_arg], "odd") == 0) {
351 set |= 0x55555555;
352 }
353 else if (strcmp(args[cur_arg], "even") == 0) {
354 set |= 0xAAAAAAAA;
355 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100356 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100357 char *dash = strchr(args[cur_arg], '-');
358
359 low = high = str2uic(args[cur_arg]);
360 if (dash)
361 high = str2uic(dash + 1);
362
363 if (high < low) {
364 unsigned int swap = low;
365 low = high;
366 high = swap;
367 }
368
369 if (low < 1 || high > 32) {
370 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
371 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200372 return -1;
373 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100374
375 while (low <= high)
376 set |= 1 << (low++ - 1);
377 }
378 else {
379 memprintf(err,
380 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
381 args[0], args[1]);
382 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200383 }
384 cur_arg++;
385 }
386 global.stats_fe->bind_proc = set;
387 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200388 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200389 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200390 return -1;
391 }
392 return 0;
393}
394
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100395/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
396 * for clearing it if needed.
397 */
398static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100399{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100400 chunk_appendf(&trash,
401 "# pxname,svname,"
402 "qcur,qmax,"
403 "scur,smax,slim,stot,"
404 "bin,bout,"
405 "dreq,dresp,"
406 "ereq,econ,eresp,"
407 "wretr,wredis,"
408 "status,weight,act,bck,"
409 "chkfail,chkdown,lastchg,downtime,qlimit,"
410 "pid,iid,sid,throttle,lbtot,tracked,type,"
411 "rate,rate_lim,rate_max,"
412 "check_status,check_code,check_duration,"
413 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
414 "req_rate,req_rate_max,req_tot,"
415 "cli_abrt,srv_abrt,"
416 "comp_in,comp_out,comp_byp,comp_rsp,"
417 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100418}
419
Simon Hormand9366582011-06-15 15:18:45 +0900420/* print a string of text buffer to <out>. The format is :
421 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
422 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
423 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
424 */
425static int dump_text(struct chunk *out, const char *buf, int bsize)
426{
427 unsigned char c;
428 int ptr = 0;
429
430 while (buf[ptr] && ptr < bsize) {
431 c = buf[ptr];
432 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
433 if (out->len > out->size - 1)
434 break;
435 out->str[out->len++] = c;
436 }
437 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
438 if (out->len > out->size - 2)
439 break;
440 out->str[out->len++] = '\\';
441 switch (c) {
442 case ' ': c = ' '; break;
443 case '\t': c = 't'; break;
444 case '\n': c = 'n'; break;
445 case '\r': c = 'r'; break;
446 case '\e': c = 'e'; break;
447 case '\\': c = '\\'; break;
448 }
449 out->str[out->len++] = c;
450 }
451 else {
452 if (out->len > out->size - 4)
453 break;
454 out->str[out->len++] = '\\';
455 out->str[out->len++] = 'x';
456 out->str[out->len++] = hextab[(c >> 4) & 0xF];
457 out->str[out->len++] = hextab[c & 0xF];
458 }
459 ptr++;
460 }
461
462 return ptr;
463}
464
465/* print a buffer in hexa.
466 * Print stopped if <bsize> is reached, or if no more place in the chunk.
467 */
468static int dump_binary(struct chunk *out, const char *buf, int bsize)
469{
470 unsigned char c;
471 int ptr = 0;
472
473 while (ptr < bsize) {
474 c = buf[ptr];
475
476 if (out->len > out->size - 2)
477 break;
478 out->str[out->len++] = hextab[(c >> 4) & 0xF];
479 out->str[out->len++] = hextab[c & 0xF];
480
481 ptr++;
482 }
483 return ptr;
484}
485
486/* Dump the status of a table to a stream interface's
487 * read buffer. It returns 0 if the output buffer is full
488 * and needs to be called again, otherwise non-zero.
489 */
490static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
491 struct proxy *proxy, struct proxy *target)
492{
Willy Tarreau306f8302013-07-08 15:53:06 +0200493 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900494
Willy Tarreau77804732012-10-29 16:14:26 +0100495 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900496 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
497
498 /* any other information should be dumped here */
499
Willy Tarreau290e63a2012-09-20 18:07:14 +0200500 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100501 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900502
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200503 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900504 return 0;
505
506 return 1;
507}
508
509/* Dump the a table entry to a stream interface's
510 * read buffer. It returns 0 if the output buffer is full
511 * and needs to be called again, otherwise non-zero.
512 */
513static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
514 struct proxy *proxy, struct stksess *entry)
515{
516 int dt;
517
Willy Tarreau77804732012-10-29 16:14:26 +0100518 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900519
520 if (proxy->table.type == STKTABLE_TYPE_IP) {
521 char addr[INET_ADDRSTRLEN];
522 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100523 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900524 }
525 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
526 char addr[INET6_ADDRSTRLEN];
527 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100528 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900529 }
530 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900532 }
533 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900535 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
536 }
537 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900539 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
540 }
541
Willy Tarreau77804732012-10-29 16:14:26 +0100542 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900543
544 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
545 void *ptr;
546
547 if (proxy->table.data_ofs[dt] == 0)
548 continue;
549 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100550 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900551 else
Willy Tarreau77804732012-10-29 16:14:26 +0100552 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900553
554 ptr = stktable_data_ptr(&proxy->table, entry, dt);
555 switch (stktable_data_types[dt].std_type) {
556 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100557 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900558 break;
559 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900561 break;
562 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900564 break;
565 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900567 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
568 proxy->table.data_arg[dt].u));
569 break;
570 }
571 }
Willy Tarreau77804732012-10-29 16:14:26 +0100572 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900573
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200574 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900575 return 0;
576
577 return 1;
578}
579
Willy Tarreaudec98142012-06-06 23:37:08 +0200580static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900581{
Willy Tarreau306f8302013-07-08 15:53:06 +0200582 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100583 struct appctx *appctx = __objt_appctx(si->end);
584 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900585 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900586 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900587 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200588 long long value;
589 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200590 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200591 void *ptr;
592 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900593
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100594 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900595
596 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100597 appctx->ctx.cli.msg = "Key value expected\n";
598 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900599 return;
600 }
601
Simon Hormanc5b89f62011-06-15 15:18:50 +0900602 switch (px->table.type) {
603 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900604 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100605 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900606 break;
607 case STKTABLE_TYPE_IPV6:
608 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100609 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900610 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900611 case STKTABLE_TYPE_INTEGER:
612 {
613 char *endptr;
614 unsigned long val;
615 errno = 0;
616 val = strtoul(args[4], &endptr, 10);
617 if ((errno == ERANGE && val == ULONG_MAX) ||
618 (errno != 0 && val == 0) || endptr == args[4] ||
619 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100620 appctx->ctx.cli.msg = "Invalid key\n";
621 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900622 return;
623 }
624 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100625 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900626 break;
627 }
628 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900629 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100630 static_table_key->key = args[4];
631 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900632 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900633 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200634 switch (action) {
635 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100636 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200637 break;
638 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100639 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200640 break;
641 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100642 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200643 break;
644 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100645 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900646 return;
647 }
648
649 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200650 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100651 appctx->ctx.cli.msg = stats_permission_denied_msg;
652 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900653 return;
654 }
655
Willy Tarreau07115412012-10-29 21:56:59 +0100656 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900657
Willy Tarreaudec98142012-06-06 23:37:08 +0200658 switch (action) {
659 case STAT_CLI_O_TAB:
660 if (!ts)
661 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100662 chunk_reset(&trash);
663 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900664 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900666 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200667
668 case STAT_CLI_O_CLR:
669 if (!ts)
670 return;
671 if (ts->ref_cnt) {
672 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100673 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
674 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200675 return;
676 }
677 stksess_kill(&px->table, ts);
678 break;
Simon Horman17bce342011-06-15 15:18:47 +0900679
Willy Tarreau654694e2012-06-07 01:03:16 +0200680 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200681 if (ts)
682 stktable_touch(&px->table, ts, 1);
683 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100684 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200685 if (!ts) {
686 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100687 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
688 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200689 return;
690 }
691 stktable_store(&px->table, ts, 1);
692 }
693
Willy Tarreau47060b62013-08-01 21:11:42 +0200694 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
695 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
697 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200698 return;
699 }
700
701 data_type = stktable_get_data_type(args[cur_arg] + 5);
702 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100703 appctx->ctx.cli.msg = "Unknown data type\n";
704 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200705 return;
706 }
707
708 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100709 appctx->ctx.cli.msg = "Data type not stored in this table\n";
710 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200711 return;
712 }
713
714 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
716 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200717 return;
718 }
719
720 ptr = stktable_data_ptr(&px->table, ts, data_type);
721
722 switch (stktable_data_types[data_type].std_type) {
723 case STD_T_SINT:
724 stktable_data_cast(ptr, std_t_sint) = value;
725 break;
726 case STD_T_UINT:
727 stktable_data_cast(ptr, std_t_uint) = value;
728 break;
729 case STD_T_ULL:
730 stktable_data_cast(ptr, std_t_ull) = value;
731 break;
732 case STD_T_FRQP:
733 /* We set both the current and previous values. That way
734 * the reported frequency is stable during all the period
735 * then slowly fades out. This allows external tools to
736 * push measures without having to update them too often.
737 */
738 frqp = &stktable_data_cast(ptr, std_t_frqp);
739 frqp->curr_tick = now_ms;
740 frqp->prev_ctr = 0;
741 frqp->curr_ctr = value;
742 break;
743 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200744 }
745 break;
746
Willy Tarreaudec98142012-06-06 23:37:08 +0200747 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100748 appctx->ctx.cli.msg = "Unknown action\n";
749 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200750 break;
Simon Horman121f3052011-06-15 15:18:46 +0900751 }
Simon Horman121f3052011-06-15 15:18:46 +0900752}
753
Willy Tarreau654694e2012-06-07 01:03:16 +0200754static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900755{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100756 struct appctx *appctx = __objt_appctx(si->end);
757
Willy Tarreau04b3a192013-04-13 09:41:37 +0200758 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100759 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
760 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200761 return;
762 }
763
Simon Hormand5b9fd92011-06-15 15:18:48 +0900764 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100765 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
766 if (appctx->ctx.table.data_type < 0) {
767 appctx->ctx.cli.msg = "Unknown data type\n";
768 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900769 return;
770 }
771
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100772 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
773 appctx->ctx.cli.msg = "Data type not stored in this table\n";
774 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900775 return;
776 }
777
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100778 appctx->ctx.table.data_op = get_std_op(args[4]);
779 if (appctx->ctx.table.data_op < 0) {
780 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
781 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900782 return;
783 }
784
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100785 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
786 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
787 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900788 return;
789 }
790}
791
Willy Tarreaudec98142012-06-06 23:37:08 +0200792static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900793{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100794 struct appctx *appctx = __objt_appctx(si->end);
795
796 appctx->ctx.table.data_type = -1;
797 appctx->st2 = STAT_ST_INIT;
798 appctx->ctx.table.target = NULL;
799 appctx->ctx.table.proxy = NULL;
800 appctx->ctx.table.entry = NULL;
801 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900802
803 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100804 appctx->ctx.table.target = find_stktable(args[2]);
805 if (!appctx->ctx.table.target) {
806 appctx->ctx.cli.msg = "No such table\n";
807 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900808 return;
809 }
810 }
811 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200812 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900813 goto err_args;
814 return;
815 }
816
817 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200818 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900819 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200820 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900821 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900822 goto err_args;
823
824 return;
825
826err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200827 switch (action) {
828 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100829 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200830 break;
831 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100832 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200833 break;
834 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200836 break;
837 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900839}
840
Willy Tarreau532a4502011-09-07 22:37:44 +0200841/* Expects to find a frontend named <arg> and returns it, otherwise displays various
842 * adequate error messages and returns NULL. This function also expects the session
843 * level to be admin.
844 */
845static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
846{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100847 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200848 struct proxy *px;
849
Willy Tarreau290e63a2012-09-20 18:07:14 +0200850 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 appctx->ctx.cli.msg = stats_permission_denied_msg;
852 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200853 return NULL;
854 }
855
856 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 appctx->ctx.cli.msg = "A frontend name is expected.\n";
858 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200859 return NULL;
860 }
861
862 px = findproxy(arg, PR_CAP_FE);
863 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 appctx->ctx.cli.msg = "No such frontend.\n";
865 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200866 return NULL;
867 }
868 return px;
869}
870
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200871/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
872 * and returns the pointer to the server. Otherwise, display adequate error messages
873 * and returns NULL. This function also expects the session level to be admin. Note:
874 * the <arg> is modified to remove the '/'.
875 */
876static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
877{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100878 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200879 struct proxy *px;
880 struct server *sv;
881 char *line;
882
Willy Tarreau290e63a2012-09-20 18:07:14 +0200883 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100884 appctx->ctx.cli.msg = stats_permission_denied_msg;
885 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200886 return NULL;
887 }
888
889 /* split "backend/server" and make <line> point to server */
890 for (line = arg; *line; line++)
891 if (*line == '/') {
892 *line++ = '\0';
893 break;
894 }
895
896 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
898 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200899 return NULL;
900 }
901
902 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200905 return NULL;
906 }
907
908 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100909 appctx->ctx.cli.msg = "Proxy is disabled.\n";
910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200911 return NULL;
912 }
913
914 return sv;
915}
916
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200917/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200918 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100919 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200920 * designating the function which will have to process the request, which can
921 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900923static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200924{
Willy Tarreau306f8302013-07-08 15:53:06 +0200925 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200927 char *args[MAX_STATS_ARGS + 1];
928 int arg;
929
930 while (isspace((unsigned char)*line))
931 line++;
932
933 arg = 0;
934 args[arg] = line;
935
936 while (*line && arg < MAX_STATS_ARGS) {
937 if (isspace((unsigned char)*line)) {
938 *line++ = '\0';
939
940 while (isspace((unsigned char)*line))
941 line++;
942
943 args[++arg] = line;
944 continue;
945 }
946
947 line++;
948 }
949
950 while (++arg <= MAX_STATS_ARGS)
951 args[arg] = line;
952
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100953 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200954 if (strcmp(args[0], "show") == 0) {
955 if (strcmp(args[1], "stat") == 0) {
956 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100957 appctx->ctx.stats.flags |= STAT_BOUND;
958 appctx->ctx.stats.iid = atoi(args[2]);
959 appctx->ctx.stats.type = atoi(args[3]);
960 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200961 }
962
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100963 appctx->st2 = STAT_ST_INIT;
964 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200965 }
966 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100967 appctx->st2 = STAT_ST_INIT;
968 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200969 }
970 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100971 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200972 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100973 appctx->ctx.cli.msg = stats_permission_denied_msg;
974 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200975 return 1;
976 }
Willy Tarreau76153662012-11-26 01:16:39 +0100977 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100978 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +0100979 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100980 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100981 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100982 appctx->ctx.sess.target = NULL;
983 appctx->ctx.sess.section = 0; /* start with session status */
984 appctx->ctx.sess.pos = 0;
985 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200986 }
987 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200988 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100989 appctx->ctx.cli.msg = stats_permission_denied_msg;
990 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200991 return 1;
992 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200993 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100994 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200995 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100996 appctx->ctx.errors.iid = -1;
997 appctx->ctx.errors.px = NULL;
998 appctx->st2 = STAT_ST_INIT;
999 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001001 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001002 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001003 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001004 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001005 return 0;
1006 }
1007 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001008 else if (strcmp(args[0], "clear") == 0) {
1009 if (strcmp(args[1], "counters") == 0) {
1010 struct proxy *px;
1011 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001012 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001013 int clrall = 0;
1014
1015 if (strcmp(args[2], "all") == 0)
1016 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001017
Willy Tarreau6162db22009-10-10 17:13:00 +02001018 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001019 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1020 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001021 appctx->ctx.cli.msg = stats_permission_denied_msg;
1022 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001023 return 1;
1024 }
1025
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001026 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001027 if (clrall) {
1028 memset(&px->be_counters, 0, sizeof(px->be_counters));
1029 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1030 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001031 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001032 px->be_counters.conn_max = 0;
1033 px->be_counters.p.http.rps_max = 0;
1034 px->be_counters.sps_max = 0;
1035 px->be_counters.cps_max = 0;
1036 px->be_counters.nbpend_max = 0;
1037
1038 px->fe_counters.conn_max = 0;
1039 px->fe_counters.p.http.rps_max = 0;
1040 px->fe_counters.sps_max = 0;
1041 px->fe_counters.cps_max = 0;
1042 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001043 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001044
1045 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001046 if (clrall)
1047 memset(&sv->counters, 0, sizeof(sv->counters));
1048 else {
1049 sv->counters.cur_sess_max = 0;
1050 sv->counters.nbpend_max = 0;
1051 sv->counters.sps_max = 0;
1052 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001053
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001054 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001055 if (li->counters) {
1056 if (clrall)
1057 memset(li->counters, 0, sizeof(*li->counters));
1058 else
1059 li->counters->conn_max = 0;
1060 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001061 }
1062
Willy Tarreau81c25d02011-09-07 15:17:21 +02001063 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001064 return 1;
1065 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001066 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001067 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001068 /* end of processing */
1069 return 1;
1070 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001071 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001072 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001073 return 0;
1074 }
1075 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001076 else if (strcmp(args[0], "get") == 0) {
1077 if (strcmp(args[1], "weight") == 0) {
1078 struct proxy *px;
1079 struct server *sv;
1080
1081 /* split "backend/server" and make <line> point to server */
1082 for (line = args[2]; *line; line++)
1083 if (*line == '/') {
1084 *line++ = '\0';
1085 break;
1086 }
1087
1088 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1090 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001091 return 1;
1092 }
1093
1094 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001095 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1096 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001097 return 1;
1098 }
1099
1100 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1102 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001103 return 1;
1104 }
1105 else { /* not "get weight" */
1106 return 0;
1107 }
1108 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001109 else if (strcmp(args[0], "set") == 0) {
1110 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001111 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001112 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001113
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001114 sv = expect_server_admin(s, si, args[2]);
1115 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001116 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001117
Simon Horman7d09b9a2013-02-12 10:45:51 +09001118 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001119 /*
1120 * The user-weight may now be zero and thus
1121 * the server considered to be draining.
1122 * Update the server's drain state as necessary.
1123 */
1124 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001125 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->ctx.cli.msg = warning;
1127 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001128 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001129 return 1;
1130 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001131 else if (strcmp(args[1], "timeout") == 0) {
1132 if (strcmp(args[2], "cli") == 0) {
1133 unsigned timeout;
1134 const char *res;
1135
1136 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001137 appctx->ctx.cli.msg = "Expects an integer value.\n";
1138 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001139 return 1;
1140 }
1141
1142 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1143 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001144 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1145 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001146 return 1;
1147 }
1148
1149 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1150 return 1;
1151 }
1152 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001153 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1154 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001155 return 1;
1156 }
1157 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001158 else if (strcmp(args[1], "maxconn") == 0) {
1159 if (strcmp(args[2], "frontend") == 0) {
1160 struct proxy *px;
1161 struct listener *l;
1162 int v;
1163
Willy Tarreau532a4502011-09-07 22:37:44 +02001164 px = expect_frontend_admin(s, si, args[3]);
1165 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001166 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001167
1168 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001169 appctx->ctx.cli.msg = "Integer value expected.\n";
1170 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001171 return 1;
1172 }
1173
1174 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001175 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001176 appctx->ctx.cli.msg = "Value out of range.\n";
1177 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001178 return 1;
1179 }
1180
1181 /* OK, the value is fine, so we assign it to the proxy and to all of
1182 * its listeners. The blocked ones will be dequeued.
1183 */
1184 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001185 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001186 l->maxconn = v;
1187 if (l->state == LI_FULL)
1188 resume_listener(l);
1189 }
1190
1191 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1192 dequeue_all_listeners(&s->fe->listener_queue);
1193
1194 return 1;
1195 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001196 else if (strcmp(args[2], "global") == 0) {
1197 int v;
1198
Willy Tarreau290e63a2012-09-20 18:07:14 +02001199 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001200 appctx->ctx.cli.msg = stats_permission_denied_msg;
1201 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001202 return 1;
1203 }
1204
1205 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001206 appctx->ctx.cli.msg = "Expects an integer value.\n";
1207 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001208 return 1;
1209 }
1210
1211 v = atoi(args[3]);
1212 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001213 appctx->ctx.cli.msg = "Value out of range.\n";
1214 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001215 return 1;
1216 }
1217
1218 /* check for unlimited values */
1219 if (v <= 0)
1220 v = global.hardmaxconn;
1221
1222 global.maxconn = v;
1223
1224 /* Dequeues all of the listeners waiting for a resource */
1225 if (!LIST_ISEMPTY(&global_listener_queue))
1226 dequeue_all_listeners(&global_listener_queue);
1227
1228 return 1;
1229 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001230 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001231 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1232 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001233 return 1;
1234 }
1235 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001236 else if (strcmp(args[1], "rate-limit") == 0) {
1237 if (strcmp(args[2], "connections") == 0) {
1238 if (strcmp(args[3], "global") == 0) {
1239 int v;
1240
Willy Tarreau290e63a2012-09-20 18:07:14 +02001241 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001242 appctx->ctx.cli.msg = stats_permission_denied_msg;
1243 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001244 return 1;
1245 }
1246
1247 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001248 appctx->ctx.cli.msg = "Expects an integer value.\n";
1249 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001250 return 1;
1251 }
1252
1253 v = atoi(args[4]);
1254 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001255 appctx->ctx.cli.msg = "Value out of range.\n";
1256 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001257 return 1;
1258 }
1259
1260 global.cps_lim = v;
1261
1262 /* Dequeues all of the listeners waiting for a resource */
1263 if (!LIST_ISEMPTY(&global_listener_queue))
1264 dequeue_all_listeners(&global_listener_queue);
1265
1266 return 1;
1267 }
1268 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001269 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1270 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001271 return 1;
1272 }
1273 }
William Lallemandd85f9172012-11-09 17:05:39 +01001274 else if (strcmp(args[2], "http-compression") == 0) {
1275 if (strcmp(args[3], "global") == 0) {
1276 int v;
1277
Willy Tarreau85d47f92012-11-21 00:29:50 +01001278 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001279 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1280 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001281 return 1;
1282 }
1283
William Lallemandd85f9172012-11-09 17:05:39 +01001284 v = atoi(args[4]);
1285 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1286 }
1287 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001288 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1289 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001290 return 1;
1291 }
1292 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001293 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001294 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
1295 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001296 return 1;
1297 }
1298 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001299 else if (strcmp(args[1], "table") == 0) {
1300 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1301 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001302 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001303 return 0;
1304 }
1305 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001306 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001307 if (strcmp(args[1], "agent") == 0) {
1308 struct server *sv;
1309
1310 sv = expect_server_admin(s, si, args[2]);
1311 if (!sv)
1312 return 1;
1313
1314 sv->agent.state &= ~CHK_STATE_DISABLED;
1315
1316 return 1;
1317 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001318 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001319 struct server *sv;
1320
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001321 sv = expect_server_admin(s, si, args[2]);
1322 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001323 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001324
Cyril Bontécd19e512010-01-31 22:34:03 +01001325 if (sv->state & SRV_MAINTAIN) {
1326 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001327 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001328 /* If this server tracks the status of another one,
1329 * we must restore the good status.
1330 */
Willy Tarreau44267702011-10-28 15:35:33 +02001331 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001332 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001333 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001334 } else {
1335 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001336 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001337 }
1338 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001339 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001340 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001341 }
1342 }
1343
Willy Tarreau532a4502011-09-07 22:37:44 +02001344 return 1;
1345 }
1346 else if (strcmp(args[1], "frontend") == 0) {
1347 struct proxy *px;
1348
1349 px = expect_frontend_admin(s, si, args[2]);
1350 if (!px)
1351 return 1;
1352
1353 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001354 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1355 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001356 return 1;
1357 }
1358
1359 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001360 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1361 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001362 return 1;
1363 }
1364
1365 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001366 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1367 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001368 return 1;
1369 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001370 return 1;
1371 }
1372 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001373 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1374 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001375 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001376 }
1377 }
1378 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001379 if (strcmp(args[1], "agent") == 0) {
1380 struct server *sv;
1381
1382 sv = expect_server_admin(s, si, args[2]);
1383 if (!sv)
1384 return 1;
1385
1386 sv->agent.state |= CHK_STATE_DISABLED;
1387
1388 return 1;
1389 }
1390 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001391 struct server *sv;
1392
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001393 sv = expect_server_admin(s, si, args[2]);
1394 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001395 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001396
Cyril Bontécd19e512010-01-31 22:34:03 +01001397 if (! (sv->state & SRV_MAINTAIN)) {
1398 /* Not already in maintenance, we can change the server state */
1399 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001400 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001401 }
1402
Willy Tarreau532a4502011-09-07 22:37:44 +02001403 return 1;
1404 }
1405 else if (strcmp(args[1], "frontend") == 0) {
1406 struct proxy *px;
1407
1408 px = expect_frontend_admin(s, si, args[2]);
1409 if (!px)
1410 return 1;
1411
1412 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001413 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1414 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001415 return 1;
1416 }
1417
1418 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001419 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1420 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001421 return 1;
1422 }
1423
1424 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1426 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001427 return 1;
1428 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001429 return 1;
1430 }
1431 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001432 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1433 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001434 return 1;
1435 }
1436 }
1437 else if (strcmp(args[0], "shutdown") == 0) {
1438 if (strcmp(args[1], "frontend") == 0) {
1439 struct proxy *px;
1440
1441 px = expect_frontend_admin(s, si, args[2]);
1442 if (!px)
1443 return 1;
1444
1445 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001446 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001448 return 1;
1449 }
1450
1451 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1452 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1453 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1454 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1455 stop_proxy(px);
1456 return 1;
1457 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001458 else if (strcmp(args[1], "session") == 0) {
1459 struct session *sess, *ptr;
1460
Willy Tarreau290e63a2012-09-20 18:07:14 +02001461 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001462 appctx->ctx.cli.msg = stats_permission_denied_msg;
1463 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001464 return 1;
1465 }
1466
1467 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001468 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1469 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001470 return 1;
1471 }
1472
1473 ptr = (void *)strtoul(args[2], NULL, 0);
1474
1475 /* first, look for the requested session in the session table */
1476 list_for_each_entry(sess, &sessions, list) {
1477 if (sess == ptr)
1478 break;
1479 }
1480
1481 /* do we have the session ? */
1482 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001483 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1484 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001485 return 1;
1486 }
1487
1488 session_shutdown(sess, SN_ERR_KILLED);
1489 return 1;
1490 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001491 else if (strcmp(args[1], "sessions") == 0) {
1492 if (strcmp(args[2], "server") == 0) {
1493 struct server *sv;
1494 struct session *sess, *sess_bck;
1495
1496 sv = expect_server_admin(s, si, args[3]);
1497 if (!sv)
1498 return 1;
1499
1500 /* kill all the session that are on this server */
1501 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1502 if (sess->srv_conn == sv)
1503 session_shutdown(sess, SN_ERR_KILLED);
1504
1505 return 1;
1506 }
1507 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001508 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1509 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001510 return 1;
1511 }
1512 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001513 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001514 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1515 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001516 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001517 }
1518 }
1519 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001520 return 0;
1521 }
1522 return 1;
1523}
1524
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001526 * used to processes I/O from/to the stats unix socket. The system relies on a
1527 * state machine handling requests and various responses. We read a request,
1528 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 * Then we can read again. The state is stored in appctx->st0 and is one of the
1530 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001531 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001532 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001533static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001534{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001535 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001536 struct channel *req = si->ob;
1537 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 int reql;
1539 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1542 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001543
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001544 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001545 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001547 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1548 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001549 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001550 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001551 /* Let's close for real now. We just close the request
1552 * side, the conditions below will complete if needed.
1553 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001554 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001555 break;
1556 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001557 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001558 /* ensure we have some output room left in the event we
1559 * would want to return some info right after parsing.
1560 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001561 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001562 break;
1563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001564 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001565 if (reql <= 0) { /* closed or EOL not found */
1566 if (reql == 0)
1567 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001568 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001569 continue;
1570 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001571
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 /* seek for a possible semi-colon. If we find one, we
1573 * replace it with an LF and skip only this part.
1574 */
1575 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001576 if (trash.str[len] == ';') {
1577 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001578 reql = len + 1;
1579 break;
1580 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001581
Willy Tarreau816fc222009-10-04 07:36:58 +02001582 /* now it is time to check that we have a full line,
1583 * remove the trailing \n and possibly \r, then cut the
1584 * line.
1585 */
1586 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001587 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001589 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001590 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001591
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001592 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001593 len--;
1594
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001595 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001597 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001598 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001599 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001600 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001601 continue;
1602 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001603 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001604 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001605 else if (strcmp(trash.str, "help") == 0 ||
1606 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001607 appctx->ctx.cli.msg = stats_sock_usage_msg;
1608 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001609 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001610 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001612 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001613 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001615 /* if prompt is disabled, print help on empty lines,
1616 * so that the user at least knows how to enable
1617 * prompt and find help.
1618 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001619 appctx->ctx.cli.msg = stats_sock_usage_msg;
1620 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 }
1622
1623 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001624 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001625 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001626 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001627 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001628 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001629 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001630 continue;
1631 }
1632
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001633 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001634 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001635 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
1636 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001637 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001638 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001639 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001640 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001641 break;
1642 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001643 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001644 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001645 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001646 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001647 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001648 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001649 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001650 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001651 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001652 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001653 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001654 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001655 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001656 if (stats_table_request(si, appctx->st0))
1657 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001658 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001659 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001660 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001661 break;
1662 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001663
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001664 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001665 if (appctx->st0 == STAT_CLI_PROMPT) {
1666 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
1667 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001668 }
1669
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001670 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001671 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001672 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001673
1674 /* Now we close the output if one of the writers did so,
1675 * or if we're not in interactive mode and the request
1676 * buffer is empty. This still allows pipelined requests
1677 * to be sent in non-interactive mode.
1678 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001679 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
1680 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001681 continue;
1682 }
1683
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001684 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001686 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001687 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001688
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001689 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001690 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1691 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001692 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001693 * and nothing more to consume. This is comparable to a broken pipe, so
1694 * we forward the close to the request side so that it flows upstream to
1695 * the client.
1696 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001697 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001698 }
1699
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001700 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001701 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1702 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1703 /* We have no more processing to do, and nothing more to send, and
1704 * the client side has closed. So we'll forward this state downstream
1705 * on the response buffer.
1706 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001707 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001708 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001709 }
1710
1711 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001712 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001713
1714 /* we don't want to expire timeouts while we're processing requests */
1715 si->ib->rex = TICK_ETERNITY;
1716 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001717
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001718 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001719 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 +02001720 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001721 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 +02001722
1723 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1724 /* check that we have released everything then unregister */
1725 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001726 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001727}
1728
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001729/* This function dumps information onto the stream interface's read buffer.
1730 * It returns 0 as long as it does not complete, non-zero upon completion.
1731 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001732 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001733static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001734{
1735 unsigned int up = (now.tv_sec - start_date.tv_sec);
1736
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001737 chunk_printf(&trash,
1738 "Name: " PRODUCT_NAME "\n"
1739 "Version: " HAPROXY_VERSION "\n"
1740 "Release_date: " HAPROXY_DATE "\n"
1741 "Nbproc: %d\n"
1742 "Process_num: %d\n"
1743 "Pid: %d\n"
1744 "Uptime: %dd %dh%02dm%02ds\n"
1745 "Uptime_sec: %d\n"
1746 "Memmax_MB: %d\n"
1747 "Ulimit-n: %d\n"
1748 "Maxsock: %d\n"
1749 "Maxconn: %d\n"
1750 "Hard_maxconn: %d\n"
1751 "Maxpipes: %d\n"
1752 "CurrConns: %d\n"
1753 "PipesUsed: %d\n"
1754 "PipesFree: %d\n"
1755 "ConnRate: %d\n"
1756 "ConnRateLimit: %d\n"
1757 "MaxConnRate: %d\n"
1758 "CompressBpsIn: %u\n"
1759 "CompressBpsOut: %u\n"
1760 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001761#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001762 "ZlibMemUsage: %ld\n"
1763 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001764#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001765 "Tasks: %d\n"
1766 "Run_queue: %d\n"
1767 "Idle_pct: %d\n"
1768 "node: %s\n"
1769 "description: %s\n"
1770 "",
1771 global.nbproc,
1772 relative_pid,
1773 pid,
1774 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1775 up,
1776 global.rlimit_memmax,
1777 global.rlimit_nofile,
1778 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1779 actconn, pipes_used, pipes_free,
1780 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1781 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1782 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001783#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001784 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001785#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001786 nb_tasks_cur, run_queue_cur, idle_pct,
1787 global.node, global.desc ? global.desc : ""
1788 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001789
1790 if (bi_putchk(si->ib, &trash) == -1)
1791 return 0;
1792
1793 return 1;
1794}
1795
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001796/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1797 * the state from stream interface <si>. The caller is responsible for clearing
1798 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001799 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001800static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001801{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001802 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001803 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001804
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001805 if (!(px->cap & PR_CAP_FE))
1806 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001807
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001808 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001809 return 0;
1810
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001811 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001812 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001813 /* name, queue */
1814 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001816 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001817 /* Column sub-heading for Enable or Disable server */
1818 chunk_appendf(&trash, "<td></td>");
1819 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001820
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001821 chunk_appendf(&trash,
1822 "<td class=ac>"
1823 "<a name=\"%s/Frontend\"></a>"
1824 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1825 "<td colspan=3></td>"
1826 "",
1827 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001828
Willy Tarreau466c9b52012-12-23 02:25:03 +01001829 chunk_appendf(&trash,
1830 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001831 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001832 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1833 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1834 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001835 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1836 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1837 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001838
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001839 if (px->mode == PR_MODE_HTTP)
1840 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001841 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001842 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001843
1844 chunk_appendf(&trash,
1845 "</table></div></u></td>"
1846 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001847 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001848 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1849 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1850 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001851 U2H(px->fe_counters.sps_max),
1852 U2H(px->fe_counters.cps_max),
1853 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001854
1855 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001856 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001857 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001858 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001859
1860 chunk_appendf(&trash,
1861 "</table></div></u></td>"
1862 /* sessions rate : limit */
1863 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001864 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001865
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001866 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001867 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001868 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001869 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001870 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1871 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001872 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001873 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1874 U2H(px->fe_counters.cum_sess),
1875 U2H(px->fe_counters.cum_conn),
1876 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001877
Willy Tarreau466c9b52012-12-23 02:25:03 +01001878 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001879 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001880 chunk_appendf(&trash,
1881 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1882 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1883 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1884 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1885 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1886 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1887 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1888 "<tr><th>- other responses:</th><td>%s</td></tr>"
1889 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1890 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001891 U2H(px->fe_counters.p.http.cum_req),
1892 U2H(px->fe_counters.p.http.rsp[1]),
1893 U2H(px->fe_counters.p.http.rsp[2]),
1894 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001895 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001896 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001897 U2H(px->fe_counters.p.http.rsp[3]),
1898 U2H(px->fe_counters.p.http.rsp[4]),
1899 U2H(px->fe_counters.p.http.rsp[5]),
1900 U2H(px->fe_counters.p.http.rsp[0]),
1901 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001902 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001903
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001904 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001905 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001906 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001907 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001908 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001909 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001910 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001911 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001912
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001913 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001914 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001915 "<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 +01001916 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001917 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001918 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1919 px->fe_counters.comp_in ?
1920 (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 +01001921 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001922
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001923 chunk_appendf(&trash,
1924 /* denied: req, resp */
1925 "<td>%s</td><td>%s</td>"
1926 /* errors : request, connect, response */
1927 "<td>%s</td><td></td><td></td>"
1928 /* warnings: retries, redispatches */
1929 "<td></td><td></td>"
1930 /* server status : reflect frontend status */
1931 "<td class=ac>%s</td>"
1932 /* rest of server: nothing */
1933 "<td class=ac colspan=8></td></tr>"
1934 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001935 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1936 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001937 px->state == PR_STREADY ? "OPEN" :
1938 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001939 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001940 else { /* CSV mode */
1941 chunk_appendf(&trash,
1942 /* pxid, name, queue cur, queue max, */
1943 "%s,FRONTEND,,,"
1944 /* sessions : current, max, limit, total */
1945 "%d,%d,%d,%lld,"
1946 /* bytes : in, out */
1947 "%lld,%lld,"
1948 /* denied: req, resp */
1949 "%lld,%lld,"
1950 /* errors : request, connect, response */
1951 "%lld,,,"
1952 /* warnings: retries, redispatches */
1953 ",,"
1954 /* server status : reflect frontend status */
1955 "%s,"
1956 /* rest of server: nothing */
1957 ",,,,,,,,"
1958 /* pid, iid, sid, throttle, lbtot, tracked, type */
1959 "%d,%d,0,,,,%d,"
1960 /* rate, rate_lim, rate_max */
1961 "%u,%u,%u,"
1962 /* check_status, check_code, check_duration */
1963 ",,,",
1964 px->id,
1965 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1966 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1967 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1968 px->fe_counters.failed_req,
1969 px->state == PR_STREADY ? "OPEN" :
1970 px->state == PR_STFULL ? "FULL" : "STOP",
1971 relative_pid, px->uuid, STATS_TYPE_FE,
1972 read_freq_ctr(&px->fe_sess_per_sec),
1973 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001974
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001975 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1976 if (px->mode == PR_MODE_HTTP) {
1977 for (i=1; i<6; i++)
1978 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1979 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1980 }
1981 else
1982 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001983
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001984 /* failed health analyses */
1985 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001986
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001987 /* requests : req_rate, req_rate_max, req_tot, */
1988 chunk_appendf(&trash, "%u,%u,%lld,",
1989 read_freq_ctr(&px->fe_req_per_sec),
1990 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1991
1992 /* errors: cli_aborts, srv_aborts */
1993 chunk_appendf(&trash, ",,");
1994
1995 /* compression: in, out, bypassed */
1996 chunk_appendf(&trash, "%lld,%lld,%lld,",
1997 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1998
1999 /* compression: comp_rsp */
2000 chunk_appendf(&trash, "%lld,",
2001 px->fe_counters.p.http.comp_rsp);
2002
2003 /* finish with EOL */
2004 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002005 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002006 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002007}
2008
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002009/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2010 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2011 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2012 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002013 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002014static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002015{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002016 struct appctx *appctx = __objt_appctx(si->end);
2017
2018 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002019 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002020 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002021 /* Column sub-heading for Enable or Disable server */
2022 chunk_appendf(&trash, "<td></td>");
2023 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002024 chunk_appendf(&trash,
2025 /* frontend name, listener name */
2026 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2027 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2028 "",
2029 px->id, l->name,
2030 (flags & ST_SHLGNDS)?"<u>":"",
2031 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002032
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002033 if (flags & ST_SHLGNDS) {
2034 char str[INET6_ADDRSTRLEN];
2035 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002036
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002037 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002038
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002039 port = get_host_port(&l->addr);
2040 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2041 case AF_INET:
2042 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2043 break;
2044 case AF_INET6:
2045 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2046 break;
2047 case AF_UNIX:
2048 chunk_appendf(&trash, "unix, ");
2049 break;
2050 case -1:
2051 chunk_appendf(&trash, "(%s), ", strerror(errno));
2052 break;
2053 }
Willy Tarreau91861262007-10-17 17:06:05 +02002054
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002055 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002056 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002057 }
Willy Tarreau91861262007-10-17 17:06:05 +02002058
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002059 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002060 /* queue */
2061 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002062 /* sessions rate: current, max, limit */
2063 "<td colspan=3>&nbsp;</td>"
2064 /* sessions: current, max, limit, total, lbtot */
2065 "<td>%s</td><td>%s</td><td>%s</td>"
2066 "<td>%s</td><td>&nbsp;</td>"
2067 /* bytes: in, out */
2068 "<td>%s</td><td>%s</td>"
2069 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002070 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002071 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2072 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002073
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002074 chunk_appendf(&trash,
2075 /* denied: req, resp */
2076 "<td>%s</td><td>%s</td>"
2077 /* errors: request, connect, response */
2078 "<td>%s</td><td></td><td></td>"
2079 /* warnings: retries, redispatches */
2080 "<td></td><td></td>"
2081 /* server status: reflect listener status */
2082 "<td class=ac>%s</td>"
2083 /* rest of server: nothing */
2084 "<td class=ac colspan=8></td></tr>"
2085 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002086 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2087 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002088 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2089 }
2090 else { /* CSV mode */
2091 chunk_appendf(&trash,
2092 /* pxid, name, queue cur, queue max, */
2093 "%s,%s,,,"
2094 /* sessions: current, max, limit, total */
2095 "%d,%d,%d,%lld,"
2096 /* bytes: in, out */
2097 "%lld,%lld,"
2098 /* denied: req, resp */
2099 "%lld,%lld,"
2100 /* errors: request, connect, response */
2101 "%lld,,,"
2102 /* warnings: retries, redispatches */
2103 ",,"
2104 /* server status: reflect listener status */
2105 "%s,"
2106 /* rest of server: nothing */
2107 ",,,,,,,,"
2108 /* pid, iid, sid, throttle, lbtot, tracked, type */
2109 "%d,%d,%d,,,,%d,"
2110 /* rate, rate_lim, rate_max */
2111 ",,,"
2112 /* check_status, check_code, check_duration */
2113 ",,,"
2114 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2115 ",,,,,,"
2116 /* failed health analyses */
2117 ","
2118 /* requests : req_rate, req_rate_max, req_tot, */
2119 ",,,"
2120 /* errors: cli_aborts, srv_aborts */
2121 ",,"
2122 /* compression: in, out, bypassed, comp_rsp */
2123 ",,,,"
2124 "\n",
2125 px->id, l->name,
2126 l->nbconn, l->counters->conn_max,
2127 l->maxconn, l->counters->cum_conn,
2128 l->counters->bytes_in, l->counters->bytes_out,
2129 l->counters->denied_req, l->counters->denied_resp,
2130 l->counters->failed_req,
2131 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2132 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2133 }
2134 return 1;
2135}
Willy Tarreau91861262007-10-17 17:06:05 +02002136
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002137/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2138 * from stream interface <si>, stats flags <flags>, and server state <state>.
2139 * The caller is responsible for clearing the trash if needed. Returns non-zero
2140 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002141 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2142 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002143 */
2144static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2145{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002147 struct server *ref = sv->track ? sv->track : sv;
2148 char str[INET6_ADDRSTRLEN];
2149 struct chunk src;
2150 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002151
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002153 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002154 "DOWN",
2155 "DN %d/%d &uarr;",
2156 "UP %d/%d &darr;",
2157 "UP",
2158 "NOLB %d/%d &darr;",
2159 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002160 "DRAIN %d/%d &darr;",
2161 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002162 "<i>no check</i>"
2163 };
Willy Tarreau91861262007-10-17 17:06:05 +02002164
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002165 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2166 chunk_appendf(&trash, "<tr class=\"maintain\">");
2167 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002168 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002169 "<tr class=\"%s%d\">",
2170 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002171
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002172 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002173 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002174 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2175 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002176
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002177 chunk_appendf(&trash,
2178 "<td class=ac><a name=\"%s/%s\"></a>%s"
2179 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2180 "",
2181 px->id, sv->id,
2182 (flags & ST_SHLGNDS) ? "<u>" : "",
2183 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002184
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002185 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002186 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002187
2188 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2189 case AF_INET:
2190 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2191 break;
2192 case AF_INET6:
2193 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2194 break;
2195 case AF_UNIX:
2196 chunk_appendf(&trash, "unix, ");
2197 break;
2198 case -1:
2199 chunk_appendf(&trash, "(%s), ", strerror(errno));
2200 break;
2201 default: /* address family not supported */
2202 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002203 }
Willy Tarreau91861262007-10-17 17:06:05 +02002204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002205 /* id */
2206 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002207
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002208 /* cookie */
2209 if (sv->cookie) {
2210 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002211
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002212 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2213 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002214
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002215 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002216 }
2217
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002218 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002219 }
Willy Tarreau91861262007-10-17 17:06:05 +02002220
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002221 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002222 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002223 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002224 /* sessions rate : current, max, limit */
2225 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002226 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002227 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002228 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2229 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002230
Willy Tarreau466c9b52012-12-23 02:25:03 +01002231
2232 chunk_appendf(&trash,
2233 /* sessions: current, max, limit, total */
2234 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002235 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002236 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2237 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002238 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2239 U2H(sv->counters.cum_sess),
2240 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002241
Willy Tarreau466c9b52012-12-23 02:25:03 +01002242 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2243 if (px->mode == PR_MODE_HTTP) {
2244 unsigned long long tot;
2245 for (tot = i = 0; i < 6; i++)
2246 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002247
Willy Tarreau466c9b52012-12-23 02:25:03 +01002248 chunk_appendf(&trash,
2249 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2250 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2251 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2252 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2253 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2254 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2255 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2256 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002257 U2H(tot),
2258 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2259 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2260 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2261 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2262 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2263 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 +02002264 }
Willy Tarreau91861262007-10-17 17:06:05 +02002265
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002266 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002267 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002268 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002269 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002270 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002271
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002272 chunk_appendf(&trash,
2273 /* bytes : in, out */
2274 "<td>%s</td><td>%s</td>"
2275 /* denied: req, resp */
2276 "<td></td><td>%s</td>"
2277 /* errors : request, connect */
2278 "<td></td><td>%s</td>"
2279 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002280 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002281 /* warnings: retries, redispatches */
2282 "<td>%lld</td><td>%lld</td>"
2283 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002284 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2285 U2H(sv->counters.failed_secu),
2286 U2H(sv->counters.failed_conns),
2287 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002288 sv->counters.cli_aborts,
2289 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002290 sv->counters.retries, sv->counters.redispatches);
2291
2292 /* status, lest check */
2293 chunk_appendf(&trash, "<td class=ac>");
2294
2295 if (sv->state & SRV_MAINTAIN) {
2296 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2297 chunk_appendf(&trash, "MAINT");
2298 }
2299 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2300 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2301 chunk_appendf(&trash, "MAINT(via)");
2302 }
2303 else if (ref->state & SRV_CHECKED) {
2304 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2305 chunk_appendf(&trash,
2306 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002307 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2308 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002309 }
Willy Tarreau91861262007-10-17 17:06:05 +02002310
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002311 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002312 chunk_appendf(&trash,
2313 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002314 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002315 get_check_status_info(sv->check.status));
2316
2317 if (sv->check.status >= HCHK_STATUS_L57DATA)
2318 chunk_appendf(&trash, "/%d", sv->check.code);
2319
2320 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002321 chunk_appendf(&trash, " in %lums", sv->check.duration);
2322
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002323 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002324 get_check_status_description(sv->check.status));
2325 if (*sv->check.desc) {
2326 chunk_appendf(&trash, ": ");
2327 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2328 chunk_htmlencode(&trash, &src);
2329 }
2330 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002331 }
2332 else
2333 chunk_appendf(&trash, "</td><td>");
2334
2335 chunk_appendf(&trash,
2336 /* weight */
2337 "</td><td class=ac>%d</td>"
2338 /* act, bck */
2339 "<td class=ac>%s</td><td class=ac>%s</td>"
2340 "",
2341 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2342 (sv->state & SRV_BACKUP) ? "-" : "Y",
2343 (sv->state & SRV_BACKUP) ? "Y" : "-");
2344
2345 /* check failures: unique, fatal, down time */
2346 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002347 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002348
2349 if (ref->observe)
2350 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2351
2352 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002353 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002354 "<td>%lld</td><td>%s</td>"
2355 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002356 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002357 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2358 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002359 else if (sv != ref) {
2360 if (sv->state & SRV_MAINTAIN)
2361 chunk_appendf(&trash,
2362 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2363 ref->proxy->id, ref->id);
2364 else
2365 chunk_appendf(&trash,
2366 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2367 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2368 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002369 else
2370 chunk_appendf(&trash, "<td colspan=3></td>");
2371
2372 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002373 if (sv->state & SRV_WARMINGUP)
2374 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375 else
2376 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2377 }
2378 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002379 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002380 "DOWN,",
2381 "DOWN %d/%d,",
2382 "UP %d/%d,",
2383 "UP,",
2384 "NOLB %d/%d,",
2385 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002386 "DRAIN %d/%d,",
2387 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002388 "no check,"
2389 };
2390
2391 chunk_appendf(&trash,
2392 /* pxid, name */
2393 "%s,%s,"
2394 /* queue : current, max */
2395 "%d,%d,"
2396 /* sessions : current, max, limit, total */
2397 "%d,%d,%s,%lld,"
2398 /* bytes : in, out */
2399 "%lld,%lld,"
2400 /* denied: req, resp */
2401 ",%lld,"
2402 /* errors : request, connect, response */
2403 ",%lld,%lld,"
2404 /* warnings: retries, redispatches */
2405 "%lld,%lld,"
2406 "",
2407 px->id, sv->id,
2408 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002409 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002410 sv->counters.bytes_in, sv->counters.bytes_out,
2411 sv->counters.failed_secu,
2412 sv->counters.failed_conns, sv->counters.failed_resp,
2413 sv->counters.retries, sv->counters.redispatches);
2414
2415 /* status */
2416 if (sv->state & SRV_MAINTAIN)
2417 chunk_appendf(&trash, "MAINT,");
2418 else if (ref != sv && ref->state & SRV_MAINTAIN)
2419 chunk_appendf(&trash, "MAINT(via),");
2420 else
2421 chunk_appendf(&trash,
2422 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002423 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2424 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002425
2426 chunk_appendf(&trash,
2427 /* weight, active, backup */
2428 "%d,%d,%d,"
2429 "",
2430 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2431 (sv->state & SRV_BACKUP) ? 0 : 1,
2432 (sv->state & SRV_BACKUP) ? 1 : 0);
2433
2434 /* check failures: unique, fatal; last change, total downtime */
2435 if (sv->state & SRV_CHECKED)
2436 chunk_appendf(&trash,
2437 "%lld,%lld,%d,%d,",
2438 sv->counters.failed_checks, sv->counters.down_trans,
2439 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2440 else
2441 chunk_appendf(&trash, ",,,,");
2442
2443 /* queue limit, pid, iid, sid, */
2444 chunk_appendf(&trash,
2445 "%s,"
2446 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002447 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002448 relative_pid, px->uuid, sv->puid);
2449
2450 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002451 if (sv->state & SRV_WARMINGUP)
2452 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002453
2454 /* sessions: lbtot */
2455 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2456
2457 /* tracked */
2458 if (sv->track)
2459 chunk_appendf(&trash, "%s/%s,",
2460 sv->track->proxy->id, sv->track->id);
2461 else
2462 chunk_appendf(&trash, ",");
2463
2464 /* type */
2465 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2466
2467 /* rate */
2468 chunk_appendf(&trash, "%u,,%u,",
2469 read_freq_ctr(&sv->sess_per_sec),
2470 sv->counters.sps_max);
2471
2472 if (sv->state & SRV_CHECKED) {
2473 /* check_status */
2474 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2475
2476 /* check_code */
2477 if (sv->check.status >= HCHK_STATUS_L57DATA)
2478 chunk_appendf(&trash, "%u,", sv->check.code);
2479 else
2480 chunk_appendf(&trash, ",");
2481
2482 /* check_duration */
2483 if (sv->check.status >= HCHK_STATUS_CHECKED)
2484 chunk_appendf(&trash, "%lu,", sv->check.duration);
2485 else
2486 chunk_appendf(&trash, ",");
2487
2488 }
2489 else
2490 chunk_appendf(&trash, ",,,");
2491
2492 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2493 if (px->mode == PR_MODE_HTTP) {
2494 for (i=1; i<6; i++)
2495 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2496
2497 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2498 }
2499 else
2500 chunk_appendf(&trash, ",,,,,,");
2501
2502 /* failed health analyses */
2503 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2504
2505 /* requests : req_rate, req_rate_max, req_tot, */
2506 chunk_appendf(&trash, ",,,");
2507
2508 /* errors: cli_aborts, srv_aborts */
2509 chunk_appendf(&trash, "%lld,%lld,",
2510 sv->counters.cli_aborts, sv->counters.srv_aborts);
2511
2512 /* compression: in, out, bypassed, comp_rsp */
2513 chunk_appendf(&trash, ",,,,");
2514
2515 /* finish with EOL */
2516 chunk_appendf(&trash, "\n");
2517 }
2518 return 1;
2519}
2520
2521/* Dumps a line for backend <px> to the trash for and uses the state from stream
2522 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2523 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2524 */
2525static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2526{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002527 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002528 struct chunk src;
2529 int i;
2530
2531 if (!(px->cap & PR_CAP_BE))
2532 return 0;
2533
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002534 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002535 return 0;
2536
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002537 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002538 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002539 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002540 /* Column sub-heading for Enable or Disable server */
2541 chunk_appendf(&trash, "<td></td>");
2542 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002543 chunk_appendf(&trash,
2544 "<td class=ac>"
2545 /* name */
2546 "%s<a name=\"%s/Backend\"></a>"
2547 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2548 "",
2549 (flags & ST_SHLGNDS)?"<u>":"",
2550 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002551
2552 if (flags & ST_SHLGNDS) {
2553 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002554 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2556
2557 /* cookie */
2558 if (px->cookie_name) {
2559 chunk_appendf(&trash, ", cookie: '");
2560 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2561 chunk_htmlencode(&trash, &src);
2562 chunk_appendf(&trash, "'");
2563 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002564 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002565 }
2566
2567 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002568 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569 /* queue : current, max */
2570 "<td>%s</td><td>%s</td><td></td>"
2571 /* sessions rate : current, max, limit */
2572 "<td>%s</td><td>%s</td><td></td>"
2573 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002574 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002575 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2576 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577
2578 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002579 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002581 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002582 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002584 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2585 U2H(px->be_counters.cum_conn),
2586 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002587
Willy Tarreau466c9b52012-12-23 02:25:03 +01002588 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002590 chunk_appendf(&trash,
2591 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2592 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2593 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2594 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2595 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2596 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2597 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2598 "<tr><th>- other responses:</th><td>%s</td></tr>"
2599 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2600 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002601 U2H(px->be_counters.p.http.cum_req),
2602 U2H(px->be_counters.p.http.rsp[1]),
2603 U2H(px->be_counters.p.http.rsp[2]),
2604 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002605 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002606 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002607 U2H(px->be_counters.p.http.rsp[3]),
2608 U2H(px->be_counters.p.http.rsp[4]),
2609 U2H(px->be_counters.p.http.rsp[5]),
2610 U2H(px->be_counters.p.http.rsp[0]),
2611 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 }
2613
2614 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002615 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002616 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002617 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002618 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002619 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002620 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002621 U2H(px->be_counters.cum_lbconn),
2622 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002625 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002626 "<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 +01002627 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002628 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002629 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2630 px->be_counters.comp_in ?
2631 (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 +01002632 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2633
2634 chunk_appendf(&trash,
2635 /* denied: req, resp */
2636 "<td>%s</td><td>%s</td>"
2637 /* errors : request, connect */
2638 "<td></td><td>%s</td>"
2639 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002640 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002641 /* warnings: retries, redispatches */
2642 "<td>%lld</td><td>%lld</td>"
2643 /* backend status: reflect backend status (up/down): we display UP
2644 * if the backend has known working servers or if it has no server at
2645 * all (eg: for stats). Then we display the total weight, number of
2646 * active and backups. */
2647 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2648 "<td class=ac>%d</td><td class=ac>%d</td>"
2649 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002650 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2651 U2H(px->be_counters.failed_conns),
2652 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002653 px->be_counters.cli_aborts,
2654 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002655 px->be_counters.retries, px->be_counters.redispatches,
2656 human_time(now.tv_sec - px->last_change, 1),
2657 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2658 "<font color=\"red\"><b>DOWN</b></font>",
2659 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2660 px->srv_act, px->srv_bck);
2661
2662 chunk_appendf(&trash,
2663 /* rest of backend: nothing, down transitions, total downtime, throttle */
2664 "<td class=ac>&nbsp;</td><td>%d</td>"
2665 "<td>%s</td>"
2666 "<td></td>"
2667 "</tr>",
2668 px->down_trans,
2669 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2670 }
2671 else { /* CSV mode */
2672 chunk_appendf(&trash,
2673 /* pxid, name */
2674 "%s,BACKEND,"
2675 /* queue : current, max */
2676 "%d,%d,"
2677 /* sessions : current, max, limit, total */
2678 "%d,%d,%d,%lld,"
2679 /* bytes : in, out */
2680 "%lld,%lld,"
2681 /* denied: req, resp */
2682 "%lld,%lld,"
2683 /* errors : request, connect, response */
2684 ",%lld,%lld,"
2685 /* warnings: retries, redispatches */
2686 "%lld,%lld,"
2687 /* backend status: reflect backend status (up/down): we display UP
2688 * if the backend has known working servers or if it has no server at
2689 * all (eg: for stats). Then we display the total weight, number of
2690 * active and backups. */
2691 "%s,"
2692 "%d,%d,%d,"
2693 /* rest of backend: nothing, down transitions, last change, total downtime */
2694 ",%d,%d,%d,,"
2695 /* pid, iid, sid, throttle, lbtot, tracked, type */
2696 "%d,%d,0,,%lld,,%d,"
2697 /* rate, rate_lim, rate_max, */
2698 "%u,,%u,"
2699 /* check_status, check_code, check_duration */
2700 ",,,",
2701 px->id,
2702 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2703 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2704 px->be_counters.bytes_in, px->be_counters.bytes_out,
2705 px->be_counters.denied_req, px->be_counters.denied_resp,
2706 px->be_counters.failed_conns, px->be_counters.failed_resp,
2707 px->be_counters.retries, px->be_counters.redispatches,
2708 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2709 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2710 px->srv_act, px->srv_bck,
2711 px->down_trans, (int)(now.tv_sec - px->last_change),
2712 px->srv?be_downtime(px):0,
2713 relative_pid, px->uuid,
2714 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2715 read_freq_ctr(&px->be_sess_per_sec),
2716 px->be_counters.sps_max);
2717
2718 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2719 if (px->mode == PR_MODE_HTTP) {
2720 for (i=1; i<6; i++)
2721 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2722 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2723 }
2724 else
2725 chunk_appendf(&trash, ",,,,,,");
2726
2727 /* failed health analyses */
2728 chunk_appendf(&trash, ",");
2729
2730 /* requests : req_rate, req_rate_max, req_tot, */
2731 chunk_appendf(&trash, ",,,");
2732
2733 /* errors: cli_aborts, srv_aborts */
2734 chunk_appendf(&trash, "%lld,%lld,",
2735 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2736
2737 /* compression: in, out, bypassed */
2738 chunk_appendf(&trash, "%lld,%lld,%lld,",
2739 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2740
2741 /* compression: comp_rsp */
2742 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2743
2744 /* finish with EOL */
2745 chunk_appendf(&trash, "\n");
2746 }
2747 return 1;
2748}
2749
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002750/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002751 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002752 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002754static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002756 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002757 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2758
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002759 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002760 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002761
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002762 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002763 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002764 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002765 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002766 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
2767 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002768 }
2769
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002770 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002771 "<form action=\"%s%s%s%s\" method=\"post\">",
2772 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002773 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2774 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002775 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 }
2777
2778 /* print a new table */
2779 chunk_appendf(&trash,
2780 "<table class=\"tbl\" width=\"100%%\">\n"
2781 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002782 "<th class=\"pxname\" width=\"10%%\">");
2783
2784 chunk_appendf(&trash,
2785 "<a name=\"%s\"></a>%s"
2786 "<a class=px href=\"#%s\">%s</a>",
2787 px->id,
2788 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2789 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790
2791 if (uri->flags & ST_SHLGNDS) {
2792 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002793 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2795 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002796 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797 }
2798
2799 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002800 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801 "<th class=\"%s\" width=\"90%%\">%s</th>"
2802 "</tr>\n"
2803 "</table>\n"
2804 "<table class=\"tbl\" width=\"100%%\">\n"
2805 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002806 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2807 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2808
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002809 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810 /* Column heading for Enable or Disable server */
2811 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002812 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002813
2814 chunk_appendf(&trash,
2815 "<th rowspan=2></th>"
2816 "<th colspan=3>Queue</th>"
2817 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2818 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2819 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2820 "<th colspan=9>Server</th>"
2821 "</tr>\n"
2822 "<tr class=\"titre\">"
2823 "<th>Cur</th><th>Max</th><th>Limit</th>"
2824 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2825 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2826 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2827 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2828 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2829 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2830 "<th>Thrtle</th>\n"
2831 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002832}
2833
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002834/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002835 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002836 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002837static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002838{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002839 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002840 chunk_appendf(&trash, "</table>");
2841
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002842 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843 /* close the form used to enable/disable this proxy servers */
2844 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002845 "Choose the action to perform on the checked servers : "
2846 "<select name=action>"
2847 "<option value=\"\"></option>"
2848 "<option value=\"disable\">Disable</option>"
2849 "<option value=\"enable\">Enable</option>"
2850 "<option value=\"stop\">Soft Stop</option>"
2851 "<option value=\"start\">Soft Start</option>"
2852 "<option value=\"shutdown\">Kill Sessions</option>"
2853 "</select>"
2854 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2855 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2856 "</form>",
2857 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858 }
2859
2860 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002861}
Willy Tarreau91861262007-10-17 17:06:05 +02002862
2863/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002864 * Dumps statistics for a proxy. The output is sent to the stream interface's
2865 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2866 * buffer space, or non-zero if everything completed. This function is used
2867 * both by the CLI and the HTTP entry points, and is able to dump the output
2868 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002869 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002870static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002871{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002872 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02002873 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002874 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002875 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002876 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002877
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002878 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002879
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002880 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002881 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002882 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002883 if (uri && uri->scope) {
2884 /* we have a limited scope, we have to check the proxy name */
2885 struct stat_scope *scope;
2886 int len;
2887
2888 len = strlen(px->id);
2889 scope = uri->scope;
2890
2891 while (scope) {
2892 /* match exact proxy name */
2893 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2894 break;
2895
2896 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002897 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002898 break;
2899 scope = scope->next;
2900 }
2901
2902 /* proxy name not found : don't dump anything */
2903 if (scope == NULL)
2904 return 1;
2905 }
2906
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002907 /* if the user has requested a limited output and the proxy
2908 * name does not match, skip it.
2909 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002910 if (appctx->ctx.stats.scope_len &&
2911 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002912 return 1;
2913
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002914 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2915 (appctx->ctx.stats.iid != -1) &&
2916 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002917 return 1;
2918
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002919 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002920 /* fall through */
2921
Willy Tarreau295a8372011-03-10 11:25:07 +01002922 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002923 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002924 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002925 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002926 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002927 }
Willy Tarreau91861262007-10-17 17:06:05 +02002928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002929 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002930 /* fall through */
2931
Willy Tarreau295a8372011-03-10 11:25:07 +01002932 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002933 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002934 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002935 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002936 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002937
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938 appctx->ctx.stats.l = px->conf.listeners.n;
2939 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002940 /* fall through */
2941
Willy Tarreau295a8372011-03-10 11:25:07 +01002942 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002943 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002945 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002946 return 0;
2947
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002948 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002949 if (!l->counters)
2950 continue;
2951
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002952 if (appctx->ctx.stats.flags & STAT_BOUND) {
2953 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002954 break;
2955
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002956 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002957 continue;
2958 }
2959
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002960 /* print the frontend */
2961 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2962 if (bi_putchk(rep, &trash) == -1)
2963 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002964 }
2965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002966 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2967 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002968 /* fall through */
2969
Willy Tarreau295a8372011-03-10 11:25:07 +01002970 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002971 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002972 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002973 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 +02002974
Willy Tarreau9b28e032012-10-12 23:49:43 +02002975 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002976 return 0;
2977
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002978 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002979
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002980 if (appctx->ctx.stats.flags & STAT_BOUND) {
2981 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002982 break;
2983
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002984 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002985 continue;
2986 }
2987
Willy Tarreau44267702011-10-28 15:35:33 +02002988 if (sv->track)
2989 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002990 else
2991 svs = sv;
2992
Willy Tarreau91861262007-10-17 17:06:05 +02002993 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002994 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09002995 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002996 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09002997 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002998 sv_state = 3; /* UP */
2999 else
3000 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003001
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003002 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003003 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003004 else if (svs->state & SRV_GOINGDOWN)
3005 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003006 }
Willy Tarreau91861262007-10-17 17:06:05 +02003007 else
Simon Horman125d0992013-02-24 17:23:38 +09003008 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003009 sv_state = 1; /* going up */
3010 else
3011 sv_state = 0; /* DOWN */
3012
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003013 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003014 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003015 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003016 continue;
3017 }
3018
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003019 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3020 if (bi_putchk(rep, &trash) == -1)
3021 return 0;
3022 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003023
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003024 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003026
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027 case STAT_PX_ST_BE:
3028 /* print the backend */
3029 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3030 if (bi_putchk(rep, &trash) == -1)
3031 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003032
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003033 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003034 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003035
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003036 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003037 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003038 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003039 if (bi_putchk(rep, &trash) == -1)
3040 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003041 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003042
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003043 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003044 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003045
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003046 case STAT_PX_ST_FIN:
3047 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003048
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003049 default:
3050 /* unknown state, we should put an abort() here ! */
3051 return 1;
3052 }
3053}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003054
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003055/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3056 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003057 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003058static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003059{
3060 /* WARNING! This must fit in the first buffer !!! */
3061 chunk_appendf(&trash,
3062 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3063 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3064 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3065 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3066 "<style type=\"text/css\"><!--\n"
3067 "body {"
3068 " font-family: arial, helvetica, sans-serif;"
3069 " font-size: 12px;"
3070 " font-weight: normal;"
3071 " color: black;"
3072 " background: white;"
3073 "}\n"
3074 "th,td {"
3075 " font-size: 10px;"
3076 "}\n"
3077 "h1 {"
3078 " font-size: x-large;"
3079 " margin-bottom: 0.5em;"
3080 "}\n"
3081 "h2 {"
3082 " font-family: helvetica, arial;"
3083 " font-size: x-large;"
3084 " font-weight: bold;"
3085 " font-style: italic;"
3086 " color: #6020a0;"
3087 " margin-top: 0em;"
3088 " margin-bottom: 0em;"
3089 "}\n"
3090 "h3 {"
3091 " font-family: helvetica, arial;"
3092 " font-size: 16px;"
3093 " font-weight: bold;"
3094 " color: #b00040;"
3095 " background: #e8e8d0;"
3096 " margin-top: 0em;"
3097 " margin-bottom: 0em;"
3098 "}\n"
3099 "li {"
3100 " margin-top: 0.25em;"
3101 " margin-right: 2em;"
3102 "}\n"
3103 ".hr {margin-top: 0.25em;"
3104 " border-color: black;"
3105 " border-bottom-style: solid;"
3106 "}\n"
3107 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3108 ".total {background: #20D0D0;color: #ffff80;}\n"
3109 ".frontend {background: #e8e8d0;}\n"
3110 ".socket {background: #d0d0d0;}\n"
3111 ".backend {background: #e8e8d0;}\n"
3112 ".active0 {background: #ff9090;}\n"
3113 ".active1 {background: #ffd020;}\n"
3114 ".active2 {background: #ffffa0;}\n"
3115 ".active3 {background: #c0ffc0;}\n"
3116 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003117 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3118 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3119 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003120 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003121 ".backup0 {background: #ff9090;}\n"
3122 ".backup1 {background: #ff80ff;}\n"
3123 ".backup2 {background: #c060ff;}\n"
3124 ".backup3 {background: #b0d0ff;}\n"
3125 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3126 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003127 ".backup6 {background: #c060ff;}\n"
3128 ".backup7 {background: #cc9900;}\n"
3129 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 ".maintain {background: #c07820;}\n"
3131 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3132 "\n"
3133 "a.px:link {color: #ffff40; text-decoration: none;}"
3134 "a.px:visited {color: #ffff40; text-decoration: none;}"
3135 "a.px:hover {color: #ffffff; text-decoration: none;}"
3136 "a.lfsb:link {color: #000000; text-decoration: none;}"
3137 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3138 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3139 "\n"
3140 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3141 "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"
3142 "table.tbl td.ac { text-align: center;}\n"
3143 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3144 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3145 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3146 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3147 "\n"
3148 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3149 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3150 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003151 "table.det { border-collapse: collapse; border-style: none; }\n"
3152 "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"
3153 "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 +01003154 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003155 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003156 " display:block;\n"
3157 " visibility:hidden;\n"
3158 " z-index:2147483647;\n"
3159 " position:absolute;\n"
3160 " padding:2px 4px 3px;\n"
3161 " background:#f0f060; color:#000000;\n"
3162 " border:1px solid #7040c0;\n"
3163 " white-space:nowrap;\n"
3164 " font-style:normal;font-size:11px;font-weight:normal;\n"
3165 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3166 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3167 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003168 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003169 "-->\n"
3170 "</style></head>\n",
3171 (uri->flags & ST_SHNODE) ? " on " : "",
3172 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3173 );
3174}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003175
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003176/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003177 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003178 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003180static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003181{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003182 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003183 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003184 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003185
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003186 /* WARNING! this has to fit the first packet too.
3187 * We are around 3.5 kB, add adding entries will
3188 * become tricky if we want to support 4kB buffers !
3189 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003190 chunk_appendf(&trash,
3191 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3192 PRODUCT_NAME "%s</a></h1>\n"
3193 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3194 "<hr width=\"100%%\" class=\"hr\">\n"
3195 "<h3>&gt; General process information</h3>\n"
3196 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3197 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3198 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3199 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3200 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3201 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3202 "Running tasks: %d/%d; idle = %d %%<br>\n"
3203 "</td><td align=\"center\" nowrap>\n"
3204 "<table class=\"lgd\"><tr>\n"
3205 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3206 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3207 "</tr><tr>\n"
3208 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3209 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3210 "</tr><tr>\n"
3211 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3212 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3213 "</tr><tr>\n"
3214 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003215 "</tr><tr>\n"
3216 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 "</tr><tr>\n"
3218 "<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 -07003219 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003220 "<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 +01003221 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003222 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 "</td>"
3224 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3225 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3226 "",
3227 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3228 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3229 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3230 (uri->flags & ST_SHDESC) ? ": " : "",
3231 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3232 pid, relative_pid, global.nbproc,
3233 up / 86400, (up % 86400) / 3600,
3234 (up % 3600) / 60, (up % 60),
3235 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3236 global.rlimit_memmax ? " MB" : "",
3237 global.rlimit_nofile,
3238 global.maxsock, global.maxconn, global.maxpipes,
3239 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3240 run_queue_cur, nb_tasks_cur, idle_pct
3241 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003242
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003243 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3244 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3245 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003246
3247 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003248 "<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 +02003249 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003250 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3251 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3252 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003253 STAT_SCOPE_TXT_MAXLEN);
3254
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003256 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003257 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003258 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003259 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3260 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003261 }
3262
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003263 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003264 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003265 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003266 uri->uri_prefix,
3267 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003268 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003269 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 else
3271 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003272 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003273 uri->uri_prefix,
3274 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003276 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003277
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003279 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
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\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003283 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003284 "",
3285 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003286 else
3287 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003288 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003289 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003290 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003291 ";norefresh",
3292 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003293 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003294
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003295 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003296 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003297 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3299 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003300 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003301
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003302 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003303 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003304 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003305 (uri->refresh > 0) ? ";norefresh" : "",
3306 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003307
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308 chunk_appendf(&trash,
3309 "</ul></td>"
3310 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3311 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3312 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3313 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3314 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3315 "</ul>"
3316 "</td>"
3317 "</tr></table>\n"
3318 ""
3319 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003320
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003321 if (appctx->ctx.stats.st_code) {
3322 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 case STAT_STATUS_DONE:
3324 chunk_appendf(&trash,
3325 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003326 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003328 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003329 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3330 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003331 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 break;
3333 case STAT_STATUS_NONE:
3334 chunk_appendf(&trash,
3335 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003336 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003338 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003339 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3340 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003341 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003342 break;
3343 case STAT_STATUS_PART:
3344 chunk_appendf(&trash,
3345 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003346 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 "Action partially processed.<br>"
3348 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003349 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003350 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3351 (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_ERRP:
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 "Action not processed because of invalid parameters."
3359 "<ul>"
3360 "<li>The action is maybe unknown.</li>"
3361 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3362 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3363 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003364 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003365 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3366 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003367 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368 break;
3369 case STAT_STATUS_EXCD:
3370 chunk_appendf(&trash,
3371 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003372 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3374 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003375 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003376 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3377 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003378 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 break;
3380 case STAT_STATUS_DENY:
3381 chunk_appendf(&trash,
3382 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003383 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003385 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003386 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3387 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003388 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 break;
3390 default:
3391 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003392 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003393 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003394 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003395 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003396 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3397 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003398 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 }
3400 chunk_appendf(&trash, "<p>\n");
3401 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003403
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003404/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3405 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003406 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003407static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003411
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003412/* This function dumps statistics onto the stream interface's read buffer in
3413 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003414 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3415 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3416 * and the session must be closed, or -1 in case of any error. This function is
3417 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003418 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003419static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003421 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 struct channel *rep = si->ib;
3423 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003426
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003427 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003429 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003431
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003433 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003434 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003435 else
3436 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 if (bi_putchk(rep, &trash) == -1)
3439 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003440
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003441 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003442 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003443
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003445 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003446 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003447 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003448 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003449 }
Willy Tarreau91861262007-10-17 17:06:05 +02003450
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003451 appctx->ctx.stats.px = proxy;
3452 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3453 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003454 /* fall through */
3455
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 case STAT_ST_LIST:
3457 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003458 while (appctx->ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 if (buffer_almost_full(rep->buf))
3460 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003461
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003462 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 /* skip the disabled proxies, global frontend and non-networked ones */
3464 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003465 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003466 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003467
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003468 appctx->ctx.stats.px = px->next;
3469 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 }
3471 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003472
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003473 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003474 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003477 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003478 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003479 if (bi_putchk(rep, &trash) == -1)
3480 return 0;
3481 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003482
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003483 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003484 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003485
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003486 case STAT_ST_FIN:
3487 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003488
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003489 default:
3490 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003491 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 return -1;
3493 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494}
Willy Tarreauae526782010-03-04 20:34:23 +01003495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003496/* We reached the stats page through a POST request. The appctx is
3497 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01003498 * Parse the posted data and enable/disable servers if necessary.
3499 * Returns 1 if request was parsed or zero if it needs more data.
3500 */
3501static int stats_process_http_post(struct stream_interface *si)
3502{
3503 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003504 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003505
3506 struct proxy *px = NULL;
3507 struct server *sv = NULL;
3508
3509 char key[LINESIZE];
3510 int action = ST_ADM_ACTION_NONE;
3511 int reprocess = 0;
3512
3513 int total_servers = 0;
3514 int altered_servers = 0;
3515
3516 char *first_param, *cur_param, *next_param, *end_params;
3517 char *st_cur_param = NULL;
3518 char *st_next_param = NULL;
3519
3520 struct chunk *temp;
3521 int reql;
3522
3523 temp = get_trash_chunk();
3524 if (temp->size < s->txn.req.body_len) {
3525 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003526 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003527 goto out;
3528 }
3529
3530 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3531 if (reql <= 0) {
3532 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003533 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003534 return 0;
3535 }
3536
3537 first_param = temp->str;
3538 end_params = temp->str + reql;
3539 cur_param = next_param = end_params;
3540 *end_params = '\0';
3541
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003542 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003543
3544 /*
3545 * Parse the parameters in reverse order to only store the last value.
3546 * From the html form, the backend and the action are at the end.
3547 */
3548 while (cur_param > first_param) {
3549 char *value;
3550 int poffset, plen;
3551
3552 cur_param--;
3553
3554 if ((*cur_param == '&') || (cur_param == first_param)) {
3555 reprocess_servers:
3556 /* Parse the key */
3557 poffset = (cur_param != first_param ? 1 : 0);
3558 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3559 if ((plen > 0) && (plen <= sizeof(key))) {
3560 strncpy(key, cur_param + poffset, plen);
3561 key[plen - 1] = '\0';
3562 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003563 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003564 goto out;
3565 }
3566
3567 /* Parse the value */
3568 value = key;
3569 while (*value != '\0' && *value != '=') {
3570 value++;
3571 }
3572 if (*value == '=') {
3573 /* Ok, a value is found, we can mark the end of the key */
3574 *value++ = '\0';
3575 }
3576 if (url_decode(key) < 0 || url_decode(value) < 0)
3577 break;
3578
3579 /* Now we can check the key to see what to do */
3580 if (!px && (strcmp(key, "b") == 0)) {
3581 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
3582 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003583 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003584 goto out;
3585 }
3586 }
3587 else if (!action && (strcmp(key, "action") == 0)) {
3588 if (strcmp(value, "disable") == 0) {
3589 action = ST_ADM_ACTION_DISABLE;
3590 }
3591 else if (strcmp(value, "enable") == 0) {
3592 action = ST_ADM_ACTION_ENABLE;
3593 }
3594 else if (strcmp(value, "stop") == 0) {
3595 action = ST_ADM_ACTION_STOP;
3596 }
3597 else if (strcmp(value, "start") == 0) {
3598 action = ST_ADM_ACTION_START;
3599 }
3600 else if (strcmp(value, "shutdown") == 0) {
3601 action = ST_ADM_ACTION_SHUTDOWN;
3602 }
3603 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003604 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003605 goto out;
3606 }
3607 }
3608 else if (strcmp(key, "s") == 0) {
3609 if (!(px && action)) {
3610 /*
3611 * Indicates that we'll need to reprocess the parameters
3612 * as soon as backend and action are known
3613 */
3614 if (!reprocess) {
3615 st_cur_param = cur_param;
3616 st_next_param = next_param;
3617 }
3618 reprocess = 1;
3619 }
3620 else if ((sv = findserver(px, value)) != NULL) {
3621 switch (action) {
3622 case ST_ADM_ACTION_DISABLE:
3623 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
3624 /* Not already in maintenance, we can change the server state */
3625 sv->state |= SRV_MAINTAIN;
3626 set_server_down(&sv->check);
3627 altered_servers++;
3628 total_servers++;
3629 }
3630 break;
3631 case ST_ADM_ACTION_ENABLE:
3632 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
3633 /* Already in maintenance, we can change the server state */
3634 set_server_up(&sv->check);
3635 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
3636 altered_servers++;
3637 total_servers++;
3638 }
3639 break;
3640 case ST_ADM_ACTION_STOP:
3641 case ST_ADM_ACTION_START:
3642 if (action == ST_ADM_ACTION_START)
3643 sv->uweight = sv->iweight;
3644 else
3645 sv->uweight = 0;
3646
3647 server_recalc_eweight(sv);
3648 set_server_drain_state(sv);
3649
3650 altered_servers++;
3651 total_servers++;
3652 break;
3653 case ST_ADM_ACTION_SHUTDOWN:
3654 if (px->state != PR_STSTOPPED) {
3655 struct session *sess, *sess_bck;
3656
3657 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3658 if (sess->srv_conn == sv)
3659 session_shutdown(sess, SN_ERR_KILLED);
3660
3661 altered_servers++;
3662 total_servers++;
3663 }
3664 break;
3665 }
3666 } else {
3667 /* the server name is unknown or ambiguous (duplicate names) */
3668 total_servers++;
3669 }
3670 }
3671 if (reprocess && px && action) {
3672 /* Now, we know the backend and the action chosen by the user.
3673 * We can safely restart from the first server parameter
3674 * to reprocess them
3675 */
3676 cur_param = st_cur_param;
3677 next_param = st_next_param;
3678 reprocess = 0;
3679 goto reprocess_servers;
3680 }
3681
3682 next_param = cur_param;
3683 }
3684 }
3685
3686 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003687 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003688 }
3689 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003690 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003691 }
3692 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003693 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003694 }
3695 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003696 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003697 }
3698 out:
3699 return 1;
3700}
3701
3702
3703static int stats_send_http_headers(struct stream_interface *si)
3704{
3705 struct session *s = session_from_task(si->owner);
3706 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003707 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003708
3709 chunk_printf(&trash,
3710 "HTTP/1.0 200 OK\r\n"
3711 "Cache-Control: no-cache\r\n"
3712 "Connection: close\r\n"
3713 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003714 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01003715
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003716 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01003717 chunk_appendf(&trash, "Refresh: %d\r\n",
3718 uri->refresh);
3719
3720 chunk_appendf(&trash, "\r\n");
3721
3722 s->txn.status = 200;
3723 s->logs.tv_request = now;
3724
3725 if (bi_putchk(si->ib, &trash) == -1)
3726 return 0;
3727
3728 return 1;
3729}
3730
3731static int stats_send_http_redirect(struct stream_interface *si)
3732{
3733 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3734 struct session *s = session_from_task(si->owner);
3735 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003736 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003737
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003738 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003739 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003740 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003741 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003742 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3743 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003744 }
3745
3746 /* We don't want to land on the posted stats page because a refresh will
3747 * repost the data. We don't want this to happen on accident so we redirect
3748 * the browse to the stats page with a GET.
3749 */
3750 chunk_printf(&trash,
3751 "HTTP/1.1 303 See Other\r\n"
3752 "Cache-Control: no-cache\r\n"
3753 "Content-Type: text/plain\r\n"
3754 "Connection: close\r\n"
3755 "Location: %s;st=%s%s%s%s\r\n"
3756 "\r\n",
3757 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003758 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3759 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3760 stat_status_codes[appctx->ctx.stats.st_code]) ?
3761 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01003762 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003763 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3764 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01003765 scope_txt);
3766
3767 s->txn.status = 303;
3768 s->logs.tv_request = now;
3769
3770 if (bi_putchk(si->ib, &trash) == -1)
3771 return 0;
3772
3773 return 1;
3774}
3775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003776/* This I/O handler runs as an applet embedded in a stream interface. It is
3777 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003778 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003779 * automatically unregisters itself once transfer is complete.
3780 */
3781static void http_stats_io_handler(struct stream_interface *si)
3782{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003783 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003784 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 struct channel *req = si->ob;
3786 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003787
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003788 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3789 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003790
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003791 /* check that the output is not closed */
3792 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003793 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003794
Willy Tarreau347a35d2013-11-22 17:51:09 +01003795 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003796 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003797 if (stats_send_http_headers(si)) {
3798 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003799 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003800 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003801 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003802 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003803 }
3804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003805 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003806 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003807 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003809
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003810 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003811 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003812 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003813 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003814 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003815 }
3816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003817 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01003818 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003819 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003820 }
3821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003822 if (appctx->st0 == STAT_HTTP_DONE)
Willy Tarreau347a35d2013-11-22 17:51:09 +01003823 si_shutw(si);
3824
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003825 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3826 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003827
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003828 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01003829 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3830 si_shutr(si);
3831 res->flags |= CF_READ_NULL;
3832 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833 }
Willy Tarreau91861262007-10-17 17:06:05 +02003834
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003835 /* update all other flags and resync with the other side */
3836 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003837
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838 /* we don't want to expire timeouts while we're processing requests */
3839 si->ib->rex = TICK_ETERNITY;
3840 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003841
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842 out:
3843 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3844 /* check that we have released everything then unregister */
3845 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003846 }
3847}
3848
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849
Willy Tarreau909d5172012-11-26 03:04:41 +01003850static inline const char *get_conn_ctrl_name(const struct connection *conn)
3851{
Willy Tarreauf79c8172013-10-21 16:30:56 +02003852 if (!(conn->flags & CO_FL_CTRL_READY) || !conn->ctrl)
Willy Tarreau909d5172012-11-26 03:04:41 +01003853 return "NONE";
3854 return conn->ctrl->name;
3855}
3856
3857static inline const char *get_conn_xprt_name(const struct connection *conn)
3858{
3859 static char ptr[17];
3860
Willy Tarreauf79c8172013-10-21 16:30:56 +02003861 if (!(conn->flags & CO_FL_XPRT_READY) || !conn->xprt)
Willy Tarreau909d5172012-11-26 03:04:41 +01003862 return "NONE";
3863
3864 if (conn->xprt == &raw_sock)
3865 return "RAW";
3866
3867#ifdef USE_OPENSSL
3868 if (conn->xprt == &ssl_sock)
3869 return "SSL";
3870#endif
3871 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3872 return ptr;
3873}
3874
3875static inline const char *get_conn_data_name(const struct connection *conn)
3876{
3877 static char ptr[17];
3878
3879 if (!conn->data)
3880 return "NONE";
3881
3882 if (conn->data == &sess_conn_cb)
3883 return "SESS";
3884
3885 if (conn->data == &si_conn_cb)
3886 return "STRM";
3887
3888 if (conn->data == &check_conn_cb)
3889 return "CHCK";
3890
3891 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3892 return ptr;
3893}
3894
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003895/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003896 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003897 * 0 if the output buffer is full and it needs to be called again, otherwise
3898 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003899 */
Willy Tarreau76153662012-11-26 01:16:39 +01003900static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003901{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003902 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003903 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003904 extern const char *monthname[12];
3905 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003906 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01003907 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003908
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003909 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003910
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003911 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003912 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003913 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3914 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003915 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003916 appctx->ctx.sess.uid = 0;
3917 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003918 return 1;
3919 }
3920
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003921 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003922 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003923 appctx->ctx.sess.uid = sess->uniq_id;
3924 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003925 /* fall through */
3926
3927 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003928 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003929 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003930 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003931 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003932 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3933 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003934 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003935 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003936
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003937 conn = objt_conn(sess->si[0].end);
3938 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003939 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003940 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003941 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003942 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003943 break;
3944 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003945 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003946 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003947 default:
3948 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003949 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003950 break;
3951 }
3952
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003954 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003955 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003956
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003957 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003958 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003959 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3960 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3961 sess->listener ? sess->listener->luid : 0);
3962
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003963 if (conn)
3964 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003965
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003966 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003967 case AF_INET:
3968 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003969 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003970 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003971 break;
3972 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003973 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003974 break;
3975 default:
3976 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003977 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003978 break;
3979 }
3980
Willy Tarreau50943332011-09-02 17:33:05 +02003981 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003982 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003983 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003984 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003985 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3986 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003987 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003988
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003989 conn = objt_conn(sess->si[1].end);
3990 if (conn)
3991 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02003992
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003993 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003994 case AF_INET:
3995 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003996 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003997 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003998 break;
3999 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004000 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004001 break;
4002 default:
4003 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004004 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004005 break;
4006 }
4007
4008 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004009 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004010 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004011 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4012 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004013 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004014 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004015
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004016 if (conn)
4017 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004018
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004019 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004020 case AF_INET:
4021 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004022 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004023 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004024 break;
4025 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004026 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004027 break;
4028 default:
4029 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004030 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004031 break;
4032 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004033
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004034 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004035 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004036 sess->task,
4037 sess->task->state,
4038 sess->task->nice, sess->task->calls,
4039 sess->task->expire ?
4040 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4041 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4042 TICKS_TO_MS(1000)) : "<NEVER>",
4043 task_in_rq(sess->task) ? ", running" : "");
4044
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004045 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004046 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004047 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4048
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004049 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004050 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4051 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4052 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4053
4054 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004055 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004056 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004057 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004058 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004059 obj_type_name(sess->si[0].end),
4060 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004061 sess->si[0].exp ?
4062 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4063 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4064 TICKS_TO_MS(1000)) : "<NEVER>",
4065 sess->si[0].err_type);
4066
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004067 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004068 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004069 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004070 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004071 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004072 obj_type_name(sess->si[1].end),
4073 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004074 sess->si[1].exp ?
4075 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4076 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4077 TICKS_TO_MS(1000)) : "<NEVER>",
4078 sess->si[1].err_type);
4079
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004080 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004081 chunk_appendf(&trash,
4082 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004083 conn,
4084 get_conn_ctrl_name(conn),
4085 get_conn_xprt_name(conn),
4086 get_conn_data_name(conn),
4087 obj_type_name(conn->target),
4088 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004089
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004090 chunk_appendf(&trash,
4091 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004092 conn->flags,
4093 conn->t.sock.fd,
4094 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4095 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4096 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004097 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004098 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4099 chunk_appendf(&trash,
4100 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4101 tmpctx,
4102 tmpctx->st0,
4103 tmpctx->st1,
4104 tmpctx->st2,
4105 tmpctx->applet->name);
4106 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004107
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004108 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004109 chunk_appendf(&trash,
4110 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004111 conn,
4112 get_conn_ctrl_name(conn),
4113 get_conn_xprt_name(conn),
4114 get_conn_data_name(conn),
4115 obj_type_name(conn->target),
4116 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004117
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004118 chunk_appendf(&trash,
4119 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004120 conn->flags,
4121 conn->t.sock.fd,
4122 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4123 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4124 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004125 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004126 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4127 chunk_appendf(&trash,
4128 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4129 tmpctx,
4130 tmpctx->st0,
4131 tmpctx->st1,
4132 tmpctx->st2,
4133 tmpctx->applet->name);
4134 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004135
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004136 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004137 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004138 " an_exp=%s",
4139 sess->req,
4140 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004141 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004142 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004143 sess->req->analyse_exp ?
4144 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4145 TICKS_TO_MS(1000)) : "<NEVER>");
4146
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004147 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004148 " rex=%s",
4149 sess->req->rex ?
4150 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4151 TICKS_TO_MS(1000)) : "<NEVER>");
4152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004153 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004154 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004155 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004156 sess->req->wex ?
4157 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4158 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004159 sess->req->buf,
4160 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004161 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004162 sess->txn.req.next, sess->req->buf->i,
4163 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004164
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004165 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004166 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004167 " an_exp=%s",
4168 sess->rep,
4169 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004170 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004171 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004172 sess->rep->analyse_exp ?
4173 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4174 TICKS_TO_MS(1000)) : "<NEVER>");
4175
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004176 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004177 " rex=%s",
4178 sess->rep->rex ?
4179 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4180 TICKS_TO_MS(1000)) : "<NEVER>");
4181
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004182 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004183 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004184 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004185 sess->rep->wex ?
4186 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4187 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004188 sess->rep->buf,
4189 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004190 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004191 sess->txn.rsp.next, sess->rep->buf->i,
4192 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004193
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004194 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004195 return 0;
4196
4197 /* use other states to dump the contents */
4198 }
4199 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004200 appctx->ctx.sess.uid = 0;
4201 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004202 return 1;
4203}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004204
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004205/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004206 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004207 * to be called again, otherwise non-zero. It is designed to be called
4208 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004209 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004210static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004211{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004212 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004213 struct connection *conn;
4214
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004215 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004216 /* If we're forced to shut down, we might have to remove our
4217 * reference to the last session being dumped.
4218 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004219 if (appctx->st2 == STAT_ST_LIST) {
4220 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4221 LIST_DEL(&appctx->ctx.sess.bref.users);
4222 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004223 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004224 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004225 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004226 }
4227
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004228 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004229
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004231 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004232 /* the function had not been called yet, let's prepare the
4233 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004234 * pointer to the first in the global list. When a target
4235 * session is being destroyed, it is responsible for updating
4236 * this pointer. We know we have reached the end when this
4237 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004238 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004239 LIST_INIT(&appctx->ctx.sess.bref.users);
4240 appctx->ctx.sess.bref.ref = sessions.n;
4241 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004242 /* fall through */
4243
Willy Tarreau295a8372011-03-10 11:25:07 +01004244 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004245 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004246 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4247 LIST_DEL(&appctx->ctx.sess.bref.users);
4248 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004249 }
4250
4251 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004252 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004253 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004254 struct session *curr_sess;
4255
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004256 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004257
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004258 if (appctx->ctx.sess.target) {
4259 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004260 goto next_sess;
4261
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004262 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004263 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01004264 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004265 return 0;
4266
4267 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004268 LIST_DEL(&appctx->ctx.sess.bref.users);
4269 LIST_INIT(&appctx->ctx.sess.bref.users);
4270 if (appctx->ctx.sess.target != (void *)-1) {
4271 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01004272 break;
4273 }
4274 else
4275 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004276 }
4277
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004278 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004279 "%p: proto=%s",
4280 curr_sess,
4281 curr_sess->listener->proto->name);
4282
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004283
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004284 conn = objt_conn(curr_sess->si[0].end);
4285 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02004286 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004287 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004288 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004289 " src=%s:%d fe=%s be=%s srv=%s",
4290 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004291 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004292 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004293 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004294 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004295 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004296 break;
4297 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004298 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02004299 " src=unix:%d fe=%s be=%s srv=%s",
4300 curr_sess->listener->luid,
4301 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004302 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004303 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02004304 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004305 break;
4306 }
4307
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004308 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02004309 " ts=%02x age=%s calls=%d",
4310 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01004311 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
4312 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004313
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004314 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004315 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004316 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004317 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004318 curr_sess->req->analysers,
4319 curr_sess->req->rex ?
4320 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
4321 TICKS_TO_MS(1000)) : "");
4322
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004323 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004324 ",wx=%s",
4325 curr_sess->req->wex ?
4326 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
4327 TICKS_TO_MS(1000)) : "");
4328
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004329 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004330 ",ax=%s]",
4331 curr_sess->req->analyse_exp ?
4332 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
4333 TICKS_TO_MS(1000)) : "");
4334
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004335 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004336 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004337 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004338 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004339 curr_sess->rep->analysers,
4340 curr_sess->rep->rex ?
4341 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
4342 TICKS_TO_MS(1000)) : "");
4343
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004344 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004345 ",wx=%s",
4346 curr_sess->rep->wex ?
4347 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
4348 TICKS_TO_MS(1000)) : "");
4349
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004350 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004351 ",ax=%s]",
4352 curr_sess->rep->analyse_exp ?
4353 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
4354 TICKS_TO_MS(1000)) : "");
4355
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004356 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004357 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004358 " s0=[%d,%1xh,fd=%d,ex=%s]",
4359 curr_sess->si[0].state,
4360 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004361 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004362 curr_sess->si[0].exp ?
4363 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4364 TICKS_TO_MS(1000)) : "");
4365
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004366 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004367 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004368 " s1=[%d,%1xh,fd=%d,ex=%s]",
4369 curr_sess->si[1].state,
4370 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004371 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004372 curr_sess->si[1].exp ?
4373 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4374 TICKS_TO_MS(1000)) : "");
4375
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004376 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004377 " exp=%s",
4378 curr_sess->task->expire ?
4379 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4380 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004381 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004382 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004383
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004384 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004385
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004386 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004387 /* let's try again later from this session. We add ourselves into
4388 * this session's users so that it can remove us upon termination.
4389 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004390 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004391 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004392 }
4393
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004394 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004395 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004396 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004397
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004398 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004399 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004400 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004401 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004402 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004403 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004404
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004405 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004406 return 0;
4407
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004408 appctx->ctx.sess.target = NULL;
4409 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004410 return 1;
4411 }
4412
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004413 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004414 /* fall through */
4415
4416 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004417 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004418 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004419 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004420}
4421
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004422/* This is called when the stream interface is closed. For instance, upon an
4423 * external abort, we won't call the i/o handler anymore so we may need to
4424 * remove back references to the session currently being dumped.
4425 */
Simon Horman74d88312013-02-12 10:45:50 +09004426static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004427{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004428 struct appctx *appctx = __objt_appctx(si->end);
4429
4430 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
4431 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
4432 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004433 }
4434}
4435
Willy Tarreau20e99322013-04-13 09:22:25 +02004436/* This function is used to either dump tables states (when action is set
4437 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02004438 * It returns 0 if the output buffer is full and it needs to be called
4439 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004440 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004441static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004442{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004443 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004444 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004445 struct ebmb_node *eb;
4446 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004447 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004448 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004449
4450 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004451 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01004452 * - STAT_ST_INIT : the first call
4453 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004454 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004455 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004456 * and the entry pointer points to the next entry to be dumped,
4457 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004458 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004459 * data though.
4460 */
4461
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004462 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004463 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004464 if (appctx->st2 == STAT_ST_LIST) {
4465 appctx->ctx.table.entry->ref_cnt--;
4466 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004467 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004468 return 1;
4469 }
4470
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004471 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004472
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004473 while (appctx->st2 != STAT_ST_FIN) {
4474 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004475 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004476 appctx->ctx.table.proxy = appctx->ctx.table.target;
4477 if (!appctx->ctx.table.proxy)
4478 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004479
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004480 appctx->ctx.table.entry = NULL;
4481 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004482 break;
4483
Willy Tarreau295a8372011-03-10 11:25:07 +01004484 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004485 if (!appctx->ctx.table.proxy ||
4486 (appctx->ctx.table.target &&
4487 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
4488 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004489 break;
4490 }
4491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004492 if (appctx->ctx.table.proxy->table.size) {
4493 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
4494 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004495 return 0;
4496
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004497 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004498 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004499 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004500 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004501 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004502 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4503 appctx->ctx.table.entry->ref_cnt++;
4504 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004505 break;
4506 }
4507 }
4508 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004509 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004510 break;
4511
Willy Tarreau295a8372011-03-10 11:25:07 +01004512 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004513 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004514
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004515 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004516 /* we're filtering on some data contents */
4517 void *ptr;
4518 long long data;
4519
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 dt = appctx->ctx.table.data_type;
4521 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
4522 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004523 dt);
4524
4525 data = 0;
4526 switch (stktable_data_types[dt].std_type) {
4527 case STD_T_SINT:
4528 data = stktable_data_cast(ptr, std_t_sint);
4529 break;
4530 case STD_T_UINT:
4531 data = stktable_data_cast(ptr, std_t_uint);
4532 break;
4533 case STD_T_ULL:
4534 data = stktable_data_cast(ptr, std_t_ull);
4535 break;
4536 case STD_T_FRQP:
4537 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004538 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004539 break;
4540 }
4541
4542 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004543 if ((data < appctx->ctx.table.value &&
4544 (appctx->ctx.table.data_op == STD_OP_EQ ||
4545 appctx->ctx.table.data_op == STD_OP_GT ||
4546 appctx->ctx.table.data_op == STD_OP_GE)) ||
4547 (data == appctx->ctx.table.value &&
4548 (appctx->ctx.table.data_op == STD_OP_NE ||
4549 appctx->ctx.table.data_op == STD_OP_GT ||
4550 appctx->ctx.table.data_op == STD_OP_LT)) ||
4551 (data > appctx->ctx.table.value &&
4552 (appctx->ctx.table.data_op == STD_OP_EQ ||
4553 appctx->ctx.table.data_op == STD_OP_LT ||
4554 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004555 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004556 }
4557
Simon Hormanc88b8872011-06-15 15:18:49 +09004558 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004559 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
4560 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09004561 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004562
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004563 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004564
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004565 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004566 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004567 struct stksess *old = appctx->ctx.table.entry;
4568 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004569 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004570 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
4571 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
4572 stksess_kill(&appctx->ctx.table.proxy->table, old);
4573 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004574 break;
4575 }
4576
Simon Hormanc88b8872011-06-15 15:18:49 +09004577
4578 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004579 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
4580 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
4581 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09004582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004583 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
4584 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004585 break;
4586
Willy Tarreau295a8372011-03-10 11:25:07 +01004587 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004588 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004589 break;
4590 }
4591 }
4592 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004593}
4594
Willy Tarreaud426a182010-03-05 14:58:26 +01004595/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004596 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4597 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4598 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4599 * lines are respected within the limit of 70 output chars. Lines that are
4600 * continuation of a previous truncated line begin with "+" instead of " "
4601 * after the offset. The new pointer is returned.
4602 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004603static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4604 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004605{
4606 int end;
4607 unsigned char c;
4608
4609 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004610 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004611 return ptr;
4612
Willy Tarreau77804732012-10-29 16:14:26 +01004613 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004614
Willy Tarreaud426a182010-03-05 14:58:26 +01004615 while (ptr < len && ptr < bsize) {
4616 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004617 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004618 if (out->len > end - 2)
4619 break;
4620 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004621 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004622 if (out->len > end - 3)
4623 break;
4624 out->str[out->len++] = '\\';
4625 switch (c) {
4626 case '\t': c = 't'; break;
4627 case '\n': c = 'n'; break;
4628 case '\r': c = 'r'; break;
4629 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004630 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004631 }
4632 out->str[out->len++] = c;
4633 } else {
4634 if (out->len > end - 5)
4635 break;
4636 out->str[out->len++] = '\\';
4637 out->str[out->len++] = 'x';
4638 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4639 out->str[out->len++] = hextab[c & 0xF];
4640 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004641 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004642 /* we had a line break, let's return now */
4643 out->str[out->len++] = '\n';
4644 *line = ptr;
4645 return ptr;
4646 }
4647 }
4648 /* we have an incomplete line, we return it as-is */
4649 out->str[out->len++] = '\n';
4650 return ptr;
4651}
4652
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004653/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004654 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004655 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004656 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004657static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004658{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004659 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004660 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004661
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004662 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004663 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004664
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004665 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004666
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004667 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004668 /* the function had not been called yet, let's prepare the
4669 * buffer for a response.
4670 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004671 struct tm tm;
4672
4673 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004674 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004675 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4676 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4677 error_snapshot_id);
4678
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004679 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004680 /* Socket buffer full. Let's try again later from the same point */
4681 return 0;
4682 }
4683
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004684 appctx->ctx.errors.px = proxy;
4685 appctx->ctx.errors.buf = 0;
4686 appctx->ctx.errors.bol = 0;
4687 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004688 }
4689
4690 /* we have two inner loops here, one for the proxy, the other one for
4691 * the buffer.
4692 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004693 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004694 struct error_snapshot *es;
4695
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004696 if (appctx->ctx.errors.buf == 0)
4697 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004698 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004699 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004700
4701 if (!es->when.tv_sec)
4702 goto next;
4703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004704 if (appctx->ctx.errors.iid >= 0 &&
4705 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
4706 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004707 goto next;
4708
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004709 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004710 /* just print headers now */
4711
4712 char pn[INET6_ADDRSTRLEN];
4713 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004714 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004715
4716 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004717 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004718 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004719 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004720
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004721 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4722 case AF_INET:
4723 case AF_INET6:
4724 port = get_host_port(&es->src);
4725 break;
4726 default:
4727 port = 0;
4728 }
4729
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004730 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004731 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004732 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004733 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004734 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004735 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004736 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4737 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004738 break;
4739 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004740 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004741 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004742 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004743 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004744 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004745 break;
4746 }
4747
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004748 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004749 ", server %s (#%d), event #%u\n"
4750 " src %s:%d, session #%d, session flags 0x%08x\n"
4751 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4752 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4753 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4754 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4755 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4756 es->ev_id,
4757 pn, port, es->sid, es->s_flags,
4758 es->state, es->m_flags, es->t_flags,
4759 es->m_clen, es->m_blen,
4760 es->b_flags, es->b_out, es->b_tot,
4761 es->len, es->b_wrap, es->pos);
4762
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004763 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004764 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004765 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004766 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004767 appctx->ctx.errors.ptr = 0;
4768 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004769 }
4770
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004771 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004772 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004773 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004774 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004775 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004776 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004777 goto next;
4778 }
4779
4780 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004781 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004782 int newptr;
4783 int newline;
4784
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004785 newline = appctx->ctx.errors.bol;
4786 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
4787 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004788 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004790 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004791 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004792 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004793 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004794 appctx->ctx.errors.ptr = newptr;
4795 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004796 };
4797 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004798 appctx->ctx.errors.bol = 0;
4799 appctx->ctx.errors.ptr = -1;
4800 appctx->ctx.errors.buf++;
4801 if (appctx->ctx.errors.buf > 1) {
4802 appctx->ctx.errors.buf = 0;
4803 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004804 }
4805 }
4806
4807 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004808 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004809}
4810
Willy Tarreaud5781202012-09-22 19:32:35 +02004811/* parse the "level" argument on the bind lines */
4812static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4813{
4814 if (!*args[cur_arg + 1]) {
4815 memprintf(err, "'%s' : missing level", args[cur_arg]);
4816 return ERR_ALERT | ERR_FATAL;
4817 }
4818
4819 if (!strcmp(args[cur_arg+1], "user"))
4820 conf->level = ACCESS_LVL_USER;
4821 else if (!strcmp(args[cur_arg+1], "operator"))
4822 conf->level = ACCESS_LVL_OPER;
4823 else if (!strcmp(args[cur_arg+1], "admin"))
4824 conf->level = ACCESS_LVL_ADMIN;
4825 else {
4826 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4827 args[cur_arg], args[cur_arg+1]);
4828 return ERR_ALERT | ERR_FATAL;
4829 }
4830
4831 return 0;
4832}
4833
Willy Tarreaub24281b2011-02-13 13:16:36 +01004834struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004835 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004836 .name = "<STATS>", /* used for logging */
4837 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004838 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004839};
4840
Simon Horman9bd2c732011-06-15 15:18:44 +09004841static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004842 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004843 .name = "<CLI>", /* used for logging */
4844 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004845 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004846};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004847
Willy Tarreaudc13c112013-06-21 23:16:39 +02004848static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004849 { CFG_GLOBAL, "stats", stats_parse_global },
4850 { 0, NULL, NULL },
4851}};
4852
Willy Tarreaud5781202012-09-22 19:32:35 +02004853static struct bind_kw_list bind_kws = { "STAT", { }, {
4854 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4855 { NULL, NULL, 0 },
4856}};
4857
Willy Tarreau10522fd2008-07-09 20:12:41 +02004858__attribute__((constructor))
4859static void __dumpstats_module_init(void)
4860{
4861 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004862 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004863}
4864
Willy Tarreau91861262007-10-17 17:06:05 +02004865/*
4866 * Local variables:
4867 * c-indent-level: 8
4868 * c-basic-offset: 8
4869 * End:
4870 */