blob: 661aa3f539601acb0e4beb553985fad365aabee2 [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 Tarreaud9bdcd52012-12-22 20:31:10 +010064static int stats_dump_raw_info_to_buffer(struct stream_interface *si);
65static int stats_dump_raw_stat_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010066static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090067static int stats_dump_sess_to_buffer(struct stream_interface *si);
68static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010069static int stats_table_request(struct stream_interface *si, int show);
Simon Horman9bd2c732011-06-15 15:18:44 +090070static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
71static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
72
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010073/*
74 cli_io_handler()
75 -> stats_dump_sess_to_buffer() // "show sess"
76 -> stats_dump_errors_to_buffer() // "show errors"
77 -> stats_dump_raw_info_to_buffer() // "show info"
78 -> stats_dump_raw_info()
79 -> stats_dump_raw_stat_to_buffer() // "show stat"
80 -> stats_dump_csv_header()
81 -> stats_dump_proxy()
82 -> stats_dump_px_hdr()
83 -> stats_dump_fe_stats()
84 -> stats_dump_li_stats()
85 -> stats_dump_sv_stats()
86 -> stats_dump_be_stats()
87 -> stats_dump_px_end()
88
89 http_stats_io_handler()
90 -> stats_http_redir()
91 -> stats_dump_http() // also emits the HTTP headers
92 -> stats_dump_html_head() // emits the HTML headers
93 -> stats_dump_csv_header() // emits the CSV headers (same as above)
94 -> stats_dump_http_info() // note: ignores non-HTML output
95 -> stats_dump_proxy() // same as above
96 -> stats_dump_http_end() // emits HTML trailer
97 */
98
Simon Horman9bd2c732011-06-15 15:18:44 +090099static struct si_applet cli_applet;
100
101static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200102 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200103 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200104 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200105 " help : this message\n"
106 " prompt : toggle interactive mode with prompt\n"
107 " quit : disconnect\n"
108 " show info : report information about the running process\n"
109 " show stat : report counters for each proxy and server\n"
110 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100111 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200112 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200113 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200114 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200115 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100116 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200117 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200118 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200119 " disable : put a server or frontend in maintenance mode\n"
120 " enable : re-enable a server or frontend which is in maintenance mode\n"
121 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200122 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200123
Simon Horman9bd2c732011-06-15 15:18:44 +0900124static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200125 "Permission denied\n"
126 "";
127
Willy Tarreau295a8372011-03-10 11:25:07 +0100128/* data transmission states for the stats responses */
129enum {
130 STAT_ST_INIT = 0,
131 STAT_ST_HEAD,
132 STAT_ST_INFO,
133 STAT_ST_LIST,
134 STAT_ST_END,
135 STAT_ST_FIN,
136};
137
138/* data transmission states for the stats responses inside a proxy */
139enum {
140 STAT_PX_ST_INIT = 0,
141 STAT_PX_ST_TH,
142 STAT_PX_ST_FE,
143 STAT_PX_ST_LI,
144 STAT_PX_ST_SV,
145 STAT_PX_ST_BE,
146 STAT_PX_ST_END,
147 STAT_PX_ST_FIN,
148};
149
Cyril Bonté19979e12012-04-04 12:57:21 +0200150extern const char *stat_status_codes[];
151
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200152/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200153 * a new stats socket. It returns a positive value upon success, 0 if the connection
154 * needs to be closed and ignored, or a negative value upon critical failure.
155 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900156static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200157{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200158 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100159 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100160 s->target = s->si[1].conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200161 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100162 s->si[1].applet.st1 = 0;
163 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200164
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200165 tv_zero(&s->logs.tv_request);
166 s->logs.t_queue = 0;
167 s->logs.t_connect = 0;
168 s->logs.t_data = 0;
169 s->logs.t_close = 0;
170 s->logs.bytes_in = s->logs.bytes_out = 0;
171 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
172 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200173
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200174 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200175
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200176 if (s->listener->timeout) {
177 s->req->rto = *s->listener->timeout;
178 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200181}
182
Willy Tarreau07e9e642010-08-17 21:48:17 +0200183/* allocate a new stats frontend named <name>, and return it
184 * (or NULL in case of lack of memory).
185 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200186static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200187{
188 struct proxy *fe;
189
190 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
191 if (!fe)
192 return NULL;
193
Willy Tarreau237250c2011-07-29 01:49:03 +0200194 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200195 fe->next = proxy;
196 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200197 fe->last_change = now.tv_sec;
198 fe->id = strdup("GLOBAL");
199 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200200 fe->maxconn = 10; /* default to 10 concurrent connections */
201 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200202 fe->conf.file = strdup(file);
203 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200204 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200205
206 /* the stats frontend is the only one able to assign ID #0 */
207 fe->conf.id.key = fe->uuid = 0;
208 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200209 return fe;
210}
211
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200213 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
214 * error message into the <err> buffer which will be preallocated. The trailing
215 * '\n' must not be written. The function must be called with <args> pointing to
216 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200217 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200218static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200219 struct proxy *defpx, const char *file, int line,
220 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200221{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200222 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200223 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200224
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226 int cur_arg;
227
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200228 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200229 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200230 return -1;
231 }
232
Willy Tarreau89a63132009-08-16 17:41:45 +0200233 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200234 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200235 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200236 return -1;
237 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200238 }
239
Willy Tarreau4348fad2012-09-20 16:48:07 +0200240 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200241 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200242
Willy Tarreauc53d4222012-09-20 20:19:28 +0200243 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
244 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
245 file, line, args[0], args[1], err && *err ? *err : "error");
246 return -1;
247 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200249 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200250 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200251 static int bind_dumped;
252 struct bind_kw *kw;
253
254 kw = bind_find_kw(args[cur_arg]);
255 if (kw) {
256 if (!kw->parse) {
257 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
258 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200259 return -1;
260 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200261
262 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
263 if (err && *err)
264 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
265 else
266 memprintf(err, "'%s %s' : error encountered while processing '%s'",
267 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200268 return -1;
269 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200270
271 cur_arg += 1 + kw->skip;
272 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200273 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200274
275 if (!bind_dumped) {
276 bind_dump_kws(err);
277 indent_msg(err, 4);
278 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200280
281 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
282 args[0], args[1], args[cur_arg],
283 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
284 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200285 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100286
Willy Tarreauc53d4222012-09-20 20:19:28 +0200287 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
288 l->maxconn = global.stats_fe->maxconn;
289 l->backlog = global.stats_fe->backlog;
290 l->timeout = &global.stats_fe->timeout.client;
291 l->accept = session_accept;
292 l->handler = process_session;
293 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
294 l->nice = -64; /* we want to boost priority for local stats */
295 global.maxsock += l->maxconn;
296 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200298 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100299 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100301
302 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304 return -1;
305 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100307 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200308 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 return -1;
310 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200311 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200312 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200313 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200314 return -1;
315 }
316 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200317 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200319 else if (!strcmp(args[1], "maxconn")) {
320 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321
322 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200323 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 return -1;
325 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200326
327 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200328 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200329 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200330 return -1;
331 }
332 }
333 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200334 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200335 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
336 int cur_arg = 2;
337 unsigned int set = 0;
338
339 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100340 unsigned int low, high;
341
Willy Tarreau35b7b162012-10-22 23:17:18 +0200342 if (strcmp(args[cur_arg], "all") == 0) {
343 set = 0;
344 break;
345 }
346 else if (strcmp(args[cur_arg], "odd") == 0) {
347 set |= 0x55555555;
348 }
349 else if (strcmp(args[cur_arg], "even") == 0) {
350 set |= 0xAAAAAAAA;
351 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100352 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100353 char *dash = strchr(args[cur_arg], '-');
354
355 low = high = str2uic(args[cur_arg]);
356 if (dash)
357 high = str2uic(dash + 1);
358
359 if (high < low) {
360 unsigned int swap = low;
361 low = high;
362 high = swap;
363 }
364
365 if (low < 1 || high > 32) {
366 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
367 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200368 return -1;
369 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100370
371 while (low <= high)
372 set |= 1 << (low++ - 1);
373 }
374 else {
375 memprintf(err,
376 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
377 args[0], args[1]);
378 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200379 }
380 cur_arg++;
381 }
382 global.stats_fe->bind_proc = set;
383 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200384 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200385 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200386 return -1;
387 }
388 return 0;
389}
390
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100391/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
392 * for clearing it if needed.
393 */
394static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100395{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100396 chunk_appendf(&trash,
397 "# pxname,svname,"
398 "qcur,qmax,"
399 "scur,smax,slim,stot,"
400 "bin,bout,"
401 "dreq,dresp,"
402 "ereq,econ,eresp,"
403 "wretr,wredis,"
404 "status,weight,act,bck,"
405 "chkfail,chkdown,lastchg,downtime,qlimit,"
406 "pid,iid,sid,throttle,lbtot,tracked,type,"
407 "rate,rate_lim,rate_max,"
408 "check_status,check_code,check_duration,"
409 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
410 "req_rate,req_rate_max,req_tot,"
411 "cli_abrt,srv_abrt,"
412 "comp_in,comp_out,comp_byp,comp_rsp,"
413 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100414}
415
Simon Hormand9366582011-06-15 15:18:45 +0900416/* print a string of text buffer to <out>. The format is :
417 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
418 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
419 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
420 */
421static int dump_text(struct chunk *out, const char *buf, int bsize)
422{
423 unsigned char c;
424 int ptr = 0;
425
426 while (buf[ptr] && ptr < bsize) {
427 c = buf[ptr];
428 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
429 if (out->len > out->size - 1)
430 break;
431 out->str[out->len++] = c;
432 }
433 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
434 if (out->len > out->size - 2)
435 break;
436 out->str[out->len++] = '\\';
437 switch (c) {
438 case ' ': c = ' '; break;
439 case '\t': c = 't'; break;
440 case '\n': c = 'n'; break;
441 case '\r': c = 'r'; break;
442 case '\e': c = 'e'; break;
443 case '\\': c = '\\'; break;
444 }
445 out->str[out->len++] = c;
446 }
447 else {
448 if (out->len > out->size - 4)
449 break;
450 out->str[out->len++] = '\\';
451 out->str[out->len++] = 'x';
452 out->str[out->len++] = hextab[(c >> 4) & 0xF];
453 out->str[out->len++] = hextab[c & 0xF];
454 }
455 ptr++;
456 }
457
458 return ptr;
459}
460
461/* print a buffer in hexa.
462 * Print stopped if <bsize> is reached, or if no more place in the chunk.
463 */
464static int dump_binary(struct chunk *out, const char *buf, int bsize)
465{
466 unsigned char c;
467 int ptr = 0;
468
469 while (ptr < bsize) {
470 c = buf[ptr];
471
472 if (out->len > out->size - 2)
473 break;
474 out->str[out->len++] = hextab[(c >> 4) & 0xF];
475 out->str[out->len++] = hextab[c & 0xF];
476
477 ptr++;
478 }
479 return ptr;
480}
481
482/* Dump the status of a table to a stream interface's
483 * read buffer. It returns 0 if the output buffer is full
484 * and needs to be called again, otherwise non-zero.
485 */
486static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
487 struct proxy *proxy, struct proxy *target)
488{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200489 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900490
Willy Tarreau77804732012-10-29 16:14:26 +0100491 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900492 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
493
494 /* any other information should be dumped here */
495
Willy Tarreau290e63a2012-09-20 18:07:14 +0200496 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100497 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900498
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200499 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900500 return 0;
501
502 return 1;
503}
504
505/* Dump the a table entry to a stream interface's
506 * read buffer. It returns 0 if the output buffer is full
507 * and needs to be called again, otherwise non-zero.
508 */
509static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
510 struct proxy *proxy, struct stksess *entry)
511{
512 int dt;
513
Willy Tarreau77804732012-10-29 16:14:26 +0100514 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900515
516 if (proxy->table.type == STKTABLE_TYPE_IP) {
517 char addr[INET_ADDRSTRLEN];
518 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100519 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900520 }
521 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
522 char addr[INET6_ADDRSTRLEN];
523 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100524 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900525 }
526 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100527 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900528 }
529 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100530 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900531 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
532 }
533 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900535 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
536 }
537
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900539
540 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
541 void *ptr;
542
543 if (proxy->table.data_ofs[dt] == 0)
544 continue;
545 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100546 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900547 else
Willy Tarreau77804732012-10-29 16:14:26 +0100548 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900549
550 ptr = stktable_data_ptr(&proxy->table, entry, dt);
551 switch (stktable_data_types[dt].std_type) {
552 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100553 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900554 break;
555 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900557 break;
558 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100559 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900560 break;
561 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100562 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900563 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
564 proxy->table.data_arg[dt].u));
565 break;
566 }
567 }
Willy Tarreau77804732012-10-29 16:14:26 +0100568 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900569
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200570 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900571 return 0;
572
573 return 1;
574}
575
Willy Tarreaudec98142012-06-06 23:37:08 +0200576static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900577{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200578 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900579 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900580 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900581 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900582 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200583 long long value;
584 int data_type;
585 void *ptr;
586 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900587
Simon Hormand5b9fd92011-06-15 15:18:48 +0900588 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900589
590 if (!*args[4]) {
591 si->applet.ctx.cli.msg = "Key value expected\n";
592 si->applet.st0 = STAT_CLI_PRINT;
593 return;
594 }
595
Simon Hormanc5b89f62011-06-15 15:18:50 +0900596 switch (px->table.type) {
597 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900598 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100599 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900600 break;
601 case STKTABLE_TYPE_IPV6:
602 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100603 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900604 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900605 case STKTABLE_TYPE_INTEGER:
606 {
607 char *endptr;
608 unsigned long val;
609 errno = 0;
610 val = strtoul(args[4], &endptr, 10);
611 if ((errno == ERANGE && val == ULONG_MAX) ||
612 (errno != 0 && val == 0) || endptr == args[4] ||
613 val > 0xffffffff) {
614 si->applet.ctx.cli.msg = "Invalid key\n";
615 si->applet.st0 = STAT_CLI_PRINT;
616 return;
617 }
618 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100619 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900620 break;
621 }
622 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900623 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100624 static_table_key->key = args[4];
625 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900626 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900627 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200628 switch (action) {
629 case STAT_CLI_O_TAB:
630 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
631 break;
632 case STAT_CLI_O_CLR:
633 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
634 break;
635 default:
636 si->applet.ctx.cli.msg = "Unknown action\n";
637 break;
638 }
Simon Horman121f3052011-06-15 15:18:46 +0900639 si->applet.st0 = STAT_CLI_PRINT;
640 return;
641 }
642
643 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200644 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900645 si->applet.ctx.cli.msg = stats_permission_denied_msg;
646 si->applet.st0 = STAT_CLI_PRINT;
647 return;
648 }
649
Willy Tarreau07115412012-10-29 21:56:59 +0100650 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900651
Willy Tarreaudec98142012-06-06 23:37:08 +0200652 switch (action) {
653 case STAT_CLI_O_TAB:
654 if (!ts)
655 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100656 chunk_reset(&trash);
657 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900658 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100659 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900660 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200661
662 case STAT_CLI_O_CLR:
663 if (!ts)
664 return;
665 if (ts->ref_cnt) {
666 /* don't delete an entry which is currently referenced */
667 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
668 si->applet.st0 = STAT_CLI_PRINT;
669 return;
670 }
671 stksess_kill(&px->table, ts);
672 break;
Simon Horman17bce342011-06-15 15:18:47 +0900673
Willy Tarreau654694e2012-06-07 01:03:16 +0200674 case STAT_CLI_O_SET:
675 if (strncmp(args[5], "data.", 5) != 0) {
676 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
677 si->applet.st0 = STAT_CLI_PRINT;
678 return;
679 }
680
681 data_type = stktable_get_data_type(args[5] + 5);
682 if (data_type < 0) {
683 si->applet.ctx.cli.msg = "Unknown data type\n";
684 si->applet.st0 = STAT_CLI_PRINT;
685 return;
686 }
687
688 if (!px->table.data_ofs[data_type]) {
689 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
690 si->applet.st0 = STAT_CLI_PRINT;
691 return;
692 }
693
694 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
695 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
696 si->applet.st0 = STAT_CLI_PRINT;
697 return;
698 }
699
700 if (ts)
701 stktable_touch(&px->table, ts, 1);
702 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100703 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200704 if (!ts) {
705 /* don't delete an entry which is currently referenced */
706 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
707 si->applet.st0 = STAT_CLI_PRINT;
708 return;
709 }
710 stktable_store(&px->table, ts, 1);
711 }
712
713 ptr = stktable_data_ptr(&px->table, ts, data_type);
714 switch (stktable_data_types[data_type].std_type) {
715 case STD_T_SINT:
716 stktable_data_cast(ptr, std_t_sint) = value;
717 break;
718 case STD_T_UINT:
719 stktable_data_cast(ptr, std_t_uint) = value;
720 break;
721 case STD_T_ULL:
722 stktable_data_cast(ptr, std_t_ull) = value;
723 break;
724 case STD_T_FRQP:
725 /* We only reset the previous value so that it slowly fades out */
726 frqp = &stktable_data_cast(ptr, std_t_frqp);
727 frqp->curr_tick = now_ms;
728 frqp->prev_ctr = value;
729 frqp->curr_ctr = 0;
730 break;
731 }
732 break;
733
Willy Tarreaudec98142012-06-06 23:37:08 +0200734 default:
735 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900736 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200737 break;
Simon Horman121f3052011-06-15 15:18:46 +0900738 }
Simon Horman121f3052011-06-15 15:18:46 +0900739}
740
Willy Tarreau654694e2012-06-07 01:03:16 +0200741static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900742{
Willy Tarreau654694e2012-06-07 01:03:16 +0200743 if (action != STAT_CLI_O_TAB) {
744 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
745 si->applet.st0 = STAT_CLI_PRINT;
746 return;
747 }
748
Simon Hormand5b9fd92011-06-15 15:18:48 +0900749 /* condition on stored data value */
750 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
751 if (si->applet.ctx.table.data_type < 0) {
752 si->applet.ctx.cli.msg = "Unknown data type\n";
753 si->applet.st0 = STAT_CLI_PRINT;
754 return;
755 }
756
757 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
758 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
759 si->applet.st0 = STAT_CLI_PRINT;
760 return;
761 }
762
763 si->applet.ctx.table.data_op = get_std_op(args[4]);
764 if (si->applet.ctx.table.data_op < 0) {
765 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
766 si->applet.st0 = STAT_CLI_PRINT;
767 return;
768 }
769
770 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
771 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
772 si->applet.st0 = STAT_CLI_PRINT;
773 return;
774 }
775}
776
Willy Tarreaudec98142012-06-06 23:37:08 +0200777static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900778{
779 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200780 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200781 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900782 si->applet.ctx.table.proxy = NULL;
783 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200784 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900785
786 if (*args[2]) {
787 si->applet.ctx.table.target = find_stktable(args[2]);
788 if (!si->applet.ctx.table.target) {
789 si->applet.ctx.cli.msg = "No such table\n";
790 si->applet.st0 = STAT_CLI_PRINT;
791 return;
792 }
793 }
794 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200795 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900796 goto err_args;
797 return;
798 }
799
800 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200801 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900802 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200803 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900804 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900805 goto err_args;
806
807 return;
808
809err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200810 switch (action) {
811 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200813 break;
814 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900815 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200816 break;
817 default:
818 si->applet.ctx.cli.msg = "Unknown action\n";
819 break;
820 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900821 si->applet.st0 = STAT_CLI_PRINT;
822}
823
Willy Tarreau532a4502011-09-07 22:37:44 +0200824/* Expects to find a frontend named <arg> and returns it, otherwise displays various
825 * adequate error messages and returns NULL. This function also expects the session
826 * level to be admin.
827 */
828static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
829{
830 struct proxy *px;
831
Willy Tarreau290e63a2012-09-20 18:07:14 +0200832 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200833 si->applet.ctx.cli.msg = stats_permission_denied_msg;
834 si->applet.st0 = STAT_CLI_PRINT;
835 return NULL;
836 }
837
838 if (!*arg) {
839 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
840 si->applet.st0 = STAT_CLI_PRINT;
841 return NULL;
842 }
843
844 px = findproxy(arg, PR_CAP_FE);
845 if (!px) {
846 si->applet.ctx.cli.msg = "No such frontend.\n";
847 si->applet.st0 = STAT_CLI_PRINT;
848 return NULL;
849 }
850 return px;
851}
852
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200853/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
854 * and returns the pointer to the server. Otherwise, display adequate error messages
855 * and returns NULL. This function also expects the session level to be admin. Note:
856 * the <arg> is modified to remove the '/'.
857 */
858static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
859{
860 struct proxy *px;
861 struct server *sv;
862 char *line;
863
Willy Tarreau290e63a2012-09-20 18:07:14 +0200864 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200865 si->applet.ctx.cli.msg = stats_permission_denied_msg;
866 si->applet.st0 = STAT_CLI_PRINT;
867 return NULL;
868 }
869
870 /* split "backend/server" and make <line> point to server */
871 for (line = arg; *line; line++)
872 if (*line == '/') {
873 *line++ = '\0';
874 break;
875 }
876
877 if (!*line || !*arg) {
878 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
879 si->applet.st0 = STAT_CLI_PRINT;
880 return NULL;
881 }
882
883 if (!get_backend_server(arg, line, &px, &sv)) {
884 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
885 si->applet.st0 = STAT_CLI_PRINT;
886 return NULL;
887 }
888
889 if (px->state == PR_STSTOPPED) {
890 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
891 si->applet.st0 = STAT_CLI_PRINT;
892 return NULL;
893 }
894
895 return sv;
896}
897
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200898/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200899 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100900 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200901 * designating the function which will have to process the request, which can
902 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200903 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900904static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200905{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200906 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200907 char *args[MAX_STATS_ARGS + 1];
908 int arg;
909
910 while (isspace((unsigned char)*line))
911 line++;
912
913 arg = 0;
914 args[arg] = line;
915
916 while (*line && arg < MAX_STATS_ARGS) {
917 if (isspace((unsigned char)*line)) {
918 *line++ = '\0';
919
920 while (isspace((unsigned char)*line))
921 line++;
922
923 args[++arg] = line;
924 continue;
925 }
926
927 line++;
928 }
929
930 while (++arg <= MAX_STATS_ARGS)
931 args[arg] = line;
932
Willy Tarreau295a8372011-03-10 11:25:07 +0100933 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200934 if (strcmp(args[0], "show") == 0) {
935 if (strcmp(args[1], "stat") == 0) {
936 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100937 si->applet.ctx.stats.flags |= STAT_BOUND;
938 si->applet.ctx.stats.iid = atoi(args[2]);
939 si->applet.ctx.stats.type = atoi(args[3]);
940 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200941 }
942
Willy Tarreau295a8372011-03-10 11:25:07 +0100943 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200944 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100945 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_raw_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200946 }
947 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100948 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200949 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100950 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200951 }
952 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200953 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200954 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100955 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100956 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200957 return 1;
958 }
Willy Tarreau76153662012-11-26 01:16:39 +0100959 if (*args[2] && strcmp(args[2], "all") == 0)
960 si->applet.ctx.sess.target = (void *)-1;
961 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100962 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100963 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100964 si->applet.ctx.sess.target = NULL;
965 si->applet.ctx.sess.section = 0; /* start with session status */
966 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100967 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200968 }
969 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200970 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100971 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100972 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200973 return 1;
974 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200975 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100976 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200977 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100978 si->applet.ctx.errors.iid = -1;
979 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200980 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100981 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200982 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200983 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200984 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200985 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700986 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200987 return 0;
988 }
989 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200990 else if (strcmp(args[0], "clear") == 0) {
991 if (strcmp(args[1], "counters") == 0) {
992 struct proxy *px;
993 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200994 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200995 int clrall = 0;
996
997 if (strcmp(args[2], "all") == 0)
998 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200999
Willy Tarreau6162db22009-10-10 17:13:00 +02001000 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001001 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1002 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001003 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001004 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001005 return 1;
1006 }
1007
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001008 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001009 if (clrall) {
1010 memset(&px->be_counters, 0, sizeof(px->be_counters));
1011 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1012 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001013 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001014 px->be_counters.conn_max = 0;
1015 px->be_counters.p.http.rps_max = 0;
1016 px->be_counters.sps_max = 0;
1017 px->be_counters.cps_max = 0;
1018 px->be_counters.nbpend_max = 0;
1019
1020 px->fe_counters.conn_max = 0;
1021 px->fe_counters.p.http.rps_max = 0;
1022 px->fe_counters.sps_max = 0;
1023 px->fe_counters.cps_max = 0;
1024 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001025 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001026
1027 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001028 if (clrall)
1029 memset(&sv->counters, 0, sizeof(sv->counters));
1030 else {
1031 sv->counters.cur_sess_max = 0;
1032 sv->counters.nbpend_max = 0;
1033 sv->counters.sps_max = 0;
1034 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001035
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001036 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001037 if (li->counters) {
1038 if (clrall)
1039 memset(li->counters, 0, sizeof(*li->counters));
1040 else
1041 li->counters->conn_max = 0;
1042 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001043 }
1044
Willy Tarreau81c25d02011-09-07 15:17:21 +02001045 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001046 return 1;
1047 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001048 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001049 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001050 /* end of processing */
1051 return 1;
1052 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001053 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001054 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001055 return 0;
1056 }
1057 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001058 else if (strcmp(args[0], "get") == 0) {
1059 if (strcmp(args[1], "weight") == 0) {
1060 struct proxy *px;
1061 struct server *sv;
1062
1063 /* split "backend/server" and make <line> point to server */
1064 for (line = args[2]; *line; line++)
1065 if (*line == '/') {
1066 *line++ = '\0';
1067 break;
1068 }
1069
1070 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001071 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001072 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001073 return 1;
1074 }
1075
1076 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001077 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001078 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001079 return 1;
1080 }
1081
1082 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001083 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1084 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001085 return 1;
1086 }
1087 else { /* not "get weight" */
1088 return 0;
1089 }
1090 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001091 else if (strcmp(args[0], "set") == 0) {
1092 if (strcmp(args[1], "weight") == 0) {
1093 struct proxy *px;
1094 struct server *sv;
1095 int w;
1096
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001097 sv = expect_server_admin(s, si, args[2]);
1098 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001099 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001100 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001101
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001102 /* if the weight is terminated with '%', it is set relative to
1103 * the initial weight, otherwise it is absolute.
1104 */
1105 if (!*args[3]) {
1106 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001107 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001108 return 1;
1109 }
1110
Willy Tarreau4483d432009-10-10 19:30:08 +02001111 w = atoi(args[3]);
1112 if (strchr(args[3], '%') != NULL) {
1113 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001114 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001115 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001116 return 1;
1117 }
1118 w = sv->iweight * w / 100;
1119 }
1120 else {
1121 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001122 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001123 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001124 return 1;
1125 }
1126 }
1127
1128 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001129 si->applet.ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001130 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001131 return 1;
1132 }
1133
1134 sv->uweight = w;
1135
1136 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1137 /* we must take care of not pushing the server to full throttle during slow starts */
1138 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1139 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1140 else
1141 sv->eweight = BE_WEIGHT_SCALE;
1142 sv->eweight *= sv->uweight;
1143 } else {
1144 sv->eweight = sv->uweight;
1145 }
1146
1147 /* static LB algorithms are a bit harder to update */
1148 if (px->lbprm.update_server_eweight)
1149 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001150 else if (sv->eweight) {
1151 if (px->lbprm.set_server_status_up)
1152 px->lbprm.set_server_status_up(sv);
1153 }
1154 else {
1155 if (px->lbprm.set_server_status_down)
1156 px->lbprm.set_server_status_down(sv);
1157 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001158
1159 return 1;
1160 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001161 else if (strcmp(args[1], "timeout") == 0) {
1162 if (strcmp(args[2], "cli") == 0) {
1163 unsigned timeout;
1164 const char *res;
1165
1166 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001167 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001168 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001169 return 1;
1170 }
1171
1172 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1173 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001174 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001175 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001176 return 1;
1177 }
1178
1179 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1180 return 1;
1181 }
1182 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001183 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001184 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001185 return 1;
1186 }
1187 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001188 else if (strcmp(args[1], "maxconn") == 0) {
1189 if (strcmp(args[2], "frontend") == 0) {
1190 struct proxy *px;
1191 struct listener *l;
1192 int v;
1193
Willy Tarreau532a4502011-09-07 22:37:44 +02001194 px = expect_frontend_admin(s, si, args[3]);
1195 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001196 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001197
1198 if (!*args[4]) {
1199 si->applet.ctx.cli.msg = "Integer value expected.\n";
1200 si->applet.st0 = STAT_CLI_PRINT;
1201 return 1;
1202 }
1203
1204 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001205 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001206 si->applet.ctx.cli.msg = "Value out of range.\n";
1207 si->applet.st0 = STAT_CLI_PRINT;
1208 return 1;
1209 }
1210
1211 /* OK, the value is fine, so we assign it to the proxy and to all of
1212 * its listeners. The blocked ones will be dequeued.
1213 */
1214 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001215 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001216 l->maxconn = v;
1217 if (l->state == LI_FULL)
1218 resume_listener(l);
1219 }
1220
1221 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1222 dequeue_all_listeners(&s->fe->listener_queue);
1223
1224 return 1;
1225 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001226 else if (strcmp(args[2], "global") == 0) {
1227 int v;
1228
Willy Tarreau290e63a2012-09-20 18:07:14 +02001229 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001230 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1231 si->applet.st0 = STAT_CLI_PRINT;
1232 return 1;
1233 }
1234
1235 if (!*args[3]) {
1236 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1237 si->applet.st0 = STAT_CLI_PRINT;
1238 return 1;
1239 }
1240
1241 v = atoi(args[3]);
1242 if (v > global.hardmaxconn) {
1243 si->applet.ctx.cli.msg = "Value out of range.\n";
1244 si->applet.st0 = STAT_CLI_PRINT;
1245 return 1;
1246 }
1247
1248 /* check for unlimited values */
1249 if (v <= 0)
1250 v = global.hardmaxconn;
1251
1252 global.maxconn = v;
1253
1254 /* Dequeues all of the listeners waiting for a resource */
1255 if (!LIST_ISEMPTY(&global_listener_queue))
1256 dequeue_all_listeners(&global_listener_queue);
1257
1258 return 1;
1259 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001260 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001261 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001262 si->applet.st0 = STAT_CLI_PRINT;
1263 return 1;
1264 }
1265 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001266 else if (strcmp(args[1], "rate-limit") == 0) {
1267 if (strcmp(args[2], "connections") == 0) {
1268 if (strcmp(args[3], "global") == 0) {
1269 int v;
1270
Willy Tarreau290e63a2012-09-20 18:07:14 +02001271 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001272 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1273 si->applet.st0 = STAT_CLI_PRINT;
1274 return 1;
1275 }
1276
1277 if (!*args[4]) {
1278 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1279 si->applet.st0 = STAT_CLI_PRINT;
1280 return 1;
1281 }
1282
1283 v = atoi(args[4]);
1284 if (v < 0) {
1285 si->applet.ctx.cli.msg = "Value out of range.\n";
1286 si->applet.st0 = STAT_CLI_PRINT;
1287 return 1;
1288 }
1289
1290 global.cps_lim = v;
1291
1292 /* Dequeues all of the listeners waiting for a resource */
1293 if (!LIST_ISEMPTY(&global_listener_queue))
1294 dequeue_all_listeners(&global_listener_queue);
1295
1296 return 1;
1297 }
1298 else {
1299 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1300 si->applet.st0 = STAT_CLI_PRINT;
1301 return 1;
1302 }
1303 }
William Lallemandd85f9172012-11-09 17:05:39 +01001304 else if (strcmp(args[2], "http-compression") == 0) {
1305 if (strcmp(args[3], "global") == 0) {
1306 int v;
1307
Willy Tarreau85d47f92012-11-21 00:29:50 +01001308 if (!*args[4]) {
1309 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1310 si->applet.st0 = STAT_CLI_PRINT;
1311 return 1;
1312 }
1313
William Lallemandd85f9172012-11-09 17:05:39 +01001314 v = atoi(args[4]);
1315 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1316 }
1317 else {
1318 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1319 si->applet.st0 = STAT_CLI_PRINT;
1320 return 1;
1321 }
1322 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001323 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001324 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001325 si->applet.st0 = STAT_CLI_PRINT;
1326 return 1;
1327 }
1328 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001329 else if (strcmp(args[1], "table") == 0) {
1330 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1331 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001332 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001333 return 0;
1334 }
1335 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001336 else if (strcmp(args[0], "enable") == 0) {
1337 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001338 struct server *sv;
1339
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001340 sv = expect_server_admin(s, si, args[2]);
1341 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001342 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001343
Cyril Bontécd19e512010-01-31 22:34:03 +01001344 if (sv->state & SRV_MAINTAIN) {
1345 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001346 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001347 /* If this server tracks the status of another one,
1348 * we must restore the good status.
1349 */
Willy Tarreau44267702011-10-28 15:35:33 +02001350 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001351 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001352 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001353 } else {
1354 sv->state &= ~SRV_MAINTAIN;
1355 set_server_down(sv);
1356 }
1357 } else {
1358 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001359 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001360 }
1361 }
1362
Willy Tarreau532a4502011-09-07 22:37:44 +02001363 return 1;
1364 }
1365 else if (strcmp(args[1], "frontend") == 0) {
1366 struct proxy *px;
1367
1368 px = expect_frontend_admin(s, si, args[2]);
1369 if (!px)
1370 return 1;
1371
1372 if (px->state == PR_STSTOPPED) {
1373 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1374 si->applet.st0 = STAT_CLI_PRINT;
1375 return 1;
1376 }
1377
1378 if (px->state != PR_STPAUSED) {
1379 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1380 si->applet.st0 = STAT_CLI_PRINT;
1381 return 1;
1382 }
1383
1384 if (!resume_proxy(px)) {
1385 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1386 si->applet.st0 = STAT_CLI_PRINT;
1387 return 1;
1388 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001389 return 1;
1390 }
1391 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001392 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1393 si->applet.st0 = STAT_CLI_PRINT;
1394 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001395 }
1396 }
1397 else if (strcmp(args[0], "disable") == 0) {
1398 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001399 struct server *sv;
1400
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001401 sv = expect_server_admin(s, si, args[2]);
1402 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001403 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001404
Cyril Bontécd19e512010-01-31 22:34:03 +01001405 if (! (sv->state & SRV_MAINTAIN)) {
1406 /* Not already in maintenance, we can change the server state */
1407 sv->state |= SRV_MAINTAIN;
1408 set_server_down(sv);
1409 }
1410
Willy Tarreau532a4502011-09-07 22:37:44 +02001411 return 1;
1412 }
1413 else if (strcmp(args[1], "frontend") == 0) {
1414 struct proxy *px;
1415
1416 px = expect_frontend_admin(s, si, args[2]);
1417 if (!px)
1418 return 1;
1419
1420 if (px->state == PR_STSTOPPED) {
1421 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1422 si->applet.st0 = STAT_CLI_PRINT;
1423 return 1;
1424 }
1425
1426 if (px->state == PR_STPAUSED) {
1427 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1428 si->applet.st0 = STAT_CLI_PRINT;
1429 return 1;
1430 }
1431
1432 if (!pause_proxy(px)) {
1433 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1434 si->applet.st0 = STAT_CLI_PRINT;
1435 return 1;
1436 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001437 return 1;
1438 }
1439 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001440 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1441 si->applet.st0 = STAT_CLI_PRINT;
1442 return 1;
1443 }
1444 }
1445 else if (strcmp(args[0], "shutdown") == 0) {
1446 if (strcmp(args[1], "frontend") == 0) {
1447 struct proxy *px;
1448
1449 px = expect_frontend_admin(s, si, args[2]);
1450 if (!px)
1451 return 1;
1452
1453 if (px->state == PR_STSTOPPED) {
1454 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1455 si->applet.st0 = STAT_CLI_PRINT;
1456 return 1;
1457 }
1458
1459 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1460 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1461 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1462 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1463 stop_proxy(px);
1464 return 1;
1465 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001466 else if (strcmp(args[1], "session") == 0) {
1467 struct session *sess, *ptr;
1468
Willy Tarreau290e63a2012-09-20 18:07:14 +02001469 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001470 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1471 si->applet.st0 = STAT_CLI_PRINT;
1472 return 1;
1473 }
1474
1475 if (!*args[2]) {
1476 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1477 si->applet.st0 = STAT_CLI_PRINT;
1478 return 1;
1479 }
1480
1481 ptr = (void *)strtoul(args[2], NULL, 0);
1482
1483 /* first, look for the requested session in the session table */
1484 list_for_each_entry(sess, &sessions, list) {
1485 if (sess == ptr)
1486 break;
1487 }
1488
1489 /* do we have the session ? */
1490 if (sess != ptr) {
1491 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1492 si->applet.st0 = STAT_CLI_PRINT;
1493 return 1;
1494 }
1495
1496 session_shutdown(sess, SN_ERR_KILLED);
1497 return 1;
1498 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001499 else if (strcmp(args[1], "sessions") == 0) {
1500 if (strcmp(args[2], "server") == 0) {
1501 struct server *sv;
1502 struct session *sess, *sess_bck;
1503
1504 sv = expect_server_admin(s, si, args[3]);
1505 if (!sv)
1506 return 1;
1507
1508 /* kill all the session that are on this server */
1509 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1510 if (sess->srv_conn == sv)
1511 session_shutdown(sess, SN_ERR_KILLED);
1512
1513 return 1;
1514 }
1515 else {
1516 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1517 si->applet.st0 = STAT_CLI_PRINT;
1518 return 1;
1519 }
1520 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001521 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001522 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001523 si->applet.st0 = STAT_CLI_PRINT;
1524 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001525 }
1526 }
1527 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001528 return 0;
1529 }
1530 return 1;
1531}
1532
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001533/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001534 * used to processes I/O from/to the stats unix socket. The system relies on a
1535 * state machine handling requests and various responses. We read a request,
1536 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001537 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1538 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001539 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001541static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001542{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001543 struct channel *req = si->ob;
1544 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 int reql;
1546 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001547
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001548 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1549 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001550
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001551 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001552 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001553 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001554 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001555 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001556 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001557 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001558 /* Let's close for real now. We just close the request
1559 * side, the conditions below will complete if needed.
1560 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001561 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001562 break;
1563 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001564 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001565 /* ensure we have some output room left in the event we
1566 * would want to return some info right after parsing.
1567 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001568 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001569 break;
1570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001571 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 if (reql <= 0) { /* closed or EOL not found */
1573 if (reql == 0)
1574 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001575 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001576 continue;
1577 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001578
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001579 /* seek for a possible semi-colon. If we find one, we
1580 * replace it with an LF and skip only this part.
1581 */
1582 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001583 if (trash.str[len] == ';') {
1584 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001585 reql = len + 1;
1586 break;
1587 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001588
Willy Tarreau816fc222009-10-04 07:36:58 +02001589 /* now it is time to check that we have a full line,
1590 * remove the trailing \n and possibly \r, then cut the
1591 * line.
1592 */
1593 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001594 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001595 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001596 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001597 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001598
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001599 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001600 len--;
1601
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001602 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001603
Willy Tarreaubc4af052011-02-13 13:25:14 +01001604 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001605 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001606 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001607 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001608 continue;
1609 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001610 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001611 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001612 else if (strcmp(trash.str, "help") == 0 ||
1613 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001614 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001615 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001616 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001617 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001618 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001619 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001620 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001621 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001622 /* if prompt is disabled, print help on empty lines,
1623 * so that the user at least knows how to enable
1624 * prompt and find help.
1625 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001626 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001627 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001628 }
1629
1630 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001631 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001632 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001633 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001634 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001635 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001636 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001637 continue;
1638 }
1639
Willy Tarreaubc4af052011-02-13 13:25:14 +01001640 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001641 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001642 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001643 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001644 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001645 case STAT_CLI_O_INFO:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001646 if (stats_dump_raw_info_to_buffer(si))
1647 si->applet.st0 = STAT_CLI_PROMPT;
1648 break;
1649 case STAT_CLI_O_STAT:
1650 if (stats_dump_raw_stat_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001651 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001652 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001653 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001654 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001655 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001656 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001657 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001658 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001659 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001660 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001661 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001662 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001663 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001664 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001665 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001666 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001667 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001668 break;
1669 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001670
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001671 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001672 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001673 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001674 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001675 }
1676
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001677 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001678 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001679 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001680
1681 /* Now we close the output if one of the writers did so,
1682 * or if we're not in interactive mode and the request
1683 * buffer is empty. This still allows pipelined requests
1684 * to be sent in non-interactive mode.
1685 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001686 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001687 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001688 continue;
1689 }
1690
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001691 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001692 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001693 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001694 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001695
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001696 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001697 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1698 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001699 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001700 * and nothing more to consume. This is comparable to a broken pipe, so
1701 * we forward the close to the request side so that it flows upstream to
1702 * the client.
1703 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001704 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001705 }
1706
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001707 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001708 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1709 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1710 /* We have no more processing to do, and nothing more to send, and
1711 * the client side has closed. So we'll forward this state downstream
1712 * on the response buffer.
1713 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001714 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001715 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001716 }
1717
1718 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001719 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001720
1721 /* we don't want to expire timeouts while we're processing requests */
1722 si->ib->rex = TICK_ETERNITY;
1723 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001724
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001725 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001726 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 +02001727 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001728 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 +02001729
1730 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1731 /* check that we have released everything then unregister */
1732 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001733 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001734}
1735
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001736/* Dumps the raw stats information block to the trash for and uses the state from
1737 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1738 */
1739static void stats_dump_raw_info(struct stream_interface *si)
1740{
1741 unsigned int up = (now.tv_sec - start_date.tv_sec);
1742
1743 chunk_appendf(&trash,
1744 "Name: " PRODUCT_NAME "\n"
1745 "Version: " HAPROXY_VERSION "\n"
1746 "Release_date: " HAPROXY_DATE "\n"
1747 "Nbproc: %d\n"
1748 "Process_num: %d\n"
1749 "Pid: %d\n"
1750 "Uptime: %dd %dh%02dm%02ds\n"
1751 "Uptime_sec: %d\n"
1752 "Memmax_MB: %d\n"
1753 "Ulimit-n: %d\n"
1754 "Maxsock: %d\n"
1755 "Maxconn: %d\n"
1756 "Hard_maxconn: %d\n"
1757 "Maxpipes: %d\n"
1758 "CurrConns: %d\n"
1759 "PipesUsed: %d\n"
1760 "PipesFree: %d\n"
1761 "ConnRate: %d\n"
1762 "ConnRateLimit: %d\n"
1763 "MaxConnRate: %d\n"
1764 "CompressBpsIn: %u\n"
1765 "CompressBpsOut: %u\n"
1766 "CompressBpsRateLim: %u\n"
1767#ifdef USE_ZLIB
1768 "ZlibMemUsage: %ld\n"
1769 "MaxZlibMemUsage: %ld\n"
1770#endif
1771 "Tasks: %d\n"
1772 "Run_queue: %d\n"
1773 "Idle_pct: %d\n"
1774 "node: %s\n"
1775 "description: %s\n"
1776 "",
1777 global.nbproc,
1778 relative_pid,
1779 pid,
1780 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1781 up,
1782 global.rlimit_memmax,
1783 global.rlimit_nofile,
1784 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1785 actconn, pipes_used, pipes_free,
1786 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1787 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1788 global.comp_rate_lim,
1789#ifdef USE_ZLIB
1790 zlib_used_memory, global.maxzlibmem,
1791#endif
1792 nb_tasks_cur, run_queue_cur, idle_pct,
1793 global.node, global.desc ? global.desc : ""
1794 );
1795}
1796
1797/* This function dumps information onto the stream interface's read buffer.
1798 * It returns 0 as long as it does not complete, non-zero upon completion.
1799 * No state is used.
1800 */
1801static int stats_dump_raw_info_to_buffer(struct stream_interface *si)
1802{
1803 chunk_reset(&trash);
1804 stats_dump_raw_info(si);
1805
1806 if (bi_putchk(si->ib, &trash) == -1)
1807 return 0;
1808
1809 return 1;
1810}
1811
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001812/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001813 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001814 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001815 * Only states STAT_ST_INIT and STAT_ST_LIST are used.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001816 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001817static int stats_dump_raw_stat_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001818{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001819 struct proxy *px;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001820
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001821 chunk_reset(&trash);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001822
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001823 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001824 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001825 stats_dump_csv_header();
1826 if (bi_putchk(si->ib, &trash) == -1)
1827 return 0;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001828
Willy Tarreau295a8372011-03-10 11:25:07 +01001829 si->applet.ctx.stats.px = proxy;
1830 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1831 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001832 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001833 /* fall through */
1834
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001835 default:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001836 /* dump proxies */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001837 while (si->applet.ctx.stats.px) {
1838 px = si->applet.ctx.stats.px;
1839 /* skip the disabled proxies, global frontend and non-networked ones */
1840 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
1841 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
1842 if (stats_dump_proxy(si, px, NULL) == 0)
1843 return 0;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001844 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001845 si->applet.ctx.stats.px = px->next;
1846 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001847 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001848 /* here, we just have reached the last proxy */
Willy Tarreau24955a12009-10-04 11:54:04 +02001849 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001850 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001851}
1852
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001853/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1854 * the state from stream interface <si>. The caller is responsible for clearing
1855 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001856 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001857static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001858{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001859 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001861 if (!(px->cap & PR_CAP_FE))
1862 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001863
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001864 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1865 return 0;
1866
1867 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001868 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001869 /* name, queue */
1870 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001871
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001872 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1873 /* Column sub-heading for Enable or Disable server */
1874 chunk_appendf(&trash, "<td></td>");
1875 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001876
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001877 chunk_appendf(&trash,
1878 "<td class=ac>"
1879 "<a name=\"%s/Frontend\"></a>"
1880 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1881 "<td colspan=3></td>"
1882 "",
1883 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001884
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001885 if (px->mode == PR_MODE_HTTP)
1886 chunk_appendf(&trash,
1887 /* sessions rate : current, max, limit */
1888 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
1889 "",
1890 read_freq_ctr(&px->fe_req_per_sec),
1891 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1892 px->fe_counters.p.http.rps_max,
1893 U2H1(px->fe_counters.sps_max),
1894 LIM2A2(px->fe_sps_lim, "-"));
1895 else
1896 chunk_appendf(&trash,
1897 /* sessions rate : current, max, limit */
1898 "<td>%s</td><td>%s</td><td>%s</td>"
1899 "",
1900 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1901 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001902
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001903 chunk_appendf(&trash,
1904 /* sessions: current, max, limit */
1905 "<td>%s</td><td>%s</td><td>%s</td>"
1906 "<td"
1907 "",
1908 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001910 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1911 if (px->mode == PR_MODE_HTTP) {
1912 chunk_appendf(&trash, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001913
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001914 for (i = 1; i < 6; i++)
1915 chunk_appendf(&trash, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001916
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001917 chunk_appendf(&trash, " other=%lld,", px->fe_counters.p.http.rsp[0]);
1918 chunk_appendf(&trash, " compressed=%lld (%d%%)",
1919 px->fe_counters.p.http.comp_rsp,
1920 px->fe_counters.p.http.rsp[2] ?
1921 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0);
1922 chunk_appendf(&trash, " intercepted=%lld\"", px->fe_counters.intercepted_req);
1923 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001924
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001925 chunk_appendf(&trash,
1926 /* sessions: total, lbtot */
1927 ">%s%s%s</td><td></td>"
1928 /* bytes : in */
1929 "<td>%s</td><td"
1930 "",
1931 (px->mode == PR_MODE_HTTP)?"<u>":"",
1932 U2H6(px->fe_counters.cum_sess),
1933 (px->mode == PR_MODE_HTTP)?"</u>":"",
1934 U2H7(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001935
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001936 /* compression stats (via td title): comp_in, comp_out, comp_byp */
1937 chunk_appendf(&trash, " title=\"compression: in=%lld out=%lld bypassed=%lld savings=%d%%\"",
1938 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1939 px->fe_counters.comp_in ?
1940 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001941
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001942 chunk_appendf(&trash,
1943 /* bytes: out */
1944 ">%s%s%s</td>"
1945 "",
1946 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
1947 U2H0(px->fe_counters.bytes_out),
1948 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001949
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001950 chunk_appendf(&trash,
1951 /* denied: req, resp */
1952 "<td>%s</td><td>%s</td>"
1953 /* errors : request, connect, response */
1954 "<td>%s</td><td></td><td></td>"
1955 /* warnings: retries, redispatches */
1956 "<td></td><td></td>"
1957 /* server status : reflect frontend status */
1958 "<td class=ac>%s</td>"
1959 /* rest of server: nothing */
1960 "<td class=ac colspan=8></td></tr>"
1961 "",
1962 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
1963 U2H2(px->fe_counters.failed_req),
1964 px->state == PR_STREADY ? "OPEN" :
1965 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001966 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001967 else { /* CSV mode */
1968 chunk_appendf(&trash,
1969 /* pxid, name, queue cur, queue max, */
1970 "%s,FRONTEND,,,"
1971 /* sessions : current, max, limit, total */
1972 "%d,%d,%d,%lld,"
1973 /* bytes : in, out */
1974 "%lld,%lld,"
1975 /* denied: req, resp */
1976 "%lld,%lld,"
1977 /* errors : request, connect, response */
1978 "%lld,,,"
1979 /* warnings: retries, redispatches */
1980 ",,"
1981 /* server status : reflect frontend status */
1982 "%s,"
1983 /* rest of server: nothing */
1984 ",,,,,,,,"
1985 /* pid, iid, sid, throttle, lbtot, tracked, type */
1986 "%d,%d,0,,,,%d,"
1987 /* rate, rate_lim, rate_max */
1988 "%u,%u,%u,"
1989 /* check_status, check_code, check_duration */
1990 ",,,",
1991 px->id,
1992 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1993 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1994 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1995 px->fe_counters.failed_req,
1996 px->state == PR_STREADY ? "OPEN" :
1997 px->state == PR_STFULL ? "FULL" : "STOP",
1998 relative_pid, px->uuid, STATS_TYPE_FE,
1999 read_freq_ctr(&px->fe_sess_per_sec),
2000 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002001
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002002 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2003 if (px->mode == PR_MODE_HTTP) {
2004 for (i=1; i<6; i++)
2005 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2006 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2007 }
2008 else
2009 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002010
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002011 /* failed health analyses */
2012 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002013
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002014 /* requests : req_rate, req_rate_max, req_tot, */
2015 chunk_appendf(&trash, "%u,%u,%lld,",
2016 read_freq_ctr(&px->fe_req_per_sec),
2017 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2018
2019 /* errors: cli_aborts, srv_aborts */
2020 chunk_appendf(&trash, ",,");
2021
2022 /* compression: in, out, bypassed */
2023 chunk_appendf(&trash, "%lld,%lld,%lld,",
2024 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2025
2026 /* compression: comp_rsp */
2027 chunk_appendf(&trash, "%lld,",
2028 px->fe_counters.p.http.comp_rsp);
2029
2030 /* finish with EOL */
2031 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002032 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002033 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002034}
2035
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002036/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2037 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2038 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2039 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002040 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002041static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002042{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002043 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2044 chunk_appendf(&trash, "<tr class=socket>");
2045 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2046 /* Column sub-heading for Enable or Disable server */
2047 chunk_appendf(&trash, "<td></td>");
2048 }
2049 chunk_appendf(&trash, "<td class=ac");
Willy Tarreau91861262007-10-17 17:06:05 +02002050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002051 if (flags & ST_SHLGNDS) {
2052 char str[INET6_ADDRSTRLEN];
2053 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002054
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002055 chunk_appendf(&trash, " title=\"");
Willy Tarreau91861262007-10-17 17:06:05 +02002056
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002057 port = get_host_port(&l->addr);
2058 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2059 case AF_INET:
2060 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2061 break;
2062 case AF_INET6:
2063 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2064 break;
2065 case AF_UNIX:
2066 chunk_appendf(&trash, "unix, ");
2067 break;
2068 case -1:
2069 chunk_appendf(&trash, "(%s), ", strerror(errno));
2070 break;
2071 }
Willy Tarreau91861262007-10-17 17:06:05 +02002072
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002073 /* id */
2074 chunk_appendf(&trash, "id: %d\"", l->luid);
2075 }
Willy Tarreau91861262007-10-17 17:06:05 +02002076
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002077 chunk_appendf(&trash,
2078 /* name, queue */
2079 ">%s<a name=\"%s/+%s\"></a>"
2080 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
2081 /* sessions rate: current, max, limit */
2082 "<td colspan=3>&nbsp;</td>"
2083 /* sessions: current, max, limit, total, lbtot */
2084 "<td>%s</td><td>%s</td><td>%s</td>"
2085 "<td>%s</td><td>&nbsp;</td>"
2086 /* bytes: in, out */
2087 "<td>%s</td><td>%s</td>"
2088 "",
2089 (flags & ST_SHLGNDS)?"<u>":"",
2090 px->id, l->name, px->id, l->name, l->name,
2091 (flags & ST_SHLGNDS)?"</u>":"",
2092 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2093 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002094
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002095 chunk_appendf(&trash,
2096 /* denied: req, resp */
2097 "<td>%s</td><td>%s</td>"
2098 /* errors: request, connect, response */
2099 "<td>%s</td><td></td><td></td>"
2100 /* warnings: retries, redispatches */
2101 "<td></td><td></td>"
2102 /* server status: reflect listener status */
2103 "<td class=ac>%s</td>"
2104 /* rest of server: nothing */
2105 "<td class=ac colspan=8></td></tr>"
2106 "",
2107 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2108 U2H2(l->counters->failed_req),
2109 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2110 }
2111 else { /* CSV mode */
2112 chunk_appendf(&trash,
2113 /* pxid, name, queue cur, queue max, */
2114 "%s,%s,,,"
2115 /* sessions: current, max, limit, total */
2116 "%d,%d,%d,%lld,"
2117 /* bytes: in, out */
2118 "%lld,%lld,"
2119 /* denied: req, resp */
2120 "%lld,%lld,"
2121 /* errors: request, connect, response */
2122 "%lld,,,"
2123 /* warnings: retries, redispatches */
2124 ",,"
2125 /* server status: reflect listener status */
2126 "%s,"
2127 /* rest of server: nothing */
2128 ",,,,,,,,"
2129 /* pid, iid, sid, throttle, lbtot, tracked, type */
2130 "%d,%d,%d,,,,%d,"
2131 /* rate, rate_lim, rate_max */
2132 ",,,"
2133 /* check_status, check_code, check_duration */
2134 ",,,"
2135 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2136 ",,,,,,"
2137 /* failed health analyses */
2138 ","
2139 /* requests : req_rate, req_rate_max, req_tot, */
2140 ",,,"
2141 /* errors: cli_aborts, srv_aborts */
2142 ",,"
2143 /* compression: in, out, bypassed, comp_rsp */
2144 ",,,,"
2145 "\n",
2146 px->id, l->name,
2147 l->nbconn, l->counters->conn_max,
2148 l->maxconn, l->counters->cum_conn,
2149 l->counters->bytes_in, l->counters->bytes_out,
2150 l->counters->denied_req, l->counters->denied_resp,
2151 l->counters->failed_req,
2152 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2153 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2154 }
2155 return 1;
2156}
Willy Tarreau91861262007-10-17 17:06:05 +02002157
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002158/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2159 * from stream interface <si>, stats flags <flags>, and server state <state>.
2160 * The caller is responsible for clearing the trash if needed. Returns non-zero
2161 * if it emits anything, zero otherwise. The <state> parameter can take the
2162 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked.
2163 */
2164static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2165{
2166 struct server *ref = sv->track ? sv->track : sv;
2167 char str[INET6_ADDRSTRLEN];
2168 struct chunk src;
2169 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002171 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) { /* HTML mode */
2172 static char *srv_hlt_st[7] = {
2173 "DOWN",
2174 "DN %d/%d &uarr;",
2175 "UP %d/%d &darr;",
2176 "UP",
2177 "NOLB %d/%d &darr;",
2178 "NOLB",
2179 "<i>no check</i>"
2180 };
Willy Tarreau91861262007-10-17 17:06:05 +02002181
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002182 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2183 chunk_appendf(&trash, "<tr class=\"maintain\">");
2184 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002185 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002186 "<tr class=\"%s%d\">",
2187 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002188
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002189 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002190 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002191 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2192 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002193
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002194 chunk_appendf(&trash, "<td class=ac");
Willy Tarreau91861262007-10-17 17:06:05 +02002195
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002196 if (flags & ST_SHLGNDS) {
2197 chunk_appendf(&trash, " title=\"");
2198
2199 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2200 case AF_INET:
2201 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2202 break;
2203 case AF_INET6:
2204 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2205 break;
2206 case AF_UNIX:
2207 chunk_appendf(&trash, "unix, ");
2208 break;
2209 case -1:
2210 chunk_appendf(&trash, "(%s), ", strerror(errno));
2211 break;
2212 default: /* address family not supported */
2213 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002214 }
Willy Tarreau91861262007-10-17 17:06:05 +02002215
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002216 /* id */
2217 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002218
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002219 /* cookie */
2220 if (sv->cookie) {
2221 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002222
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002223 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2224 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002225
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002226 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002227 }
2228
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002229 chunk_appendf(&trash, "\"");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002230 }
Willy Tarreau91861262007-10-17 17:06:05 +02002231
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002232 chunk_appendf(&trash,
2233 ">%s<a name=\"%s/%s\"></a>"
2234 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
2235 /* queue : current, max, limit */
2236 "<td>%s</td><td>%s</td><td>%s</td>"
2237 /* sessions rate : current, max, limit */
2238 "<td>%s</td><td>%s</td><td></td>"
2239 /* sessions: current, max, limit */
2240 "<td>%s</td><td>%s</td><td>%s</td>"
2241 "<td"
2242 "",
2243 (flags & ST_SHLGNDS) ? "<u>" : "",
2244 px->id, sv->id, px->id, sv->id, sv->id,
2245 (flags & ST_SHLGNDS) ? "</u>" : "",
2246 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2247 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
2248 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
Willy Tarreau91861262007-10-17 17:06:05 +02002249
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002250 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2251 if (px->mode == PR_MODE_HTTP) {
2252 chunk_appendf(&trash, " title=\"rsp codes:");
Willy Tarreau91861262007-10-17 17:06:05 +02002253
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002254 for (i = 1; i < 6; i++)
2255 chunk_appendf(&trash, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2256
2257 chunk_appendf(&trash, " other=%lld\"", sv->counters.p.http.rsp[0]);
Willy Tarreau91861262007-10-17 17:06:05 +02002258 }
Willy Tarreau91861262007-10-17 17:06:05 +02002259
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002260 chunk_appendf(&trash,
2261 /* sessions: total, lbtot */
2262 ">%s%s%s</td><td>%s</td>",
2263 (px->mode == PR_MODE_HTTP)?"<u>":"",
2264 U2H0(sv->counters.cum_sess),
2265 (px->mode == PR_MODE_HTTP)?"</u>":"",
2266 U2H1(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002267
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002268 chunk_appendf(&trash,
2269 /* bytes : in, out */
2270 "<td>%s</td><td>%s</td>"
2271 /* denied: req, resp */
2272 "<td></td><td>%s</td>"
2273 /* errors : request, connect */
2274 "<td></td><td>%s</td>"
2275 /* errors : response */
2276 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
2277 /* warnings: retries, redispatches */
2278 "<td>%lld</td><td>%lld</td>"
2279 "",
2280 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2281 U2H2(sv->counters.failed_secu),
2282 U2H3(sv->counters.failed_conns),
2283 sv->counters.cli_aborts,
2284 sv->counters.srv_aborts,
2285 U2H6(sv->counters.failed_resp),
2286 sv->counters.retries, sv->counters.redispatches);
2287
2288 /* status, lest check */
2289 chunk_appendf(&trash, "<td class=ac>");
2290
2291 if (sv->state & SRV_MAINTAIN) {
2292 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2293 chunk_appendf(&trash, "MAINT");
2294 }
2295 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2296 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2297 chunk_appendf(&trash, "MAINT(via)");
2298 }
2299 else if (ref->state & SRV_CHECKED) {
2300 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2301 chunk_appendf(&trash,
2302 srv_hlt_st[state],
2303 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2304 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002305 }
Willy Tarreau91861262007-10-17 17:06:05 +02002306
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002307 if (sv->state & SRV_CHECKED) {
2308 chunk_appendf(&trash, "</td><td class=ac title=\"%s",
2309 get_check_status_description(sv->check.status));
Willy Tarreau91861262007-10-17 17:06:05 +02002310
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002311 if (*sv->check.desc) {
2312 chunk_appendf(&trash, ": ");
2313 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2314 chunk_htmlencode(&trash, &src);
2315 }
Willy Tarreau91861262007-10-17 17:06:05 +02002316
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002317 chunk_appendf(&trash, "\"><u> %s%s",
2318 (sv->state & SRV_CHK_RUNNING) ? "* " : "",
2319 get_check_status_info(sv->check.status));
2320
2321 if (sv->check.status >= HCHK_STATUS_L57DATA)
2322 chunk_appendf(&trash, "/%d", sv->check.code);
2323
2324 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
2325 chunk_appendf(&trash, " in %lums</u>", sv->check.duration);
2326 }
2327 else
2328 chunk_appendf(&trash, "</td><td>");
2329
2330 chunk_appendf(&trash,
2331 /* weight */
2332 "</td><td class=ac>%d</td>"
2333 /* act, bck */
2334 "<td class=ac>%s</td><td class=ac>%s</td>"
2335 "",
2336 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2337 (sv->state & SRV_BACKUP) ? "-" : "Y",
2338 (sv->state & SRV_BACKUP) ? "Y" : "-");
2339
2340 /* check failures: unique, fatal, down time */
2341 if (sv->state & SRV_CHECKED) {
2342 chunk_appendf(&trash, "<td title=\"Failed Health Checks%s\"><u>%lld",
2343 ref->observe?"/Health Analyses":"", ref->counters.failed_checks);
2344
2345 if (ref->observe)
2346 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2347
2348 chunk_appendf(&trash,
2349 "</u></td>"
2350 "<td>%lld</td><td>%s</td>"
2351 "",
2352 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2353 }
2354 else if (sv != ref)
2355 chunk_appendf(&trash,
2356 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2357 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2358 else
2359 chunk_appendf(&trash, "<td colspan=3></td>");
2360
2361 /* throttle */
2362 if ((sv->state & SRV_WARMINGUP) &&
2363 now.tv_sec < sv->last_change + sv->slowstart &&
2364 now.tv_sec >= sv->last_change) {
2365 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n",
2366 (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2367 }
2368 else
2369 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2370 }
2371 else { /* CSV mode */
2372 static char *srv_hlt_st[7] = {
2373 "DOWN,",
2374 "DOWN %d/%d,",
2375 "UP %d/%d,",
2376 "UP,",
2377 "NOLB %d/%d,",
2378 "NOLB,",
2379 "no check,"
2380 };
2381
2382 chunk_appendf(&trash,
2383 /* pxid, name */
2384 "%s,%s,"
2385 /* queue : current, max */
2386 "%d,%d,"
2387 /* sessions : current, max, limit, total */
2388 "%d,%d,%s,%lld,"
2389 /* bytes : in, out */
2390 "%lld,%lld,"
2391 /* denied: req, resp */
2392 ",%lld,"
2393 /* errors : request, connect, response */
2394 ",%lld,%lld,"
2395 /* warnings: retries, redispatches */
2396 "%lld,%lld,"
2397 "",
2398 px->id, sv->id,
2399 sv->nbpend, sv->counters.nbpend_max,
2400 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
2401 sv->counters.bytes_in, sv->counters.bytes_out,
2402 sv->counters.failed_secu,
2403 sv->counters.failed_conns, sv->counters.failed_resp,
2404 sv->counters.retries, sv->counters.redispatches);
2405
2406 /* status */
2407 if (sv->state & SRV_MAINTAIN)
2408 chunk_appendf(&trash, "MAINT,");
2409 else if (ref != sv && ref->state & SRV_MAINTAIN)
2410 chunk_appendf(&trash, "MAINT(via),");
2411 else
2412 chunk_appendf(&trash,
2413 srv_hlt_st[state],
2414 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2415 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
2416
2417 chunk_appendf(&trash,
2418 /* weight, active, backup */
2419 "%d,%d,%d,"
2420 "",
2421 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2422 (sv->state & SRV_BACKUP) ? 0 : 1,
2423 (sv->state & SRV_BACKUP) ? 1 : 0);
2424
2425 /* check failures: unique, fatal; last change, total downtime */
2426 if (sv->state & SRV_CHECKED)
2427 chunk_appendf(&trash,
2428 "%lld,%lld,%d,%d,",
2429 sv->counters.failed_checks, sv->counters.down_trans,
2430 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2431 else
2432 chunk_appendf(&trash, ",,,,");
2433
2434 /* queue limit, pid, iid, sid, */
2435 chunk_appendf(&trash,
2436 "%s,"
2437 "%d,%d,%d,",
2438 LIM2A0(sv->maxqueue, ""),
2439 relative_pid, px->uuid, sv->puid);
2440
2441 /* throttle */
2442 if ((sv->state & SRV_WARMINGUP) &&
2443 now.tv_sec < sv->last_change + sv->slowstart &&
2444 now.tv_sec >= sv->last_change)
2445 chunk_appendf(&trash, "%d", (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2446
2447 /* sessions: lbtot */
2448 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2449
2450 /* tracked */
2451 if (sv->track)
2452 chunk_appendf(&trash, "%s/%s,",
2453 sv->track->proxy->id, sv->track->id);
2454 else
2455 chunk_appendf(&trash, ",");
2456
2457 /* type */
2458 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2459
2460 /* rate */
2461 chunk_appendf(&trash, "%u,,%u,",
2462 read_freq_ctr(&sv->sess_per_sec),
2463 sv->counters.sps_max);
2464
2465 if (sv->state & SRV_CHECKED) {
2466 /* check_status */
2467 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2468
2469 /* check_code */
2470 if (sv->check.status >= HCHK_STATUS_L57DATA)
2471 chunk_appendf(&trash, "%u,", sv->check.code);
2472 else
2473 chunk_appendf(&trash, ",");
2474
2475 /* check_duration */
2476 if (sv->check.status >= HCHK_STATUS_CHECKED)
2477 chunk_appendf(&trash, "%lu,", sv->check.duration);
2478 else
2479 chunk_appendf(&trash, ",");
2480
2481 }
2482 else
2483 chunk_appendf(&trash, ",,,");
2484
2485 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2486 if (px->mode == PR_MODE_HTTP) {
2487 for (i=1; i<6; i++)
2488 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2489
2490 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2491 }
2492 else
2493 chunk_appendf(&trash, ",,,,,,");
2494
2495 /* failed health analyses */
2496 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2497
2498 /* requests : req_rate, req_rate_max, req_tot, */
2499 chunk_appendf(&trash, ",,,");
2500
2501 /* errors: cli_aborts, srv_aborts */
2502 chunk_appendf(&trash, "%lld,%lld,",
2503 sv->counters.cli_aborts, sv->counters.srv_aborts);
2504
2505 /* compression: in, out, bypassed, comp_rsp */
2506 chunk_appendf(&trash, ",,,,");
2507
2508 /* finish with EOL */
2509 chunk_appendf(&trash, "\n");
2510 }
2511 return 1;
2512}
2513
2514/* Dumps a line for backend <px> to the trash for and uses the state from stream
2515 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2516 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2517 */
2518static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2519{
2520 struct chunk src;
2521 int i;
2522
2523 if (!(px->cap & PR_CAP_BE))
2524 return 0;
2525
2526 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2527 return 0;
2528
2529 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) { /* HTML mode */
2530 chunk_appendf(&trash, "<tr class=\"backend\">");
2531 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2532 /* Column sub-heading for Enable or Disable server */
2533 chunk_appendf(&trash, "<td></td>");
2534 }
2535 chunk_appendf(&trash, "<td class=ac");
2536
2537 if (flags & ST_SHLGNDS) {
2538 /* balancing */
2539 chunk_appendf(&trash, " title=\"balancing: %s",
2540 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2541
2542 /* cookie */
2543 if (px->cookie_name) {
2544 chunk_appendf(&trash, ", cookie: '");
2545 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2546 chunk_htmlencode(&trash, &src);
2547 chunk_appendf(&trash, "'");
2548 }
2549 chunk_appendf(&trash, "\"");
2550 }
2551
2552 chunk_appendf(&trash,
2553 /* name */
2554 ">%s<a name=\"%s/Backend\"></a>"
2555 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
2556 /* queue : current, max */
2557 "<td>%s</td><td>%s</td><td></td>"
2558 /* sessions rate : current, max, limit */
2559 "<td>%s</td><td>%s</td><td></td>"
2560 "",
2561 (flags & ST_SHLGNDS)?"<u>":"",
2562 px->id, px->id,
2563 (flags & ST_SHLGNDS)?"</u>":"",
2564 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2565 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
2566
2567 chunk_appendf(&trash,
2568 /* sessions: current, max, limit */
2569 "<td>%s</td><td>%s</td><td>%s</td>"
2570 "<td"
2571 "",
2572 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
2573
2574 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2575 if (px->mode == PR_MODE_HTTP) {
2576 chunk_appendf(&trash, " title=\"%lld requests:", px->be_counters.p.http.cum_req);
2577
2578 for (i = 1; i < 6; i++)
2579 chunk_appendf(&trash, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
2580
2581 chunk_appendf(&trash, " other=%lld ", px->be_counters.p.http.rsp[0]);
2582 chunk_appendf(&trash, " compressed=%lld (%d%%)\"",
2583 px->be_counters.p.http.comp_rsp,
2584 px->be_counters.p.http.rsp[2] ?
2585 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0);
2586 }
2587
2588 chunk_appendf(&trash,
2589 /* sessions: total, lbtot */
2590 ">%s%s%s</td><td>%s</td>"
2591 /* bytes: in */
2592 "<td>%s</td><td"
2593 "",
2594 (px->mode == PR_MODE_HTTP)?"<u>":"",
2595 U2H6(px->be_counters.cum_conn),
2596 (px->mode == PR_MODE_HTTP)?"</u>":"",
2597 U2H7(px->be_counters.cum_lbconn),
2598 U2H8(px->be_counters.bytes_in));
2599
2600 /* compression stats (via td title): comp_in, comp_out, comp_byp */
2601 chunk_appendf(&trash, " title=\"compression: in=%lld out=%lld bypassed=%lld savings=%d%%\"",
2602 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2603 px->be_counters.comp_in ?
2604 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0);
2605
2606 chunk_appendf(&trash,
2607 /* bytes: out */
2608 ">%s%s%s</td>"
2609 "",
2610 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
2611 U2H0(px->be_counters.bytes_out),
2612 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2613
2614 chunk_appendf(&trash,
2615 /* denied: req, resp */
2616 "<td>%s</td><td>%s</td>"
2617 /* errors : request, connect */
2618 "<td></td><td>%s</td>"
2619 /* errors : response */
2620 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
2621 /* warnings: retries, redispatches */
2622 "<td>%lld</td><td>%lld</td>"
2623 /* backend status: reflect backend status (up/down): we display UP
2624 * if the backend has known working servers or if it has no server at
2625 * all (eg: for stats). Then we display the total weight, number of
2626 * active and backups. */
2627 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2628 "<td class=ac>%d</td><td class=ac>%d</td>"
2629 "",
2630 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2631 U2H2(px->be_counters.failed_conns),
2632 px->be_counters.cli_aborts,
2633 px->be_counters.srv_aborts,
2634 U2H5(px->be_counters.failed_resp),
2635 px->be_counters.retries, px->be_counters.redispatches,
2636 human_time(now.tv_sec - px->last_change, 1),
2637 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2638 "<font color=\"red\"><b>DOWN</b></font>",
2639 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2640 px->srv_act, px->srv_bck);
2641
2642 chunk_appendf(&trash,
2643 /* rest of backend: nothing, down transitions, total downtime, throttle */
2644 "<td class=ac>&nbsp;</td><td>%d</td>"
2645 "<td>%s</td>"
2646 "<td></td>"
2647 "</tr>",
2648 px->down_trans,
2649 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2650 }
2651 else { /* CSV mode */
2652 chunk_appendf(&trash,
2653 /* pxid, name */
2654 "%s,BACKEND,"
2655 /* queue : current, max */
2656 "%d,%d,"
2657 /* sessions : current, max, limit, total */
2658 "%d,%d,%d,%lld,"
2659 /* bytes : in, out */
2660 "%lld,%lld,"
2661 /* denied: req, resp */
2662 "%lld,%lld,"
2663 /* errors : request, connect, response */
2664 ",%lld,%lld,"
2665 /* warnings: retries, redispatches */
2666 "%lld,%lld,"
2667 /* backend status: reflect backend status (up/down): we display UP
2668 * if the backend has known working servers or if it has no server at
2669 * all (eg: for stats). Then we display the total weight, number of
2670 * active and backups. */
2671 "%s,"
2672 "%d,%d,%d,"
2673 /* rest of backend: nothing, down transitions, last change, total downtime */
2674 ",%d,%d,%d,,"
2675 /* pid, iid, sid, throttle, lbtot, tracked, type */
2676 "%d,%d,0,,%lld,,%d,"
2677 /* rate, rate_lim, rate_max, */
2678 "%u,,%u,"
2679 /* check_status, check_code, check_duration */
2680 ",,,",
2681 px->id,
2682 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2683 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2684 px->be_counters.bytes_in, px->be_counters.bytes_out,
2685 px->be_counters.denied_req, px->be_counters.denied_resp,
2686 px->be_counters.failed_conns, px->be_counters.failed_resp,
2687 px->be_counters.retries, px->be_counters.redispatches,
2688 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2689 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2690 px->srv_act, px->srv_bck,
2691 px->down_trans, (int)(now.tv_sec - px->last_change),
2692 px->srv?be_downtime(px):0,
2693 relative_pid, px->uuid,
2694 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2695 read_freq_ctr(&px->be_sess_per_sec),
2696 px->be_counters.sps_max);
2697
2698 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2699 if (px->mode == PR_MODE_HTTP) {
2700 for (i=1; i<6; i++)
2701 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2702 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2703 }
2704 else
2705 chunk_appendf(&trash, ",,,,,,");
2706
2707 /* failed health analyses */
2708 chunk_appendf(&trash, ",");
2709
2710 /* requests : req_rate, req_rate_max, req_tot, */
2711 chunk_appendf(&trash, ",,,");
2712
2713 /* errors: cli_aborts, srv_aborts */
2714 chunk_appendf(&trash, "%lld,%lld,",
2715 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2716
2717 /* compression: in, out, bypassed */
2718 chunk_appendf(&trash, "%lld,%lld,%lld,",
2719 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2720
2721 /* compression: comp_rsp */
2722 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2723
2724 /* finish with EOL */
2725 chunk_appendf(&trash, "\n");
2726 }
2727 return 1;
2728}
2729
2730/* Dumps the table header for proxy <px> to the trash for and uses the state from
2731 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2732 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2733 * otherwise.
2734 */
2735static int stats_dump_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
2736{
2737 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
2738 return 0;
2739
2740 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2741 /* A form to enable/disable this proxy servers */
2742 chunk_appendf(&trash,
2743 "<form action=\"%s\" method=\"post\">",
2744 uri->uri_prefix);
2745 }
2746
2747 /* print a new table */
2748 chunk_appendf(&trash,
2749 "<table class=\"tbl\" width=\"100%%\">\n"
2750 "<tr class=\"titre\">"
2751 "<th class=\"pxname\" width=\"10%%\"");
2752
2753 if (uri->flags & ST_SHLGNDS) {
2754 /* cap, mode, id */
2755 chunk_appendf(&trash, " title=\"cap: %s, mode: %s, id: %d",
2756 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2757 px->uuid);
2758 chunk_appendf(&trash, "\"");
2759 }
2760
2761 chunk_appendf(&trash,
2762 ">%s<a name=\"%s\"></a>"
2763 "<a class=px href=\"#%s\">%s</a>%s</th>"
2764 "<th class=\"%s\" width=\"90%%\">%s</th>"
2765 "</tr>\n"
2766 "</table>\n"
2767 "<table class=\"tbl\" width=\"100%%\">\n"
2768 "<tr class=\"titre\">",
2769 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2770 px->id, px->id, px->id,
2771 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2772 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2773
2774 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2775 /* Column heading for Enable or Disable server */
2776 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002777 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778
2779 chunk_appendf(&trash,
2780 "<th rowspan=2></th>"
2781 "<th colspan=3>Queue</th>"
2782 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2783 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2784 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2785 "<th colspan=9>Server</th>"
2786 "</tr>\n"
2787 "<tr class=\"titre\">"
2788 "<th>Cur</th><th>Max</th><th>Limit</th>"
2789 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2790 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2791 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2792 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2793 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2794 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2795 "<th>Thrtle</th>\n"
2796 "</tr>");
2797 return 1;
Willy Tarreau91861262007-10-17 17:06:05 +02002798}
2799
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002800/* Dumps the table trailer for proxy <px> to the trash for and uses the state from
2801 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2802 * Returns non-zero if it emits anything, zero otherwise.
2803 */
2804static int stats_dump_px_end(struct stream_interface *si, struct proxy *px)
2805{
2806 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
2807 return 0;
2808
2809 chunk_appendf(&trash, "</table>");
2810
2811 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2812 /* close the form used to enable/disable this proxy servers */
2813 chunk_appendf(&trash,
2814 "Choose the action to perform on the checked servers : "
2815 "<select name=action>"
2816 "<option value=\"\"></option>"
2817 "<option value=\"disable\">Disable</option>"
2818 "<option value=\"enable\">Enable</option>"
2819 "<option value=\"stop\">Soft Stop</option>"
2820 "<option value=\"start\">Soft Start</option>"
2821 "<option value=\"shutdown\">Kill Sessions</option>"
2822 "</select>"
2823 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2824 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2825 "</form>",
2826 px->uuid);
2827 }
2828
2829 chunk_appendf(&trash, "<p>\n");
2830 return 1;
2831}
Willy Tarreau91861262007-10-17 17:06:05 +02002832
2833/*
2834 * Dumps statistics for a proxy.
2835 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2836 * ot non-zero if everything completed.
2837 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002838static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002839{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002840 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002841 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002842 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002843 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002844
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002845 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002846
Willy Tarreau295a8372011-03-10 11:25:07 +01002847 switch (si->applet.ctx.stats.px_st) {
2848 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002849 /* we are on a new proxy */
2850
2851 if (uri && uri->scope) {
2852 /* we have a limited scope, we have to check the proxy name */
2853 struct stat_scope *scope;
2854 int len;
2855
2856 len = strlen(px->id);
2857 scope = uri->scope;
2858
2859 while (scope) {
2860 /* match exact proxy name */
2861 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2862 break;
2863
2864 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002865 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002866 break;
2867 scope = scope->next;
2868 }
2869
2870 /* proxy name not found : don't dump anything */
2871 if (scope == NULL)
2872 return 1;
2873 }
2874
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002875 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2876 (si->applet.ctx.stats.iid != -1) &&
2877 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002878 return 1;
2879
Willy Tarreau295a8372011-03-10 11:25:07 +01002880 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002881 /* fall through */
2882
Willy Tarreau295a8372011-03-10 11:25:07 +01002883 case STAT_PX_ST_TH:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002884 if (stats_dump_px_hdr(si, px, uri))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002885 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002886 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002887
Willy Tarreau295a8372011-03-10 11:25:07 +01002888 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002889 /* fall through */
2890
Willy Tarreau295a8372011-03-10 11:25:07 +01002891 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002892 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002893 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002894 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002895 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002896
Willy Tarreau4348fad2012-09-20 16:48:07 +02002897 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002898 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002899 /* fall through */
2900
Willy Tarreau295a8372011-03-10 11:25:07 +01002901 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002902 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002903 for (; si->applet.ctx.stats.l != &px->conf.listeners; si->applet.ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002905 return 0;
2906
Willy Tarreau4348fad2012-09-20 16:48:07 +02002907 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002908 if (!l->counters)
2909 continue;
2910
Willy Tarreau295a8372011-03-10 11:25:07 +01002911 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2912 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002913 break;
2914
Willy Tarreau295a8372011-03-10 11:25:07 +01002915 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002916 continue;
2917 }
2918
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002919 /* print the frontend */
2920 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2921 if (bi_putchk(rep, &trash) == -1)
2922 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002923 }
2924
Willy Tarreau295a8372011-03-10 11:25:07 +01002925 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2926 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002927 /* fall through */
2928
Willy Tarreau295a8372011-03-10 11:25:07 +01002929 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002930 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002931 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002932 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 +02002933
Willy Tarreau9b28e032012-10-12 23:49:43 +02002934 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002935 return 0;
2936
Willy Tarreau295a8372011-03-10 11:25:07 +01002937 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002938
Willy Tarreau295a8372011-03-10 11:25:07 +01002939 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2940 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002941 break;
2942
Willy Tarreau295a8372011-03-10 11:25:07 +01002943 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002944 continue;
2945 }
2946
Willy Tarreau44267702011-10-28 15:35:33 +02002947 if (sv->track)
2948 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002949 else
2950 svs = sv;
2951
Willy Tarreau91861262007-10-17 17:06:05 +02002952 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002953 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002954 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002955 else if (svs->state & SRV_RUNNING) {
2956 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002957 sv_state = 3; /* UP */
2958 else
2959 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002960
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002961 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002962 sv_state += 2;
2963 }
Willy Tarreau91861262007-10-17 17:06:05 +02002964 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002965 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002966 sv_state = 1; /* going up */
2967 else
2968 sv_state = 0; /* DOWN */
2969
Willy Tarreau295a8372011-03-10 11:25:07 +01002970 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002971 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002972 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002973 continue;
2974 }
2975
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002976 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
2977 if (bi_putchk(rep, &trash) == -1)
2978 return 0;
2979 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02002980
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002981 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
2982 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002983
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002984 case STAT_PX_ST_BE:
2985 /* print the backend */
2986 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
2987 if (bi_putchk(rep, &trash) == -1)
2988 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002989
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
2991 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002992
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993 case STAT_PX_ST_END:
2994 if (stats_dump_px_end(si, px))
2995 if (bi_putchk(rep, &trash) == -1)
2996 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002997
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002998 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
2999 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003000
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003001 case STAT_PX_ST_FIN:
3002 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003003
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003004 default:
3005 /* unknown state, we should put an abort() here ! */
3006 return 1;
3007 }
3008}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003009
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003010/* Dumps the HTTP stats head block to the trash for and uses the state from
3011 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3012 * for clearing the trash if needed.
3013 */
3014static void stats_dump_html_head(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
3015{
3016 /* WARNING! This must fit in the first buffer !!! */
3017 chunk_appendf(&trash,
3018 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3019 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3020 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3021 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3022 "<style type=\"text/css\"><!--\n"
3023 "body {"
3024 " font-family: arial, helvetica, sans-serif;"
3025 " font-size: 12px;"
3026 " font-weight: normal;"
3027 " color: black;"
3028 " background: white;"
3029 "}\n"
3030 "th,td {"
3031 " font-size: 10px;"
3032 "}\n"
3033 "h1 {"
3034 " font-size: x-large;"
3035 " margin-bottom: 0.5em;"
3036 "}\n"
3037 "h2 {"
3038 " font-family: helvetica, arial;"
3039 " font-size: x-large;"
3040 " font-weight: bold;"
3041 " font-style: italic;"
3042 " color: #6020a0;"
3043 " margin-top: 0em;"
3044 " margin-bottom: 0em;"
3045 "}\n"
3046 "h3 {"
3047 " font-family: helvetica, arial;"
3048 " font-size: 16px;"
3049 " font-weight: bold;"
3050 " color: #b00040;"
3051 " background: #e8e8d0;"
3052 " margin-top: 0em;"
3053 " margin-bottom: 0em;"
3054 "}\n"
3055 "li {"
3056 " margin-top: 0.25em;"
3057 " margin-right: 2em;"
3058 "}\n"
3059 ".hr {margin-top: 0.25em;"
3060 " border-color: black;"
3061 " border-bottom-style: solid;"
3062 "}\n"
3063 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3064 ".total {background: #20D0D0;color: #ffff80;}\n"
3065 ".frontend {background: #e8e8d0;}\n"
3066 ".socket {background: #d0d0d0;}\n"
3067 ".backend {background: #e8e8d0;}\n"
3068 ".active0 {background: #ff9090;}\n"
3069 ".active1 {background: #ffd020;}\n"
3070 ".active2 {background: #ffffa0;}\n"
3071 ".active3 {background: #c0ffc0;}\n"
3072 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3073 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
3074 ".active6 {background: #e0e0e0;}\n"
3075 ".backup0 {background: #ff9090;}\n"
3076 ".backup1 {background: #ff80ff;}\n"
3077 ".backup2 {background: #c060ff;}\n"
3078 ".backup3 {background: #b0d0ff;}\n"
3079 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3080 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3081 ".backup6 {background: #e0e0e0;}\n"
3082 ".maintain {background: #c07820;}\n"
3083 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3084 "\n"
3085 "a.px:link {color: #ffff40; text-decoration: none;}"
3086 "a.px:visited {color: #ffff40; text-decoration: none;}"
3087 "a.px:hover {color: #ffffff; text-decoration: none;}"
3088 "a.lfsb:link {color: #000000; text-decoration: none;}"
3089 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3090 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3091 "\n"
3092 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3093 "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"
3094 "table.tbl td.ac { text-align: center;}\n"
3095 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3096 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3097 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3098 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3099 "\n"
3100 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3101 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3102 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3103 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
3104 "-->\n"
3105 "</style></head>\n",
3106 (uri->flags & ST_SHNODE) ? " on " : "",
3107 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3108 );
3109}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003110
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003111/* Dumps the HTTP stats information block to the trash for and uses the state from
3112 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3113 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3114 * otherwise.
3115 */
3116static int stats_dump_http_info(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
3117{
3118 unsigned int up = (now.tv_sec - start_date.tv_sec);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003119
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120 /* WARNING! this has to fit the first packet too.
3121 * We are around 3.5 kB, add adding entries will
3122 * become tricky if we want to support 4kB buffers !
3123 */
3124 if ((si->applet.ctx.stats.flags & STAT_FMT_CSV))
3125 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003126
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003127 chunk_appendf(&trash,
3128 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3129 PRODUCT_NAME "%s</a></h1>\n"
3130 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3131 "<hr width=\"100%%\" class=\"hr\">\n"
3132 "<h3>&gt; General process information</h3>\n"
3133 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3134 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3135 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3136 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3137 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3138 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3139 "Running tasks: %d/%d; idle = %d %%<br>\n"
3140 "</td><td align=\"center\" nowrap>\n"
3141 "<table class=\"lgd\"><tr>\n"
3142 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3143 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3144 "</tr><tr>\n"
3145 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3146 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3147 "</tr><tr>\n"
3148 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3149 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3150 "</tr><tr>\n"
3151 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3152 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
3153 "</tr><tr>\n"
3154 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3155 "</tr></table>\n"
3156 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3157 "</td>"
3158 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3159 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3160 "",
3161 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3162 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3163 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3164 (uri->flags & ST_SHDESC) ? ": " : "",
3165 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3166 pid, relative_pid, global.nbproc,
3167 up / 86400, (up % 86400) / 3600,
3168 (up % 3600) / 60, (up % 60),
3169 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3170 global.rlimit_memmax ? " MB" : "",
3171 global.rlimit_nofile,
3172 global.maxsock, global.maxconn, global.maxpipes,
3173 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3174 run_queue_cur, nb_tasks_cur, idle_pct
3175 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003176
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003177 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3178 chunk_appendf(&trash,
3179 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
3180 uri->uri_prefix,
3181 "",
3182 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
3183 else
3184 chunk_appendf(&trash,
3185 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3186 uri->uri_prefix,
3187 ";up",
3188 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02003189
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003190 if (uri->refresh > 0) {
3191 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3192 chunk_appendf(&trash,
3193 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
3194 uri->uri_prefix,
3195 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3196 "");
3197 else
3198 chunk_appendf(&trash,
3199 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
3200 uri->uri_prefix,
3201 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3202 ";norefresh");
3203 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003205 chunk_appendf(&trash,
3206 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
3207 uri->uri_prefix,
3208 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3209 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211 chunk_appendf(&trash,
3212 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
3213 uri->uri_prefix,
3214 (uri->refresh > 0) ? ";norefresh" : "");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003215
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003216 chunk_appendf(&trash,
3217 "</ul></td>"
3218 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3219 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3220 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3221 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3222 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3223 "</ul>"
3224 "</td>"
3225 "</tr></table>\n"
3226 ""
3227 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003228
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 if (si->applet.ctx.stats.st_code) {
3230 switch (si->applet.ctx.stats.st_code) {
3231 case STAT_STATUS_DONE:
3232 chunk_appendf(&trash,
3233 "<p><div class=active3>"
3234 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3235 "Action processed successfully."
3236 "</div>\n", uri->uri_prefix);
3237 break;
3238 case STAT_STATUS_NONE:
3239 chunk_appendf(&trash,
3240 "<p><div class=active2>"
3241 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3242 "Nothing has changed."
3243 "</div>\n", uri->uri_prefix);
3244 break;
3245 case STAT_STATUS_PART:
3246 chunk_appendf(&trash,
3247 "<p><div class=active2>"
3248 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3249 "Action partially processed.<br>"
3250 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3251 "</div>\n", uri->uri_prefix);
3252 break;
3253 case STAT_STATUS_ERRP:
3254 chunk_appendf(&trash,
3255 "<p><div class=active0>"
3256 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3257 "Action not processed because of invalid parameters."
3258 "<ul>"
3259 "<li>The action is maybe unknown.</li>"
3260 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3261 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3262 "</ul>"
3263 "</div>\n", uri->uri_prefix);
3264 break;
3265 case STAT_STATUS_EXCD:
3266 chunk_appendf(&trash,
3267 "<p><div class=active0>"
3268 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3269 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3270 "You should retry with less servers at a time.</b>"
3271 "</div>\n", uri->uri_prefix);
3272 break;
3273 case STAT_STATUS_DENY:
3274 chunk_appendf(&trash,
3275 "<p><div class=active0>"
3276 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3277 "<b>Action denied.</b>"
3278 "</div>\n", uri->uri_prefix);
3279 break;
3280 default:
3281 chunk_appendf(&trash,
3282 "<p><div class=active6>"
3283 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3284 "Unexpected result."
3285 "</div>\n", uri->uri_prefix);
3286 }
3287 chunk_appendf(&trash, "<p>\n");
3288 }
3289 return 1;
3290}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003291
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292/* Dumps the HTTP stats trailer block to the trash for and uses the state from
3293 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3294 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3295 * otherwise.
3296 */
3297static int stats_dump_http_end(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
3298{
3299 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
3300 return 0;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003301
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003302 chunk_appendf(&trash, "</body></html>\n");
3303 return 1;
3304}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003305
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003306/* This function dumps statistics in HTTP format onto the stream interface's
3307 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3308 * properly set. It returns 0 if it had to stop writing data and an I/O is
3309 * needed, 1 if the dump is finished and the session must be closed, or -1
3310 * in case of any error.
3311 */
3312static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
3313{
3314 struct session *s = si->conn->xprt_ctx;
3315 struct channel *rep = si->ib;
3316 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003317
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003318 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003319
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 switch (si->conn->xprt_st) {
3321 case STAT_ST_INIT:
3322 chunk_appendf(&trash,
3323 "HTTP/1.0 200 OK\r\n"
3324 "Cache-Control: no-cache\r\n"
3325 "Connection: close\r\n"
3326 "Content-Type: %s\r\n",
3327 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003328
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3330 chunk_appendf(&trash, "Refresh: %d\r\n",
3331 uri->refresh);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003332
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333 chunk_appendf(&trash, "\r\n");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003334
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 s->txn.status = 200;
3336 if (bi_putchk(rep, &trash) == -1)
3337 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003338
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3340 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3341 if (!(s->flags & SN_FINST_MASK))
3342 s->flags |= SN_FINST_R;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003343
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003344 if (s->txn.meth == HTTP_METH_HEAD) {
3345 /* that's all we return in case of HEAD request */
3346 si->conn->xprt_st = STAT_ST_FIN;
3347 return 1;
3348 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003349
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3351 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003352
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 case STAT_ST_HEAD:
3354 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
3355 stats_dump_csv_header();
3356 else
3357 stats_dump_html_head(si, px, uri);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003358
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003359 if (bi_putchk(rep, &trash) == -1)
3360 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003361
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362 si->conn->xprt_st = STAT_ST_INFO;
3363 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003364
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 case STAT_ST_INFO:
3366 if (stats_dump_http_info(si, px, uri)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003367 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003368 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 }
Willy Tarreau91861262007-10-17 17:06:05 +02003370
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003371 si->applet.ctx.stats.px = proxy;
3372 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3373 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003374 /* fall through */
3375
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 case STAT_ST_LIST:
3377 /* dump proxies */
3378 while (si->applet.ctx.stats.px) {
3379 if (buffer_almost_full(rep->buf))
3380 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003381
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382 px = si->applet.ctx.stats.px;
3383 /* skip the disabled proxies, global frontend and non-networked ones */
3384 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3385 if (stats_dump_proxy(si, px, uri) == 0)
3386 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003387
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388 si->applet.ctx.stats.px = px->next;
3389 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3390 }
3391 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003392
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 si->conn->xprt_st = STAT_ST_END;
3394 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003395
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 case STAT_ST_END:
3397 if (stats_dump_http_end(si, px, uri)) {
3398 if (bi_putchk(rep, &trash) == -1)
3399 return 0;
3400 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003401
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402 si->conn->xprt_st = STAT_ST_FIN;
3403 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 case STAT_ST_FIN:
3406 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003407
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408 default:
3409 /* unknown state ! */
3410 si->conn->xprt_st = STAT_ST_FIN;
3411 return -1;
3412 }
3413}
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003414
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415/* We don't want to land on the posted stats page because a refresh will
3416 * repost the data. We don't want this to happen on accident so we redirect
3417 * the browse to the stats page with a GET.
3418 */
3419static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
3420{
3421 struct session *s = si->conn->xprt_ctx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003422
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003423 chunk_reset(&trash);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425 switch (si->conn->xprt_st) {
3426 case STAT_ST_INIT:
3427 chunk_appendf(&trash,
3428 "HTTP/1.0 303 See Other\r\n"
3429 "Cache-Control: no-cache\r\n"
3430 "Content-Type: text/plain\r\n"
3431 "Connection: close\r\n"
3432 "Location: %s;st=%s",
3433 uri->uri_prefix,
3434 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3435 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3436 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3437 stat_status_codes[si->applet.ctx.stats.st_code] :
3438 stat_status_codes[STAT_STATUS_UNKN]);
3439 chunk_appendf(&trash, "\r\n\r\n");
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003440
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 if (bi_putchk(si->ib, &trash) == -1)
3442 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003443
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 s->txn.status = 303;
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003445
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3447 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3448 if (!(s->flags & SN_FINST_MASK))
3449 s->flags |= SN_FINST_R;
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003450
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 si->conn->xprt_st = STAT_ST_FIN;
3452 return 1;
3453 }
3454 return 1;
3455}
Willy Tarreauae526782010-03-04 20:34:23 +01003456
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003457/* This I/O handler runs as an applet embedded in a stream interface. It is
3458 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3459 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3460 * automatically unregisters itself once transfer is complete.
3461 */
3462static void http_stats_io_handler(struct stream_interface *si)
3463{
3464 struct session *s = si->conn->xprt_ctx;
3465 struct channel *req = si->ob;
3466 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003467
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003468 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3469 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003470
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003471 /* check that the output is not closed */
3472 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3473 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003474
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475 if (!si->applet.st0) {
3476 if (s->txn.meth == HTTP_METH_POST) {
3477 if (stats_http_redir(si, s->be->uri_auth)) {
3478 si->applet.st0 = 1;
3479 si_shutw(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003480 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003481 } else {
3482 if (stats_dump_http(si, s->be->uri_auth)) {
3483 si->applet.st0 = 1;
3484 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003485 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003486 }
3487 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003488
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003489 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3490 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3493 si_shutr(si);
3494 res->flags |= CF_READ_NULL;
3495 }
Willy Tarreau91861262007-10-17 17:06:05 +02003496
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 /* update all other flags and resync with the other side */
3498 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003499
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003500 /* we don't want to expire timeouts while we're processing requests */
3501 si->ib->rex = TICK_ETERNITY;
3502 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 out:
3505 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3506 /* check that we have released everything then unregister */
3507 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003508 }
3509}
3510
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003511
Willy Tarreau909d5172012-11-26 03:04:41 +01003512static inline const char *get_conn_ctrl_name(const struct connection *conn)
3513{
3514 if (!conn->ctrl)
3515 return "NONE";
3516 return conn->ctrl->name;
3517}
3518
3519static inline const char *get_conn_xprt_name(const struct connection *conn)
3520{
3521 static char ptr[17];
3522
3523 if (!conn->xprt)
3524 return "NONE";
3525
3526 if (conn->xprt == &raw_sock)
3527 return "RAW";
3528
3529#ifdef USE_OPENSSL
3530 if (conn->xprt == &ssl_sock)
3531 return "SSL";
3532#endif
3533 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3534 return ptr;
3535}
3536
3537static inline const char *get_conn_data_name(const struct connection *conn)
3538{
3539 static char ptr[17];
3540
3541 if (!conn->data)
3542 return "NONE";
3543
3544 if (conn->data == &sess_conn_cb)
3545 return "SESS";
3546
3547 if (conn->data == &si_conn_cb)
3548 return "STRM";
3549
3550 if (conn->data == &check_conn_cb)
3551 return "CHCK";
3552
3553 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3554 return ptr;
3555}
3556
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003557/* This function dumps a complete session state onto the stream interface's
3558 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3559 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003560 * 0 if the output buffer is full and it needs to be called again, otherwise
3561 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003562 */
Willy Tarreau76153662012-11-26 01:16:39 +01003563static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003564{
3565 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003566 extern const char *monthname[12];
3567 char pn[INET6_ADDRSTRLEN];
3568
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003569 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003570
Willy Tarreau295a8372011-03-10 11:25:07 +01003571 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003572 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003573 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3574 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003575 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003576 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003577 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003578 return 1;
3579 }
3580
Willy Tarreau295a8372011-03-10 11:25:07 +01003581 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003582 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003583 si->applet.ctx.sess.uid = sess->uniq_id;
3584 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003585 /* fall through */
3586
3587 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003588 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003589 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003590 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003591 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003592 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3593 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003594 sess->uniq_id,
3595 sess->listener->proto->name);
3596
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003597 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003598 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003599 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003600 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003601 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003602 break;
3603 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003604 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003605 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003606 default:
3607 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003608 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003609 break;
3610 }
3611
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003612 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003613 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003614 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003615
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003616 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003617 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003618 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3619 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3620 sess->listener ? sess->listener->luid : 0);
3621
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003622 conn_get_to_addr(sess->si[0].conn);
3623 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003624 case AF_INET:
3625 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003626 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003627 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003628 break;
3629 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003630 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003631 break;
3632 default:
3633 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003634 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003635 break;
3636 }
3637
Willy Tarreau50943332011-09-02 17:33:05 +02003638 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003639 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003640 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003641 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003642 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3643 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003644 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003645
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003646 conn_get_from_addr(sess->si[1].conn);
3647 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003648 case AF_INET:
3649 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003650 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003651 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003652 break;
3653 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003654 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003655 break;
3656 default:
3657 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003658 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003659 break;
3660 }
3661
3662 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003663 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003664 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003665 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3666 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003667 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003668 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003669
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003670 conn_get_to_addr(sess->si[1].conn);
3671 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003672 case AF_INET:
3673 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003674 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003675 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003676 break;
3677 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003678 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003679 break;
3680 default:
3681 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003682 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003683 break;
3684 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003685
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003686 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003687 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003688 sess->task,
3689 sess->task->state,
3690 sess->task->nice, sess->task->calls,
3691 sess->task->expire ?
3692 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3693 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3694 TICKS_TO_MS(1000)) : "<NEVER>",
3695 task_in_rq(sess->task) ? ", running" : "");
3696
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003697 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003698 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003699 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3700
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003701 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003702 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3703 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3704 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3705
3706 chunk_appendf(&trash,
3707 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003708 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003709 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003710 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003711 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003712 sess->si[0].exp ?
3713 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3714 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3715 TICKS_TO_MS(1000)) : "<NEVER>",
3716 sess->si[0].err_type);
3717
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003718 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003719 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003720 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003721 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003722 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003723 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003724 sess->si[1].exp ?
3725 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3726 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3727 TICKS_TO_MS(1000)) : "<NEVER>",
3728 sess->si[1].err_type);
3729
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003730 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003731 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003732 sess->si[0].conn,
3733 get_conn_ctrl_name(sess->si[0].conn),
3734 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003735 get_conn_data_name(sess->si[0].conn),
3736 obj_type_name(sess->si[0].conn->target),
3737 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003738
3739 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003740 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003741 sess->si[0].conn->flags,
3742 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003743 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3744 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3745 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].updated : 0);
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003746
3747 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003748 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003749 sess->si[1].conn,
3750 get_conn_ctrl_name(sess->si[1].conn),
3751 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003752 get_conn_data_name(sess->si[1].conn),
3753 obj_type_name(sess->si[1].conn->target),
3754 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003755
3756 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003757 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003758 sess->si[1].conn->flags,
3759 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003760 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3761 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3762 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].updated : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003763
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003764 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003765 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003766 " an_exp=%s",
3767 sess->req,
3768 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003769 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003770 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003771 sess->req->analyse_exp ?
3772 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3773 TICKS_TO_MS(1000)) : "<NEVER>");
3774
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003775 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003776 " rex=%s",
3777 sess->req->rex ?
3778 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3779 TICKS_TO_MS(1000)) : "<NEVER>");
3780
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003781 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003782 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003783 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003784 sess->req->wex ?
3785 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3786 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003787 sess->req->buf,
3788 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003789 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003790 sess->txn.req.next, sess->req->buf->i,
3791 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003792
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003793 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003794 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003795 " an_exp=%s",
3796 sess->rep,
3797 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003798 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003799 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003800 sess->rep->analyse_exp ?
3801 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3802 TICKS_TO_MS(1000)) : "<NEVER>");
3803
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003804 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003805 " rex=%s",
3806 sess->rep->rex ?
3807 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3808 TICKS_TO_MS(1000)) : "<NEVER>");
3809
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003810 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003811 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003812 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003813 sess->rep->wex ?
3814 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3815 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003816 sess->rep->buf,
3817 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003818 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003819 sess->txn.rsp.next, sess->rep->buf->i,
3820 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003821
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003822 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003823 return 0;
3824
3825 /* use other states to dump the contents */
3826 }
3827 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003828 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003829 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003830 return 1;
3831}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003832
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003833/* This function dumps all sessions' states onto the stream interface's
3834 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003835 * properly set. It returns 0 if the output buffer is full and it needs
3836 * to be called again, otherwise non-zero. It is designed to be called
3837 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003838 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003839static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003840{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003841 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003842 /* If we're forced to shut down, we might have to remove our
3843 * reference to the last session being dumped.
3844 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003845 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003846 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3847 LIST_DEL(&si->applet.ctx.sess.bref.users);
3848 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003849 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003850 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003851 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003852 }
3853
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003854 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003855
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003856 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003857 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003858 /* the function had not been called yet, let's prepare the
3859 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003860 * pointer to the first in the global list. When a target
3861 * session is being destroyed, it is responsible for updating
3862 * this pointer. We know we have reached the end when this
3863 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003864 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003865 LIST_INIT(&si->applet.ctx.sess.bref.users);
3866 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003867 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003868 /* fall through */
3869
Willy Tarreau295a8372011-03-10 11:25:07 +01003870 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003871 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003872 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3873 LIST_DEL(&si->applet.ctx.sess.bref.users);
3874 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003875 }
3876
3877 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003878 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003879 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003880 struct session *curr_sess;
3881
Willy Tarreau295a8372011-03-10 11:25:07 +01003882 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003883
Willy Tarreau295a8372011-03-10 11:25:07 +01003884 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003885 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003886 goto next_sess;
3887
Willy Tarreau295a8372011-03-10 11:25:07 +01003888 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003889 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003890 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003891 return 0;
3892
3893 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003894 LIST_DEL(&si->applet.ctx.sess.bref.users);
3895 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003896 if (si->applet.ctx.sess.target != (void *)-1) {
3897 si->applet.ctx.sess.target = NULL;
3898 break;
3899 }
3900 else
3901 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003902 }
3903
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003904 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003905 "%p: proto=%s",
3906 curr_sess,
3907 curr_sess->listener->proto->name);
3908
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003909
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003910 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003911 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003912 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003913 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003914 " src=%s:%d fe=%s be=%s srv=%s",
3915 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003916 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003917 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003918 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003919 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003920 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003921 break;
3922 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003923 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003924 " src=unix:%d fe=%s be=%s srv=%s",
3925 curr_sess->listener->luid,
3926 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003927 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003928 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003929 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003930 break;
3931 }
3932
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003933 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003934 " ts=%02x age=%s calls=%d",
3935 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003936 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3937 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003939 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003940 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003941 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003942 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003943 curr_sess->req->analysers,
3944 curr_sess->req->rex ?
3945 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3946 TICKS_TO_MS(1000)) : "");
3947
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003948 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003949 ",wx=%s",
3950 curr_sess->req->wex ?
3951 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3952 TICKS_TO_MS(1000)) : "");
3953
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003954 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003955 ",ax=%s]",
3956 curr_sess->req->analyse_exp ?
3957 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3958 TICKS_TO_MS(1000)) : "");
3959
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003960 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003961 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003962 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003963 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003964 curr_sess->rep->analysers,
3965 curr_sess->rep->rex ?
3966 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3967 TICKS_TO_MS(1000)) : "");
3968
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003969 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003970 ",wx=%s",
3971 curr_sess->rep->wex ?
3972 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3973 TICKS_TO_MS(1000)) : "");
3974
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003975 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003976 ",ax=%s]",
3977 curr_sess->rep->analyse_exp ?
3978 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3979 TICKS_TO_MS(1000)) : "");
3980
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003981 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003982 " s0=[%d,%1xh,fd=%d,ex=%s]",
3983 curr_sess->si[0].state,
3984 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003985 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003986 curr_sess->si[0].exp ?
3987 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3988 TICKS_TO_MS(1000)) : "");
3989
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003990 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003991 " s1=[%d,%1xh,fd=%d,ex=%s]",
3992 curr_sess->si[1].state,
3993 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003994 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003995 curr_sess->si[1].exp ?
3996 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3997 TICKS_TO_MS(1000)) : "");
3998
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003999 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004000 " exp=%s",
4001 curr_sess->task->expire ?
4002 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4003 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004004 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004005 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004006
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004007 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004008
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004009 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004010 /* let's try again later from this session. We add ourselves into
4011 * this session's users so that it can remove us upon termination.
4012 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004013 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004014 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004015 }
4016
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004017 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01004018 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004019 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004020
Willy Tarreau76153662012-11-26 01:16:39 +01004021 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004022 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01004023 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004024 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004025 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004026 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004027
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004028 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004029 return 0;
4030
Willy Tarreau295a8372011-03-10 11:25:07 +01004031 si->applet.ctx.sess.target = NULL;
4032 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004033 return 1;
4034 }
4035
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004036 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004037 /* fall through */
4038
4039 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004040 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004041 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004042 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004043}
4044
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004045/* This is called when the stream interface is closed. For instance, upon an
4046 * external abort, we won't call the i/o handler anymore so we may need to
4047 * remove back references to the session currently being dumped.
4048 */
4049void cli_release_handler(struct stream_interface *si)
4050{
4051 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
4052 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
4053 LIST_DEL(&si->applet.ctx.sess.bref.users);
4054 }
4055}
4056
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004057/* This function dumps all tables' states onto the stream interface's
4058 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004059 * properly set. It returns 0 if the output buffer is full and it needs
4060 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004061 */
Willy Tarreau44455022012-12-05 23:01:12 +01004062static int stats_table_request(struct stream_interface *si, int show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004063{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004064 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004065 struct ebmb_node *eb;
4066 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004067 int skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004068
4069 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004070 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01004071 * - STAT_ST_INIT : the first call
4072 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004073 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004074 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004075 * and the entry pointer points to the next entry to be dumped,
4076 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004077 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004078 * data though.
4079 */
4080
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004081 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004082 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004083 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004084 si->applet.ctx.table.entry->ref_cnt--;
4085 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004086 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004087 return 1;
4088 }
4089
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004090 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004091
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004092 while (si->conn->xprt_st != STAT_ST_FIN) {
4093 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004094 case STAT_ST_INIT:
4095 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4096 if (!si->applet.ctx.table.proxy)
4097 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004098
Willy Tarreau295a8372011-03-10 11:25:07 +01004099 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004100 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004101 break;
4102
Willy Tarreau295a8372011-03-10 11:25:07 +01004103 case STAT_ST_INFO:
4104 if (!si->applet.ctx.table.proxy ||
4105 (si->applet.ctx.table.target &&
4106 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004107 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004108 break;
4109 }
4110
Willy Tarreau295a8372011-03-10 11:25:07 +01004111 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004112 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004113 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004114 return 0;
4115
Willy Tarreau295a8372011-03-10 11:25:07 +01004116 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004117 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004118 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004119 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004120 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004121 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4122 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004123 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004124 break;
4125 }
4126 }
4127 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004128 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004129 break;
4130
Willy Tarreau295a8372011-03-10 11:25:07 +01004131 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004132 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004133
Willy Tarreau295a8372011-03-10 11:25:07 +01004134 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004135 /* we're filtering on some data contents */
4136 void *ptr;
4137 long long data;
4138
Willy Tarreau295a8372011-03-10 11:25:07 +01004139 dt = si->applet.ctx.table.data_type;
4140 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4141 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004142 dt);
4143
4144 data = 0;
4145 switch (stktable_data_types[dt].std_type) {
4146 case STD_T_SINT:
4147 data = stktable_data_cast(ptr, std_t_sint);
4148 break;
4149 case STD_T_UINT:
4150 data = stktable_data_cast(ptr, std_t_uint);
4151 break;
4152 case STD_T_ULL:
4153 data = stktable_data_cast(ptr, std_t_ull);
4154 break;
4155 case STD_T_FRQP:
4156 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004157 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004158 break;
4159 }
4160
4161 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004162 if ((data < si->applet.ctx.table.value &&
4163 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4164 si->applet.ctx.table.data_op == STD_OP_GT ||
4165 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4166 (data == si->applet.ctx.table.value &&
4167 (si->applet.ctx.table.data_op == STD_OP_NE ||
4168 si->applet.ctx.table.data_op == STD_OP_GT ||
4169 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4170 (data > si->applet.ctx.table.value &&
4171 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4172 si->applet.ctx.table.data_op == STD_OP_LT ||
4173 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004174 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004175 }
4176
Simon Hormanc88b8872011-06-15 15:18:49 +09004177 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004178 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004179 si->applet.ctx.table.entry))
4180 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004181
Willy Tarreau295a8372011-03-10 11:25:07 +01004182 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004183
Willy Tarreau295a8372011-03-10 11:25:07 +01004184 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004185 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004186 struct stksess *old = si->applet.ctx.table.entry;
4187 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004188 if (show)
4189 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
4190 else
4191 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004192 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004193 break;
4194 }
4195
Simon Hormanc88b8872011-06-15 15:18:49 +09004196
4197 if (show)
4198 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4199 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4200 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4201
Willy Tarreau295a8372011-03-10 11:25:07 +01004202 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004203 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004204 break;
4205
Willy Tarreau295a8372011-03-10 11:25:07 +01004206 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004207 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004208 break;
4209 }
4210 }
4211 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004212}
4213
Willy Tarreaud426a182010-03-05 14:58:26 +01004214/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004215 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4216 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4217 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4218 * lines are respected within the limit of 70 output chars. Lines that are
4219 * continuation of a previous truncated line begin with "+" instead of " "
4220 * after the offset. The new pointer is returned.
4221 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004222static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4223 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004224{
4225 int end;
4226 unsigned char c;
4227
4228 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004229 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004230 return ptr;
4231
Willy Tarreau77804732012-10-29 16:14:26 +01004232 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004233
Willy Tarreaud426a182010-03-05 14:58:26 +01004234 while (ptr < len && ptr < bsize) {
4235 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004236 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004237 if (out->len > end - 2)
4238 break;
4239 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004240 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004241 if (out->len > end - 3)
4242 break;
4243 out->str[out->len++] = '\\';
4244 switch (c) {
4245 case '\t': c = 't'; break;
4246 case '\n': c = 'n'; break;
4247 case '\r': c = 'r'; break;
4248 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004249 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004250 }
4251 out->str[out->len++] = c;
4252 } else {
4253 if (out->len > end - 5)
4254 break;
4255 out->str[out->len++] = '\\';
4256 out->str[out->len++] = 'x';
4257 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4258 out->str[out->len++] = hextab[c & 0xF];
4259 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004260 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004261 /* we had a line break, let's return now */
4262 out->str[out->len++] = '\n';
4263 *line = ptr;
4264 return ptr;
4265 }
4266 }
4267 /* we have an incomplete line, we return it as-is */
4268 out->str[out->len++] = '\n';
4269 return ptr;
4270}
4271
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004272/* This function dumps all captured errors onto the stream interface's
4273 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004274 * properly set. It returns 0 if the output buffer is full and it needs
4275 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004276 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004277static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004278{
4279 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004280
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004281 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004282 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004283
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004284 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004285
Willy Tarreau295a8372011-03-10 11:25:07 +01004286 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004287 /* the function had not been called yet, let's prepare the
4288 * buffer for a response.
4289 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004290 struct tm tm;
4291
4292 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004293 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004294 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4295 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4296 error_snapshot_id);
4297
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004298 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004299 /* Socket buffer full. Let's try again later from the same point */
4300 return 0;
4301 }
4302
Willy Tarreau295a8372011-03-10 11:25:07 +01004303 si->applet.ctx.errors.px = proxy;
4304 si->applet.ctx.errors.buf = 0;
4305 si->applet.ctx.errors.bol = 0;
4306 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004307 }
4308
4309 /* we have two inner loops here, one for the proxy, the other one for
4310 * the buffer.
4311 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004312 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004313 struct error_snapshot *es;
4314
Willy Tarreau295a8372011-03-10 11:25:07 +01004315 if (si->applet.ctx.errors.buf == 0)
4316 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004317 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004318 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004319
4320 if (!es->when.tv_sec)
4321 goto next;
4322
Willy Tarreau295a8372011-03-10 11:25:07 +01004323 if (si->applet.ctx.errors.iid >= 0 &&
4324 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4325 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004326 goto next;
4327
Willy Tarreau295a8372011-03-10 11:25:07 +01004328 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004329 /* just print headers now */
4330
4331 char pn[INET6_ADDRSTRLEN];
4332 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004333 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004334
4335 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004336 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004337 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004338 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004339
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004340 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4341 case AF_INET:
4342 case AF_INET6:
4343 port = get_host_port(&es->src);
4344 break;
4345 default:
4346 port = 0;
4347 }
4348
Willy Tarreau295a8372011-03-10 11:25:07 +01004349 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004350 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004351 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004352 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004353 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004354 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004355 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4356 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004357 break;
4358 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004359 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004360 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004361 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004362 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004363 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004364 break;
4365 }
4366
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004367 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004368 ", server %s (#%d), event #%u\n"
4369 " src %s:%d, session #%d, session flags 0x%08x\n"
4370 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4371 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4372 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4373 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4374 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4375 es->ev_id,
4376 pn, port, es->sid, es->s_flags,
4377 es->state, es->m_flags, es->t_flags,
4378 es->m_clen, es->m_blen,
4379 es->b_flags, es->b_out, es->b_tot,
4380 es->len, es->b_wrap, es->pos);
4381
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004382 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004383 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004384 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004385 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004386 si->applet.ctx.errors.ptr = 0;
4387 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004388 }
4389
Willy Tarreau295a8372011-03-10 11:25:07 +01004390 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004391 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004392 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004393 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004394 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004395 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004396 goto next;
4397 }
4398
4399 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004400 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004401 int newptr;
4402 int newline;
4403
Willy Tarreau295a8372011-03-10 11:25:07 +01004404 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004405 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
Willy Tarreau295a8372011-03-10 11:25:07 +01004406 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004407 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004409 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004410 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004411 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004412 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004413 si->applet.ctx.errors.ptr = newptr;
4414 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004415 };
4416 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004417 si->applet.ctx.errors.bol = 0;
4418 si->applet.ctx.errors.ptr = -1;
4419 si->applet.ctx.errors.buf++;
4420 if (si->applet.ctx.errors.buf > 1) {
4421 si->applet.ctx.errors.buf = 0;
4422 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004423 }
4424 }
4425
4426 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004427 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004428}
4429
Willy Tarreaud5781202012-09-22 19:32:35 +02004430/* parse the "level" argument on the bind lines */
4431static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4432{
4433 if (!*args[cur_arg + 1]) {
4434 memprintf(err, "'%s' : missing level", args[cur_arg]);
4435 return ERR_ALERT | ERR_FATAL;
4436 }
4437
4438 if (!strcmp(args[cur_arg+1], "user"))
4439 conf->level = ACCESS_LVL_USER;
4440 else if (!strcmp(args[cur_arg+1], "operator"))
4441 conf->level = ACCESS_LVL_OPER;
4442 else if (!strcmp(args[cur_arg+1], "admin"))
4443 conf->level = ACCESS_LVL_ADMIN;
4444 else {
4445 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4446 args[cur_arg], args[cur_arg+1]);
4447 return ERR_ALERT | ERR_FATAL;
4448 }
4449
4450 return 0;
4451}
4452
Willy Tarreaub24281b2011-02-13 13:16:36 +01004453struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004454 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004455 .name = "<STATS>", /* used for logging */
4456 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004457 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004458};
4459
Simon Horman9bd2c732011-06-15 15:18:44 +09004460static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004461 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004462 .name = "<CLI>", /* used for logging */
4463 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004464 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004465};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004466
Willy Tarreau10522fd2008-07-09 20:12:41 +02004467static struct cfg_kw_list cfg_kws = {{ },{
4468 { CFG_GLOBAL, "stats", stats_parse_global },
4469 { 0, NULL, NULL },
4470}};
4471
Willy Tarreaud5781202012-09-22 19:32:35 +02004472static struct bind_kw_list bind_kws = { "STAT", { }, {
4473 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4474 { NULL, NULL, 0 },
4475}};
4476
Willy Tarreau10522fd2008-07-09 20:12:41 +02004477__attribute__((constructor))
4478static void __dumpstats_module_init(void)
4479{
4480 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004481 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004482}
4483
Willy Tarreau91861262007-10-17 17:06:05 +02004484/*
4485 * Local variables:
4486 * c-indent-level: 8
4487 * c-basic-offset: 8
4488 * End:
4489 */