blob: e9e7d14b26ddc37b69ceeb2aeb91c0d04790b352 [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()
Willy Tarreau1facd6d2012-12-22 22:03:39 +010090 -> stats_dump_http()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010091 -> stats_dump_html_head() // emits the HTML headers
92 -> stats_dump_csv_header() // emits the CSV headers (same as above)
93 -> stats_dump_http_info() // note: ignores non-HTML output
94 -> stats_dump_proxy() // same as above
95 -> stats_dump_http_end() // emits HTML trailer
96 */
97
Simon Horman9bd2c732011-06-15 15:18:44 +090098static struct si_applet cli_applet;
99
100static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200101 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200102 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200103 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200104 " help : this message\n"
105 " prompt : toggle interactive mode with prompt\n"
106 " quit : disconnect\n"
107 " show info : report information about the running process\n"
108 " show stat : report counters for each proxy and server\n"
109 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100110 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200111 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200112 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200113 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200114 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100115 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200116 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200117 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200118 " disable : put a server or frontend in maintenance mode\n"
119 " enable : re-enable a server or frontend which is in maintenance mode\n"
120 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200121 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200122
Simon Horman9bd2c732011-06-15 15:18:44 +0900123static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200124 "Permission denied\n"
125 "";
126
Willy Tarreau295a8372011-03-10 11:25:07 +0100127/* data transmission states for the stats responses */
128enum {
129 STAT_ST_INIT = 0,
130 STAT_ST_HEAD,
131 STAT_ST_INFO,
132 STAT_ST_LIST,
133 STAT_ST_END,
134 STAT_ST_FIN,
135};
136
137/* data transmission states for the stats responses inside a proxy */
138enum {
139 STAT_PX_ST_INIT = 0,
140 STAT_PX_ST_TH,
141 STAT_PX_ST_FE,
142 STAT_PX_ST_LI,
143 STAT_PX_ST_SV,
144 STAT_PX_ST_BE,
145 STAT_PX_ST_END,
146 STAT_PX_ST_FIN,
147};
148
Cyril Bonté19979e12012-04-04 12:57:21 +0200149extern const char *stat_status_codes[];
150
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200151/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200152 * a new stats socket. It returns a positive value upon success, 0 if the connection
153 * needs to be closed and ignored, or a negative value upon critical failure.
154 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900155static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200156{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200157 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100158 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100159 s->target = s->si[1].conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200160 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100161 s->si[1].applet.st1 = 0;
162 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200163
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200164 tv_zero(&s->logs.tv_request);
165 s->logs.t_queue = 0;
166 s->logs.t_connect = 0;
167 s->logs.t_data = 0;
168 s->logs.t_close = 0;
169 s->logs.bytes_in = s->logs.bytes_out = 0;
170 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
171 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200172
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200173 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200174
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200175 if (s->listener->timeout) {
176 s->req->rto = *s->listener->timeout;
177 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200178 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200180}
181
Willy Tarreau07e9e642010-08-17 21:48:17 +0200182/* allocate a new stats frontend named <name>, and return it
183 * (or NULL in case of lack of memory).
184 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200185static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200186{
187 struct proxy *fe;
188
189 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
190 if (!fe)
191 return NULL;
192
Willy Tarreau237250c2011-07-29 01:49:03 +0200193 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200194 fe->next = proxy;
195 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200196 fe->last_change = now.tv_sec;
197 fe->id = strdup("GLOBAL");
198 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200199 fe->maxconn = 10; /* default to 10 concurrent connections */
200 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200201 fe->conf.file = strdup(file);
202 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200203 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200204
205 /* the stats frontend is the only one able to assign ID #0 */
206 fe->conf.id.key = fe->uuid = 0;
207 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200208 return fe;
209}
210
Willy Tarreaufbee7132007-10-18 13:53:22 +0200211/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200212 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
213 * error message into the <err> buffer which will be preallocated. The trailing
214 * '\n' must not be written. The function must be called with <args> pointing to
215 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200216 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200217static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200218 struct proxy *defpx, const char *file, int line,
219 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200220{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200221 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200222 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200223
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200224 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200225 int cur_arg;
226
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200227 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200228 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 +0200229 return -1;
230 }
231
Willy Tarreau89a63132009-08-16 17:41:45 +0200232 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200233 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200234 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200235 return -1;
236 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200237 }
238
Willy Tarreau4348fad2012-09-20 16:48:07 +0200239 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200240 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200241
Willy Tarreauc53d4222012-09-20 20:19:28 +0200242 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
243 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
244 file, line, args[0], args[1], err && *err ? *err : "error");
245 return -1;
246 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200247
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200248 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200249 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200250 static int bind_dumped;
251 struct bind_kw *kw;
252
253 kw = bind_find_kw(args[cur_arg]);
254 if (kw) {
255 if (!kw->parse) {
256 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
257 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200258 return -1;
259 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200260
261 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
262 if (err && *err)
263 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
264 else
265 memprintf(err, "'%s %s' : error encountered while processing '%s'",
266 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200267 return -1;
268 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200269
270 cur_arg += 1 + kw->skip;
271 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200272 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200273
274 if (!bind_dumped) {
275 bind_dump_kws(err);
276 indent_msg(err, 4);
277 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200279
280 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
281 args[0], args[1], args[cur_arg],
282 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
283 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100285
Willy Tarreauc53d4222012-09-20 20:19:28 +0200286 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
287 l->maxconn = global.stats_fe->maxconn;
288 l->backlog = global.stats_fe->backlog;
289 l->timeout = &global.stats_fe->timeout.client;
290 l->accept = session_accept;
291 l->handler = process_session;
292 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
293 l->nice = -64; /* we want to boost priority for local stats */
294 global.maxsock += l->maxconn;
295 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200296 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100298 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100300
301 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100303 return -1;
304 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100306 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200307 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200308 return -1;
309 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200310 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200311 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200312 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200313 return -1;
314 }
315 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200316 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200317 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200318 else if (!strcmp(args[1], "maxconn")) {
319 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200320
321 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200322 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200323 return -1;
324 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200325
326 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200327 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200328 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200329 return -1;
330 }
331 }
332 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200333 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200334 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
335 int cur_arg = 2;
336 unsigned int set = 0;
337
338 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100339 unsigned int low, high;
340
Willy Tarreau35b7b162012-10-22 23:17:18 +0200341 if (strcmp(args[cur_arg], "all") == 0) {
342 set = 0;
343 break;
344 }
345 else if (strcmp(args[cur_arg], "odd") == 0) {
346 set |= 0x55555555;
347 }
348 else if (strcmp(args[cur_arg], "even") == 0) {
349 set |= 0xAAAAAAAA;
350 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100351 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100352 char *dash = strchr(args[cur_arg], '-');
353
354 low = high = str2uic(args[cur_arg]);
355 if (dash)
356 high = str2uic(dash + 1);
357
358 if (high < low) {
359 unsigned int swap = low;
360 low = high;
361 high = swap;
362 }
363
364 if (low < 1 || high > 32) {
365 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
366 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200367 return -1;
368 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100369
370 while (low <= high)
371 set |= 1 << (low++ - 1);
372 }
373 else {
374 memprintf(err,
375 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
376 args[0], args[1]);
377 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200378 }
379 cur_arg++;
380 }
381 global.stats_fe->bind_proc = set;
382 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200383 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200384 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200385 return -1;
386 }
387 return 0;
388}
389
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100390/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
391 * for clearing it if needed.
392 */
393static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100394{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100395 chunk_appendf(&trash,
396 "# pxname,svname,"
397 "qcur,qmax,"
398 "scur,smax,slim,stot,"
399 "bin,bout,"
400 "dreq,dresp,"
401 "ereq,econ,eresp,"
402 "wretr,wredis,"
403 "status,weight,act,bck,"
404 "chkfail,chkdown,lastchg,downtime,qlimit,"
405 "pid,iid,sid,throttle,lbtot,tracked,type,"
406 "rate,rate_lim,rate_max,"
407 "check_status,check_code,check_duration,"
408 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
409 "req_rate,req_rate_max,req_tot,"
410 "cli_abrt,srv_abrt,"
411 "comp_in,comp_out,comp_byp,comp_rsp,"
412 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100413}
414
Simon Hormand9366582011-06-15 15:18:45 +0900415/* print a string of text buffer to <out>. The format is :
416 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
417 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
418 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
419 */
420static int dump_text(struct chunk *out, const char *buf, int bsize)
421{
422 unsigned char c;
423 int ptr = 0;
424
425 while (buf[ptr] && ptr < bsize) {
426 c = buf[ptr];
427 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
428 if (out->len > out->size - 1)
429 break;
430 out->str[out->len++] = c;
431 }
432 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
433 if (out->len > out->size - 2)
434 break;
435 out->str[out->len++] = '\\';
436 switch (c) {
437 case ' ': c = ' '; break;
438 case '\t': c = 't'; break;
439 case '\n': c = 'n'; break;
440 case '\r': c = 'r'; break;
441 case '\e': c = 'e'; break;
442 case '\\': c = '\\'; break;
443 }
444 out->str[out->len++] = c;
445 }
446 else {
447 if (out->len > out->size - 4)
448 break;
449 out->str[out->len++] = '\\';
450 out->str[out->len++] = 'x';
451 out->str[out->len++] = hextab[(c >> 4) & 0xF];
452 out->str[out->len++] = hextab[c & 0xF];
453 }
454 ptr++;
455 }
456
457 return ptr;
458}
459
460/* print a buffer in hexa.
461 * Print stopped if <bsize> is reached, or if no more place in the chunk.
462 */
463static int dump_binary(struct chunk *out, const char *buf, int bsize)
464{
465 unsigned char c;
466 int ptr = 0;
467
468 while (ptr < bsize) {
469 c = buf[ptr];
470
471 if (out->len > out->size - 2)
472 break;
473 out->str[out->len++] = hextab[(c >> 4) & 0xF];
474 out->str[out->len++] = hextab[c & 0xF];
475
476 ptr++;
477 }
478 return ptr;
479}
480
481/* Dump the status of a table to a stream interface's
482 * read buffer. It returns 0 if the output buffer is full
483 * and needs to be called again, otherwise non-zero.
484 */
485static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
486 struct proxy *proxy, struct proxy *target)
487{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200488 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900489
Willy Tarreau77804732012-10-29 16:14:26 +0100490 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900491 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
492
493 /* any other information should be dumped here */
494
Willy Tarreau290e63a2012-09-20 18:07:14 +0200495 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100496 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900497
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200498 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900499 return 0;
500
501 return 1;
502}
503
504/* Dump the a table entry to a stream interface's
505 * read buffer. It returns 0 if the output buffer is full
506 * and needs to be called again, otherwise non-zero.
507 */
508static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
509 struct proxy *proxy, struct stksess *entry)
510{
511 int dt;
512
Willy Tarreau77804732012-10-29 16:14:26 +0100513 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900514
515 if (proxy->table.type == STKTABLE_TYPE_IP) {
516 char addr[INET_ADDRSTRLEN];
517 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100518 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900519 }
520 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
521 char addr[INET6_ADDRSTRLEN];
522 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100523 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900524 }
525 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100526 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900527 }
528 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100529 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900530 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
531 }
532 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100533 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900534 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
535 }
536
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900538
539 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
540 void *ptr;
541
542 if (proxy->table.data_ofs[dt] == 0)
543 continue;
544 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100545 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900546 else
Willy Tarreau77804732012-10-29 16:14:26 +0100547 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900548
549 ptr = stktable_data_ptr(&proxy->table, entry, dt);
550 switch (stktable_data_types[dt].std_type) {
551 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100552 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900553 break;
554 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100555 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900556 break;
557 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900559 break;
560 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100561 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900562 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
563 proxy->table.data_arg[dt].u));
564 break;
565 }
566 }
Willy Tarreau77804732012-10-29 16:14:26 +0100567 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900568
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200569 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900570 return 0;
571
572 return 1;
573}
574
Willy Tarreaudec98142012-06-06 23:37:08 +0200575static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900576{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200577 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900578 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900579 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900580 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900581 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200582 long long value;
583 int data_type;
584 void *ptr;
585 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900586
Simon Hormand5b9fd92011-06-15 15:18:48 +0900587 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900588
589 if (!*args[4]) {
590 si->applet.ctx.cli.msg = "Key value expected\n";
591 si->applet.st0 = STAT_CLI_PRINT;
592 return;
593 }
594
Simon Hormanc5b89f62011-06-15 15:18:50 +0900595 switch (px->table.type) {
596 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900597 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100598 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900599 break;
600 case STKTABLE_TYPE_IPV6:
601 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100602 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900603 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900604 case STKTABLE_TYPE_INTEGER:
605 {
606 char *endptr;
607 unsigned long val;
608 errno = 0;
609 val = strtoul(args[4], &endptr, 10);
610 if ((errno == ERANGE && val == ULONG_MAX) ||
611 (errno != 0 && val == 0) || endptr == args[4] ||
612 val > 0xffffffff) {
613 si->applet.ctx.cli.msg = "Invalid key\n";
614 si->applet.st0 = STAT_CLI_PRINT;
615 return;
616 }
617 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100618 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900619 break;
620 }
621 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900622 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100623 static_table_key->key = args[4];
624 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900625 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900626 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200627 switch (action) {
628 case STAT_CLI_O_TAB:
629 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
630 break;
631 case STAT_CLI_O_CLR:
632 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
633 break;
634 default:
635 si->applet.ctx.cli.msg = "Unknown action\n";
636 break;
637 }
Simon Horman121f3052011-06-15 15:18:46 +0900638 si->applet.st0 = STAT_CLI_PRINT;
639 return;
640 }
641
642 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200643 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900644 si->applet.ctx.cli.msg = stats_permission_denied_msg;
645 si->applet.st0 = STAT_CLI_PRINT;
646 return;
647 }
648
Willy Tarreau07115412012-10-29 21:56:59 +0100649 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900650
Willy Tarreaudec98142012-06-06 23:37:08 +0200651 switch (action) {
652 case STAT_CLI_O_TAB:
653 if (!ts)
654 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100655 chunk_reset(&trash);
656 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900657 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100658 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900659 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200660
661 case STAT_CLI_O_CLR:
662 if (!ts)
663 return;
664 if (ts->ref_cnt) {
665 /* don't delete an entry which is currently referenced */
666 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
667 si->applet.st0 = STAT_CLI_PRINT;
668 return;
669 }
670 stksess_kill(&px->table, ts);
671 break;
Simon Horman17bce342011-06-15 15:18:47 +0900672
Willy Tarreau654694e2012-06-07 01:03:16 +0200673 case STAT_CLI_O_SET:
674 if (strncmp(args[5], "data.", 5) != 0) {
675 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
676 si->applet.st0 = STAT_CLI_PRINT;
677 return;
678 }
679
680 data_type = stktable_get_data_type(args[5] + 5);
681 if (data_type < 0) {
682 si->applet.ctx.cli.msg = "Unknown data type\n";
683 si->applet.st0 = STAT_CLI_PRINT;
684 return;
685 }
686
687 if (!px->table.data_ofs[data_type]) {
688 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
689 si->applet.st0 = STAT_CLI_PRINT;
690 return;
691 }
692
693 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
694 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
695 si->applet.st0 = STAT_CLI_PRINT;
696 return;
697 }
698
699 if (ts)
700 stktable_touch(&px->table, ts, 1);
701 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100702 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200703 if (!ts) {
704 /* don't delete an entry which is currently referenced */
705 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
706 si->applet.st0 = STAT_CLI_PRINT;
707 return;
708 }
709 stktable_store(&px->table, ts, 1);
710 }
711
712 ptr = stktable_data_ptr(&px->table, ts, data_type);
713 switch (stktable_data_types[data_type].std_type) {
714 case STD_T_SINT:
715 stktable_data_cast(ptr, std_t_sint) = value;
716 break;
717 case STD_T_UINT:
718 stktable_data_cast(ptr, std_t_uint) = value;
719 break;
720 case STD_T_ULL:
721 stktable_data_cast(ptr, std_t_ull) = value;
722 break;
723 case STD_T_FRQP:
724 /* We only reset the previous value so that it slowly fades out */
725 frqp = &stktable_data_cast(ptr, std_t_frqp);
726 frqp->curr_tick = now_ms;
727 frqp->prev_ctr = value;
728 frqp->curr_ctr = 0;
729 break;
730 }
731 break;
732
Willy Tarreaudec98142012-06-06 23:37:08 +0200733 default:
734 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900735 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200736 break;
Simon Horman121f3052011-06-15 15:18:46 +0900737 }
Simon Horman121f3052011-06-15 15:18:46 +0900738}
739
Willy Tarreau654694e2012-06-07 01:03:16 +0200740static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900741{
Willy Tarreau654694e2012-06-07 01:03:16 +0200742 if (action != STAT_CLI_O_TAB) {
743 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
744 si->applet.st0 = STAT_CLI_PRINT;
745 return;
746 }
747
Simon Hormand5b9fd92011-06-15 15:18:48 +0900748 /* condition on stored data value */
749 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
750 if (si->applet.ctx.table.data_type < 0) {
751 si->applet.ctx.cli.msg = "Unknown data type\n";
752 si->applet.st0 = STAT_CLI_PRINT;
753 return;
754 }
755
756 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
757 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
758 si->applet.st0 = STAT_CLI_PRINT;
759 return;
760 }
761
762 si->applet.ctx.table.data_op = get_std_op(args[4]);
763 if (si->applet.ctx.table.data_op < 0) {
764 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
765 si->applet.st0 = STAT_CLI_PRINT;
766 return;
767 }
768
769 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
770 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
771 si->applet.st0 = STAT_CLI_PRINT;
772 return;
773 }
774}
775
Willy Tarreaudec98142012-06-06 23:37:08 +0200776static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900777{
778 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200779 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200780 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900781 si->applet.ctx.table.proxy = NULL;
782 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200783 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900784
785 if (*args[2]) {
786 si->applet.ctx.table.target = find_stktable(args[2]);
787 if (!si->applet.ctx.table.target) {
788 si->applet.ctx.cli.msg = "No such table\n";
789 si->applet.st0 = STAT_CLI_PRINT;
790 return;
791 }
792 }
793 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200794 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900795 goto err_args;
796 return;
797 }
798
799 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200800 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900801 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200802 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900803 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900804 goto err_args;
805
806 return;
807
808err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200809 switch (action) {
810 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811 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 +0200812 break;
813 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900814 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 +0200815 break;
816 default:
817 si->applet.ctx.cli.msg = "Unknown action\n";
818 break;
819 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820 si->applet.st0 = STAT_CLI_PRINT;
821}
822
Willy Tarreau532a4502011-09-07 22:37:44 +0200823/* Expects to find a frontend named <arg> and returns it, otherwise displays various
824 * adequate error messages and returns NULL. This function also expects the session
825 * level to be admin.
826 */
827static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
828{
829 struct proxy *px;
830
Willy Tarreau290e63a2012-09-20 18:07:14 +0200831 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200832 si->applet.ctx.cli.msg = stats_permission_denied_msg;
833 si->applet.st0 = STAT_CLI_PRINT;
834 return NULL;
835 }
836
837 if (!*arg) {
838 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
839 si->applet.st0 = STAT_CLI_PRINT;
840 return NULL;
841 }
842
843 px = findproxy(arg, PR_CAP_FE);
844 if (!px) {
845 si->applet.ctx.cli.msg = "No such frontend.\n";
846 si->applet.st0 = STAT_CLI_PRINT;
847 return NULL;
848 }
849 return px;
850}
851
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200852/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
853 * and returns the pointer to the server. Otherwise, display adequate error messages
854 * and returns NULL. This function also expects the session level to be admin. Note:
855 * the <arg> is modified to remove the '/'.
856 */
857static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
858{
859 struct proxy *px;
860 struct server *sv;
861 char *line;
862
Willy Tarreau290e63a2012-09-20 18:07:14 +0200863 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200864 si->applet.ctx.cli.msg = stats_permission_denied_msg;
865 si->applet.st0 = STAT_CLI_PRINT;
866 return NULL;
867 }
868
869 /* split "backend/server" and make <line> point to server */
870 for (line = arg; *line; line++)
871 if (*line == '/') {
872 *line++ = '\0';
873 break;
874 }
875
876 if (!*line || !*arg) {
877 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
878 si->applet.st0 = STAT_CLI_PRINT;
879 return NULL;
880 }
881
882 if (!get_backend_server(arg, line, &px, &sv)) {
883 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
884 si->applet.st0 = STAT_CLI_PRINT;
885 return NULL;
886 }
887
888 if (px->state == PR_STSTOPPED) {
889 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
890 si->applet.st0 = STAT_CLI_PRINT;
891 return NULL;
892 }
893
894 return sv;
895}
896
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200897/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200898 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100899 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200900 * designating the function which will have to process the request, which can
901 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200902 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900903static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200904{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200905 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200906 char *args[MAX_STATS_ARGS + 1];
907 int arg;
908
909 while (isspace((unsigned char)*line))
910 line++;
911
912 arg = 0;
913 args[arg] = line;
914
915 while (*line && arg < MAX_STATS_ARGS) {
916 if (isspace((unsigned char)*line)) {
917 *line++ = '\0';
918
919 while (isspace((unsigned char)*line))
920 line++;
921
922 args[++arg] = line;
923 continue;
924 }
925
926 line++;
927 }
928
929 while (++arg <= MAX_STATS_ARGS)
930 args[arg] = line;
931
Willy Tarreau295a8372011-03-10 11:25:07 +0100932 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200933 if (strcmp(args[0], "show") == 0) {
934 if (strcmp(args[1], "stat") == 0) {
935 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100936 si->applet.ctx.stats.flags |= STAT_BOUND;
937 si->applet.ctx.stats.iid = atoi(args[2]);
938 si->applet.ctx.stats.type = atoi(args[3]);
939 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200940 }
941
Willy Tarreau295a8372011-03-10 11:25:07 +0100942 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200943 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100944 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_raw_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200945 }
946 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100947 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200948 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100949 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200950 }
951 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200952 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200953 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100954 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100955 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200956 return 1;
957 }
Willy Tarreau76153662012-11-26 01:16:39 +0100958 if (*args[2] && strcmp(args[2], "all") == 0)
959 si->applet.ctx.sess.target = (void *)-1;
960 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100961 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100962 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100963 si->applet.ctx.sess.target = NULL;
964 si->applet.ctx.sess.section = 0; /* start with session status */
965 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100966 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200967 }
968 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200969 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100970 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100971 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200972 return 1;
973 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200974 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100975 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200976 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 si->applet.ctx.errors.iid = -1;
978 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200979 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100980 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200981 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200982 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200983 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200984 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700985 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200986 return 0;
987 }
988 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200989 else if (strcmp(args[0], "clear") == 0) {
990 if (strcmp(args[1], "counters") == 0) {
991 struct proxy *px;
992 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200993 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200994 int clrall = 0;
995
996 if (strcmp(args[2], "all") == 0)
997 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200998
Willy Tarreau6162db22009-10-10 17:13:00 +0200999 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001000 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1001 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001002 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001003 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001004 return 1;
1005 }
1006
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001007 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001008 if (clrall) {
1009 memset(&px->be_counters, 0, sizeof(px->be_counters));
1010 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1011 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001012 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001013 px->be_counters.conn_max = 0;
1014 px->be_counters.p.http.rps_max = 0;
1015 px->be_counters.sps_max = 0;
1016 px->be_counters.cps_max = 0;
1017 px->be_counters.nbpend_max = 0;
1018
1019 px->fe_counters.conn_max = 0;
1020 px->fe_counters.p.http.rps_max = 0;
1021 px->fe_counters.sps_max = 0;
1022 px->fe_counters.cps_max = 0;
1023 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001024 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001025
1026 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001027 if (clrall)
1028 memset(&sv->counters, 0, sizeof(sv->counters));
1029 else {
1030 sv->counters.cur_sess_max = 0;
1031 sv->counters.nbpend_max = 0;
1032 sv->counters.sps_max = 0;
1033 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001034
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001035 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001036 if (li->counters) {
1037 if (clrall)
1038 memset(li->counters, 0, sizeof(*li->counters));
1039 else
1040 li->counters->conn_max = 0;
1041 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001042 }
1043
Willy Tarreau81c25d02011-09-07 15:17:21 +02001044 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001045 return 1;
1046 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001047 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001048 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001049 /* end of processing */
1050 return 1;
1051 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001052 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001053 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001054 return 0;
1055 }
1056 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001057 else if (strcmp(args[0], "get") == 0) {
1058 if (strcmp(args[1], "weight") == 0) {
1059 struct proxy *px;
1060 struct server *sv;
1061
1062 /* split "backend/server" and make <line> point to server */
1063 for (line = args[2]; *line; line++)
1064 if (*line == '/') {
1065 *line++ = '\0';
1066 break;
1067 }
1068
1069 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001070 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001071 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001072 return 1;
1073 }
1074
1075 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001076 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001077 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001078 return 1;
1079 }
1080
1081 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001082 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1083 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001084 return 1;
1085 }
1086 else { /* not "get weight" */
1087 return 0;
1088 }
1089 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001090 else if (strcmp(args[0], "set") == 0) {
1091 if (strcmp(args[1], "weight") == 0) {
1092 struct proxy *px;
1093 struct server *sv;
1094 int w;
1095
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001096 sv = expect_server_admin(s, si, args[2]);
1097 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001098 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001099 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001100
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001101 /* if the weight is terminated with '%', it is set relative to
1102 * the initial weight, otherwise it is absolute.
1103 */
1104 if (!*args[3]) {
1105 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001106 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001107 return 1;
1108 }
1109
Willy Tarreau4483d432009-10-10 19:30:08 +02001110 w = atoi(args[3]);
1111 if (strchr(args[3], '%') != NULL) {
1112 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001113 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001114 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001115 return 1;
1116 }
1117 w = sv->iweight * w / 100;
1118 }
1119 else {
1120 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001121 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001122 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001123 return 1;
1124 }
1125 }
1126
1127 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001128 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 +01001129 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001130 return 1;
1131 }
1132
1133 sv->uweight = w;
1134
1135 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1136 /* we must take care of not pushing the server to full throttle during slow starts */
1137 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1138 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1139 else
1140 sv->eweight = BE_WEIGHT_SCALE;
1141 sv->eweight *= sv->uweight;
1142 } else {
1143 sv->eweight = sv->uweight;
1144 }
1145
1146 /* static LB algorithms are a bit harder to update */
1147 if (px->lbprm.update_server_eweight)
1148 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001149 else if (sv->eweight) {
1150 if (px->lbprm.set_server_status_up)
1151 px->lbprm.set_server_status_up(sv);
1152 }
1153 else {
1154 if (px->lbprm.set_server_status_down)
1155 px->lbprm.set_server_status_down(sv);
1156 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001157
1158 return 1;
1159 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001160 else if (strcmp(args[1], "timeout") == 0) {
1161 if (strcmp(args[2], "cli") == 0) {
1162 unsigned timeout;
1163 const char *res;
1164
1165 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001166 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001167 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001168 return 1;
1169 }
1170
1171 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1172 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001173 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001174 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001175 return 1;
1176 }
1177
1178 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1179 return 1;
1180 }
1181 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001182 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001183 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001184 return 1;
1185 }
1186 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001187 else if (strcmp(args[1], "maxconn") == 0) {
1188 if (strcmp(args[2], "frontend") == 0) {
1189 struct proxy *px;
1190 struct listener *l;
1191 int v;
1192
Willy Tarreau532a4502011-09-07 22:37:44 +02001193 px = expect_frontend_admin(s, si, args[3]);
1194 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001195 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001196
1197 if (!*args[4]) {
1198 si->applet.ctx.cli.msg = "Integer value expected.\n";
1199 si->applet.st0 = STAT_CLI_PRINT;
1200 return 1;
1201 }
1202
1203 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001204 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001205 si->applet.ctx.cli.msg = "Value out of range.\n";
1206 si->applet.st0 = STAT_CLI_PRINT;
1207 return 1;
1208 }
1209
1210 /* OK, the value is fine, so we assign it to the proxy and to all of
1211 * its listeners. The blocked ones will be dequeued.
1212 */
1213 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001214 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001215 l->maxconn = v;
1216 if (l->state == LI_FULL)
1217 resume_listener(l);
1218 }
1219
1220 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1221 dequeue_all_listeners(&s->fe->listener_queue);
1222
1223 return 1;
1224 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001225 else if (strcmp(args[2], "global") == 0) {
1226 int v;
1227
Willy Tarreau290e63a2012-09-20 18:07:14 +02001228 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001229 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1230 si->applet.st0 = STAT_CLI_PRINT;
1231 return 1;
1232 }
1233
1234 if (!*args[3]) {
1235 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1236 si->applet.st0 = STAT_CLI_PRINT;
1237 return 1;
1238 }
1239
1240 v = atoi(args[3]);
1241 if (v > global.hardmaxconn) {
1242 si->applet.ctx.cli.msg = "Value out of range.\n";
1243 si->applet.st0 = STAT_CLI_PRINT;
1244 return 1;
1245 }
1246
1247 /* check for unlimited values */
1248 if (v <= 0)
1249 v = global.hardmaxconn;
1250
1251 global.maxconn = v;
1252
1253 /* Dequeues all of the listeners waiting for a resource */
1254 if (!LIST_ISEMPTY(&global_listener_queue))
1255 dequeue_all_listeners(&global_listener_queue);
1256
1257 return 1;
1258 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001259 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001260 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001261 si->applet.st0 = STAT_CLI_PRINT;
1262 return 1;
1263 }
1264 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001265 else if (strcmp(args[1], "rate-limit") == 0) {
1266 if (strcmp(args[2], "connections") == 0) {
1267 if (strcmp(args[3], "global") == 0) {
1268 int v;
1269
Willy Tarreau290e63a2012-09-20 18:07:14 +02001270 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001271 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1272 si->applet.st0 = STAT_CLI_PRINT;
1273 return 1;
1274 }
1275
1276 if (!*args[4]) {
1277 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1278 si->applet.st0 = STAT_CLI_PRINT;
1279 return 1;
1280 }
1281
1282 v = atoi(args[4]);
1283 if (v < 0) {
1284 si->applet.ctx.cli.msg = "Value out of range.\n";
1285 si->applet.st0 = STAT_CLI_PRINT;
1286 return 1;
1287 }
1288
1289 global.cps_lim = v;
1290
1291 /* Dequeues all of the listeners waiting for a resource */
1292 if (!LIST_ISEMPTY(&global_listener_queue))
1293 dequeue_all_listeners(&global_listener_queue);
1294
1295 return 1;
1296 }
1297 else {
1298 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1299 si->applet.st0 = STAT_CLI_PRINT;
1300 return 1;
1301 }
1302 }
William Lallemandd85f9172012-11-09 17:05:39 +01001303 else if (strcmp(args[2], "http-compression") == 0) {
1304 if (strcmp(args[3], "global") == 0) {
1305 int v;
1306
Willy Tarreau85d47f92012-11-21 00:29:50 +01001307 if (!*args[4]) {
1308 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1309 si->applet.st0 = STAT_CLI_PRINT;
1310 return 1;
1311 }
1312
William Lallemandd85f9172012-11-09 17:05:39 +01001313 v = atoi(args[4]);
1314 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1315 }
1316 else {
1317 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1318 si->applet.st0 = STAT_CLI_PRINT;
1319 return 1;
1320 }
1321 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001322 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001323 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001324 si->applet.st0 = STAT_CLI_PRINT;
1325 return 1;
1326 }
1327 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001328 else if (strcmp(args[1], "table") == 0) {
1329 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1330 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001331 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001332 return 0;
1333 }
1334 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001335 else if (strcmp(args[0], "enable") == 0) {
1336 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001337 struct server *sv;
1338
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001339 sv = expect_server_admin(s, si, args[2]);
1340 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001341 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001342
Cyril Bontécd19e512010-01-31 22:34:03 +01001343 if (sv->state & SRV_MAINTAIN) {
1344 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001345 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001346 /* If this server tracks the status of another one,
1347 * we must restore the good status.
1348 */
Willy Tarreau44267702011-10-28 15:35:33 +02001349 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001350 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001351 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001352 } else {
1353 sv->state &= ~SRV_MAINTAIN;
1354 set_server_down(sv);
1355 }
1356 } else {
1357 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001358 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001359 }
1360 }
1361
Willy Tarreau532a4502011-09-07 22:37:44 +02001362 return 1;
1363 }
1364 else if (strcmp(args[1], "frontend") == 0) {
1365 struct proxy *px;
1366
1367 px = expect_frontend_admin(s, si, args[2]);
1368 if (!px)
1369 return 1;
1370
1371 if (px->state == PR_STSTOPPED) {
1372 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1373 si->applet.st0 = STAT_CLI_PRINT;
1374 return 1;
1375 }
1376
1377 if (px->state != PR_STPAUSED) {
1378 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1379 si->applet.st0 = STAT_CLI_PRINT;
1380 return 1;
1381 }
1382
1383 if (!resume_proxy(px)) {
1384 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1385 si->applet.st0 = STAT_CLI_PRINT;
1386 return 1;
1387 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001388 return 1;
1389 }
1390 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001391 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1392 si->applet.st0 = STAT_CLI_PRINT;
1393 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001394 }
1395 }
1396 else if (strcmp(args[0], "disable") == 0) {
1397 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001398 struct server *sv;
1399
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001400 sv = expect_server_admin(s, si, args[2]);
1401 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001402 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001403
Cyril Bontécd19e512010-01-31 22:34:03 +01001404 if (! (sv->state & SRV_MAINTAIN)) {
1405 /* Not already in maintenance, we can change the server state */
1406 sv->state |= SRV_MAINTAIN;
1407 set_server_down(sv);
1408 }
1409
Willy Tarreau532a4502011-09-07 22:37:44 +02001410 return 1;
1411 }
1412 else if (strcmp(args[1], "frontend") == 0) {
1413 struct proxy *px;
1414
1415 px = expect_frontend_admin(s, si, args[2]);
1416 if (!px)
1417 return 1;
1418
1419 if (px->state == PR_STSTOPPED) {
1420 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1421 si->applet.st0 = STAT_CLI_PRINT;
1422 return 1;
1423 }
1424
1425 if (px->state == PR_STPAUSED) {
1426 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1427 si->applet.st0 = STAT_CLI_PRINT;
1428 return 1;
1429 }
1430
1431 if (!pause_proxy(px)) {
1432 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1433 si->applet.st0 = STAT_CLI_PRINT;
1434 return 1;
1435 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001436 return 1;
1437 }
1438 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001439 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1440 si->applet.st0 = STAT_CLI_PRINT;
1441 return 1;
1442 }
1443 }
1444 else if (strcmp(args[0], "shutdown") == 0) {
1445 if (strcmp(args[1], "frontend") == 0) {
1446 struct proxy *px;
1447
1448 px = expect_frontend_admin(s, si, args[2]);
1449 if (!px)
1450 return 1;
1451
1452 if (px->state == PR_STSTOPPED) {
1453 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1454 si->applet.st0 = STAT_CLI_PRINT;
1455 return 1;
1456 }
1457
1458 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1459 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1460 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1461 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1462 stop_proxy(px);
1463 return 1;
1464 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001465 else if (strcmp(args[1], "session") == 0) {
1466 struct session *sess, *ptr;
1467
Willy Tarreau290e63a2012-09-20 18:07:14 +02001468 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001469 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1470 si->applet.st0 = STAT_CLI_PRINT;
1471 return 1;
1472 }
1473
1474 if (!*args[2]) {
1475 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1476 si->applet.st0 = STAT_CLI_PRINT;
1477 return 1;
1478 }
1479
1480 ptr = (void *)strtoul(args[2], NULL, 0);
1481
1482 /* first, look for the requested session in the session table */
1483 list_for_each_entry(sess, &sessions, list) {
1484 if (sess == ptr)
1485 break;
1486 }
1487
1488 /* do we have the session ? */
1489 if (sess != ptr) {
1490 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1491 si->applet.st0 = STAT_CLI_PRINT;
1492 return 1;
1493 }
1494
1495 session_shutdown(sess, SN_ERR_KILLED);
1496 return 1;
1497 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001498 else if (strcmp(args[1], "sessions") == 0) {
1499 if (strcmp(args[2], "server") == 0) {
1500 struct server *sv;
1501 struct session *sess, *sess_bck;
1502
1503 sv = expect_server_admin(s, si, args[3]);
1504 if (!sv)
1505 return 1;
1506
1507 /* kill all the session that are on this server */
1508 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1509 if (sess->srv_conn == sv)
1510 session_shutdown(sess, SN_ERR_KILLED);
1511
1512 return 1;
1513 }
1514 else {
1515 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1516 si->applet.st0 = STAT_CLI_PRINT;
1517 return 1;
1518 }
1519 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001520 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001521 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001522 si->applet.st0 = STAT_CLI_PRINT;
1523 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001524 }
1525 }
1526 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001527 return 0;
1528 }
1529 return 1;
1530}
1531
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001533 * used to processes I/O from/to the stats unix socket. The system relies on a
1534 * state machine handling requests and various responses. We read a request,
1535 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001536 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1537 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001538 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001539 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001540static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001541{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001542 struct channel *req = si->ob;
1543 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001544 int reql;
1545 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001546
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001547 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1548 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001550 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001551 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001552 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001553 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001554 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001555 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001556 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001557 /* Let's close for real now. We just close the request
1558 * side, the conditions below will complete if needed.
1559 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001560 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001561 break;
1562 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001564 /* ensure we have some output room left in the event we
1565 * would want to return some info right after parsing.
1566 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001567 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001568 break;
1569
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001570 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001571 if (reql <= 0) { /* closed or EOL not found */
1572 if (reql == 0)
1573 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001574 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001575 continue;
1576 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001577
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001578 /* seek for a possible semi-colon. If we find one, we
1579 * replace it with an LF and skip only this part.
1580 */
1581 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001582 if (trash.str[len] == ';') {
1583 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001584 reql = len + 1;
1585 break;
1586 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001587
Willy Tarreau816fc222009-10-04 07:36:58 +02001588 /* now it is time to check that we have a full line,
1589 * remove the trailing \n and possibly \r, then cut the
1590 * line.
1591 */
1592 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001593 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001594 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001595 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001596 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001598 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001599 len--;
1600
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001601 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001602
Willy Tarreaubc4af052011-02-13 13:25:14 +01001603 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001604 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001605 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001606 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001607 continue;
1608 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001609 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001610 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001611 else if (strcmp(trash.str, "help") == 0 ||
1612 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001613 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001614 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001615 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001616 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001617 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001618 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001619 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001620 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001621 /* if prompt is disabled, print help on empty lines,
1622 * so that the user at least knows how to enable
1623 * prompt and find help.
1624 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001625 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001626 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001627 }
1628
1629 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001630 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001631 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001632 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001633 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001634 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001635 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001636 continue;
1637 }
1638
Willy Tarreaubc4af052011-02-13 13:25:14 +01001639 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001640 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001641 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001642 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001643 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001644 case STAT_CLI_O_INFO:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001645 if (stats_dump_raw_info_to_buffer(si))
1646 si->applet.st0 = STAT_CLI_PROMPT;
1647 break;
1648 case STAT_CLI_O_STAT:
1649 if (stats_dump_raw_stat_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001650 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001651 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001652 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001653 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001654 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001655 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001656 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001657 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001658 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001659 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001660 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001661 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001662 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001663 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001664 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001665 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001666 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667 break;
1668 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001669
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001670 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001671 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001672 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001673 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001674 }
1675
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001676 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001677 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001678 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001679
1680 /* Now we close the output if one of the writers did so,
1681 * or if we're not in interactive mode and the request
1682 * buffer is empty. This still allows pipelined requests
1683 * to be sent in non-interactive mode.
1684 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001685 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001686 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001687 continue;
1688 }
1689
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001690 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001691 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001692 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001693 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001694
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001695 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001696 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1697 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001698 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001699 * and nothing more to consume. This is comparable to a broken pipe, so
1700 * we forward the close to the request side so that it flows upstream to
1701 * the client.
1702 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001703 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001704 }
1705
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001706 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001707 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1708 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1709 /* We have no more processing to do, and nothing more to send, and
1710 * the client side has closed. So we'll forward this state downstream
1711 * on the response buffer.
1712 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001713 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001714 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001715 }
1716
1717 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001718 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001719
1720 /* we don't want to expire timeouts while we're processing requests */
1721 si->ib->rex = TICK_ETERNITY;
1722 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001723
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001724 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001725 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001726 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001727 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001728
1729 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1730 /* check that we have released everything then unregister */
1731 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001732 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001733}
1734
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001735/* Dumps the raw stats information block to the trash for and uses the state from
1736 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1737 */
1738static void stats_dump_raw_info(struct stream_interface *si)
1739{
1740 unsigned int up = (now.tv_sec - start_date.tv_sec);
1741
1742 chunk_appendf(&trash,
1743 "Name: " PRODUCT_NAME "\n"
1744 "Version: " HAPROXY_VERSION "\n"
1745 "Release_date: " HAPROXY_DATE "\n"
1746 "Nbproc: %d\n"
1747 "Process_num: %d\n"
1748 "Pid: %d\n"
1749 "Uptime: %dd %dh%02dm%02ds\n"
1750 "Uptime_sec: %d\n"
1751 "Memmax_MB: %d\n"
1752 "Ulimit-n: %d\n"
1753 "Maxsock: %d\n"
1754 "Maxconn: %d\n"
1755 "Hard_maxconn: %d\n"
1756 "Maxpipes: %d\n"
1757 "CurrConns: %d\n"
1758 "PipesUsed: %d\n"
1759 "PipesFree: %d\n"
1760 "ConnRate: %d\n"
1761 "ConnRateLimit: %d\n"
1762 "MaxConnRate: %d\n"
1763 "CompressBpsIn: %u\n"
1764 "CompressBpsOut: %u\n"
1765 "CompressBpsRateLim: %u\n"
1766#ifdef USE_ZLIB
1767 "ZlibMemUsage: %ld\n"
1768 "MaxZlibMemUsage: %ld\n"
1769#endif
1770 "Tasks: %d\n"
1771 "Run_queue: %d\n"
1772 "Idle_pct: %d\n"
1773 "node: %s\n"
1774 "description: %s\n"
1775 "",
1776 global.nbproc,
1777 relative_pid,
1778 pid,
1779 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1780 up,
1781 global.rlimit_memmax,
1782 global.rlimit_nofile,
1783 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1784 actconn, pipes_used, pipes_free,
1785 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1786 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1787 global.comp_rate_lim,
1788#ifdef USE_ZLIB
1789 zlib_used_memory, global.maxzlibmem,
1790#endif
1791 nb_tasks_cur, run_queue_cur, idle_pct,
1792 global.node, global.desc ? global.desc : ""
1793 );
1794}
1795
1796/* This function dumps information onto the stream interface's read buffer.
1797 * It returns 0 as long as it does not complete, non-zero upon completion.
1798 * No state is used.
1799 */
1800static int stats_dump_raw_info_to_buffer(struct stream_interface *si)
1801{
1802 chunk_reset(&trash);
1803 stats_dump_raw_info(si);
1804
1805 if (bi_putchk(si->ib, &trash) == -1)
1806 return 0;
1807
1808 return 1;
1809}
1810
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001811/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001812 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001813 * It returns 0 as long as it does not complete, non-zero upon completion.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001814 * Only states STAT_ST_INIT and STAT_ST_LIST are used.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001815 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001816static int stats_dump_raw_stat_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001817{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001818 struct proxy *px;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001819
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001820 chunk_reset(&trash);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001821
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001822 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001823 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001824 stats_dump_csv_header();
1825 if (bi_putchk(si->ib, &trash) == -1)
1826 return 0;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001827
Willy Tarreau295a8372011-03-10 11:25:07 +01001828 si->applet.ctx.stats.px = proxy;
1829 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1830 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02001831 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001832 /* fall through */
1833
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001834 default:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001835 /* dump proxies */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001836 while (si->applet.ctx.stats.px) {
1837 px = si->applet.ctx.stats.px;
1838 /* skip the disabled proxies, global frontend and non-networked ones */
1839 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
1840 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
1841 if (stats_dump_proxy(si, px, NULL) == 0)
1842 return 0;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001843 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001844 si->applet.ctx.stats.px = px->next;
1845 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001846 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001847 /* here, we just have reached the last proxy */
Willy Tarreau24955a12009-10-04 11:54:04 +02001848 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001849 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001850}
1851
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001852/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1853 * the state from stream interface <si>. The caller is responsible for clearing
1854 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001855 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001856static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001857{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001858 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001859
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001860 if (!(px->cap & PR_CAP_FE))
1861 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001862
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001863 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1864 return 0;
1865
1866 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001867 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001868 /* name, queue */
1869 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001870
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001871 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1872 /* Column sub-heading for Enable or Disable server */
1873 chunk_appendf(&trash, "<td></td>");
1874 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001875
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001876 chunk_appendf(&trash,
1877 "<td class=ac>"
1878 "<a name=\"%s/Frontend\"></a>"
1879 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1880 "<td colspan=3></td>"
1881 "",
1882 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001883
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001884 if (px->mode == PR_MODE_HTTP)
1885 chunk_appendf(&trash,
1886 /* sessions rate : current, max, limit */
1887 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
1888 "",
1889 read_freq_ctr(&px->fe_req_per_sec),
1890 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1891 px->fe_counters.p.http.rps_max,
1892 U2H1(px->fe_counters.sps_max),
1893 LIM2A2(px->fe_sps_lim, "-"));
1894 else
1895 chunk_appendf(&trash,
1896 /* sessions rate : current, max, limit */
1897 "<td>%s</td><td>%s</td><td>%s</td>"
1898 "",
1899 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
1900 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001901
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001902 chunk_appendf(&trash,
1903 /* sessions: current, max, limit */
1904 "<td>%s</td><td>%s</td><td>%s</td>"
1905 "<td"
1906 "",
1907 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001908
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001909 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1910 if (px->mode == PR_MODE_HTTP) {
1911 chunk_appendf(&trash, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001912
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001913 for (i = 1; i < 6; i++)
1914 chunk_appendf(&trash, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001915
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001916 chunk_appendf(&trash, " other=%lld,", px->fe_counters.p.http.rsp[0]);
1917 chunk_appendf(&trash, " compressed=%lld (%d%%)",
1918 px->fe_counters.p.http.comp_rsp,
1919 px->fe_counters.p.http.rsp[2] ?
1920 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0);
1921 chunk_appendf(&trash, " intercepted=%lld\"", px->fe_counters.intercepted_req);
1922 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001923
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001924 chunk_appendf(&trash,
1925 /* sessions: total, lbtot */
1926 ">%s%s%s</td><td></td>"
1927 /* bytes : in */
1928 "<td>%s</td><td"
1929 "",
1930 (px->mode == PR_MODE_HTTP)?"<u>":"",
1931 U2H6(px->fe_counters.cum_sess),
1932 (px->mode == PR_MODE_HTTP)?"</u>":"",
1933 U2H7(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001934
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001935 /* compression stats (via td title): comp_in, comp_out, comp_byp */
1936 chunk_appendf(&trash, " title=\"compression: in=%lld out=%lld bypassed=%lld savings=%d%%\"",
1937 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1938 px->fe_counters.comp_in ?
1939 (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 +02001940
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001941 chunk_appendf(&trash,
1942 /* bytes: out */
1943 ">%s%s%s</td>"
1944 "",
1945 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
1946 U2H0(px->fe_counters.bytes_out),
1947 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001948
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001949 chunk_appendf(&trash,
1950 /* denied: req, resp */
1951 "<td>%s</td><td>%s</td>"
1952 /* errors : request, connect, response */
1953 "<td>%s</td><td></td><td></td>"
1954 /* warnings: retries, redispatches */
1955 "<td></td><td></td>"
1956 /* server status : reflect frontend status */
1957 "<td class=ac>%s</td>"
1958 /* rest of server: nothing */
1959 "<td class=ac colspan=8></td></tr>"
1960 "",
1961 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
1962 U2H2(px->fe_counters.failed_req),
1963 px->state == PR_STREADY ? "OPEN" :
1964 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001965 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001966 else { /* CSV mode */
1967 chunk_appendf(&trash,
1968 /* pxid, name, queue cur, queue max, */
1969 "%s,FRONTEND,,,"
1970 /* sessions : current, max, limit, total */
1971 "%d,%d,%d,%lld,"
1972 /* bytes : in, out */
1973 "%lld,%lld,"
1974 /* denied: req, resp */
1975 "%lld,%lld,"
1976 /* errors : request, connect, response */
1977 "%lld,,,"
1978 /* warnings: retries, redispatches */
1979 ",,"
1980 /* server status : reflect frontend status */
1981 "%s,"
1982 /* rest of server: nothing */
1983 ",,,,,,,,"
1984 /* pid, iid, sid, throttle, lbtot, tracked, type */
1985 "%d,%d,0,,,,%d,"
1986 /* rate, rate_lim, rate_max */
1987 "%u,%u,%u,"
1988 /* check_status, check_code, check_duration */
1989 ",,,",
1990 px->id,
1991 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1992 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1993 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1994 px->fe_counters.failed_req,
1995 px->state == PR_STREADY ? "OPEN" :
1996 px->state == PR_STFULL ? "FULL" : "STOP",
1997 relative_pid, px->uuid, STATS_TYPE_FE,
1998 read_freq_ctr(&px->fe_sess_per_sec),
1999 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002000
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002001 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2002 if (px->mode == PR_MODE_HTTP) {
2003 for (i=1; i<6; i++)
2004 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2005 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2006 }
2007 else
2008 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002009
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002010 /* failed health analyses */
2011 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002013 /* requests : req_rate, req_rate_max, req_tot, */
2014 chunk_appendf(&trash, "%u,%u,%lld,",
2015 read_freq_ctr(&px->fe_req_per_sec),
2016 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2017
2018 /* errors: cli_aborts, srv_aborts */
2019 chunk_appendf(&trash, ",,");
2020
2021 /* compression: in, out, bypassed */
2022 chunk_appendf(&trash, "%lld,%lld,%lld,",
2023 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2024
2025 /* compression: comp_rsp */
2026 chunk_appendf(&trash, "%lld,",
2027 px->fe_counters.p.http.comp_rsp);
2028
2029 /* finish with EOL */
2030 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002031 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002032 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002033}
2034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002035/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2036 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2037 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2038 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002039 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002040static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002041{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002042 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2043 chunk_appendf(&trash, "<tr class=socket>");
2044 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2045 /* Column sub-heading for Enable or Disable server */
2046 chunk_appendf(&trash, "<td></td>");
2047 }
2048 chunk_appendf(&trash, "<td class=ac");
Willy Tarreau91861262007-10-17 17:06:05 +02002049
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002050 if (flags & ST_SHLGNDS) {
2051 char str[INET6_ADDRSTRLEN];
2052 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002053
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002054 chunk_appendf(&trash, " title=\"");
Willy Tarreau91861262007-10-17 17:06:05 +02002055
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002056 port = get_host_port(&l->addr);
2057 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2058 case AF_INET:
2059 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2060 break;
2061 case AF_INET6:
2062 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2063 break;
2064 case AF_UNIX:
2065 chunk_appendf(&trash, "unix, ");
2066 break;
2067 case -1:
2068 chunk_appendf(&trash, "(%s), ", strerror(errno));
2069 break;
2070 }
Willy Tarreau91861262007-10-17 17:06:05 +02002071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002072 /* id */
2073 chunk_appendf(&trash, "id: %d\"", l->luid);
2074 }
Willy Tarreau91861262007-10-17 17:06:05 +02002075
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002076 chunk_appendf(&trash,
2077 /* name, queue */
2078 ">%s<a name=\"%s/+%s\"></a>"
2079 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
2080 /* sessions rate: current, max, limit */
2081 "<td colspan=3>&nbsp;</td>"
2082 /* sessions: current, max, limit, total, lbtot */
2083 "<td>%s</td><td>%s</td><td>%s</td>"
2084 "<td>%s</td><td>&nbsp;</td>"
2085 /* bytes: in, out */
2086 "<td>%s</td><td>%s</td>"
2087 "",
2088 (flags & ST_SHLGNDS)?"<u>":"",
2089 px->id, l->name, px->id, l->name, l->name,
2090 (flags & ST_SHLGNDS)?"</u>":"",
2091 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2092 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002093
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002094 chunk_appendf(&trash,
2095 /* denied: req, resp */
2096 "<td>%s</td><td>%s</td>"
2097 /* errors: request, connect, response */
2098 "<td>%s</td><td></td><td></td>"
2099 /* warnings: retries, redispatches */
2100 "<td></td><td></td>"
2101 /* server status: reflect listener status */
2102 "<td class=ac>%s</td>"
2103 /* rest of server: nothing */
2104 "<td class=ac colspan=8></td></tr>"
2105 "",
2106 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2107 U2H2(l->counters->failed_req),
2108 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2109 }
2110 else { /* CSV mode */
2111 chunk_appendf(&trash,
2112 /* pxid, name, queue cur, queue max, */
2113 "%s,%s,,,"
2114 /* sessions: current, max, limit, total */
2115 "%d,%d,%d,%lld,"
2116 /* bytes: in, out */
2117 "%lld,%lld,"
2118 /* denied: req, resp */
2119 "%lld,%lld,"
2120 /* errors: request, connect, response */
2121 "%lld,,,"
2122 /* warnings: retries, redispatches */
2123 ",,"
2124 /* server status: reflect listener status */
2125 "%s,"
2126 /* rest of server: nothing */
2127 ",,,,,,,,"
2128 /* pid, iid, sid, throttle, lbtot, tracked, type */
2129 "%d,%d,%d,,,,%d,"
2130 /* rate, rate_lim, rate_max */
2131 ",,,"
2132 /* check_status, check_code, check_duration */
2133 ",,,"
2134 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2135 ",,,,,,"
2136 /* failed health analyses */
2137 ","
2138 /* requests : req_rate, req_rate_max, req_tot, */
2139 ",,,"
2140 /* errors: cli_aborts, srv_aborts */
2141 ",,"
2142 /* compression: in, out, bypassed, comp_rsp */
2143 ",,,,"
2144 "\n",
2145 px->id, l->name,
2146 l->nbconn, l->counters->conn_max,
2147 l->maxconn, l->counters->cum_conn,
2148 l->counters->bytes_in, l->counters->bytes_out,
2149 l->counters->denied_req, l->counters->denied_resp,
2150 l->counters->failed_req,
2151 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2152 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2153 }
2154 return 1;
2155}
Willy Tarreau91861262007-10-17 17:06:05 +02002156
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002157/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2158 * from stream interface <si>, stats flags <flags>, and server state <state>.
2159 * The caller is responsible for clearing the trash if needed. Returns non-zero
2160 * if it emits anything, zero otherwise. The <state> parameter can take the
2161 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked.
2162 */
2163static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2164{
2165 struct server *ref = sv->track ? sv->track : sv;
2166 char str[INET6_ADDRSTRLEN];
2167 struct chunk src;
2168 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002169
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002170 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) { /* HTML mode */
2171 static char *srv_hlt_st[7] = {
2172 "DOWN",
2173 "DN %d/%d &uarr;",
2174 "UP %d/%d &darr;",
2175 "UP",
2176 "NOLB %d/%d &darr;",
2177 "NOLB",
2178 "<i>no check</i>"
2179 };
Willy Tarreau91861262007-10-17 17:06:05 +02002180
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002181 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2182 chunk_appendf(&trash, "<tr class=\"maintain\">");
2183 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002184 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002185 "<tr class=\"%s%d\">",
2186 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002187
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002188 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002189 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002190 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2191 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002192
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002193 chunk_appendf(&trash, "<td class=ac");
Willy Tarreau91861262007-10-17 17:06:05 +02002194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002195 if (flags & ST_SHLGNDS) {
2196 chunk_appendf(&trash, " title=\"");
2197
2198 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2199 case AF_INET:
2200 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2201 break;
2202 case AF_INET6:
2203 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2204 break;
2205 case AF_UNIX:
2206 chunk_appendf(&trash, "unix, ");
2207 break;
2208 case -1:
2209 chunk_appendf(&trash, "(%s), ", strerror(errno));
2210 break;
2211 default: /* address family not supported */
2212 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002213 }
Willy Tarreau91861262007-10-17 17:06:05 +02002214
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002215 /* id */
2216 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002217
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002218 /* cookie */
2219 if (sv->cookie) {
2220 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002221
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002222 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2223 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002224
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002225 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002226 }
2227
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002228 chunk_appendf(&trash, "\"");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002229 }
Willy Tarreau91861262007-10-17 17:06:05 +02002230
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002231 chunk_appendf(&trash,
2232 ">%s<a name=\"%s/%s\"></a>"
2233 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
2234 /* queue : current, max, limit */
2235 "<td>%s</td><td>%s</td><td>%s</td>"
2236 /* sessions rate : current, max, limit */
2237 "<td>%s</td><td>%s</td><td></td>"
2238 /* sessions: current, max, limit */
2239 "<td>%s</td><td>%s</td><td>%s</td>"
2240 "<td"
2241 "",
2242 (flags & ST_SHLGNDS) ? "<u>" : "",
2243 px->id, sv->id, px->id, sv->id, sv->id,
2244 (flags & ST_SHLGNDS) ? "</u>" : "",
2245 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2246 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
2247 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
Willy Tarreau91861262007-10-17 17:06:05 +02002248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002249 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2250 if (px->mode == PR_MODE_HTTP) {
2251 chunk_appendf(&trash, " title=\"rsp codes:");
Willy Tarreau91861262007-10-17 17:06:05 +02002252
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002253 for (i = 1; i < 6; i++)
2254 chunk_appendf(&trash, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2255
2256 chunk_appendf(&trash, " other=%lld\"", sv->counters.p.http.rsp[0]);
Willy Tarreau91861262007-10-17 17:06:05 +02002257 }
Willy Tarreau91861262007-10-17 17:06:05 +02002258
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002259 chunk_appendf(&trash,
2260 /* sessions: total, lbtot */
2261 ">%s%s%s</td><td>%s</td>",
2262 (px->mode == PR_MODE_HTTP)?"<u>":"",
2263 U2H0(sv->counters.cum_sess),
2264 (px->mode == PR_MODE_HTTP)?"</u>":"",
2265 U2H1(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002266
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002267 chunk_appendf(&trash,
2268 /* bytes : in, out */
2269 "<td>%s</td><td>%s</td>"
2270 /* denied: req, resp */
2271 "<td></td><td>%s</td>"
2272 /* errors : request, connect */
2273 "<td></td><td>%s</td>"
2274 /* errors : response */
2275 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
2276 /* warnings: retries, redispatches */
2277 "<td>%lld</td><td>%lld</td>"
2278 "",
2279 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2280 U2H2(sv->counters.failed_secu),
2281 U2H3(sv->counters.failed_conns),
2282 sv->counters.cli_aborts,
2283 sv->counters.srv_aborts,
2284 U2H6(sv->counters.failed_resp),
2285 sv->counters.retries, sv->counters.redispatches);
2286
2287 /* status, lest check */
2288 chunk_appendf(&trash, "<td class=ac>");
2289
2290 if (sv->state & SRV_MAINTAIN) {
2291 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2292 chunk_appendf(&trash, "MAINT");
2293 }
2294 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2295 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2296 chunk_appendf(&trash, "MAINT(via)");
2297 }
2298 else if (ref->state & SRV_CHECKED) {
2299 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2300 chunk_appendf(&trash,
2301 srv_hlt_st[state],
2302 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2303 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002304 }
Willy Tarreau91861262007-10-17 17:06:05 +02002305
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002306 if (sv->state & SRV_CHECKED) {
2307 chunk_appendf(&trash, "</td><td class=ac title=\"%s",
2308 get_check_status_description(sv->check.status));
Willy Tarreau91861262007-10-17 17:06:05 +02002309
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002310 if (*sv->check.desc) {
2311 chunk_appendf(&trash, ": ");
2312 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2313 chunk_htmlencode(&trash, &src);
2314 }
Willy Tarreau91861262007-10-17 17:06:05 +02002315
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002316 chunk_appendf(&trash, "\"><u> %s%s",
2317 (sv->state & SRV_CHK_RUNNING) ? "* " : "",
2318 get_check_status_info(sv->check.status));
2319
2320 if (sv->check.status >= HCHK_STATUS_L57DATA)
2321 chunk_appendf(&trash, "/%d", sv->check.code);
2322
2323 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
2324 chunk_appendf(&trash, " in %lums</u>", sv->check.duration);
2325 }
2326 else
2327 chunk_appendf(&trash, "</td><td>");
2328
2329 chunk_appendf(&trash,
2330 /* weight */
2331 "</td><td class=ac>%d</td>"
2332 /* act, bck */
2333 "<td class=ac>%s</td><td class=ac>%s</td>"
2334 "",
2335 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2336 (sv->state & SRV_BACKUP) ? "-" : "Y",
2337 (sv->state & SRV_BACKUP) ? "Y" : "-");
2338
2339 /* check failures: unique, fatal, down time */
2340 if (sv->state & SRV_CHECKED) {
2341 chunk_appendf(&trash, "<td title=\"Failed Health Checks%s\"><u>%lld",
2342 ref->observe?"/Health Analyses":"", ref->counters.failed_checks);
2343
2344 if (ref->observe)
2345 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2346
2347 chunk_appendf(&trash,
2348 "</u></td>"
2349 "<td>%lld</td><td>%s</td>"
2350 "",
2351 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2352 }
2353 else if (sv != ref)
2354 chunk_appendf(&trash,
2355 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2356 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2357 else
2358 chunk_appendf(&trash, "<td colspan=3></td>");
2359
2360 /* throttle */
2361 if ((sv->state & SRV_WARMINGUP) &&
2362 now.tv_sec < sv->last_change + sv->slowstart &&
2363 now.tv_sec >= sv->last_change) {
2364 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n",
2365 (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2366 }
2367 else
2368 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2369 }
2370 else { /* CSV mode */
2371 static char *srv_hlt_st[7] = {
2372 "DOWN,",
2373 "DOWN %d/%d,",
2374 "UP %d/%d,",
2375 "UP,",
2376 "NOLB %d/%d,",
2377 "NOLB,",
2378 "no check,"
2379 };
2380
2381 chunk_appendf(&trash,
2382 /* pxid, name */
2383 "%s,%s,"
2384 /* queue : current, max */
2385 "%d,%d,"
2386 /* sessions : current, max, limit, total */
2387 "%d,%d,%s,%lld,"
2388 /* bytes : in, out */
2389 "%lld,%lld,"
2390 /* denied: req, resp */
2391 ",%lld,"
2392 /* errors : request, connect, response */
2393 ",%lld,%lld,"
2394 /* warnings: retries, redispatches */
2395 "%lld,%lld,"
2396 "",
2397 px->id, sv->id,
2398 sv->nbpend, sv->counters.nbpend_max,
2399 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
2400 sv->counters.bytes_in, sv->counters.bytes_out,
2401 sv->counters.failed_secu,
2402 sv->counters.failed_conns, sv->counters.failed_resp,
2403 sv->counters.retries, sv->counters.redispatches);
2404
2405 /* status */
2406 if (sv->state & SRV_MAINTAIN)
2407 chunk_appendf(&trash, "MAINT,");
2408 else if (ref != sv && ref->state & SRV_MAINTAIN)
2409 chunk_appendf(&trash, "MAINT(via),");
2410 else
2411 chunk_appendf(&trash,
2412 srv_hlt_st[state],
2413 (ref->state & SRV_RUNNING) ? (ref->health - ref->rise + 1) : (ref->health),
2414 (ref->state & SRV_RUNNING) ? (ref->fall) : (ref->rise));
2415
2416 chunk_appendf(&trash,
2417 /* weight, active, backup */
2418 "%d,%d,%d,"
2419 "",
2420 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2421 (sv->state & SRV_BACKUP) ? 0 : 1,
2422 (sv->state & SRV_BACKUP) ? 1 : 0);
2423
2424 /* check failures: unique, fatal; last change, total downtime */
2425 if (sv->state & SRV_CHECKED)
2426 chunk_appendf(&trash,
2427 "%lld,%lld,%d,%d,",
2428 sv->counters.failed_checks, sv->counters.down_trans,
2429 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2430 else
2431 chunk_appendf(&trash, ",,,,");
2432
2433 /* queue limit, pid, iid, sid, */
2434 chunk_appendf(&trash,
2435 "%s,"
2436 "%d,%d,%d,",
2437 LIM2A0(sv->maxqueue, ""),
2438 relative_pid, px->uuid, sv->puid);
2439
2440 /* throttle */
2441 if ((sv->state & SRV_WARMINGUP) &&
2442 now.tv_sec < sv->last_change + sv->slowstart &&
2443 now.tv_sec >= sv->last_change)
2444 chunk_appendf(&trash, "%d", (int)MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart));
2445
2446 /* sessions: lbtot */
2447 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2448
2449 /* tracked */
2450 if (sv->track)
2451 chunk_appendf(&trash, "%s/%s,",
2452 sv->track->proxy->id, sv->track->id);
2453 else
2454 chunk_appendf(&trash, ",");
2455
2456 /* type */
2457 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2458
2459 /* rate */
2460 chunk_appendf(&trash, "%u,,%u,",
2461 read_freq_ctr(&sv->sess_per_sec),
2462 sv->counters.sps_max);
2463
2464 if (sv->state & SRV_CHECKED) {
2465 /* check_status */
2466 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2467
2468 /* check_code */
2469 if (sv->check.status >= HCHK_STATUS_L57DATA)
2470 chunk_appendf(&trash, "%u,", sv->check.code);
2471 else
2472 chunk_appendf(&trash, ",");
2473
2474 /* check_duration */
2475 if (sv->check.status >= HCHK_STATUS_CHECKED)
2476 chunk_appendf(&trash, "%lu,", sv->check.duration);
2477 else
2478 chunk_appendf(&trash, ",");
2479
2480 }
2481 else
2482 chunk_appendf(&trash, ",,,");
2483
2484 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2485 if (px->mode == PR_MODE_HTTP) {
2486 for (i=1; i<6; i++)
2487 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2488
2489 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2490 }
2491 else
2492 chunk_appendf(&trash, ",,,,,,");
2493
2494 /* failed health analyses */
2495 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2496
2497 /* requests : req_rate, req_rate_max, req_tot, */
2498 chunk_appendf(&trash, ",,,");
2499
2500 /* errors: cli_aborts, srv_aborts */
2501 chunk_appendf(&trash, "%lld,%lld,",
2502 sv->counters.cli_aborts, sv->counters.srv_aborts);
2503
2504 /* compression: in, out, bypassed, comp_rsp */
2505 chunk_appendf(&trash, ",,,,");
2506
2507 /* finish with EOL */
2508 chunk_appendf(&trash, "\n");
2509 }
2510 return 1;
2511}
2512
2513/* Dumps a line for backend <px> to the trash for and uses the state from stream
2514 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2515 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2516 */
2517static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2518{
2519 struct chunk src;
2520 int i;
2521
2522 if (!(px->cap & PR_CAP_BE))
2523 return 0;
2524
2525 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2526 return 0;
2527
2528 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) { /* HTML mode */
2529 chunk_appendf(&trash, "<tr class=\"backend\">");
2530 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2531 /* Column sub-heading for Enable or Disable server */
2532 chunk_appendf(&trash, "<td></td>");
2533 }
2534 chunk_appendf(&trash, "<td class=ac");
2535
2536 if (flags & ST_SHLGNDS) {
2537 /* balancing */
2538 chunk_appendf(&trash, " title=\"balancing: %s",
2539 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2540
2541 /* cookie */
2542 if (px->cookie_name) {
2543 chunk_appendf(&trash, ", cookie: '");
2544 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2545 chunk_htmlencode(&trash, &src);
2546 chunk_appendf(&trash, "'");
2547 }
2548 chunk_appendf(&trash, "\"");
2549 }
2550
2551 chunk_appendf(&trash,
2552 /* name */
2553 ">%s<a name=\"%s/Backend\"></a>"
2554 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
2555 /* queue : current, max */
2556 "<td>%s</td><td>%s</td><td></td>"
2557 /* sessions rate : current, max, limit */
2558 "<td>%s</td><td>%s</td><td></td>"
2559 "",
2560 (flags & ST_SHLGNDS)?"<u>":"",
2561 px->id, px->id,
2562 (flags & ST_SHLGNDS)?"</u>":"",
2563 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2564 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
2565
2566 chunk_appendf(&trash,
2567 /* sessions: current, max, limit */
2568 "<td>%s</td><td>%s</td><td>%s</td>"
2569 "<td"
2570 "",
2571 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
2572
2573 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2574 if (px->mode == PR_MODE_HTTP) {
2575 chunk_appendf(&trash, " title=\"%lld requests:", px->be_counters.p.http.cum_req);
2576
2577 for (i = 1; i < 6; i++)
2578 chunk_appendf(&trash, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
2579
2580 chunk_appendf(&trash, " other=%lld ", px->be_counters.p.http.rsp[0]);
2581 chunk_appendf(&trash, " compressed=%lld (%d%%)\"",
2582 px->be_counters.p.http.comp_rsp,
2583 px->be_counters.p.http.rsp[2] ?
2584 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0);
2585 }
2586
2587 chunk_appendf(&trash,
2588 /* sessions: total, lbtot */
2589 ">%s%s%s</td><td>%s</td>"
2590 /* bytes: in */
2591 "<td>%s</td><td"
2592 "",
2593 (px->mode == PR_MODE_HTTP)?"<u>":"",
2594 U2H6(px->be_counters.cum_conn),
2595 (px->mode == PR_MODE_HTTP)?"</u>":"",
2596 U2H7(px->be_counters.cum_lbconn),
2597 U2H8(px->be_counters.bytes_in));
2598
2599 /* compression stats (via td title): comp_in, comp_out, comp_byp */
2600 chunk_appendf(&trash, " title=\"compression: in=%lld out=%lld bypassed=%lld savings=%d%%\"",
2601 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2602 px->be_counters.comp_in ?
2603 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0);
2604
2605 chunk_appendf(&trash,
2606 /* bytes: out */
2607 ">%s%s%s</td>"
2608 "",
2609 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
2610 U2H0(px->be_counters.bytes_out),
2611 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2612
2613 chunk_appendf(&trash,
2614 /* denied: req, resp */
2615 "<td>%s</td><td>%s</td>"
2616 /* errors : request, connect */
2617 "<td></td><td>%s</td>"
2618 /* errors : response */
2619 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
2620 /* warnings: retries, redispatches */
2621 "<td>%lld</td><td>%lld</td>"
2622 /* backend status: reflect backend status (up/down): we display UP
2623 * if the backend has known working servers or if it has no server at
2624 * all (eg: for stats). Then we display the total weight, number of
2625 * active and backups. */
2626 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2627 "<td class=ac>%d</td><td class=ac>%d</td>"
2628 "",
2629 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
2630 U2H2(px->be_counters.failed_conns),
2631 px->be_counters.cli_aborts,
2632 px->be_counters.srv_aborts,
2633 U2H5(px->be_counters.failed_resp),
2634 px->be_counters.retries, px->be_counters.redispatches,
2635 human_time(now.tv_sec - px->last_change, 1),
2636 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2637 "<font color=\"red\"><b>DOWN</b></font>",
2638 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2639 px->srv_act, px->srv_bck);
2640
2641 chunk_appendf(&trash,
2642 /* rest of backend: nothing, down transitions, total downtime, throttle */
2643 "<td class=ac>&nbsp;</td><td>%d</td>"
2644 "<td>%s</td>"
2645 "<td></td>"
2646 "</tr>",
2647 px->down_trans,
2648 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2649 }
2650 else { /* CSV mode */
2651 chunk_appendf(&trash,
2652 /* pxid, name */
2653 "%s,BACKEND,"
2654 /* queue : current, max */
2655 "%d,%d,"
2656 /* sessions : current, max, limit, total */
2657 "%d,%d,%d,%lld,"
2658 /* bytes : in, out */
2659 "%lld,%lld,"
2660 /* denied: req, resp */
2661 "%lld,%lld,"
2662 /* errors : request, connect, response */
2663 ",%lld,%lld,"
2664 /* warnings: retries, redispatches */
2665 "%lld,%lld,"
2666 /* backend status: reflect backend status (up/down): we display UP
2667 * if the backend has known working servers or if it has no server at
2668 * all (eg: for stats). Then we display the total weight, number of
2669 * active and backups. */
2670 "%s,"
2671 "%d,%d,%d,"
2672 /* rest of backend: nothing, down transitions, last change, total downtime */
2673 ",%d,%d,%d,,"
2674 /* pid, iid, sid, throttle, lbtot, tracked, type */
2675 "%d,%d,0,,%lld,,%d,"
2676 /* rate, rate_lim, rate_max, */
2677 "%u,,%u,"
2678 /* check_status, check_code, check_duration */
2679 ",,,",
2680 px->id,
2681 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2682 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2683 px->be_counters.bytes_in, px->be_counters.bytes_out,
2684 px->be_counters.denied_req, px->be_counters.denied_resp,
2685 px->be_counters.failed_conns, px->be_counters.failed_resp,
2686 px->be_counters.retries, px->be_counters.redispatches,
2687 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2688 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2689 px->srv_act, px->srv_bck,
2690 px->down_trans, (int)(now.tv_sec - px->last_change),
2691 px->srv?be_downtime(px):0,
2692 relative_pid, px->uuid,
2693 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2694 read_freq_ctr(&px->be_sess_per_sec),
2695 px->be_counters.sps_max);
2696
2697 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2698 if (px->mode == PR_MODE_HTTP) {
2699 for (i=1; i<6; i++)
2700 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2701 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2702 }
2703 else
2704 chunk_appendf(&trash, ",,,,,,");
2705
2706 /* failed health analyses */
2707 chunk_appendf(&trash, ",");
2708
2709 /* requests : req_rate, req_rate_max, req_tot, */
2710 chunk_appendf(&trash, ",,,");
2711
2712 /* errors: cli_aborts, srv_aborts */
2713 chunk_appendf(&trash, "%lld,%lld,",
2714 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2715
2716 /* compression: in, out, bypassed */
2717 chunk_appendf(&trash, "%lld,%lld,%lld,",
2718 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2719
2720 /* compression: comp_rsp */
2721 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2722
2723 /* finish with EOL */
2724 chunk_appendf(&trash, "\n");
2725 }
2726 return 1;
2727}
2728
2729/* Dumps the table header for proxy <px> to the trash for and uses the state from
2730 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2731 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2732 * otherwise.
2733 */
2734static int stats_dump_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
2735{
2736 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
2737 return 0;
2738
2739 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2740 /* A form to enable/disable this proxy servers */
2741 chunk_appendf(&trash,
2742 "<form action=\"%s\" method=\"post\">",
2743 uri->uri_prefix);
2744 }
2745
2746 /* print a new table */
2747 chunk_appendf(&trash,
2748 "<table class=\"tbl\" width=\"100%%\">\n"
2749 "<tr class=\"titre\">"
2750 "<th class=\"pxname\" width=\"10%%\"");
2751
2752 if (uri->flags & ST_SHLGNDS) {
2753 /* cap, mode, id */
2754 chunk_appendf(&trash, " title=\"cap: %s, mode: %s, id: %d",
2755 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2756 px->uuid);
2757 chunk_appendf(&trash, "\"");
2758 }
2759
2760 chunk_appendf(&trash,
2761 ">%s<a name=\"%s\"></a>"
2762 "<a class=px href=\"#%s\">%s</a>%s</th>"
2763 "<th class=\"%s\" width=\"90%%\">%s</th>"
2764 "</tr>\n"
2765 "</table>\n"
2766 "<table class=\"tbl\" width=\"100%%\">\n"
2767 "<tr class=\"titre\">",
2768 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2769 px->id, px->id, px->id,
2770 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2771 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2772
2773 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2774 /* Column heading for Enable or Disable server */
2775 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002776 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002777
2778 chunk_appendf(&trash,
2779 "<th rowspan=2></th>"
2780 "<th colspan=3>Queue</th>"
2781 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2782 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2783 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2784 "<th colspan=9>Server</th>"
2785 "</tr>\n"
2786 "<tr class=\"titre\">"
2787 "<th>Cur</th><th>Max</th><th>Limit</th>"
2788 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2789 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2790 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2791 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2792 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2793 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2794 "<th>Thrtle</th>\n"
2795 "</tr>");
2796 return 1;
Willy Tarreau91861262007-10-17 17:06:05 +02002797}
2798
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002799/* Dumps the table trailer for proxy <px> to the trash for and uses the state from
2800 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2801 * Returns non-zero if it emits anything, zero otherwise.
2802 */
2803static int stats_dump_px_end(struct stream_interface *si, struct proxy *px)
2804{
2805 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
2806 return 0;
2807
2808 chunk_appendf(&trash, "</table>");
2809
2810 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2811 /* close the form used to enable/disable this proxy servers */
2812 chunk_appendf(&trash,
2813 "Choose the action to perform on the checked servers : "
2814 "<select name=action>"
2815 "<option value=\"\"></option>"
2816 "<option value=\"disable\">Disable</option>"
2817 "<option value=\"enable\">Enable</option>"
2818 "<option value=\"stop\">Soft Stop</option>"
2819 "<option value=\"start\">Soft Start</option>"
2820 "<option value=\"shutdown\">Kill Sessions</option>"
2821 "</select>"
2822 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2823 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2824 "</form>",
2825 px->uuid);
2826 }
2827
2828 chunk_appendf(&trash, "<p>\n");
2829 return 1;
2830}
Willy Tarreau91861262007-10-17 17:06:05 +02002831
2832/*
2833 * Dumps statistics for a proxy.
2834 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2835 * ot non-zero if everything completed.
2836 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002837static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002838{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002839 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002840 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002841 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002842 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002843
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002844 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002845
Willy Tarreau295a8372011-03-10 11:25:07 +01002846 switch (si->applet.ctx.stats.px_st) {
2847 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002848 /* we are on a new proxy */
2849
2850 if (uri && uri->scope) {
2851 /* we have a limited scope, we have to check the proxy name */
2852 struct stat_scope *scope;
2853 int len;
2854
2855 len = strlen(px->id);
2856 scope = uri->scope;
2857
2858 while (scope) {
2859 /* match exact proxy name */
2860 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2861 break;
2862
2863 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002864 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002865 break;
2866 scope = scope->next;
2867 }
2868
2869 /* proxy name not found : don't dump anything */
2870 if (scope == NULL)
2871 return 1;
2872 }
2873
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002874 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2875 (si->applet.ctx.stats.iid != -1) &&
2876 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002877 return 1;
2878
Willy Tarreau295a8372011-03-10 11:25:07 +01002879 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002880 /* fall through */
2881
Willy Tarreau295a8372011-03-10 11:25:07 +01002882 case STAT_PX_ST_TH:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002883 if (stats_dump_px_hdr(si, px, uri))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002884 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002885 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002886
Willy Tarreau295a8372011-03-10 11:25:07 +01002887 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002888 /* fall through */
2889
Willy Tarreau295a8372011-03-10 11:25:07 +01002890 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002891 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002892 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002893 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002894 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002895
Willy Tarreau4348fad2012-09-20 16:48:07 +02002896 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002897 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002898 /* fall through */
2899
Willy Tarreau295a8372011-03-10 11:25:07 +01002900 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002901 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002902 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 +02002903 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002904 return 0;
2905
Willy Tarreau4348fad2012-09-20 16:48:07 +02002906 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002907 if (!l->counters)
2908 continue;
2909
Willy Tarreau295a8372011-03-10 11:25:07 +01002910 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2911 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002912 break;
2913
Willy Tarreau295a8372011-03-10 11:25:07 +01002914 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002915 continue;
2916 }
2917
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002918 /* print the frontend */
2919 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2920 if (bi_putchk(rep, &trash) == -1)
2921 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002922 }
2923
Willy Tarreau295a8372011-03-10 11:25:07 +01002924 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2925 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002926 /* fall through */
2927
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002929 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002930 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002931 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 +02002932
Willy Tarreau9b28e032012-10-12 23:49:43 +02002933 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002934 return 0;
2935
Willy Tarreau295a8372011-03-10 11:25:07 +01002936 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002937
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2939 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002940 break;
2941
Willy Tarreau295a8372011-03-10 11:25:07 +01002942 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002943 continue;
2944 }
2945
Willy Tarreau44267702011-10-28 15:35:33 +02002946 if (sv->track)
2947 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002948 else
2949 svs = sv;
2950
Willy Tarreau91861262007-10-17 17:06:05 +02002951 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002952 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002953 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002954 else if (svs->state & SRV_RUNNING) {
2955 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002956 sv_state = 3; /* UP */
2957 else
2958 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002959
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002960 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002961 sv_state += 2;
2962 }
Willy Tarreau91861262007-10-17 17:06:05 +02002963 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002964 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002965 sv_state = 1; /* going up */
2966 else
2967 sv_state = 0; /* DOWN */
2968
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002970 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002971 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002972 continue;
2973 }
2974
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002975 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
2976 if (bi_putchk(rep, &trash) == -1)
2977 return 0;
2978 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02002979
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
2981 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002982
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 case STAT_PX_ST_BE:
2984 /* print the backend */
2985 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
2986 if (bi_putchk(rep, &trash) == -1)
2987 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002989 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
2990 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002991
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002992 case STAT_PX_ST_END:
2993 if (stats_dump_px_end(si, px))
2994 if (bi_putchk(rep, &trash) == -1)
2995 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002996
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002997 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
2998 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003000 case STAT_PX_ST_FIN:
3001 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003 default:
3004 /* unknown state, we should put an abort() here ! */
3005 return 1;
3006 }
3007}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003008
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003009/* Dumps the HTTP stats head block to the trash for and uses the state from
3010 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3011 * for clearing the trash if needed.
3012 */
3013static void stats_dump_html_head(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
3014{
3015 /* WARNING! This must fit in the first buffer !!! */
3016 chunk_appendf(&trash,
3017 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3018 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3019 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3020 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3021 "<style type=\"text/css\"><!--\n"
3022 "body {"
3023 " font-family: arial, helvetica, sans-serif;"
3024 " font-size: 12px;"
3025 " font-weight: normal;"
3026 " color: black;"
3027 " background: white;"
3028 "}\n"
3029 "th,td {"
3030 " font-size: 10px;"
3031 "}\n"
3032 "h1 {"
3033 " font-size: x-large;"
3034 " margin-bottom: 0.5em;"
3035 "}\n"
3036 "h2 {"
3037 " font-family: helvetica, arial;"
3038 " font-size: x-large;"
3039 " font-weight: bold;"
3040 " font-style: italic;"
3041 " color: #6020a0;"
3042 " margin-top: 0em;"
3043 " margin-bottom: 0em;"
3044 "}\n"
3045 "h3 {"
3046 " font-family: helvetica, arial;"
3047 " font-size: 16px;"
3048 " font-weight: bold;"
3049 " color: #b00040;"
3050 " background: #e8e8d0;"
3051 " margin-top: 0em;"
3052 " margin-bottom: 0em;"
3053 "}\n"
3054 "li {"
3055 " margin-top: 0.25em;"
3056 " margin-right: 2em;"
3057 "}\n"
3058 ".hr {margin-top: 0.25em;"
3059 " border-color: black;"
3060 " border-bottom-style: solid;"
3061 "}\n"
3062 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3063 ".total {background: #20D0D0;color: #ffff80;}\n"
3064 ".frontend {background: #e8e8d0;}\n"
3065 ".socket {background: #d0d0d0;}\n"
3066 ".backend {background: #e8e8d0;}\n"
3067 ".active0 {background: #ff9090;}\n"
3068 ".active1 {background: #ffd020;}\n"
3069 ".active2 {background: #ffffa0;}\n"
3070 ".active3 {background: #c0ffc0;}\n"
3071 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3072 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
3073 ".active6 {background: #e0e0e0;}\n"
3074 ".backup0 {background: #ff9090;}\n"
3075 ".backup1 {background: #ff80ff;}\n"
3076 ".backup2 {background: #c060ff;}\n"
3077 ".backup3 {background: #b0d0ff;}\n"
3078 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3079 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3080 ".backup6 {background: #e0e0e0;}\n"
3081 ".maintain {background: #c07820;}\n"
3082 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3083 "\n"
3084 "a.px:link {color: #ffff40; text-decoration: none;}"
3085 "a.px:visited {color: #ffff40; text-decoration: none;}"
3086 "a.px:hover {color: #ffffff; text-decoration: none;}"
3087 "a.lfsb:link {color: #000000; text-decoration: none;}"
3088 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3089 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3090 "\n"
3091 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3092 "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"
3093 "table.tbl td.ac { text-align: center;}\n"
3094 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3095 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3096 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3097 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3098 "\n"
3099 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3100 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3101 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
3102 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
3103 "-->\n"
3104 "</style></head>\n",
3105 (uri->flags & ST_SHNODE) ? " on " : "",
3106 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3107 );
3108}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003109
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110/* Dumps the HTTP stats information block to the trash for and uses the state from
3111 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3112 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3113 * otherwise.
3114 */
3115static int stats_dump_http_info(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
3116{
3117 unsigned int up = (now.tv_sec - start_date.tv_sec);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003118
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 /* WARNING! this has to fit the first packet too.
3120 * We are around 3.5 kB, add adding entries will
3121 * become tricky if we want to support 4kB buffers !
3122 */
3123 if ((si->applet.ctx.stats.flags & STAT_FMT_CSV))
3124 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003125
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003126 chunk_appendf(&trash,
3127 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3128 PRODUCT_NAME "%s</a></h1>\n"
3129 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3130 "<hr width=\"100%%\" class=\"hr\">\n"
3131 "<h3>&gt; General process information</h3>\n"
3132 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3133 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3134 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3135 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3136 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3137 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3138 "Running tasks: %d/%d; idle = %d %%<br>\n"
3139 "</td><td align=\"center\" nowrap>\n"
3140 "<table class=\"lgd\"><tr>\n"
3141 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3142 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3143 "</tr><tr>\n"
3144 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3145 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3146 "</tr><tr>\n"
3147 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3148 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3149 "</tr><tr>\n"
3150 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
3151 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
3152 "</tr><tr>\n"
3153 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
3154 "</tr></table>\n"
3155 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3156 "</td>"
3157 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3158 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3159 "",
3160 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3161 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3162 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3163 (uri->flags & ST_SHDESC) ? ": " : "",
3164 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3165 pid, relative_pid, global.nbproc,
3166 up / 86400, (up % 86400) / 3600,
3167 (up % 3600) / 60, (up % 60),
3168 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3169 global.rlimit_memmax ? " MB" : "",
3170 global.rlimit_nofile,
3171 global.maxsock, global.maxconn, global.maxpipes,
3172 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3173 run_queue_cur, nb_tasks_cur, idle_pct
3174 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003175
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3177 chunk_appendf(&trash,
3178 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
3179 uri->uri_prefix,
3180 "",
3181 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
3182 else
3183 chunk_appendf(&trash,
3184 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
3185 uri->uri_prefix,
3186 ";up",
3187 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02003188
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 if (uri->refresh > 0) {
3190 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3191 chunk_appendf(&trash,
3192 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
3193 uri->uri_prefix,
3194 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3195 "");
3196 else
3197 chunk_appendf(&trash,
3198 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
3199 uri->uri_prefix,
3200 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3201 ";norefresh");
3202 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003204 chunk_appendf(&trash,
3205 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
3206 uri->uri_prefix,
3207 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3208 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003209
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003210 chunk_appendf(&trash,
3211 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
3212 uri->uri_prefix,
3213 (uri->refresh > 0) ? ";norefresh" : "");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003214
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 chunk_appendf(&trash,
3216 "</ul></td>"
3217 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3218 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3219 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3220 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3221 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3222 "</ul>"
3223 "</td>"
3224 "</tr></table>\n"
3225 ""
3226 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003227
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003228 if (si->applet.ctx.stats.st_code) {
3229 switch (si->applet.ctx.stats.st_code) {
3230 case STAT_STATUS_DONE:
3231 chunk_appendf(&trash,
3232 "<p><div class=active3>"
3233 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3234 "Action processed successfully."
3235 "</div>\n", uri->uri_prefix);
3236 break;
3237 case STAT_STATUS_NONE:
3238 chunk_appendf(&trash,
3239 "<p><div class=active2>"
3240 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3241 "Nothing has changed."
3242 "</div>\n", uri->uri_prefix);
3243 break;
3244 case STAT_STATUS_PART:
3245 chunk_appendf(&trash,
3246 "<p><div class=active2>"
3247 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3248 "Action partially processed.<br>"
3249 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
3250 "</div>\n", uri->uri_prefix);
3251 break;
3252 case STAT_STATUS_ERRP:
3253 chunk_appendf(&trash,
3254 "<p><div class=active0>"
3255 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3256 "Action not processed because of invalid parameters."
3257 "<ul>"
3258 "<li>The action is maybe unknown.</li>"
3259 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3260 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3261 "</ul>"
3262 "</div>\n", uri->uri_prefix);
3263 break;
3264 case STAT_STATUS_EXCD:
3265 chunk_appendf(&trash,
3266 "<p><div class=active0>"
3267 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3268 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3269 "You should retry with less servers at a time.</b>"
3270 "</div>\n", uri->uri_prefix);
3271 break;
3272 case STAT_STATUS_DENY:
3273 chunk_appendf(&trash,
3274 "<p><div class=active0>"
3275 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3276 "<b>Action denied.</b>"
3277 "</div>\n", uri->uri_prefix);
3278 break;
3279 default:
3280 chunk_appendf(&trash,
3281 "<p><div class=active6>"
3282 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
3283 "Unexpected result."
3284 "</div>\n", uri->uri_prefix);
3285 }
3286 chunk_appendf(&trash, "<p>\n");
3287 }
3288 return 1;
3289}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003290
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003291/* Dumps the HTTP stats trailer block to the trash for and uses the state from
3292 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
3293 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3294 * otherwise.
3295 */
3296static int stats_dump_http_end(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
3297{
3298 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
3299 return 0;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003300
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301 chunk_appendf(&trash, "</body></html>\n");
3302 return 1;
3303}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003304
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305/* This function dumps statistics in HTTP format onto the stream interface's
3306 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3307 * properly set. It returns 0 if it had to stop writing data and an I/O is
3308 * needed, 1 if the dump is finished and the session must be closed, or -1
3309 * in case of any error.
3310 */
3311static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
3312{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003313 struct channel *rep = si->ib;
3314 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003315
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003317
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003318 switch (si->conn->xprt_st) {
3319 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3321 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003322
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 case STAT_ST_HEAD:
3324 if (si->applet.ctx.stats.flags & STAT_FMT_CSV)
3325 stats_dump_csv_header();
3326 else
3327 stats_dump_html_head(si, px, uri);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003328
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 if (bi_putchk(rep, &trash) == -1)
3330 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003331
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 si->conn->xprt_st = STAT_ST_INFO;
3333 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003334
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 case STAT_ST_INFO:
3336 if (stats_dump_http_info(si, px, uri)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003337 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003338 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 }
Willy Tarreau91861262007-10-17 17:06:05 +02003340
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 si->applet.ctx.stats.px = proxy;
3342 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3343 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003344 /* fall through */
3345
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003346 case STAT_ST_LIST:
3347 /* dump proxies */
3348 while (si->applet.ctx.stats.px) {
3349 if (buffer_almost_full(rep->buf))
3350 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003351
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 px = si->applet.ctx.stats.px;
3353 /* skip the disabled proxies, global frontend and non-networked ones */
3354 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
3355 if (stats_dump_proxy(si, px, uri) == 0)
3356 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003357
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 si->applet.ctx.stats.px = px->next;
3359 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3360 }
3361 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003362
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 si->conn->xprt_st = STAT_ST_END;
3364 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003365
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 case STAT_ST_END:
3367 if (stats_dump_http_end(si, px, uri)) {
3368 if (bi_putchk(rep, &trash) == -1)
3369 return 0;
3370 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003371
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003372 si->conn->xprt_st = STAT_ST_FIN;
3373 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003374
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 case STAT_ST_FIN:
3376 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003377
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 default:
3379 /* unknown state ! */
3380 si->conn->xprt_st = STAT_ST_FIN;
3381 return -1;
3382 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003383}
Willy Tarreauae526782010-03-04 20:34:23 +01003384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385/* This I/O handler runs as an applet embedded in a stream interface. It is
3386 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3387 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3388 * automatically unregisters itself once transfer is complete.
3389 */
3390static void http_stats_io_handler(struct stream_interface *si)
3391{
3392 struct session *s = si->conn->xprt_ctx;
3393 struct channel *req = si->ob;
3394 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003395
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3397 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003398
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 /* check that the output is not closed */
3400 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3401 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 if (!si->applet.st0) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003404 if (stats_dump_http(si, s->be->uri_auth)) {
3405 si->applet.st0 = 1;
3406 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003407 }
3408 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003409
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3411 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003412
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003413 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3414 si_shutr(si);
3415 res->flags |= CF_READ_NULL;
3416 }
Willy Tarreau91861262007-10-17 17:06:05 +02003417
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003418 /* update all other flags and resync with the other side */
3419 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003420
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421 /* we don't want to expire timeouts while we're processing requests */
3422 si->ib->rex = TICK_ETERNITY;
3423 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425 out:
3426 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3427 /* check that we have released everything then unregister */
3428 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003429 }
3430}
3431
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432
Willy Tarreau909d5172012-11-26 03:04:41 +01003433static inline const char *get_conn_ctrl_name(const struct connection *conn)
3434{
3435 if (!conn->ctrl)
3436 return "NONE";
3437 return conn->ctrl->name;
3438}
3439
3440static inline const char *get_conn_xprt_name(const struct connection *conn)
3441{
3442 static char ptr[17];
3443
3444 if (!conn->xprt)
3445 return "NONE";
3446
3447 if (conn->xprt == &raw_sock)
3448 return "RAW";
3449
3450#ifdef USE_OPENSSL
3451 if (conn->xprt == &ssl_sock)
3452 return "SSL";
3453#endif
3454 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3455 return ptr;
3456}
3457
3458static inline const char *get_conn_data_name(const struct connection *conn)
3459{
3460 static char ptr[17];
3461
3462 if (!conn->data)
3463 return "NONE";
3464
3465 if (conn->data == &sess_conn_cb)
3466 return "SESS";
3467
3468 if (conn->data == &si_conn_cb)
3469 return "STRM";
3470
3471 if (conn->data == &check_conn_cb)
3472 return "CHCK";
3473
3474 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3475 return ptr;
3476}
3477
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003478/* This function dumps a complete session state onto the stream interface's
3479 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3480 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003481 * 0 if the output buffer is full and it needs to be called again, otherwise
3482 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003483 */
Willy Tarreau76153662012-11-26 01:16:39 +01003484static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003485{
3486 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003487 extern const char *monthname[12];
3488 char pn[INET6_ADDRSTRLEN];
3489
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003490 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003491
Willy Tarreau295a8372011-03-10 11:25:07 +01003492 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003493 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003494 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3495 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003496 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003497 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003498 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003499 return 1;
3500 }
3501
Willy Tarreau295a8372011-03-10 11:25:07 +01003502 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003503 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003504 si->applet.ctx.sess.uid = sess->uniq_id;
3505 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003506 /* fall through */
3507
3508 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003509 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003510 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003511 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003512 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003513 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3514 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003515 sess->uniq_id,
3516 sess->listener->proto->name);
3517
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003518 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003519 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003520 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003521 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003522 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003523 break;
3524 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003525 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003526 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003527 default:
3528 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003529 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003530 break;
3531 }
3532
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003533 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003534 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003535 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003536
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003537 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003538 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003539 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3540 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3541 sess->listener ? sess->listener->luid : 0);
3542
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003543 conn_get_to_addr(sess->si[0].conn);
3544 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003545 case AF_INET:
3546 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003547 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003548 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003549 break;
3550 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003551 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003552 break;
3553 default:
3554 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003555 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003556 break;
3557 }
3558
Willy Tarreau50943332011-09-02 17:33:05 +02003559 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003560 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003561 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003562 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003563 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3564 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003565 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003566
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003567 conn_get_from_addr(sess->si[1].conn);
3568 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003569 case AF_INET:
3570 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003571 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003572 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003573 break;
3574 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003575 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003576 break;
3577 default:
3578 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003579 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003580 break;
3581 }
3582
3583 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003584 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003585 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003586 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3587 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003588 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003589 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003590
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003591 conn_get_to_addr(sess->si[1].conn);
3592 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003593 case AF_INET:
3594 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003595 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003596 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003597 break;
3598 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003599 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003600 break;
3601 default:
3602 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003603 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003604 break;
3605 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003606
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003607 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003608 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003609 sess->task,
3610 sess->task->state,
3611 sess->task->nice, sess->task->calls,
3612 sess->task->expire ?
3613 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3614 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3615 TICKS_TO_MS(1000)) : "<NEVER>",
3616 task_in_rq(sess->task) ? ", running" : "");
3617
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003618 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003619 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003620 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3621
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003622 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003623 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3624 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3625 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3626
3627 chunk_appendf(&trash,
3628 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003629 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003630 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003631 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003632 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003633 sess->si[0].exp ?
3634 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3635 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3636 TICKS_TO_MS(1000)) : "<NEVER>",
3637 sess->si[0].err_type);
3638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003639 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003640 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003641 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003642 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003643 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003644 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003645 sess->si[1].exp ?
3646 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3647 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3648 TICKS_TO_MS(1000)) : "<NEVER>",
3649 sess->si[1].err_type);
3650
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003651 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003652 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003653 sess->si[0].conn,
3654 get_conn_ctrl_name(sess->si[0].conn),
3655 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003656 get_conn_data_name(sess->si[0].conn),
3657 obj_type_name(sess->si[0].conn->target),
3658 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003659
3660 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003661 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003662 sess->si[0].conn->flags,
3663 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003664 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3665 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3666 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 +01003667
3668 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003669 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003670 sess->si[1].conn,
3671 get_conn_ctrl_name(sess->si[1].conn),
3672 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003673 get_conn_data_name(sess->si[1].conn),
3674 obj_type_name(sess->si[1].conn->target),
3675 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003676
3677 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003678 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003679 sess->si[1].conn->flags,
3680 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003681 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3682 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3683 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 +01003684
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003685 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003686 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003687 " an_exp=%s",
3688 sess->req,
3689 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003690 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003691 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003692 sess->req->analyse_exp ?
3693 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3694 TICKS_TO_MS(1000)) : "<NEVER>");
3695
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003696 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003697 " rex=%s",
3698 sess->req->rex ?
3699 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3700 TICKS_TO_MS(1000)) : "<NEVER>");
3701
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003702 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003703 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003704 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003705 sess->req->wex ?
3706 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3707 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003708 sess->req->buf,
3709 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003710 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003711 sess->txn.req.next, sess->req->buf->i,
3712 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003713
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003714 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003715 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003716 " an_exp=%s",
3717 sess->rep,
3718 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003719 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003720 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003721 sess->rep->analyse_exp ?
3722 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3723 TICKS_TO_MS(1000)) : "<NEVER>");
3724
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003725 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003726 " rex=%s",
3727 sess->rep->rex ?
3728 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3729 TICKS_TO_MS(1000)) : "<NEVER>");
3730
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003731 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003732 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003733 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003734 sess->rep->wex ?
3735 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3736 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003737 sess->rep->buf,
3738 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003739 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003740 sess->txn.rsp.next, sess->rep->buf->i,
3741 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003743 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003744 return 0;
3745
3746 /* use other states to dump the contents */
3747 }
3748 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003749 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003750 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003751 return 1;
3752}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003753
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003754/* This function dumps all sessions' states onto the stream interface's
3755 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003756 * properly set. It returns 0 if the output buffer is full and it needs
3757 * to be called again, otherwise non-zero. It is designed to be called
3758 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003759 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003760static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003761{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003762 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003763 /* If we're forced to shut down, we might have to remove our
3764 * reference to the last session being dumped.
3765 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003766 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003767 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3768 LIST_DEL(&si->applet.ctx.sess.bref.users);
3769 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003770 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003771 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003772 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003773 }
3774
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003775 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003776
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003777 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003778 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003779 /* the function had not been called yet, let's prepare the
3780 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003781 * pointer to the first in the global list. When a target
3782 * session is being destroyed, it is responsible for updating
3783 * this pointer. We know we have reached the end when this
3784 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003785 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003786 LIST_INIT(&si->applet.ctx.sess.bref.users);
3787 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003788 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003789 /* fall through */
3790
Willy Tarreau295a8372011-03-10 11:25:07 +01003791 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003792 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003793 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3794 LIST_DEL(&si->applet.ctx.sess.bref.users);
3795 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003796 }
3797
3798 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003799 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003800 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003801 struct session *curr_sess;
3802
Willy Tarreau295a8372011-03-10 11:25:07 +01003803 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003804
Willy Tarreau295a8372011-03-10 11:25:07 +01003805 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003806 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003807 goto next_sess;
3808
Willy Tarreau295a8372011-03-10 11:25:07 +01003809 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003810 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003811 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003812 return 0;
3813
3814 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003815 LIST_DEL(&si->applet.ctx.sess.bref.users);
3816 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003817 if (si->applet.ctx.sess.target != (void *)-1) {
3818 si->applet.ctx.sess.target = NULL;
3819 break;
3820 }
3821 else
3822 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003823 }
3824
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003825 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003826 "%p: proto=%s",
3827 curr_sess,
3828 curr_sess->listener->proto->name);
3829
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003830
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003831 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003832 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003833 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003834 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003835 " src=%s:%d fe=%s be=%s srv=%s",
3836 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003837 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003838 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003839 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003840 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003841 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003842 break;
3843 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003844 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003845 " src=unix:%d fe=%s be=%s srv=%s",
3846 curr_sess->listener->luid,
3847 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003848 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003849 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003850 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003851 break;
3852 }
3853
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003854 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003855 " ts=%02x age=%s calls=%d",
3856 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003857 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3858 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003859
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003860 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003861 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003862 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003863 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003864 curr_sess->req->analysers,
3865 curr_sess->req->rex ?
3866 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3867 TICKS_TO_MS(1000)) : "");
3868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003869 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003870 ",wx=%s",
3871 curr_sess->req->wex ?
3872 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3873 TICKS_TO_MS(1000)) : "");
3874
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003875 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003876 ",ax=%s]",
3877 curr_sess->req->analyse_exp ?
3878 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3879 TICKS_TO_MS(1000)) : "");
3880
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003881 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003882 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003883 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003884 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003885 curr_sess->rep->analysers,
3886 curr_sess->rep->rex ?
3887 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3888 TICKS_TO_MS(1000)) : "");
3889
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003890 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003891 ",wx=%s",
3892 curr_sess->rep->wex ?
3893 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3894 TICKS_TO_MS(1000)) : "");
3895
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003896 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003897 ",ax=%s]",
3898 curr_sess->rep->analyse_exp ?
3899 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3900 TICKS_TO_MS(1000)) : "");
3901
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003902 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003903 " s0=[%d,%1xh,fd=%d,ex=%s]",
3904 curr_sess->si[0].state,
3905 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003906 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003907 curr_sess->si[0].exp ?
3908 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3909 TICKS_TO_MS(1000)) : "");
3910
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003912 " s1=[%d,%1xh,fd=%d,ex=%s]",
3913 curr_sess->si[1].state,
3914 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003915 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003916 curr_sess->si[1].exp ?
3917 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3918 TICKS_TO_MS(1000)) : "");
3919
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003920 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003921 " exp=%s",
3922 curr_sess->task->expire ?
3923 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3924 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003925 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003926 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003927
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003928 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003929
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003930 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003931 /* let's try again later from this session. We add ourselves into
3932 * this session's users so that it can remove us upon termination.
3933 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003934 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003935 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003936 }
3937
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003938 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003939 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003940 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003941
Willy Tarreau76153662012-11-26 01:16:39 +01003942 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003943 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003944 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003945 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003946 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003947 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003948
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003949 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003950 return 0;
3951
Willy Tarreau295a8372011-03-10 11:25:07 +01003952 si->applet.ctx.sess.target = NULL;
3953 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003954 return 1;
3955 }
3956
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003957 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003958 /* fall through */
3959
3960 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003961 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003962 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003963 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003964}
3965
Willy Tarreau5f9a8772012-11-26 02:22:40 +01003966/* This is called when the stream interface is closed. For instance, upon an
3967 * external abort, we won't call the i/o handler anymore so we may need to
3968 * remove back references to the session currently being dumped.
3969 */
3970void cli_release_handler(struct stream_interface *si)
3971{
3972 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
3973 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
3974 LIST_DEL(&si->applet.ctx.sess.bref.users);
3975 }
3976}
3977
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003978/* This function dumps all tables' states onto the stream interface's
3979 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003980 * properly set. It returns 0 if the output buffer is full and it needs
3981 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003982 */
Willy Tarreau44455022012-12-05 23:01:12 +01003983static int stats_table_request(struct stream_interface *si, int show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003984{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003985 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003986 struct ebmb_node *eb;
3987 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01003988 int skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003989
3990 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003991 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003992 * - STAT_ST_INIT : the first call
3993 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003994 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003995 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003996 * and the entry pointer points to the next entry to be dumped,
3997 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003998 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003999 * data though.
4000 */
4001
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004002 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004003 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004004 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004005 si->applet.ctx.table.entry->ref_cnt--;
4006 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004007 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004008 return 1;
4009 }
4010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004011 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004012
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004013 while (si->conn->xprt_st != STAT_ST_FIN) {
4014 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004015 case STAT_ST_INIT:
4016 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4017 if (!si->applet.ctx.table.proxy)
4018 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004019
Willy Tarreau295a8372011-03-10 11:25:07 +01004020 si->applet.ctx.table.entry = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004021 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004022 break;
4023
Willy Tarreau295a8372011-03-10 11:25:07 +01004024 case STAT_ST_INFO:
4025 if (!si->applet.ctx.table.proxy ||
4026 (si->applet.ctx.table.target &&
4027 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004028 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004029 break;
4030 }
4031
Willy Tarreau295a8372011-03-10 11:25:07 +01004032 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004033 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004034 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004035 return 0;
4036
Willy Tarreau295a8372011-03-10 11:25:07 +01004037 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004038 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004039 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004040 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004041 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004042 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4043 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004044 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004045 break;
4046 }
4047 }
4048 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004049 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004050 break;
4051
Willy Tarreau295a8372011-03-10 11:25:07 +01004052 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004053 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004054
Willy Tarreau295a8372011-03-10 11:25:07 +01004055 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004056 /* we're filtering on some data contents */
4057 void *ptr;
4058 long long data;
4059
Willy Tarreau295a8372011-03-10 11:25:07 +01004060 dt = si->applet.ctx.table.data_type;
4061 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4062 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004063 dt);
4064
4065 data = 0;
4066 switch (stktable_data_types[dt].std_type) {
4067 case STD_T_SINT:
4068 data = stktable_data_cast(ptr, std_t_sint);
4069 break;
4070 case STD_T_UINT:
4071 data = stktable_data_cast(ptr, std_t_uint);
4072 break;
4073 case STD_T_ULL:
4074 data = stktable_data_cast(ptr, std_t_ull);
4075 break;
4076 case STD_T_FRQP:
4077 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004078 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004079 break;
4080 }
4081
4082 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004083 if ((data < si->applet.ctx.table.value &&
4084 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4085 si->applet.ctx.table.data_op == STD_OP_GT ||
4086 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4087 (data == si->applet.ctx.table.value &&
4088 (si->applet.ctx.table.data_op == STD_OP_NE ||
4089 si->applet.ctx.table.data_op == STD_OP_GT ||
4090 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4091 (data > si->applet.ctx.table.value &&
4092 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4093 si->applet.ctx.table.data_op == STD_OP_LT ||
4094 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004095 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004096 }
4097
Simon Hormanc88b8872011-06-15 15:18:49 +09004098 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004099 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004100 si->applet.ctx.table.entry))
4101 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004102
Willy Tarreau295a8372011-03-10 11:25:07 +01004103 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004104
Willy Tarreau295a8372011-03-10 11:25:07 +01004105 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004106 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004107 struct stksess *old = si->applet.ctx.table.entry;
4108 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004109 if (show)
4110 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
4111 else
4112 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004113 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004114 break;
4115 }
4116
Simon Hormanc88b8872011-06-15 15:18:49 +09004117
4118 if (show)
4119 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4120 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4121 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4122
Willy Tarreau295a8372011-03-10 11:25:07 +01004123 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004124 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004125 break;
4126
Willy Tarreau295a8372011-03-10 11:25:07 +01004127 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004128 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004129 break;
4130 }
4131 }
4132 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004133}
4134
Willy Tarreaud426a182010-03-05 14:58:26 +01004135/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004136 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4137 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4138 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4139 * lines are respected within the limit of 70 output chars. Lines that are
4140 * continuation of a previous truncated line begin with "+" instead of " "
4141 * after the offset. The new pointer is returned.
4142 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004143static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4144 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004145{
4146 int end;
4147 unsigned char c;
4148
4149 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004150 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004151 return ptr;
4152
Willy Tarreau77804732012-10-29 16:14:26 +01004153 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004154
Willy Tarreaud426a182010-03-05 14:58:26 +01004155 while (ptr < len && ptr < bsize) {
4156 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004157 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004158 if (out->len > end - 2)
4159 break;
4160 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004161 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004162 if (out->len > end - 3)
4163 break;
4164 out->str[out->len++] = '\\';
4165 switch (c) {
4166 case '\t': c = 't'; break;
4167 case '\n': c = 'n'; break;
4168 case '\r': c = 'r'; break;
4169 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004170 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004171 }
4172 out->str[out->len++] = c;
4173 } else {
4174 if (out->len > end - 5)
4175 break;
4176 out->str[out->len++] = '\\';
4177 out->str[out->len++] = 'x';
4178 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4179 out->str[out->len++] = hextab[c & 0xF];
4180 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004181 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004182 /* we had a line break, let's return now */
4183 out->str[out->len++] = '\n';
4184 *line = ptr;
4185 return ptr;
4186 }
4187 }
4188 /* we have an incomplete line, we return it as-is */
4189 out->str[out->len++] = '\n';
4190 return ptr;
4191}
4192
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004193/* This function dumps all captured errors onto the stream interface's
4194 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004195 * properly set. It returns 0 if the output buffer is full and it needs
4196 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004197 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004198static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004199{
4200 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004201
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004202 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004203 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004204
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004205 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004206
Willy Tarreau295a8372011-03-10 11:25:07 +01004207 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004208 /* the function had not been called yet, let's prepare the
4209 * buffer for a response.
4210 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004211 struct tm tm;
4212
4213 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004214 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004215 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4216 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4217 error_snapshot_id);
4218
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004219 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004220 /* Socket buffer full. Let's try again later from the same point */
4221 return 0;
4222 }
4223
Willy Tarreau295a8372011-03-10 11:25:07 +01004224 si->applet.ctx.errors.px = proxy;
4225 si->applet.ctx.errors.buf = 0;
4226 si->applet.ctx.errors.bol = 0;
4227 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004228 }
4229
4230 /* we have two inner loops here, one for the proxy, the other one for
4231 * the buffer.
4232 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004233 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004234 struct error_snapshot *es;
4235
Willy Tarreau295a8372011-03-10 11:25:07 +01004236 if (si->applet.ctx.errors.buf == 0)
4237 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004238 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004239 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004240
4241 if (!es->when.tv_sec)
4242 goto next;
4243
Willy Tarreau295a8372011-03-10 11:25:07 +01004244 if (si->applet.ctx.errors.iid >= 0 &&
4245 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4246 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004247 goto next;
4248
Willy Tarreau295a8372011-03-10 11:25:07 +01004249 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004250 /* just print headers now */
4251
4252 char pn[INET6_ADDRSTRLEN];
4253 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004254 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004255
4256 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004257 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004258 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004259 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004260
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004261 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4262 case AF_INET:
4263 case AF_INET6:
4264 port = get_host_port(&es->src);
4265 break;
4266 default:
4267 port = 0;
4268 }
4269
Willy Tarreau295a8372011-03-10 11:25:07 +01004270 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004271 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004272 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004273 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004274 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004275 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004276 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4277 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004278 break;
4279 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004280 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004281 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004282 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004283 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004284 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004285 break;
4286 }
4287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004288 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004289 ", server %s (#%d), event #%u\n"
4290 " src %s:%d, session #%d, session flags 0x%08x\n"
4291 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4292 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4293 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4294 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4295 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4296 es->ev_id,
4297 pn, port, es->sid, es->s_flags,
4298 es->state, es->m_flags, es->t_flags,
4299 es->m_clen, es->m_blen,
4300 es->b_flags, es->b_out, es->b_tot,
4301 es->len, es->b_wrap, es->pos);
4302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004303 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004304 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004305 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004306 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004307 si->applet.ctx.errors.ptr = 0;
4308 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004309 }
4310
Willy Tarreau295a8372011-03-10 11:25:07 +01004311 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004312 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004313 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004314 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004315 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004316 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004317 goto next;
4318 }
4319
4320 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004321 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004322 int newptr;
4323 int newline;
4324
Willy Tarreau295a8372011-03-10 11:25:07 +01004325 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004326 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 +01004327 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004328 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004329
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004330 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004331 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004332 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004333 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004334 si->applet.ctx.errors.ptr = newptr;
4335 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004336 };
4337 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004338 si->applet.ctx.errors.bol = 0;
4339 si->applet.ctx.errors.ptr = -1;
4340 si->applet.ctx.errors.buf++;
4341 if (si->applet.ctx.errors.buf > 1) {
4342 si->applet.ctx.errors.buf = 0;
4343 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004344 }
4345 }
4346
4347 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004348 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004349}
4350
Willy Tarreaud5781202012-09-22 19:32:35 +02004351/* parse the "level" argument on the bind lines */
4352static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4353{
4354 if (!*args[cur_arg + 1]) {
4355 memprintf(err, "'%s' : missing level", args[cur_arg]);
4356 return ERR_ALERT | ERR_FATAL;
4357 }
4358
4359 if (!strcmp(args[cur_arg+1], "user"))
4360 conf->level = ACCESS_LVL_USER;
4361 else if (!strcmp(args[cur_arg+1], "operator"))
4362 conf->level = ACCESS_LVL_OPER;
4363 else if (!strcmp(args[cur_arg+1], "admin"))
4364 conf->level = ACCESS_LVL_ADMIN;
4365 else {
4366 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4367 args[cur_arg], args[cur_arg+1]);
4368 return ERR_ALERT | ERR_FATAL;
4369 }
4370
4371 return 0;
4372}
4373
Willy Tarreaub24281b2011-02-13 13:16:36 +01004374struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004375 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004376 .name = "<STATS>", /* used for logging */
4377 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004378 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004379};
4380
Simon Horman9bd2c732011-06-15 15:18:44 +09004381static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004382 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004383 .name = "<CLI>", /* used for logging */
4384 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004385 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004386};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004387
Willy Tarreau10522fd2008-07-09 20:12:41 +02004388static struct cfg_kw_list cfg_kws = {{ },{
4389 { CFG_GLOBAL, "stats", stats_parse_global },
4390 { 0, NULL, NULL },
4391}};
4392
Willy Tarreaud5781202012-09-22 19:32:35 +02004393static struct bind_kw_list bind_kws = { "STAT", { }, {
4394 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4395 { NULL, NULL, 0 },
4396}};
4397
Willy Tarreau10522fd2008-07-09 20:12:41 +02004398__attribute__((constructor))
4399static void __dumpstats_module_init(void)
4400{
4401 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004402 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004403}
4404
Willy Tarreau91861262007-10-17 17:06:05 +02004405/*
4406 * Local variables:
4407 * c-indent-level: 8
4408 * c-basic-offset: 8
4409 * End:
4410 */