blob: 2cfd39a9494ff3d21c90a9952f1f6ec726aa4b29 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010051#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010064static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010065static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090066static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010068static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010069static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Simon Horman9bd2c732011-06-15 15:18:44 +090071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010072/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010073 * cli_io_handler()
74 * -> stats_dump_sess_to_buffer() // "show sess"
75 * -> stats_dump_errors_to_buffer() // "show errors"
76 * -> stats_dump_info_to_buffer() // "show info"
77 * -> stats_dump_stat_to_buffer() // "show stat"
78 * -> stats_dump_csv_header()
79 * -> stats_dump_proxy_to_buffer()
80 * -> stats_dump_fe_stats()
81 * -> stats_dump_li_stats()
82 * -> stats_dump_sv_stats()
83 * -> stats_dump_be_stats()
84 *
85 * http_stats_io_handler()
86 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
87 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
88 * -> stats_dump_html_head() // emits the HTML headers
89 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
90 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
91 * -> stats_dump_html_px_hdr()
92 * -> stats_dump_fe_stats()
93 * -> stats_dump_li_stats()
94 * -> stats_dump_sv_stats()
95 * -> stats_dump_be_stats()
96 * -> stats_dump_html_px_end()
97 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010098 */
99
Simon Horman9bd2c732011-06-15 15:18:44 +0900100static struct si_applet cli_applet;
101
102static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200103 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200104 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200105 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200106 " help : this message\n"
107 " prompt : toggle interactive mode with prompt\n"
108 " quit : disconnect\n"
109 " show info : report information about the running process\n"
110 " show stat : report counters for each proxy and server\n"
111 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100112 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200113 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200114 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200115 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200116 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100117 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200118 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200119 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200120 " disable : put a server or frontend in maintenance mode\n"
121 " enable : re-enable a server or frontend which is in maintenance mode\n"
122 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200123 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200126 "Permission denied\n"
127 "";
128
Willy Tarreau295a8372011-03-10 11:25:07 +0100129/* data transmission states for the stats responses */
130enum {
131 STAT_ST_INIT = 0,
132 STAT_ST_HEAD,
133 STAT_ST_INFO,
134 STAT_ST_LIST,
135 STAT_ST_END,
136 STAT_ST_FIN,
137};
138
139/* data transmission states for the stats responses inside a proxy */
140enum {
141 STAT_PX_ST_INIT = 0,
142 STAT_PX_ST_TH,
143 STAT_PX_ST_FE,
144 STAT_PX_ST_LI,
145 STAT_PX_ST_SV,
146 STAT_PX_ST_BE,
147 STAT_PX_ST_END,
148 STAT_PX_ST_FIN,
149};
150
Cyril Bonté19979e12012-04-04 12:57:21 +0200151extern const char *stat_status_codes[];
152
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200153/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the connection
155 * needs to be closed and ignored, or a negative value upon critical failure.
156 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900157static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200158{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200159 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100160 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100161 s->target = s->si[1].conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200162 s->si[1].conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100163 s->si[1].applet.st1 = 0;
164 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200165
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200166 tv_zero(&s->logs.tv_request);
167 s->logs.t_queue = 0;
168 s->logs.t_connect = 0;
169 s->logs.t_data = 0;
170 s->logs.t_close = 0;
171 s->logs.bytes_in = s->logs.bytes_out = 0;
172 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
173 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200174
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200175 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200176
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200177 if (s->listener->timeout) {
178 s->req->rto = *s->listener->timeout;
179 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200181 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200182}
183
Willy Tarreau07e9e642010-08-17 21:48:17 +0200184/* allocate a new stats frontend named <name>, and return it
185 * (or NULL in case of lack of memory).
186 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200187static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200188{
189 struct proxy *fe;
190
191 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
192 if (!fe)
193 return NULL;
194
Willy Tarreau237250c2011-07-29 01:49:03 +0200195 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200196 fe->next = proxy;
197 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200198 fe->last_change = now.tv_sec;
199 fe->id = strdup("GLOBAL");
200 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200201 fe->maxconn = 10; /* default to 10 concurrent connections */
202 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200203 fe->conf.file = strdup(file);
204 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200205 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200206
207 /* the stats frontend is the only one able to assign ID #0 */
208 fe->conf.id.key = fe->uuid = 0;
209 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200210 return fe;
211}
212
Willy Tarreaufbee7132007-10-18 13:53:22 +0200213/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200214 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
215 * error message into the <err> buffer which will be preallocated. The trailing
216 * '\n' must not be written. The function must be called with <args> pointing to
217 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200218 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200219static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200220 struct proxy *defpx, const char *file, int line,
221 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200222{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200223 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200224 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200225
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200226 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200227 int cur_arg;
228
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200229 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200230 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 +0200231 return -1;
232 }
233
Willy Tarreau89a63132009-08-16 17:41:45 +0200234 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200235 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200236 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200237 return -1;
238 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200239 }
240
Willy Tarreau4348fad2012-09-20 16:48:07 +0200241 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200242 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200243
Willy Tarreauc53d4222012-09-20 20:19:28 +0200244 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
245 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
246 file, line, args[0], args[1], err && *err ? *err : "error");
247 return -1;
248 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200249
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200250 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200251 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200252 static int bind_dumped;
253 struct bind_kw *kw;
254
255 kw = bind_find_kw(args[cur_arg]);
256 if (kw) {
257 if (!kw->parse) {
258 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
259 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200260 return -1;
261 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200262
263 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
264 if (err && *err)
265 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
266 else
267 memprintf(err, "'%s %s' : error encountered while processing '%s'",
268 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200269 return -1;
270 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200271
272 cur_arg += 1 + kw->skip;
273 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200274 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200275
276 if (!bind_dumped) {
277 bind_dump_kws(err);
278 indent_msg(err, 4);
279 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200281
282 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
283 args[0], args[1], args[cur_arg],
284 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
285 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100287
Willy Tarreauc53d4222012-09-20 20:19:28 +0200288 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
289 l->maxconn = global.stats_fe->maxconn;
290 l->backlog = global.stats_fe->backlog;
291 l->timeout = &global.stats_fe->timeout.client;
292 l->accept = session_accept;
293 l->handler = process_session;
294 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
295 l->nice = -64; /* we want to boost priority for local stats */
296 global.maxsock += l->maxconn;
297 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200298 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100300 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100302
303 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200304 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100305 return -1;
306 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100308 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200309 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200310 return -1;
311 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200312 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200313 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200314 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200315 return -1;
316 }
317 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200318 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200319 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 else if (!strcmp(args[1], "maxconn")) {
321 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322
323 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200324 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200325 return -1;
326 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200327
328 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200329 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200330 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200331 return -1;
332 }
333 }
334 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200335 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200336 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
337 int cur_arg = 2;
338 unsigned int set = 0;
339
Willy Tarreau91319572013-04-20 09:48:50 +0200340 if (!global.stats_fe) {
341 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
342 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
343 return -1;
344 }
345 }
346
Willy Tarreau35b7b162012-10-22 23:17:18 +0200347 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100348 unsigned int low, high;
349
Willy Tarreau35b7b162012-10-22 23:17:18 +0200350 if (strcmp(args[cur_arg], "all") == 0) {
351 set = 0;
352 break;
353 }
354 else if (strcmp(args[cur_arg], "odd") == 0) {
355 set |= 0x55555555;
356 }
357 else if (strcmp(args[cur_arg], "even") == 0) {
358 set |= 0xAAAAAAAA;
359 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100360 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100361 char *dash = strchr(args[cur_arg], '-');
362
363 low = high = str2uic(args[cur_arg]);
364 if (dash)
365 high = str2uic(dash + 1);
366
367 if (high < low) {
368 unsigned int swap = low;
369 low = high;
370 high = swap;
371 }
372
373 if (low < 1 || high > 32) {
374 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
375 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200376 return -1;
377 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100378
379 while (low <= high)
380 set |= 1 << (low++ - 1);
381 }
382 else {
383 memprintf(err,
384 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
385 args[0], args[1]);
386 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200387 }
388 cur_arg++;
389 }
390 global.stats_fe->bind_proc = set;
391 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200392 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200393 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200394 return -1;
395 }
396 return 0;
397}
398
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100399/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
400 * for clearing it if needed.
401 */
402static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100403{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100404 chunk_appendf(&trash,
405 "# pxname,svname,"
406 "qcur,qmax,"
407 "scur,smax,slim,stot,"
408 "bin,bout,"
409 "dreq,dresp,"
410 "ereq,econ,eresp,"
411 "wretr,wredis,"
412 "status,weight,act,bck,"
413 "chkfail,chkdown,lastchg,downtime,qlimit,"
414 "pid,iid,sid,throttle,lbtot,tracked,type,"
415 "rate,rate_lim,rate_max,"
416 "check_status,check_code,check_duration,"
417 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
418 "req_rate,req_rate_max,req_tot,"
419 "cli_abrt,srv_abrt,"
420 "comp_in,comp_out,comp_byp,comp_rsp,"
421 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100422}
423
Simon Hormand9366582011-06-15 15:18:45 +0900424/* print a string of text buffer to <out>. The format is :
425 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
426 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
427 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
428 */
429static int dump_text(struct chunk *out, const char *buf, int bsize)
430{
431 unsigned char c;
432 int ptr = 0;
433
434 while (buf[ptr] && ptr < bsize) {
435 c = buf[ptr];
436 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
437 if (out->len > out->size - 1)
438 break;
439 out->str[out->len++] = c;
440 }
441 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
442 if (out->len > out->size - 2)
443 break;
444 out->str[out->len++] = '\\';
445 switch (c) {
446 case ' ': c = ' '; break;
447 case '\t': c = 't'; break;
448 case '\n': c = 'n'; break;
449 case '\r': c = 'r'; break;
450 case '\e': c = 'e'; break;
451 case '\\': c = '\\'; break;
452 }
453 out->str[out->len++] = c;
454 }
455 else {
456 if (out->len > out->size - 4)
457 break;
458 out->str[out->len++] = '\\';
459 out->str[out->len++] = 'x';
460 out->str[out->len++] = hextab[(c >> 4) & 0xF];
461 out->str[out->len++] = hextab[c & 0xF];
462 }
463 ptr++;
464 }
465
466 return ptr;
467}
468
469/* print a buffer in hexa.
470 * Print stopped if <bsize> is reached, or if no more place in the chunk.
471 */
472static int dump_binary(struct chunk *out, const char *buf, int bsize)
473{
474 unsigned char c;
475 int ptr = 0;
476
477 while (ptr < bsize) {
478 c = buf[ptr];
479
480 if (out->len > out->size - 2)
481 break;
482 out->str[out->len++] = hextab[(c >> 4) & 0xF];
483 out->str[out->len++] = hextab[c & 0xF];
484
485 ptr++;
486 }
487 return ptr;
488}
489
490/* Dump the status of a table to a stream interface's
491 * read buffer. It returns 0 if the output buffer is full
492 * and needs to be called again, otherwise non-zero.
493 */
494static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
495 struct proxy *proxy, struct proxy *target)
496{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200497 struct session *s = si->conn->xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900498
Willy Tarreau77804732012-10-29 16:14:26 +0100499 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900500 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
501
502 /* any other information should be dumped here */
503
Willy Tarreau290e63a2012-09-20 18:07:14 +0200504 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100505 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900506
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200507 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900508 return 0;
509
510 return 1;
511}
512
513/* Dump the a table entry to a stream interface's
514 * read buffer. It returns 0 if the output buffer is full
515 * and needs to be called again, otherwise non-zero.
516 */
517static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
518 struct proxy *proxy, struct stksess *entry)
519{
520 int dt;
521
Willy Tarreau77804732012-10-29 16:14:26 +0100522 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900523
524 if (proxy->table.type == STKTABLE_TYPE_IP) {
525 char addr[INET_ADDRSTRLEN];
526 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100527 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900528 }
529 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
530 char addr[INET6_ADDRSTRLEN];
531 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100532 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900533 }
534 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100535 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900536 }
537 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900539 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
540 }
541 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100542 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900543 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
544 }
545
Willy Tarreau77804732012-10-29 16:14:26 +0100546 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900547
548 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
549 void *ptr;
550
551 if (proxy->table.data_ofs[dt] == 0)
552 continue;
553 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100554 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900555 else
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900557
558 ptr = stktable_data_ptr(&proxy->table, entry, dt);
559 switch (stktable_data_types[dt].std_type) {
560 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100561 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900562 break;
563 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100564 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900565 break;
566 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100567 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900568 break;
569 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100570 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900571 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
572 proxy->table.data_arg[dt].u));
573 break;
574 }
575 }
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900577
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200578 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900579 return 0;
580
581 return 1;
582}
583
Willy Tarreaudec98142012-06-06 23:37:08 +0200584static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900585{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200586 struct session *s = si->conn->xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900587 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900588 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900589 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900590 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200591 long long value;
592 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200593 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200594 void *ptr;
595 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900596
Simon Hormand5b9fd92011-06-15 15:18:48 +0900597 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900598
599 if (!*args[4]) {
600 si->applet.ctx.cli.msg = "Key value expected\n";
601 si->applet.st0 = STAT_CLI_PRINT;
602 return;
603 }
604
Simon Hormanc5b89f62011-06-15 15:18:50 +0900605 switch (px->table.type) {
606 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900607 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100608 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900609 break;
610 case STKTABLE_TYPE_IPV6:
611 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100612 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900613 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900614 case STKTABLE_TYPE_INTEGER:
615 {
616 char *endptr;
617 unsigned long val;
618 errno = 0;
619 val = strtoul(args[4], &endptr, 10);
620 if ((errno == ERANGE && val == ULONG_MAX) ||
621 (errno != 0 && val == 0) || endptr == args[4] ||
622 val > 0xffffffff) {
623 si->applet.ctx.cli.msg = "Invalid key\n";
624 si->applet.st0 = STAT_CLI_PRINT;
625 return;
626 }
627 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100628 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900629 break;
630 }
631 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900632 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100633 static_table_key->key = args[4];
634 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900635 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900636 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200637 switch (action) {
638 case STAT_CLI_O_TAB:
639 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
640 break;
641 case STAT_CLI_O_CLR:
642 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
643 break;
644 default:
645 si->applet.ctx.cli.msg = "Unknown action\n";
646 break;
647 }
Simon Horman121f3052011-06-15 15:18:46 +0900648 si->applet.st0 = STAT_CLI_PRINT;
649 return;
650 }
651
652 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200653 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900654 si->applet.ctx.cli.msg = stats_permission_denied_msg;
655 si->applet.st0 = STAT_CLI_PRINT;
656 return;
657 }
658
Willy Tarreau07115412012-10-29 21:56:59 +0100659 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900660
Willy Tarreaudec98142012-06-06 23:37:08 +0200661 switch (action) {
662 case STAT_CLI_O_TAB:
663 if (!ts)
664 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100665 chunk_reset(&trash);
666 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900667 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100668 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900669 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200670
671 case STAT_CLI_O_CLR:
672 if (!ts)
673 return;
674 if (ts->ref_cnt) {
675 /* don't delete an entry which is currently referenced */
676 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
677 si->applet.st0 = STAT_CLI_PRINT;
678 return;
679 }
680 stksess_kill(&px->table, ts);
681 break;
Simon Horman17bce342011-06-15 15:18:47 +0900682
Willy Tarreau654694e2012-06-07 01:03:16 +0200683 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200684 if (ts)
685 stktable_touch(&px->table, ts, 1);
686 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100687 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200688 if (!ts) {
689 /* don't delete an entry which is currently referenced */
690 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
691 si->applet.st0 = STAT_CLI_PRINT;
692 return;
693 }
694 stktable_store(&px->table, ts, 1);
695 }
696
Willy Tarreau47060b62013-08-01 21:11:42 +0200697 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
698 if (strncmp(args[cur_arg], "data.", 5) != 0) {
699 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
700 si->applet.st0 = STAT_CLI_PRINT;
701 return;
702 }
703
704 data_type = stktable_get_data_type(args[cur_arg] + 5);
705 if (data_type < 0) {
706 si->applet.ctx.cli.msg = "Unknown data type\n";
707 si->applet.st0 = STAT_CLI_PRINT;
708 return;
709 }
710
711 if (!px->table.data_ofs[data_type]) {
712 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
713 si->applet.st0 = STAT_CLI_PRINT;
714 return;
715 }
716
717 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
718 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
719 si->applet.st0 = STAT_CLI_PRINT;
720 return;
721 }
722
723 ptr = stktable_data_ptr(&px->table, ts, data_type);
724
725 switch (stktable_data_types[data_type].std_type) {
726 case STD_T_SINT:
727 stktable_data_cast(ptr, std_t_sint) = value;
728 break;
729 case STD_T_UINT:
730 stktable_data_cast(ptr, std_t_uint) = value;
731 break;
732 case STD_T_ULL:
733 stktable_data_cast(ptr, std_t_ull) = value;
734 break;
735 case STD_T_FRQP:
736 /* We set both the current and previous values. That way
737 * the reported frequency is stable during all the period
738 * then slowly fades out. This allows external tools to
739 * push measures without having to update them too often.
740 */
741 frqp = &stktable_data_cast(ptr, std_t_frqp);
742 frqp->curr_tick = now_ms;
743 frqp->prev_ctr = 0;
744 frqp->curr_ctr = value;
745 break;
746 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200747 }
748 break;
749
Willy Tarreaudec98142012-06-06 23:37:08 +0200750 default:
751 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900752 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200753 break;
Simon Horman121f3052011-06-15 15:18:46 +0900754 }
Simon Horman121f3052011-06-15 15:18:46 +0900755}
756
Willy Tarreau654694e2012-06-07 01:03:16 +0200757static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900758{
Willy Tarreau04b3a192013-04-13 09:41:37 +0200759 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
760 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau654694e2012-06-07 01:03:16 +0200761 si->applet.st0 = STAT_CLI_PRINT;
762 return;
763 }
764
Simon Hormand5b9fd92011-06-15 15:18:48 +0900765 /* condition on stored data value */
766 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
767 if (si->applet.ctx.table.data_type < 0) {
768 si->applet.ctx.cli.msg = "Unknown data type\n";
769 si->applet.st0 = STAT_CLI_PRINT;
770 return;
771 }
772
773 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
774 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
775 si->applet.st0 = STAT_CLI_PRINT;
776 return;
777 }
778
779 si->applet.ctx.table.data_op = get_std_op(args[4]);
780 if (si->applet.ctx.table.data_op < 0) {
781 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
782 si->applet.st0 = STAT_CLI_PRINT;
783 return;
784 }
785
786 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
787 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
788 si->applet.st0 = STAT_CLI_PRINT;
789 return;
790 }
791}
792
Willy Tarreaudec98142012-06-06 23:37:08 +0200793static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900794{
795 si->applet.ctx.table.data_type = -1;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200796 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200797 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900798 si->applet.ctx.table.proxy = NULL;
799 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200800 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900801
802 if (*args[2]) {
803 si->applet.ctx.table.target = find_stktable(args[2]);
804 if (!si->applet.ctx.table.target) {
805 si->applet.ctx.cli.msg = "No such table\n";
806 si->applet.st0 = STAT_CLI_PRINT;
807 return;
808 }
809 }
810 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200811 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812 goto err_args;
813 return;
814 }
815
816 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200817 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900818 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200819 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900820 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900821 goto err_args;
822
823 return;
824
825err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200826 switch (action) {
827 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900828 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 +0200829 break;
830 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900831 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 +0200832 break;
833 default:
834 si->applet.ctx.cli.msg = "Unknown action\n";
835 break;
836 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900837 si->applet.st0 = STAT_CLI_PRINT;
838}
839
Willy Tarreau532a4502011-09-07 22:37:44 +0200840/* Expects to find a frontend named <arg> and returns it, otherwise displays various
841 * adequate error messages and returns NULL. This function also expects the session
842 * level to be admin.
843 */
844static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
845{
846 struct proxy *px;
847
Willy Tarreau290e63a2012-09-20 18:07:14 +0200848 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200849 si->applet.ctx.cli.msg = stats_permission_denied_msg;
850 si->applet.st0 = STAT_CLI_PRINT;
851 return NULL;
852 }
853
854 if (!*arg) {
855 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
856 si->applet.st0 = STAT_CLI_PRINT;
857 return NULL;
858 }
859
860 px = findproxy(arg, PR_CAP_FE);
861 if (!px) {
862 si->applet.ctx.cli.msg = "No such frontend.\n";
863 si->applet.st0 = STAT_CLI_PRINT;
864 return NULL;
865 }
866 return px;
867}
868
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200869/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
870 * and returns the pointer to the server. Otherwise, display adequate error messages
871 * and returns NULL. This function also expects the session level to be admin. Note:
872 * the <arg> is modified to remove the '/'.
873 */
874static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
875{
876 struct proxy *px;
877 struct server *sv;
878 char *line;
879
Willy Tarreau290e63a2012-09-20 18:07:14 +0200880 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200881 si->applet.ctx.cli.msg = stats_permission_denied_msg;
882 si->applet.st0 = STAT_CLI_PRINT;
883 return NULL;
884 }
885
886 /* split "backend/server" and make <line> point to server */
887 for (line = arg; *line; line++)
888 if (*line == '/') {
889 *line++ = '\0';
890 break;
891 }
892
893 if (!*line || !*arg) {
894 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
895 si->applet.st0 = STAT_CLI_PRINT;
896 return NULL;
897 }
898
899 if (!get_backend_server(arg, line, &px, &sv)) {
900 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
901 si->applet.st0 = STAT_CLI_PRINT;
902 return NULL;
903 }
904
905 if (px->state == PR_STSTOPPED) {
906 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
907 si->applet.st0 = STAT_CLI_PRINT;
908 return NULL;
909 }
910
911 return sv;
912}
913
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200914/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200915 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100916 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200917 * designating the function which will have to process the request, which can
918 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200919 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900920static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200921{
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200922 struct session *s = si->conn->xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200923 char *args[MAX_STATS_ARGS + 1];
924 int arg;
925
926 while (isspace((unsigned char)*line))
927 line++;
928
929 arg = 0;
930 args[arg] = line;
931
932 while (*line && arg < MAX_STATS_ARGS) {
933 if (isspace((unsigned char)*line)) {
934 *line++ = '\0';
935
936 while (isspace((unsigned char)*line))
937 line++;
938
939 args[++arg] = line;
940 continue;
941 }
942
943 line++;
944 }
945
946 while (++arg <= MAX_STATS_ARGS)
947 args[arg] = line;
948
Willy Tarreau295a8372011-03-10 11:25:07 +0100949 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200950 if (strcmp(args[0], "show") == 0) {
951 if (strcmp(args[1], "stat") == 0) {
952 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100953 si->applet.ctx.stats.flags |= STAT_BOUND;
954 si->applet.ctx.stats.iid = atoi(args[2]);
955 si->applet.ctx.stats.type = atoi(args[3]);
956 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200957 }
958
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200959 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100960 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200961 }
962 else if (strcmp(args[1], "info") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200963 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100964 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200965 }
966 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200967 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200968 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100969 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100970 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200971 return 1;
972 }
Willy Tarreau76153662012-11-26 01:16:39 +0100973 if (*args[2] && strcmp(args[2], "all") == 0)
974 si->applet.ctx.sess.target = (void *)-1;
975 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100976 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100977 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100978 si->applet.ctx.sess.target = NULL;
979 si->applet.ctx.sess.section = 0; /* start with session status */
980 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100981 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200982 }
983 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200984 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100985 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100986 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200987 return 1;
988 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200989 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100990 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200991 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100992 si->applet.ctx.errors.iid = -1;
993 si->applet.ctx.errors.px = NULL;
Willy Tarreauf2943dc2012-10-26 20:10:28 +0200994 si->conn->xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100995 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200996 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200997 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200998 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200999 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001000 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001001 return 0;
1002 }
1003 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001004 else if (strcmp(args[0], "clear") == 0) {
1005 if (strcmp(args[1], "counters") == 0) {
1006 struct proxy *px;
1007 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001008 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001009 int clrall = 0;
1010
1011 if (strcmp(args[2], "all") == 0)
1012 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001013
Willy Tarreau6162db22009-10-10 17:13:00 +02001014 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001015 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1016 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001017 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001018 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001019 return 1;
1020 }
1021
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001022 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001023 if (clrall) {
1024 memset(&px->be_counters, 0, sizeof(px->be_counters));
1025 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1026 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001027 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001028 px->be_counters.conn_max = 0;
1029 px->be_counters.p.http.rps_max = 0;
1030 px->be_counters.sps_max = 0;
1031 px->be_counters.cps_max = 0;
1032 px->be_counters.nbpend_max = 0;
1033
1034 px->fe_counters.conn_max = 0;
1035 px->fe_counters.p.http.rps_max = 0;
1036 px->fe_counters.sps_max = 0;
1037 px->fe_counters.cps_max = 0;
1038 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001039 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001040
1041 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001042 if (clrall)
1043 memset(&sv->counters, 0, sizeof(sv->counters));
1044 else {
1045 sv->counters.cur_sess_max = 0;
1046 sv->counters.nbpend_max = 0;
1047 sv->counters.sps_max = 0;
1048 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001049
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001050 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001051 if (li->counters) {
1052 if (clrall)
1053 memset(li->counters, 0, sizeof(*li->counters));
1054 else
1055 li->counters->conn_max = 0;
1056 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001057 }
1058
Willy Tarreau81c25d02011-09-07 15:17:21 +02001059 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001060 return 1;
1061 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001062 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001063 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001064 /* end of processing */
1065 return 1;
1066 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001067 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001068 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001069 return 0;
1070 }
1071 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001072 else if (strcmp(args[0], "get") == 0) {
1073 if (strcmp(args[1], "weight") == 0) {
1074 struct proxy *px;
1075 struct server *sv;
1076
1077 /* split "backend/server" and make <line> point to server */
1078 for (line = args[2]; *line; line++)
1079 if (*line == '/') {
1080 *line++ = '\0';
1081 break;
1082 }
1083
1084 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001085 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001086 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001087 return 1;
1088 }
1089
1090 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001091 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001092 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001093 return 1;
1094 }
1095
1096 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001097 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1098 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001099 return 1;
1100 }
1101 else { /* not "get weight" */
1102 return 0;
1103 }
1104 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001105 else if (strcmp(args[0], "set") == 0) {
1106 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001107 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001108 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001109
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001110 sv = expect_server_admin(s, si, args[2]);
1111 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001112 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001113
Simon Horman7d09b9a2013-02-12 10:45:51 +09001114 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001115 /*
1116 * The user-weight may now be zero and thus
1117 * the server considered to be draining.
1118 * Update the server's drain state as necessary.
1119 */
1120 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001121 if (warning) {
1122 si->applet.ctx.cli.msg = warning;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001123 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001124 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001125 return 1;
1126 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001127 else if (strcmp(args[1], "timeout") == 0) {
1128 if (strcmp(args[2], "cli") == 0) {
1129 unsigned timeout;
1130 const char *res;
1131
1132 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001133 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001134 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001135 return 1;
1136 }
1137
1138 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1139 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001140 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001141 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001142 return 1;
1143 }
1144
1145 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1146 return 1;
1147 }
1148 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001149 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001150 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001151 return 1;
1152 }
1153 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001154 else if (strcmp(args[1], "maxconn") == 0) {
1155 if (strcmp(args[2], "frontend") == 0) {
1156 struct proxy *px;
1157 struct listener *l;
1158 int v;
1159
Willy Tarreau532a4502011-09-07 22:37:44 +02001160 px = expect_frontend_admin(s, si, args[3]);
1161 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001162 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001163
1164 if (!*args[4]) {
1165 si->applet.ctx.cli.msg = "Integer value expected.\n";
1166 si->applet.st0 = STAT_CLI_PRINT;
1167 return 1;
1168 }
1169
1170 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001171 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001172 si->applet.ctx.cli.msg = "Value out of range.\n";
1173 si->applet.st0 = STAT_CLI_PRINT;
1174 return 1;
1175 }
1176
1177 /* OK, the value is fine, so we assign it to the proxy and to all of
1178 * its listeners. The blocked ones will be dequeued.
1179 */
1180 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001181 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001182 l->maxconn = v;
1183 if (l->state == LI_FULL)
1184 resume_listener(l);
1185 }
1186
1187 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1188 dequeue_all_listeners(&s->fe->listener_queue);
1189
1190 return 1;
1191 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001192 else if (strcmp(args[2], "global") == 0) {
1193 int v;
1194
Willy Tarreau290e63a2012-09-20 18:07:14 +02001195 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001196 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1197 si->applet.st0 = STAT_CLI_PRINT;
1198 return 1;
1199 }
1200
1201 if (!*args[3]) {
1202 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1203 si->applet.st0 = STAT_CLI_PRINT;
1204 return 1;
1205 }
1206
1207 v = atoi(args[3]);
1208 if (v > global.hardmaxconn) {
1209 si->applet.ctx.cli.msg = "Value out of range.\n";
1210 si->applet.st0 = STAT_CLI_PRINT;
1211 return 1;
1212 }
1213
1214 /* check for unlimited values */
1215 if (v <= 0)
1216 v = global.hardmaxconn;
1217
1218 global.maxconn = v;
1219
1220 /* Dequeues all of the listeners waiting for a resource */
1221 if (!LIST_ISEMPTY(&global_listener_queue))
1222 dequeue_all_listeners(&global_listener_queue);
1223
1224 return 1;
1225 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001226 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001227 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001228 si->applet.st0 = STAT_CLI_PRINT;
1229 return 1;
1230 }
1231 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001232 else if (strcmp(args[1], "rate-limit") == 0) {
1233 if (strcmp(args[2], "connections") == 0) {
1234 if (strcmp(args[3], "global") == 0) {
1235 int v;
1236
Willy Tarreau290e63a2012-09-20 18:07:14 +02001237 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001238 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1239 si->applet.st0 = STAT_CLI_PRINT;
1240 return 1;
1241 }
1242
1243 if (!*args[4]) {
1244 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1245 si->applet.st0 = STAT_CLI_PRINT;
1246 return 1;
1247 }
1248
1249 v = atoi(args[4]);
1250 if (v < 0) {
1251 si->applet.ctx.cli.msg = "Value out of range.\n";
1252 si->applet.st0 = STAT_CLI_PRINT;
1253 return 1;
1254 }
1255
1256 global.cps_lim = v;
1257
1258 /* Dequeues all of the listeners waiting for a resource */
1259 if (!LIST_ISEMPTY(&global_listener_queue))
1260 dequeue_all_listeners(&global_listener_queue);
1261
1262 return 1;
1263 }
1264 else {
1265 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1266 si->applet.st0 = STAT_CLI_PRINT;
1267 return 1;
1268 }
1269 }
William Lallemandd85f9172012-11-09 17:05:39 +01001270 else if (strcmp(args[2], "http-compression") == 0) {
1271 if (strcmp(args[3], "global") == 0) {
1272 int v;
1273
Willy Tarreau85d47f92012-11-21 00:29:50 +01001274 if (!*args[4]) {
1275 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1276 si->applet.st0 = STAT_CLI_PRINT;
1277 return 1;
1278 }
1279
William Lallemandd85f9172012-11-09 17:05:39 +01001280 v = atoi(args[4]);
1281 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1282 }
1283 else {
1284 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1285 si->applet.st0 = STAT_CLI_PRINT;
1286 return 1;
1287 }
1288 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001289 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001290 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001291 si->applet.st0 = STAT_CLI_PRINT;
1292 return 1;
1293 }
1294 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001295 else if (strcmp(args[1], "table") == 0) {
1296 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1297 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001298 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001299 return 0;
1300 }
1301 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001302 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001303 if (strcmp(args[1], "agent") == 0) {
1304 struct server *sv;
1305
1306 sv = expect_server_admin(s, si, args[2]);
1307 if (!sv)
1308 return 1;
1309
1310 sv->agent.state &= ~CHK_STATE_DISABLED;
1311
1312 return 1;
1313 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001314 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001315 struct server *sv;
1316
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001317 sv = expect_server_admin(s, si, args[2]);
1318 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001319 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001320
Cyril Bontécd19e512010-01-31 22:34:03 +01001321 if (sv->state & SRV_MAINTAIN) {
1322 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001323 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001324 /* If this server tracks the status of another one,
1325 * we must restore the good status.
1326 */
Willy Tarreau44267702011-10-28 15:35:33 +02001327 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001328 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001329 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001330 } else {
1331 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001332 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001333 }
1334 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001335 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001336 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001337 }
1338 }
1339
Willy Tarreau532a4502011-09-07 22:37:44 +02001340 return 1;
1341 }
1342 else if (strcmp(args[1], "frontend") == 0) {
1343 struct proxy *px;
1344
1345 px = expect_frontend_admin(s, si, args[2]);
1346 if (!px)
1347 return 1;
1348
1349 if (px->state == PR_STSTOPPED) {
1350 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1351 si->applet.st0 = STAT_CLI_PRINT;
1352 return 1;
1353 }
1354
1355 if (px->state != PR_STPAUSED) {
1356 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1357 si->applet.st0 = STAT_CLI_PRINT;
1358 return 1;
1359 }
1360
1361 if (!resume_proxy(px)) {
1362 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1363 si->applet.st0 = STAT_CLI_PRINT;
1364 return 1;
1365 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001366 return 1;
1367 }
1368 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001369 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1370 si->applet.st0 = STAT_CLI_PRINT;
1371 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001372 }
1373 }
1374 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001375 if (strcmp(args[1], "agent") == 0) {
1376 struct server *sv;
1377
1378 sv = expect_server_admin(s, si, args[2]);
1379 if (!sv)
1380 return 1;
1381
1382 sv->agent.state |= CHK_STATE_DISABLED;
1383
1384 return 1;
1385 }
1386 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001387 struct server *sv;
1388
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001389 sv = expect_server_admin(s, si, args[2]);
1390 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001391 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001392
Cyril Bontécd19e512010-01-31 22:34:03 +01001393 if (! (sv->state & SRV_MAINTAIN)) {
1394 /* Not already in maintenance, we can change the server state */
1395 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001396 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001397 }
1398
Willy Tarreau532a4502011-09-07 22:37:44 +02001399 return 1;
1400 }
1401 else if (strcmp(args[1], "frontend") == 0) {
1402 struct proxy *px;
1403
1404 px = expect_frontend_admin(s, si, args[2]);
1405 if (!px)
1406 return 1;
1407
1408 if (px->state == PR_STSTOPPED) {
1409 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1410 si->applet.st0 = STAT_CLI_PRINT;
1411 return 1;
1412 }
1413
1414 if (px->state == PR_STPAUSED) {
1415 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1416 si->applet.st0 = STAT_CLI_PRINT;
1417 return 1;
1418 }
1419
1420 if (!pause_proxy(px)) {
1421 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1422 si->applet.st0 = STAT_CLI_PRINT;
1423 return 1;
1424 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001425 return 1;
1426 }
1427 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001428 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1429 si->applet.st0 = STAT_CLI_PRINT;
1430 return 1;
1431 }
1432 }
1433 else if (strcmp(args[0], "shutdown") == 0) {
1434 if (strcmp(args[1], "frontend") == 0) {
1435 struct proxy *px;
1436
1437 px = expect_frontend_admin(s, si, args[2]);
1438 if (!px)
1439 return 1;
1440
1441 if (px->state == PR_STSTOPPED) {
1442 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1443 si->applet.st0 = STAT_CLI_PRINT;
1444 return 1;
1445 }
1446
1447 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1448 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1449 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1450 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1451 stop_proxy(px);
1452 return 1;
1453 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001454 else if (strcmp(args[1], "session") == 0) {
1455 struct session *sess, *ptr;
1456
Willy Tarreau290e63a2012-09-20 18:07:14 +02001457 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001458 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1459 si->applet.st0 = STAT_CLI_PRINT;
1460 return 1;
1461 }
1462
1463 if (!*args[2]) {
1464 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1465 si->applet.st0 = STAT_CLI_PRINT;
1466 return 1;
1467 }
1468
1469 ptr = (void *)strtoul(args[2], NULL, 0);
1470
1471 /* first, look for the requested session in the session table */
1472 list_for_each_entry(sess, &sessions, list) {
1473 if (sess == ptr)
1474 break;
1475 }
1476
1477 /* do we have the session ? */
1478 if (sess != ptr) {
1479 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1480 si->applet.st0 = STAT_CLI_PRINT;
1481 return 1;
1482 }
1483
1484 session_shutdown(sess, SN_ERR_KILLED);
1485 return 1;
1486 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001487 else if (strcmp(args[1], "sessions") == 0) {
1488 if (strcmp(args[2], "server") == 0) {
1489 struct server *sv;
1490 struct session *sess, *sess_bck;
1491
1492 sv = expect_server_admin(s, si, args[3]);
1493 if (!sv)
1494 return 1;
1495
1496 /* kill all the session that are on this server */
1497 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1498 if (sess->srv_conn == sv)
1499 session_shutdown(sess, SN_ERR_KILLED);
1500
1501 return 1;
1502 }
1503 else {
1504 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1505 si->applet.st0 = STAT_CLI_PRINT;
1506 return 1;
1507 }
1508 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001509 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001510 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001511 si->applet.st0 = STAT_CLI_PRINT;
1512 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001513 }
1514 }
1515 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001516 return 0;
1517 }
1518 return 1;
1519}
1520
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001521/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001522 * used to processes I/O from/to the stats unix socket. The system relies on a
1523 * state machine handling requests and various responses. We read a request,
1524 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001525 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1526 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001527 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001528 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001529static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001530{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001531 struct channel *req = si->ob;
1532 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001533 int reql;
1534 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001535
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001536 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1537 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001538
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001539 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001540 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001542 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001543 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001544 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001545 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001546 /* Let's close for real now. We just close the request
1547 * side, the conditions below will complete if needed.
1548 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001549 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001550 break;
1551 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001552 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001553 /* ensure we have some output room left in the event we
1554 * would want to return some info right after parsing.
1555 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001556 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001557 break;
1558
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001559 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001560 if (reql <= 0) { /* closed or EOL not found */
1561 if (reql == 0)
1562 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001563 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001564 continue;
1565 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001566
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001567 /* seek for a possible semi-colon. If we find one, we
1568 * replace it with an LF and skip only this part.
1569 */
1570 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001571 if (trash.str[len] == ';') {
1572 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001573 reql = len + 1;
1574 break;
1575 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001576
Willy Tarreau816fc222009-10-04 07:36:58 +02001577 /* now it is time to check that we have a full line,
1578 * remove the trailing \n and possibly \r, then cut the
1579 * line.
1580 */
1581 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001582 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001583 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001584 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001585 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001586
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001587 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001588 len--;
1589
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001590 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001591
Willy Tarreaubc4af052011-02-13 13:25:14 +01001592 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001593 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001594 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001595 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001596 continue;
1597 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001598 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001599 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001600 else if (strcmp(trash.str, "help") == 0 ||
1601 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001602 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001603 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001604 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001605 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001606 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001607 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001608 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001609 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001610 /* if prompt is disabled, print help on empty lines,
1611 * so that the user at least knows how to enable
1612 * prompt and find help.
1613 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001614 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001615 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001616 }
1617
1618 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001619 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001620 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001621 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001622 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001623 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001624 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001625 continue;
1626 }
1627
Willy Tarreaubc4af052011-02-13 13:25:14 +01001628 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001629 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001630 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001631 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001632 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001633 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001634 if (stats_dump_info_to_buffer(si))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001635 si->applet.st0 = STAT_CLI_PROMPT;
1636 break;
1637 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001638 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001639 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001640 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001641 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001642 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001643 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001644 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001645 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001646 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001647 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001648 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001649 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001650 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001651 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001652 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001653 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001654 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001655 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001656 break;
1657 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001658
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001659 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001660 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001661 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001662 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001663 }
1664
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001665 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001666 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001668
1669 /* Now we close the output if one of the writers did so,
1670 * or if we're not in interactive mode and the request
1671 * buffer is empty. This still allows pipelined requests
1672 * to be sent in non-interactive mode.
1673 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001674 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001675 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001676 continue;
1677 }
1678
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001679 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001680 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001681 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001682 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001683
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001684 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001685 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1686 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001687 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001688 * and nothing more to consume. This is comparable to a broken pipe, so
1689 * we forward the close to the request side so that it flows upstream to
1690 * the client.
1691 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001692 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001693 }
1694
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001695 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001696 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1697 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1698 /* We have no more processing to do, and nothing more to send, and
1699 * the client side has closed. So we'll forward this state downstream
1700 * on the response buffer.
1701 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001702 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001703 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001704 }
1705
1706 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001707 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001708
1709 /* we don't want to expire timeouts while we're processing requests */
1710 si->ib->rex = TICK_ETERNITY;
1711 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001712
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001713 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001714 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 +02001715 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001716 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 +02001717
1718 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1719 /* check that we have released everything then unregister */
1720 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001721 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001722}
1723
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001724/* This function dumps information onto the stream interface's read buffer.
1725 * It returns 0 as long as it does not complete, non-zero upon completion.
1726 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001727 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001728static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001729{
1730 unsigned int up = (now.tv_sec - start_date.tv_sec);
1731
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001732 chunk_printf(&trash,
1733 "Name: " PRODUCT_NAME "\n"
1734 "Version: " HAPROXY_VERSION "\n"
1735 "Release_date: " HAPROXY_DATE "\n"
1736 "Nbproc: %d\n"
1737 "Process_num: %d\n"
1738 "Pid: %d\n"
1739 "Uptime: %dd %dh%02dm%02ds\n"
1740 "Uptime_sec: %d\n"
1741 "Memmax_MB: %d\n"
1742 "Ulimit-n: %d\n"
1743 "Maxsock: %d\n"
1744 "Maxconn: %d\n"
1745 "Hard_maxconn: %d\n"
1746 "Maxpipes: %d\n"
1747 "CurrConns: %d\n"
1748 "PipesUsed: %d\n"
1749 "PipesFree: %d\n"
1750 "ConnRate: %d\n"
1751 "ConnRateLimit: %d\n"
1752 "MaxConnRate: %d\n"
1753 "CompressBpsIn: %u\n"
1754 "CompressBpsOut: %u\n"
1755 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001756#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001757 "ZlibMemUsage: %ld\n"
1758 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001759#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001760 "Tasks: %d\n"
1761 "Run_queue: %d\n"
1762 "Idle_pct: %d\n"
1763 "node: %s\n"
1764 "description: %s\n"
1765 "",
1766 global.nbproc,
1767 relative_pid,
1768 pid,
1769 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1770 up,
1771 global.rlimit_memmax,
1772 global.rlimit_nofile,
1773 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1774 actconn, pipes_used, pipes_free,
1775 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1776 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1777 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001778#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001779 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001780#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001781 nb_tasks_cur, run_queue_cur, idle_pct,
1782 global.node, global.desc ? global.desc : ""
1783 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001784
1785 if (bi_putchk(si->ib, &trash) == -1)
1786 return 0;
1787
1788 return 1;
1789}
1790
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001791/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1792 * the state from stream interface <si>. The caller is responsible for clearing
1793 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001794 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001795static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001796{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001797 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001798
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001799 if (!(px->cap & PR_CAP_FE))
1800 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001801
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001802 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1803 return 0;
1804
Willy Tarreau354898b2012-12-23 18:15:23 +01001805 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001806 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001807 /* name, queue */
1808 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001809
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001810 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1811 /* Column sub-heading for Enable or Disable server */
1812 chunk_appendf(&trash, "<td></td>");
1813 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001814
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001815 chunk_appendf(&trash,
1816 "<td class=ac>"
1817 "<a name=\"%s/Frontend\"></a>"
1818 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1819 "<td colspan=3></td>"
1820 "",
1821 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001822
Willy Tarreau466c9b52012-12-23 02:25:03 +01001823 chunk_appendf(&trash,
1824 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001825 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001826 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1827 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1828 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001829 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1830 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1831 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001832
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001833 if (px->mode == PR_MODE_HTTP)
1834 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001835 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001836 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001837
1838 chunk_appendf(&trash,
1839 "</table></div></u></td>"
1840 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001841 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001842 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1843 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1844 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001845 U2H(px->fe_counters.sps_max),
1846 U2H(px->fe_counters.cps_max),
1847 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001848
1849 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001850 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001851 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001852 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001853
1854 chunk_appendf(&trash,
1855 "</table></div></u></td>"
1856 /* sessions rate : limit */
1857 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001858 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001859
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001860 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001861 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001862 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001863 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001864 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1865 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001866 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001867 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1868 U2H(px->fe_counters.cum_sess),
1869 U2H(px->fe_counters.cum_conn),
1870 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001871
Willy Tarreau466c9b52012-12-23 02:25:03 +01001872 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001873 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001874 chunk_appendf(&trash,
1875 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1876 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1877 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1878 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1879 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1880 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1881 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1882 "<tr><th>- other responses:</th><td>%s</td></tr>"
1883 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1884 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001885 U2H(px->fe_counters.p.http.cum_req),
1886 U2H(px->fe_counters.p.http.rsp[1]),
1887 U2H(px->fe_counters.p.http.rsp[2]),
1888 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001889 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001890 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001891 U2H(px->fe_counters.p.http.rsp[3]),
1892 U2H(px->fe_counters.p.http.rsp[4]),
1893 U2H(px->fe_counters.p.http.rsp[5]),
1894 U2H(px->fe_counters.p.http.rsp[0]),
1895 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001896 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001897
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001898 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001899 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001900 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001901 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001902 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001903 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001904 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001905 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001906
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001907 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001908 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001909 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001910 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001911 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001912 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1913 px->fe_counters.comp_in ?
1914 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001915 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001916
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001917 chunk_appendf(&trash,
1918 /* denied: req, resp */
1919 "<td>%s</td><td>%s</td>"
1920 /* errors : request, connect, response */
1921 "<td>%s</td><td></td><td></td>"
1922 /* warnings: retries, redispatches */
1923 "<td></td><td></td>"
1924 /* server status : reflect frontend status */
1925 "<td class=ac>%s</td>"
1926 /* rest of server: nothing */
1927 "<td class=ac colspan=8></td></tr>"
1928 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001929 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1930 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001931 px->state == PR_STREADY ? "OPEN" :
1932 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001933 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001934 else { /* CSV mode */
1935 chunk_appendf(&trash,
1936 /* pxid, name, queue cur, queue max, */
1937 "%s,FRONTEND,,,"
1938 /* sessions : current, max, limit, total */
1939 "%d,%d,%d,%lld,"
1940 /* bytes : in, out */
1941 "%lld,%lld,"
1942 /* denied: req, resp */
1943 "%lld,%lld,"
1944 /* errors : request, connect, response */
1945 "%lld,,,"
1946 /* warnings: retries, redispatches */
1947 ",,"
1948 /* server status : reflect frontend status */
1949 "%s,"
1950 /* rest of server: nothing */
1951 ",,,,,,,,"
1952 /* pid, iid, sid, throttle, lbtot, tracked, type */
1953 "%d,%d,0,,,,%d,"
1954 /* rate, rate_lim, rate_max */
1955 "%u,%u,%u,"
1956 /* check_status, check_code, check_duration */
1957 ",,,",
1958 px->id,
1959 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1960 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1961 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1962 px->fe_counters.failed_req,
1963 px->state == PR_STREADY ? "OPEN" :
1964 px->state == PR_STFULL ? "FULL" : "STOP",
1965 relative_pid, px->uuid, STATS_TYPE_FE,
1966 read_freq_ctr(&px->fe_sess_per_sec),
1967 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001968
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001969 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1970 if (px->mode == PR_MODE_HTTP) {
1971 for (i=1; i<6; i++)
1972 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1973 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1974 }
1975 else
1976 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001978 /* failed health analyses */
1979 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001980
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001981 /* requests : req_rate, req_rate_max, req_tot, */
1982 chunk_appendf(&trash, "%u,%u,%lld,",
1983 read_freq_ctr(&px->fe_req_per_sec),
1984 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1985
1986 /* errors: cli_aborts, srv_aborts */
1987 chunk_appendf(&trash, ",,");
1988
1989 /* compression: in, out, bypassed */
1990 chunk_appendf(&trash, "%lld,%lld,%lld,",
1991 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1992
1993 /* compression: comp_rsp */
1994 chunk_appendf(&trash, "%lld,",
1995 px->fe_counters.p.http.comp_rsp);
1996
1997 /* finish with EOL */
1998 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001999 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002000 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002001}
2002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002003/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2004 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2005 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2006 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002007 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002008static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002009{
Willy Tarreau354898b2012-12-23 18:15:23 +01002010 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002011 chunk_appendf(&trash, "<tr class=socket>");
2012 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2013 /* Column sub-heading for Enable or Disable server */
2014 chunk_appendf(&trash, "<td></td>");
2015 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002016 chunk_appendf(&trash,
2017 /* frontend name, listener name */
2018 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2019 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2020 "",
2021 px->id, l->name,
2022 (flags & ST_SHLGNDS)?"<u>":"",
2023 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002024
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002025 if (flags & ST_SHLGNDS) {
2026 char str[INET6_ADDRSTRLEN];
2027 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002028
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002029 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002030
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002031 port = get_host_port(&l->addr);
2032 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2033 case AF_INET:
2034 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2035 break;
2036 case AF_INET6:
2037 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2038 break;
2039 case AF_UNIX:
2040 chunk_appendf(&trash, "unix, ");
2041 break;
2042 case -1:
2043 chunk_appendf(&trash, "(%s), ", strerror(errno));
2044 break;
2045 }
Willy Tarreau91861262007-10-17 17:06:05 +02002046
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002047 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002048 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002049 }
Willy Tarreau91861262007-10-17 17:06:05 +02002050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002051 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002052 /* queue */
2053 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002054 /* sessions rate: current, max, limit */
2055 "<td colspan=3>&nbsp;</td>"
2056 /* sessions: current, max, limit, total, lbtot */
2057 "<td>%s</td><td>%s</td><td>%s</td>"
2058 "<td>%s</td><td>&nbsp;</td>"
2059 /* bytes: in, out */
2060 "<td>%s</td><td>%s</td>"
2061 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002062 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002063 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2064 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002065
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002066 chunk_appendf(&trash,
2067 /* denied: req, resp */
2068 "<td>%s</td><td>%s</td>"
2069 /* errors: request, connect, response */
2070 "<td>%s</td><td></td><td></td>"
2071 /* warnings: retries, redispatches */
2072 "<td></td><td></td>"
2073 /* server status: reflect listener status */
2074 "<td class=ac>%s</td>"
2075 /* rest of server: nothing */
2076 "<td class=ac colspan=8></td></tr>"
2077 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002078 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2079 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002080 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2081 }
2082 else { /* CSV mode */
2083 chunk_appendf(&trash,
2084 /* pxid, name, queue cur, queue max, */
2085 "%s,%s,,,"
2086 /* sessions: current, max, limit, total */
2087 "%d,%d,%d,%lld,"
2088 /* bytes: in, out */
2089 "%lld,%lld,"
2090 /* denied: req, resp */
2091 "%lld,%lld,"
2092 /* errors: request, connect, response */
2093 "%lld,,,"
2094 /* warnings: retries, redispatches */
2095 ",,"
2096 /* server status: reflect listener status */
2097 "%s,"
2098 /* rest of server: nothing */
2099 ",,,,,,,,"
2100 /* pid, iid, sid, throttle, lbtot, tracked, type */
2101 "%d,%d,%d,,,,%d,"
2102 /* rate, rate_lim, rate_max */
2103 ",,,"
2104 /* check_status, check_code, check_duration */
2105 ",,,"
2106 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2107 ",,,,,,"
2108 /* failed health analyses */
2109 ","
2110 /* requests : req_rate, req_rate_max, req_tot, */
2111 ",,,"
2112 /* errors: cli_aborts, srv_aborts */
2113 ",,"
2114 /* compression: in, out, bypassed, comp_rsp */
2115 ",,,,"
2116 "\n",
2117 px->id, l->name,
2118 l->nbconn, l->counters->conn_max,
2119 l->maxconn, l->counters->cum_conn,
2120 l->counters->bytes_in, l->counters->bytes_out,
2121 l->counters->denied_req, l->counters->denied_resp,
2122 l->counters->failed_req,
2123 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2124 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2125 }
2126 return 1;
2127}
Willy Tarreau91861262007-10-17 17:06:05 +02002128
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002129/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2130 * from stream interface <si>, stats flags <flags>, and server state <state>.
2131 * The caller is responsible for clearing the trash if needed. Returns non-zero
2132 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002133 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2134 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002135 */
2136static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2137{
2138 struct server *ref = sv->track ? sv->track : sv;
2139 char str[INET6_ADDRSTRLEN];
2140 struct chunk src;
2141 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002142
Willy Tarreau354898b2012-12-23 18:15:23 +01002143 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002144 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002145 "DOWN",
2146 "DN %d/%d &uarr;",
2147 "UP %d/%d &darr;",
2148 "UP",
2149 "NOLB %d/%d &darr;",
2150 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002151 "DRAIN %d/%d &darr;",
2152 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002153 "<i>no check</i>"
2154 };
Willy Tarreau91861262007-10-17 17:06:05 +02002155
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002156 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2157 chunk_appendf(&trash, "<tr class=\"maintain\">");
Simon Horman8c3d0be2013-11-25 10:46:40 +09002158 else if (sv->eweight == 0 && !(sv->state & SRV_DRAIN))
Geoff Bucarcc8bb922013-04-18 13:53:16 -07002159 chunk_appendf(&trash, "<tr class=\"softstop\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002160 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002161 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002162 "<tr class=\"%s%d\">",
2163 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002164
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002165 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002166 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002167 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2168 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002169
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002170 chunk_appendf(&trash,
2171 "<td class=ac><a name=\"%s/%s\"></a>%s"
2172 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2173 "",
2174 px->id, sv->id,
2175 (flags & ST_SHLGNDS) ? "<u>" : "",
2176 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002177
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002178 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002179 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002180
2181 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2182 case AF_INET:
2183 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2184 break;
2185 case AF_INET6:
2186 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2187 break;
2188 case AF_UNIX:
2189 chunk_appendf(&trash, "unix, ");
2190 break;
2191 case -1:
2192 chunk_appendf(&trash, "(%s), ", strerror(errno));
2193 break;
2194 default: /* address family not supported */
2195 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002196 }
Willy Tarreau91861262007-10-17 17:06:05 +02002197
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002198 /* id */
2199 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002200
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002201 /* cookie */
2202 if (sv->cookie) {
2203 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002205 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2206 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002207
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002208 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002209 }
2210
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002211 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002212 }
Willy Tarreau91861262007-10-17 17:06:05 +02002213
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002214 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002215 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002216 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002217 /* sessions rate : current, max, limit */
2218 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002219 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002220 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002221 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2222 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002223
Willy Tarreau466c9b52012-12-23 02:25:03 +01002224
2225 chunk_appendf(&trash,
2226 /* sessions: current, max, limit, total */
2227 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002228 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002229 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2230 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002231 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2232 U2H(sv->counters.cum_sess),
2233 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002234
Willy Tarreau466c9b52012-12-23 02:25:03 +01002235 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2236 if (px->mode == PR_MODE_HTTP) {
2237 unsigned long long tot;
2238 for (tot = i = 0; i < 6; i++)
2239 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002240
Willy Tarreau466c9b52012-12-23 02:25:03 +01002241 chunk_appendf(&trash,
2242 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2243 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2244 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2245 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2246 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2247 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2248 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2249 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002250 U2H(tot),
2251 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2252 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2253 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2254 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2255 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2256 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02002257 }
Willy Tarreau91861262007-10-17 17:06:05 +02002258
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002259 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002260 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002261 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002262 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002263 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002264
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002265 chunk_appendf(&trash,
2266 /* bytes : in, out */
2267 "<td>%s</td><td>%s</td>"
2268 /* denied: req, resp */
2269 "<td></td><td>%s</td>"
2270 /* errors : request, connect */
2271 "<td></td><td>%s</td>"
2272 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002273 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002274 /* warnings: retries, redispatches */
2275 "<td>%lld</td><td>%lld</td>"
2276 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002277 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2278 U2H(sv->counters.failed_secu),
2279 U2H(sv->counters.failed_conns),
2280 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002281 sv->counters.cli_aborts,
2282 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002283 sv->counters.retries, sv->counters.redispatches);
2284
2285 /* status, lest check */
2286 chunk_appendf(&trash, "<td class=ac>");
2287
2288 if (sv->state & SRV_MAINTAIN) {
2289 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2290 chunk_appendf(&trash, "MAINT");
2291 }
2292 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2293 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2294 chunk_appendf(&trash, "MAINT(via)");
2295 }
2296 else if (ref->state & SRV_CHECKED) {
2297 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2298 chunk_appendf(&trash,
2299 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002300 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2301 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002302 }
Willy Tarreau91861262007-10-17 17:06:05 +02002303
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002304 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002305 chunk_appendf(&trash,
2306 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002307 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002308 get_check_status_info(sv->check.status));
2309
2310 if (sv->check.status >= HCHK_STATUS_L57DATA)
2311 chunk_appendf(&trash, "/%d", sv->check.code);
2312
2313 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002314 chunk_appendf(&trash, " in %lums", sv->check.duration);
2315
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002316 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002317 get_check_status_description(sv->check.status));
2318 if (*sv->check.desc) {
2319 chunk_appendf(&trash, ": ");
2320 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2321 chunk_htmlencode(&trash, &src);
2322 }
2323 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002324 }
2325 else
2326 chunk_appendf(&trash, "</td><td>");
2327
2328 chunk_appendf(&trash,
2329 /* weight */
2330 "</td><td class=ac>%d</td>"
2331 /* act, bck */
2332 "<td class=ac>%s</td><td class=ac>%s</td>"
2333 "",
2334 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2335 (sv->state & SRV_BACKUP) ? "-" : "Y",
2336 (sv->state & SRV_BACKUP) ? "Y" : "-");
2337
2338 /* check failures: unique, fatal, down time */
2339 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002340 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002341
2342 if (ref->observe)
2343 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2344
2345 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002346 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002347 "<td>%lld</td><td>%s</td>"
2348 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002349 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002350 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2351 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002352 else if (sv != ref) {
2353 if (sv->state & SRV_MAINTAIN)
2354 chunk_appendf(&trash,
2355 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2356 ref->proxy->id, ref->id);
2357 else
2358 chunk_appendf(&trash,
2359 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2360 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2361 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002362 else
2363 chunk_appendf(&trash, "<td colspan=3></td>");
2364
2365 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002366 if (sv->state & SRV_WARMINGUP)
2367 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002368 else
2369 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2370 }
2371 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002372 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002373 "DOWN,",
2374 "DOWN %d/%d,",
2375 "UP %d/%d,",
2376 "UP,",
2377 "NOLB %d/%d,",
2378 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002379 "DRAIN %d/%d,",
2380 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002381 "no check,"
2382 };
2383
2384 chunk_appendf(&trash,
2385 /* pxid, name */
2386 "%s,%s,"
2387 /* queue : current, max */
2388 "%d,%d,"
2389 /* sessions : current, max, limit, total */
2390 "%d,%d,%s,%lld,"
2391 /* bytes : in, out */
2392 "%lld,%lld,"
2393 /* denied: req, resp */
2394 ",%lld,"
2395 /* errors : request, connect, response */
2396 ",%lld,%lld,"
2397 /* warnings: retries, redispatches */
2398 "%lld,%lld,"
2399 "",
2400 px->id, sv->id,
2401 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002402 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002403 sv->counters.bytes_in, sv->counters.bytes_out,
2404 sv->counters.failed_secu,
2405 sv->counters.failed_conns, sv->counters.failed_resp,
2406 sv->counters.retries, sv->counters.redispatches);
2407
2408 /* status */
2409 if (sv->state & SRV_MAINTAIN)
2410 chunk_appendf(&trash, "MAINT,");
2411 else if (ref != sv && ref->state & SRV_MAINTAIN)
2412 chunk_appendf(&trash, "MAINT(via),");
2413 else
2414 chunk_appendf(&trash,
2415 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002416 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2417 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002418
2419 chunk_appendf(&trash,
2420 /* weight, active, backup */
2421 "%d,%d,%d,"
2422 "",
2423 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2424 (sv->state & SRV_BACKUP) ? 0 : 1,
2425 (sv->state & SRV_BACKUP) ? 1 : 0);
2426
2427 /* check failures: unique, fatal; last change, total downtime */
2428 if (sv->state & SRV_CHECKED)
2429 chunk_appendf(&trash,
2430 "%lld,%lld,%d,%d,",
2431 sv->counters.failed_checks, sv->counters.down_trans,
2432 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2433 else
2434 chunk_appendf(&trash, ",,,,");
2435
2436 /* queue limit, pid, iid, sid, */
2437 chunk_appendf(&trash,
2438 "%s,"
2439 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002440 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002441 relative_pid, px->uuid, sv->puid);
2442
2443 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002444 if (sv->state & SRV_WARMINGUP)
2445 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002446
2447 /* sessions: lbtot */
2448 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2449
2450 /* tracked */
2451 if (sv->track)
2452 chunk_appendf(&trash, "%s/%s,",
2453 sv->track->proxy->id, sv->track->id);
2454 else
2455 chunk_appendf(&trash, ",");
2456
2457 /* type */
2458 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2459
2460 /* rate */
2461 chunk_appendf(&trash, "%u,,%u,",
2462 read_freq_ctr(&sv->sess_per_sec),
2463 sv->counters.sps_max);
2464
2465 if (sv->state & SRV_CHECKED) {
2466 /* check_status */
2467 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2468
2469 /* check_code */
2470 if (sv->check.status >= HCHK_STATUS_L57DATA)
2471 chunk_appendf(&trash, "%u,", sv->check.code);
2472 else
2473 chunk_appendf(&trash, ",");
2474
2475 /* check_duration */
2476 if (sv->check.status >= HCHK_STATUS_CHECKED)
2477 chunk_appendf(&trash, "%lu,", sv->check.duration);
2478 else
2479 chunk_appendf(&trash, ",");
2480
2481 }
2482 else
2483 chunk_appendf(&trash, ",,,");
2484
2485 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2486 if (px->mode == PR_MODE_HTTP) {
2487 for (i=1; i<6; i++)
2488 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2489
2490 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2491 }
2492 else
2493 chunk_appendf(&trash, ",,,,,,");
2494
2495 /* failed health analyses */
2496 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2497
2498 /* requests : req_rate, req_rate_max, req_tot, */
2499 chunk_appendf(&trash, ",,,");
2500
2501 /* errors: cli_aborts, srv_aborts */
2502 chunk_appendf(&trash, "%lld,%lld,",
2503 sv->counters.cli_aborts, sv->counters.srv_aborts);
2504
2505 /* compression: in, out, bypassed, comp_rsp */
2506 chunk_appendf(&trash, ",,,,");
2507
2508 /* finish with EOL */
2509 chunk_appendf(&trash, "\n");
2510 }
2511 return 1;
2512}
2513
2514/* Dumps a line for backend <px> to the trash for and uses the state from stream
2515 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2516 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2517 */
2518static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2519{
2520 struct chunk src;
2521 int i;
2522
2523 if (!(px->cap & PR_CAP_BE))
2524 return 0;
2525
2526 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2527 return 0;
2528
Willy Tarreau354898b2012-12-23 18:15:23 +01002529 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002530 chunk_appendf(&trash, "<tr class=\"backend\">");
2531 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2532 /* Column sub-heading for Enable or Disable server */
2533 chunk_appendf(&trash, "<td></td>");
2534 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002535 chunk_appendf(&trash,
2536 "<td class=ac>"
2537 /* name */
2538 "%s<a name=\"%s/Backend\"></a>"
2539 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2540 "",
2541 (flags & ST_SHLGNDS)?"<u>":"",
2542 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002543
2544 if (flags & ST_SHLGNDS) {
2545 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002546 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2548
2549 /* cookie */
2550 if (px->cookie_name) {
2551 chunk_appendf(&trash, ", cookie: '");
2552 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2553 chunk_htmlencode(&trash, &src);
2554 chunk_appendf(&trash, "'");
2555 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002556 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002557 }
2558
2559 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002560 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002561 /* queue : current, max */
2562 "<td>%s</td><td>%s</td><td></td>"
2563 /* sessions rate : current, max, limit */
2564 "<td>%s</td><td>%s</td><td></td>"
2565 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002566 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002567 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2568 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569
2570 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002571 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002573 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002574 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002576 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2577 U2H(px->be_counters.cum_conn),
2578 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579
Willy Tarreau466c9b52012-12-23 02:25:03 +01002580 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002581 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002582 chunk_appendf(&trash,
2583 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2584 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2585 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2586 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2587 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2588 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2589 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2590 "<tr><th>- other responses:</th><td>%s</td></tr>"
2591 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2592 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002593 U2H(px->be_counters.p.http.cum_req),
2594 U2H(px->be_counters.p.http.rsp[1]),
2595 U2H(px->be_counters.p.http.rsp[2]),
2596 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002598 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002599 U2H(px->be_counters.p.http.rsp[3]),
2600 U2H(px->be_counters.p.http.rsp[4]),
2601 U2H(px->be_counters.p.http.rsp[5]),
2602 U2H(px->be_counters.p.http.rsp[0]),
2603 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002604 }
2605
2606 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002607 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002608 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002609 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002611 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002613 U2H(px->be_counters.cum_lbconn),
2614 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002615
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002617 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002618 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002619 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002620 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002621 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2622 px->be_counters.comp_in ?
2623 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2625
2626 chunk_appendf(&trash,
2627 /* denied: req, resp */
2628 "<td>%s</td><td>%s</td>"
2629 /* errors : request, connect */
2630 "<td></td><td>%s</td>"
2631 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002632 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002633 /* warnings: retries, redispatches */
2634 "<td>%lld</td><td>%lld</td>"
2635 /* backend status: reflect backend status (up/down): we display UP
2636 * if the backend has known working servers or if it has no server at
2637 * all (eg: for stats). Then we display the total weight, number of
2638 * active and backups. */
2639 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2640 "<td class=ac>%d</td><td class=ac>%d</td>"
2641 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002642 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2643 U2H(px->be_counters.failed_conns),
2644 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 px->be_counters.cli_aborts,
2646 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 px->be_counters.retries, px->be_counters.redispatches,
2648 human_time(now.tv_sec - px->last_change, 1),
2649 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2650 "<font color=\"red\"><b>DOWN</b></font>",
2651 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2652 px->srv_act, px->srv_bck);
2653
2654 chunk_appendf(&trash,
2655 /* rest of backend: nothing, down transitions, total downtime, throttle */
2656 "<td class=ac>&nbsp;</td><td>%d</td>"
2657 "<td>%s</td>"
2658 "<td></td>"
2659 "</tr>",
2660 px->down_trans,
2661 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2662 }
2663 else { /* CSV mode */
2664 chunk_appendf(&trash,
2665 /* pxid, name */
2666 "%s,BACKEND,"
2667 /* queue : current, max */
2668 "%d,%d,"
2669 /* sessions : current, max, limit, total */
2670 "%d,%d,%d,%lld,"
2671 /* bytes : in, out */
2672 "%lld,%lld,"
2673 /* denied: req, resp */
2674 "%lld,%lld,"
2675 /* errors : request, connect, response */
2676 ",%lld,%lld,"
2677 /* warnings: retries, redispatches */
2678 "%lld,%lld,"
2679 /* backend status: reflect backend status (up/down): we display UP
2680 * if the backend has known working servers or if it has no server at
2681 * all (eg: for stats). Then we display the total weight, number of
2682 * active and backups. */
2683 "%s,"
2684 "%d,%d,%d,"
2685 /* rest of backend: nothing, down transitions, last change, total downtime */
2686 ",%d,%d,%d,,"
2687 /* pid, iid, sid, throttle, lbtot, tracked, type */
2688 "%d,%d,0,,%lld,,%d,"
2689 /* rate, rate_lim, rate_max, */
2690 "%u,,%u,"
2691 /* check_status, check_code, check_duration */
2692 ",,,",
2693 px->id,
2694 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2695 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2696 px->be_counters.bytes_in, px->be_counters.bytes_out,
2697 px->be_counters.denied_req, px->be_counters.denied_resp,
2698 px->be_counters.failed_conns, px->be_counters.failed_resp,
2699 px->be_counters.retries, px->be_counters.redispatches,
2700 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2701 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2702 px->srv_act, px->srv_bck,
2703 px->down_trans, (int)(now.tv_sec - px->last_change),
2704 px->srv?be_downtime(px):0,
2705 relative_pid, px->uuid,
2706 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2707 read_freq_ctr(&px->be_sess_per_sec),
2708 px->be_counters.sps_max);
2709
2710 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2711 if (px->mode == PR_MODE_HTTP) {
2712 for (i=1; i<6; i++)
2713 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2714 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2715 }
2716 else
2717 chunk_appendf(&trash, ",,,,,,");
2718
2719 /* failed health analyses */
2720 chunk_appendf(&trash, ",");
2721
2722 /* requests : req_rate, req_rate_max, req_tot, */
2723 chunk_appendf(&trash, ",,,");
2724
2725 /* errors: cli_aborts, srv_aborts */
2726 chunk_appendf(&trash, "%lld,%lld,",
2727 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2728
2729 /* compression: in, out, bypassed */
2730 chunk_appendf(&trash, "%lld,%lld,%lld,",
2731 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2732
2733 /* compression: comp_rsp */
2734 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2735
2736 /* finish with EOL */
2737 chunk_appendf(&trash, "\n");
2738 }
2739 return 1;
2740}
2741
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002742/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002743 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002744 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002745 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002746static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747{
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002748 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2749
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002750 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2751 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002752
2753 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2754 scope_txt[0] = 0;
2755 if (si->applet.ctx.stats.scope_len) {
2756 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2757 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
2758 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
2759 }
2760
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002761 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002762 "<form action=\"%s%s%s%s\" method=\"post\">",
2763 uri->uri_prefix,
2764 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2765 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2766 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 }
2768
2769 /* print a new table */
2770 chunk_appendf(&trash,
2771 "<table class=\"tbl\" width=\"100%%\">\n"
2772 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002773 "<th class=\"pxname\" width=\"10%%\">");
2774
2775 chunk_appendf(&trash,
2776 "<a name=\"%s\"></a>%s"
2777 "<a class=px href=\"#%s\">%s</a>",
2778 px->id,
2779 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2780 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002781
2782 if (uri->flags & ST_SHLGNDS) {
2783 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002784 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2786 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002787 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002788 }
2789
2790 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002791 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792 "<th class=\"%s\" width=\"90%%\">%s</th>"
2793 "</tr>\n"
2794 "</table>\n"
2795 "<table class=\"tbl\" width=\"100%%\">\n"
2796 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2798 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2799
2800 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2801 /* Column heading for Enable or Disable server */
2802 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002803 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002804
2805 chunk_appendf(&trash,
2806 "<th rowspan=2></th>"
2807 "<th colspan=3>Queue</th>"
2808 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2809 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2810 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2811 "<th colspan=9>Server</th>"
2812 "</tr>\n"
2813 "<tr class=\"titre\">"
2814 "<th>Cur</th><th>Max</th><th>Limit</th>"
2815 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2816 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2817 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2818 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2819 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2820 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2821 "<th>Thrtle</th>\n"
2822 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002823}
2824
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002825/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002826 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002828static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002830 chunk_appendf(&trash, "</table>");
2831
2832 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2833 /* close the form used to enable/disable this proxy servers */
2834 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002835 "Choose the action to perform on the checked servers : "
2836 "<select name=action>"
2837 "<option value=\"\"></option>"
2838 "<option value=\"disable\">Disable</option>"
2839 "<option value=\"enable\">Enable</option>"
2840 "<option value=\"stop\">Soft Stop</option>"
2841 "<option value=\"start\">Soft Start</option>"
2842 "<option value=\"shutdown\">Kill Sessions</option>"
2843 "</select>"
2844 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2845 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2846 "</form>",
2847 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848 }
2849
2850 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851}
Willy Tarreau91861262007-10-17 17:06:05 +02002852
2853/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002854 * Dumps statistics for a proxy. The output is sent to the stream interface's
2855 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2856 * buffer space, or non-zero if everything completed. This function is used
2857 * both by the CLI and the HTTP entry points, and is able to dump the output
2858 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002859 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002860static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002861{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02002862 struct session *s = si->conn->xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002863 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002864 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002865 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002866
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002867 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002868
Willy Tarreau295a8372011-03-10 11:25:07 +01002869 switch (si->applet.ctx.stats.px_st) {
2870 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002871 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002872 if (uri && uri->scope) {
2873 /* we have a limited scope, we have to check the proxy name */
2874 struct stat_scope *scope;
2875 int len;
2876
2877 len = strlen(px->id);
2878 scope = uri->scope;
2879
2880 while (scope) {
2881 /* match exact proxy name */
2882 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2883 break;
2884
2885 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002886 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002887 break;
2888 scope = scope->next;
2889 }
2890
2891 /* proxy name not found : don't dump anything */
2892 if (scope == NULL)
2893 return 1;
2894 }
2895
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002896 /* if the user has requested a limited output and the proxy
2897 * name does not match, skip it.
2898 */
2899 if (si->applet.ctx.stats.scope_len &&
2900 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len) == NULL)
2901 return 1;
2902
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002903 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2904 (si->applet.ctx.stats.iid != -1) &&
2905 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002906 return 1;
2907
Willy Tarreau295a8372011-03-10 11:25:07 +01002908 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002909 /* fall through */
2910
Willy Tarreau295a8372011-03-10 11:25:07 +01002911 case STAT_PX_ST_TH:
Willy Tarreau354898b2012-12-23 18:15:23 +01002912 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002913 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002914 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002915 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002916 }
Willy Tarreau91861262007-10-17 17:06:05 +02002917
Willy Tarreau295a8372011-03-10 11:25:07 +01002918 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002919 /* fall through */
2920
Willy Tarreau295a8372011-03-10 11:25:07 +01002921 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002922 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002923 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002924 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002925 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002926
Willy Tarreau4348fad2012-09-20 16:48:07 +02002927 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002929 /* fall through */
2930
Willy Tarreau295a8372011-03-10 11:25:07 +01002931 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002932 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002933 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 +02002934 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002935 return 0;
2936
Willy Tarreau4348fad2012-09-20 16:48:07 +02002937 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002938 if (!l->counters)
2939 continue;
2940
Willy Tarreau295a8372011-03-10 11:25:07 +01002941 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2942 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002943 break;
2944
Willy Tarreau295a8372011-03-10 11:25:07 +01002945 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002946 continue;
2947 }
2948
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002949 /* print the frontend */
2950 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2951 if (bi_putchk(rep, &trash) == -1)
2952 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002953 }
2954
Willy Tarreau295a8372011-03-10 11:25:07 +01002955 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2956 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002957 /* fall through */
2958
Willy Tarreau295a8372011-03-10 11:25:07 +01002959 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002960 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002961 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002962 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 +02002963
Willy Tarreau9b28e032012-10-12 23:49:43 +02002964 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002965 return 0;
2966
Willy Tarreau295a8372011-03-10 11:25:07 +01002967 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002968
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2970 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002971 break;
2972
Willy Tarreau295a8372011-03-10 11:25:07 +01002973 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002974 continue;
2975 }
2976
Willy Tarreau44267702011-10-28 15:35:33 +02002977 if (sv->track)
2978 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002979 else
2980 svs = sv;
2981
Willy Tarreau91861262007-10-17 17:06:05 +02002982 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002983 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09002984 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002985 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09002986 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002987 sv_state = 3; /* UP */
2988 else
2989 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002990
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002991 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002992 sv_state += 2;
Simon Horman8c3d0be2013-11-25 10:46:40 +09002993 else if (svs->state & SRV_DRAIN)
2994 sv_state += 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01002995 }
Willy Tarreau91861262007-10-17 17:06:05 +02002996 else
Simon Horman125d0992013-02-24 17:23:38 +09002997 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02002998 sv_state = 1; /* going up */
2999 else
3000 sv_state = 0; /* DOWN */
3001
Willy Tarreau295a8372011-03-10 11:25:07 +01003002 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003003 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01003004 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003005 continue;
3006 }
3007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003008 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3009 if (bi_putchk(rep, &trash) == -1)
3010 return 0;
3011 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
3014 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003015
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003016 case STAT_PX_ST_BE:
3017 /* print the backend */
3018 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3019 if (bi_putchk(rep, &trash) == -1)
3020 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003021
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003022 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
3023 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003024
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 case STAT_PX_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003026 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003027 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003028 if (bi_putchk(rep, &trash) == -1)
3029 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003030 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003031
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003032 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
3033 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035 case STAT_PX_ST_FIN:
3036 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003037
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003038 default:
3039 /* unknown state, we should put an abort() here ! */
3040 return 1;
3041 }
3042}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003043
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003044/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3045 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003046 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003047static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003048{
3049 /* WARNING! This must fit in the first buffer !!! */
3050 chunk_appendf(&trash,
3051 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3052 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3053 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3054 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3055 "<style type=\"text/css\"><!--\n"
3056 "body {"
3057 " font-family: arial, helvetica, sans-serif;"
3058 " font-size: 12px;"
3059 " font-weight: normal;"
3060 " color: black;"
3061 " background: white;"
3062 "}\n"
3063 "th,td {"
3064 " font-size: 10px;"
3065 "}\n"
3066 "h1 {"
3067 " font-size: x-large;"
3068 " margin-bottom: 0.5em;"
3069 "}\n"
3070 "h2 {"
3071 " font-family: helvetica, arial;"
3072 " font-size: x-large;"
3073 " font-weight: bold;"
3074 " font-style: italic;"
3075 " color: #6020a0;"
3076 " margin-top: 0em;"
3077 " margin-bottom: 0em;"
3078 "}\n"
3079 "h3 {"
3080 " font-family: helvetica, arial;"
3081 " font-size: 16px;"
3082 " font-weight: bold;"
3083 " color: #b00040;"
3084 " background: #e8e8d0;"
3085 " margin-top: 0em;"
3086 " margin-bottom: 0em;"
3087 "}\n"
3088 "li {"
3089 " margin-top: 0.25em;"
3090 " margin-right: 2em;"
3091 "}\n"
3092 ".hr {margin-top: 0.25em;"
3093 " border-color: black;"
3094 " border-bottom-style: solid;"
3095 "}\n"
3096 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3097 ".total {background: #20D0D0;color: #ffff80;}\n"
3098 ".frontend {background: #e8e8d0;}\n"
3099 ".socket {background: #d0d0d0;}\n"
3100 ".backend {background: #e8e8d0;}\n"
3101 ".active0 {background: #ff9090;}\n"
3102 ".active1 {background: #ffd020;}\n"
3103 ".active2 {background: #ffffa0;}\n"
3104 ".active3 {background: #c0ffc0;}\n"
3105 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3106 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003107 ".active6 {background: #ffffa0;}\n"
3108 ".active7 {background: #cc9900;}\n"
3109 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 ".backup0 {background: #ff9090;}\n"
3111 ".backup1 {background: #ff80ff;}\n"
3112 ".backup2 {background: #c060ff;}\n"
3113 ".backup3 {background: #b0d0ff;}\n"
3114 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3115 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003116 ".backup6 {background: #c060ff;}\n"
3117 ".backup7 {background: #cc9900;}\n"
3118 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 ".maintain {background: #c07820;}\n"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07003120 ".softstop {background: #0067FF;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003121 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3122 "\n"
3123 "a.px:link {color: #ffff40; text-decoration: none;}"
3124 "a.px:visited {color: #ffff40; text-decoration: none;}"
3125 "a.px:hover {color: #ffffff; text-decoration: none;}"
3126 "a.lfsb:link {color: #000000; text-decoration: none;}"
3127 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3128 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3129 "\n"
3130 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3131 "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"
3132 "table.tbl td.ac { text-align: center;}\n"
3133 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3134 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3135 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3136 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3137 "\n"
3138 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3139 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3140 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003141 "table.det { border-collapse: collapse; border-style: none; }\n"
3142 "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n"
3143 "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;font-family: monospace;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003144 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003145 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003146 " display:block;\n"
3147 " visibility:hidden;\n"
3148 " z-index:2147483647;\n"
3149 " position:absolute;\n"
3150 " padding:2px 4px 3px;\n"
3151 " background:#f0f060; color:#000000;\n"
3152 " border:1px solid #7040c0;\n"
3153 " white-space:nowrap;\n"
3154 " font-style:normal;font-size:11px;font-weight:normal;\n"
3155 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3156 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3157 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003158 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003159 "-->\n"
3160 "</style></head>\n",
3161 (uri->flags & ST_SHNODE) ? " on " : "",
3162 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3163 );
3164}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003165
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003166/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003168 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003169 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003170static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171{
3172 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003173 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 /* WARNING! this has to fit the first packet too.
3176 * We are around 3.5 kB, add adding entries will
3177 * become tricky if we want to support 4kB buffers !
3178 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 chunk_appendf(&trash,
3180 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3181 PRODUCT_NAME "%s</a></h1>\n"
3182 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3183 "<hr width=\"100%%\" class=\"hr\">\n"
3184 "<h3>&gt; General process information</h3>\n"
3185 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3186 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3187 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3188 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3189 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3190 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3191 "Running tasks: %d/%d; idle = %d %%<br>\n"
3192 "</td><td align=\"center\" nowrap>\n"
3193 "<table class=\"lgd\"><tr>\n"
3194 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3195 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3196 "</tr><tr>\n"
3197 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3198 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3199 "</tr><tr>\n"
3200 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3201 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3202 "</tr><tr>\n"
3203 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003204 "<td class=\"active7\"></td><td class=\"noborder\">active or backup DRAIN &nbsp;</td>"
3205 "</tr><tr>\n"
3206 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003207 "</tr><tr>\n"
3208 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07003209 "</tr><tr>\n"
3210 "<td class=\"softstop\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211 "</tr></table>\n"
3212 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
3213 "</td>"
3214 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3215 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3216 "",
3217 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3218 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3219 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3220 (uri->flags & ST_SHDESC) ? ": " : "",
3221 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3222 pid, relative_pid, global.nbproc,
3223 up / 86400, (up % 86400) / 3600,
3224 (up % 3600) / 60, (up % 60),
3225 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3226 global.rlimit_memmax ? " MB" : "",
3227 global.rlimit_nofile,
3228 global.maxsock, global.maxconn, global.maxpipes,
3229 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3230 run_queue_cur, nb_tasks_cur, idle_pct
3231 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003232
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003233 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3234 memcpy(scope_txt, bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3235 scope_txt[si->applet.ctx.stats.scope_len] = '\0';
3236
3237 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003238 "<li><form method=\"GET\" action=\"%s%s%s\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003239 uri->uri_prefix,
3240 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3241 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3242 (si->applet.ctx.stats.scope_len > 0) ? scope_txt : "",
3243 STAT_SCOPE_TXT_MAXLEN);
3244
3245 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3246 scope_txt[0] = 0;
3247 if (si->applet.ctx.stats.scope_len) {
3248 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3249 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3250 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3251 }
3252
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3254 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003255 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 uri->uri_prefix,
3257 "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003258 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3259 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003260 else
3261 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003262 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263 uri->uri_prefix,
3264 ";up",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003265 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3266 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003267
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003268 if (uri->refresh > 0) {
3269 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3270 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003271 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 uri->uri_prefix,
3273 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003274 "",
3275 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003276 else
3277 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003278 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279 uri->uri_prefix,
3280 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003281 ";norefresh",
3282 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003284
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003285 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003286 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287 uri->uri_prefix,
3288 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003289 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3290 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003291
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003293 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003295 (uri->refresh > 0) ? ";norefresh" : "",
3296 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003297
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003298 chunk_appendf(&trash,
3299 "</ul></td>"
3300 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3301 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3302 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3303 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3304 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3305 "</ul>"
3306 "</td>"
3307 "</tr></table>\n"
3308 ""
3309 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003310
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311 if (si->applet.ctx.stats.st_code) {
3312 switch (si->applet.ctx.stats.st_code) {
3313 case STAT_STATUS_DONE:
3314 chunk_appendf(&trash,
3315 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003316 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003318 "</div>\n", uri->uri_prefix,
3319 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3320 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3321 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 break;
3323 case STAT_STATUS_NONE:
3324 chunk_appendf(&trash,
3325 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003326 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003328 "</div>\n", uri->uri_prefix,
3329 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3330 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3331 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 break;
3333 case STAT_STATUS_PART:
3334 chunk_appendf(&trash,
3335 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003336 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 "Action partially processed.<br>"
3338 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003339 "</div>\n", uri->uri_prefix,
3340 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3341 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3342 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 break;
3344 case STAT_STATUS_ERRP:
3345 chunk_appendf(&trash,
3346 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003347 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003348 "Action not processed because of invalid parameters."
3349 "<ul>"
3350 "<li>The action is maybe unknown.</li>"
3351 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3352 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3353 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003354 "</div>\n", uri->uri_prefix,
3355 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3356 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3357 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 break;
3359 case STAT_STATUS_EXCD:
3360 chunk_appendf(&trash,
3361 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003362 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3364 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003365 "</div>\n", uri->uri_prefix,
3366 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3367 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3368 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 break;
3370 case STAT_STATUS_DENY:
3371 chunk_appendf(&trash,
3372 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003373 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003375 "</div>\n", uri->uri_prefix,
3376 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3377 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3378 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 break;
3380 default:
3381 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003382 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003383 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003385 "</div>\n", uri->uri_prefix,
3386 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3387 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3388 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 }
3390 chunk_appendf(&trash, "<p>\n");
3391 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003393
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003394/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3395 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003397static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003401
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003402/* This function dumps statistics onto the stream interface's read buffer in
3403 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
3404 * are ignored for CSV format (hence <uri> may be NULL there). The xprt_ctx must
3405 * have been zeroed first, and the flags properly set. It returns 0 if it had to
3406 * stop writing data and an I/O is needed, 1 if the dump is finished and the
3407 * session must be closed, or -1 in case of any error. This function is used by
3408 * both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003410static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 struct channel *rep = si->ib;
3413 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003414
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003416
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 switch (si->conn->xprt_st) {
3418 case STAT_ST_INIT:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 si->conn->xprt_st = STAT_ST_HEAD; /* let's start producing data */
3420 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003421
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 case STAT_ST_HEAD:
Willy Tarreau354898b2012-12-23 18:15:23 +01003423 if (si->applet.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003424 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003425 else
3426 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 if (bi_putchk(rep, &trash) == -1)
3429 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003430
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 si->conn->xprt_st = STAT_ST_INFO;
3432 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003433
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003434 case STAT_ST_INFO:
Willy Tarreau354898b2012-12-23 18:15:23 +01003435 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003436 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003437 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003438 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003439 }
Willy Tarreau91861262007-10-17 17:06:05 +02003440
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 si->applet.ctx.stats.px = proxy;
3442 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3443 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003444 /* fall through */
3445
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 case STAT_ST_LIST:
3447 /* dump proxies */
3448 while (si->applet.ctx.stats.px) {
3449 if (buffer_almost_full(rep->buf))
3450 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003451
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 px = si->applet.ctx.stats.px;
3453 /* skip the disabled proxies, global frontend and non-networked ones */
3454 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003455 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003457
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003458 si->applet.ctx.stats.px = px->next;
3459 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3460 }
3461 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003462
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 si->conn->xprt_st = STAT_ST_END;
3464 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003465
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003466 case STAT_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003467 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003468 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003469 if (bi_putchk(rep, &trash) == -1)
3470 return 0;
3471 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 si->conn->xprt_st = STAT_ST_FIN;
3474 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 case STAT_ST_FIN:
3477 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003478
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003479 default:
3480 /* unknown state ! */
3481 si->conn->xprt_st = STAT_ST_FIN;
3482 return -1;
3483 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003484}
Willy Tarreauae526782010-03-04 20:34:23 +01003485
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003486/* This I/O handler runs as an applet embedded in a stream interface. It is
3487 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3488 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3489 * automatically unregisters itself once transfer is complete.
3490 */
3491static void http_stats_io_handler(struct stream_interface *si)
3492{
3493 struct session *s = si->conn->xprt_ctx;
3494 struct channel *req = si->ob;
3495 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003496
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3498 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003499
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003500 /* check that the output is not closed */
3501 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3502 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 if (!si->applet.st0) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003505 if (stats_dump_stat_to_buffer(si, s->be->uri_auth)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003506 si->applet.st0 = 1;
3507 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003508 }
3509 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003510
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003511 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3512 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003513
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003514 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3515 si_shutr(si);
3516 res->flags |= CF_READ_NULL;
3517 }
Willy Tarreau91861262007-10-17 17:06:05 +02003518
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003519 /* update all other flags and resync with the other side */
3520 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003521
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003522 /* we don't want to expire timeouts while we're processing requests */
3523 si->ib->rex = TICK_ETERNITY;
3524 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003525
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003526 out:
3527 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3528 /* check that we have released everything then unregister */
3529 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003530 }
3531}
3532
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003533
Willy Tarreau909d5172012-11-26 03:04:41 +01003534static inline const char *get_conn_ctrl_name(const struct connection *conn)
3535{
3536 if (!conn->ctrl)
3537 return "NONE";
3538 return conn->ctrl->name;
3539}
3540
3541static inline const char *get_conn_xprt_name(const struct connection *conn)
3542{
3543 static char ptr[17];
3544
3545 if (!conn->xprt)
3546 return "NONE";
3547
3548 if (conn->xprt == &raw_sock)
3549 return "RAW";
3550
3551#ifdef USE_OPENSSL
3552 if (conn->xprt == &ssl_sock)
3553 return "SSL";
3554#endif
3555 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3556 return ptr;
3557}
3558
3559static inline const char *get_conn_data_name(const struct connection *conn)
3560{
3561 static char ptr[17];
3562
3563 if (!conn->data)
3564 return "NONE";
3565
3566 if (conn->data == &sess_conn_cb)
3567 return "SESS";
3568
3569 if (conn->data == &si_conn_cb)
3570 return "STRM";
3571
3572 if (conn->data == &check_conn_cb)
3573 return "CHCK";
3574
3575 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3576 return ptr;
3577}
3578
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003579/* This function dumps a complete session state onto the stream interface's
3580 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3581 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003582 * 0 if the output buffer is full and it needs to be called again, otherwise
3583 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003584 */
Willy Tarreau76153662012-11-26 01:16:39 +01003585static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003586{
3587 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003588 extern const char *monthname[12];
3589 char pn[INET6_ADDRSTRLEN];
3590
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003591 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003592
Willy Tarreau295a8372011-03-10 11:25:07 +01003593 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003594 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003595 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3596 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003597 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003598 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003599 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003600 return 1;
3601 }
3602
Willy Tarreau295a8372011-03-10 11:25:07 +01003603 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003604 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003605 si->applet.ctx.sess.uid = sess->uniq_id;
3606 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003607 /* fall through */
3608
3609 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003610 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003611 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003612 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003613 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003614 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3615 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003616 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003617 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003618
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003619 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003620 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003621 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003622 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003623 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003624 break;
3625 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003626 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003627 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003628 default:
3629 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003630 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003631 break;
3632 }
3633
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003634 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003635 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003636 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003637
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003638 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003639 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003640 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3641 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3642 sess->listener ? sess->listener->luid : 0);
3643
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003644 conn_get_to_addr(sess->si[0].conn);
3645 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003646 case AF_INET:
3647 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003648 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003649 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003650 break;
3651 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003652 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003653 break;
3654 default:
3655 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003656 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003657 break;
3658 }
3659
Willy Tarreau50943332011-09-02 17:33:05 +02003660 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003661 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003662 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003663 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003664 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3665 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003666 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003667
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003668 conn_get_from_addr(sess->si[1].conn);
3669 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003670 case AF_INET:
3671 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003672 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003673 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003674 break;
3675 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003676 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003677 break;
3678 default:
3679 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003680 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003681 break;
3682 }
3683
3684 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003685 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003686 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003687 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3688 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003689 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003690 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003691
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003692 conn_get_to_addr(sess->si[1].conn);
3693 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003694 case AF_INET:
3695 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003696 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003697 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003698 break;
3699 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003700 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003701 break;
3702 default:
3703 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003704 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003705 break;
3706 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003707
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003708 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003709 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003710 sess->task,
3711 sess->task->state,
3712 sess->task->nice, sess->task->calls,
3713 sess->task->expire ?
3714 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3715 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3716 TICKS_TO_MS(1000)) : "<NEVER>",
3717 task_in_rq(sess->task) ? ", running" : "");
3718
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003719 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003720 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003721 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003723 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003724 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3725 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3726 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3727
3728 chunk_appendf(&trash,
3729 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003730 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003731 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003732 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003733 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003734 sess->si[0].exp ?
3735 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3736 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3737 TICKS_TO_MS(1000)) : "<NEVER>",
3738 sess->si[0].err_type);
3739
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003740 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003741 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003742 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003743 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003744 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003745 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003746 sess->si[1].exp ?
3747 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3748 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3749 TICKS_TO_MS(1000)) : "<NEVER>",
3750 sess->si[1].err_type);
3751
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003752 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003753 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003754 sess->si[0].conn,
3755 get_conn_ctrl_name(sess->si[0].conn),
3756 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003757 get_conn_data_name(sess->si[0].conn),
3758 obj_type_name(sess->si[0].conn->target),
3759 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003760
3761 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003762 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003763 sess->si[0].conn->flags,
3764 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003765 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3766 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3767 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 +01003768
3769 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003770 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003771 sess->si[1].conn,
3772 get_conn_ctrl_name(sess->si[1].conn),
3773 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003774 get_conn_data_name(sess->si[1].conn),
3775 obj_type_name(sess->si[1].conn->target),
3776 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003777
3778 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003779 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003780 sess->si[1].conn->flags,
3781 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003782 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3783 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3784 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 +01003785
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003786 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003787 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003788 " an_exp=%s",
3789 sess->req,
3790 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003791 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003792 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003793 sess->req->analyse_exp ?
3794 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3795 TICKS_TO_MS(1000)) : "<NEVER>");
3796
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003797 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003798 " rex=%s",
3799 sess->req->rex ?
3800 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3801 TICKS_TO_MS(1000)) : "<NEVER>");
3802
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003803 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003804 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003805 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003806 sess->req->wex ?
3807 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3808 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003809 sess->req->buf,
3810 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003811 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003812 sess->txn.req.next, sess->req->buf->i,
3813 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003814
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003815 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003816 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003817 " an_exp=%s",
3818 sess->rep,
3819 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003820 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003821 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003822 sess->rep->analyse_exp ?
3823 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3824 TICKS_TO_MS(1000)) : "<NEVER>");
3825
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003826 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003827 " rex=%s",
3828 sess->rep->rex ?
3829 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3830 TICKS_TO_MS(1000)) : "<NEVER>");
3831
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003832 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003833 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003834 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003835 sess->rep->wex ?
3836 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3837 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003838 sess->rep->buf,
3839 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003840 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003841 sess->txn.rsp.next, sess->rep->buf->i,
3842 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003843
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003844 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003845 return 0;
3846
3847 /* use other states to dump the contents */
3848 }
3849 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003850 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003851 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003852 return 1;
3853}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003854
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003855/* This function dumps all sessions' states onto the stream interface's
3856 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003857 * properly set. It returns 0 if the output buffer is full and it needs
3858 * to be called again, otherwise non-zero. It is designed to be called
3859 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003860 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003861static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003862{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003863 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003864 /* If we're forced to shut down, we might have to remove our
3865 * reference to the last session being dumped.
3866 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003867 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003868 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3869 LIST_DEL(&si->applet.ctx.sess.bref.users);
3870 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003871 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003872 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003873 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003874 }
3875
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003876 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003877
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003878 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003879 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003880 /* the function had not been called yet, let's prepare the
3881 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003882 * pointer to the first in the global list. When a target
3883 * session is being destroyed, it is responsible for updating
3884 * this pointer. We know we have reached the end when this
3885 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003886 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003887 LIST_INIT(&si->applet.ctx.sess.bref.users);
3888 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003889 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003890 /* fall through */
3891
Willy Tarreau295a8372011-03-10 11:25:07 +01003892 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003893 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003894 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3895 LIST_DEL(&si->applet.ctx.sess.bref.users);
3896 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003897 }
3898
3899 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003900 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003901 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003902 struct session *curr_sess;
3903
Willy Tarreau295a8372011-03-10 11:25:07 +01003904 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003905
Willy Tarreau295a8372011-03-10 11:25:07 +01003906 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003907 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003908 goto next_sess;
3909
Willy Tarreau295a8372011-03-10 11:25:07 +01003910 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003911 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003912 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003913 return 0;
3914
3915 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003916 LIST_DEL(&si->applet.ctx.sess.bref.users);
3917 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003918 if (si->applet.ctx.sess.target != (void *)-1) {
3919 si->applet.ctx.sess.target = NULL;
3920 break;
3921 }
3922 else
3923 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003924 }
3925
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003926 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003927 "%p: proto=%s",
3928 curr_sess,
3929 curr_sess->listener->proto->name);
3930
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003931
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003932 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003933 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003934 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003935 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003936 " src=%s:%d fe=%s be=%s srv=%s",
3937 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003938 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003939 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003940 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003941 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003942 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003943 break;
3944 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003945 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003946 " src=unix:%d fe=%s be=%s srv=%s",
3947 curr_sess->listener->luid,
3948 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003949 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003950 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003951 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003952 break;
3953 }
3954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003955 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003956 " ts=%02x age=%s calls=%d",
3957 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003958 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3959 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003960
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003961 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003962 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003963 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003964 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003965 curr_sess->req->analysers,
3966 curr_sess->req->rex ?
3967 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3968 TICKS_TO_MS(1000)) : "");
3969
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003970 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003971 ",wx=%s",
3972 curr_sess->req->wex ?
3973 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3974 TICKS_TO_MS(1000)) : "");
3975
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003976 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003977 ",ax=%s]",
3978 curr_sess->req->analyse_exp ?
3979 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3980 TICKS_TO_MS(1000)) : "");
3981
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003982 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003983 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003984 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003985 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003986 curr_sess->rep->analysers,
3987 curr_sess->rep->rex ?
3988 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3989 TICKS_TO_MS(1000)) : "");
3990
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003992 ",wx=%s",
3993 curr_sess->rep->wex ?
3994 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3995 TICKS_TO_MS(1000)) : "");
3996
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003997 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003998 ",ax=%s]",
3999 curr_sess->rep->analyse_exp ?
4000 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
4001 TICKS_TO_MS(1000)) : "");
4002
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004003 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004004 " s0=[%d,%1xh,fd=%d,ex=%s]",
4005 curr_sess->si[0].state,
4006 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004007 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004008 curr_sess->si[0].exp ?
4009 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4010 TICKS_TO_MS(1000)) : "");
4011
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004012 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004013 " s1=[%d,%1xh,fd=%d,ex=%s]",
4014 curr_sess->si[1].state,
4015 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004016 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004017 curr_sess->si[1].exp ?
4018 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4019 TICKS_TO_MS(1000)) : "");
4020
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004021 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004022 " exp=%s",
4023 curr_sess->task->expire ?
4024 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4025 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004026 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004027 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004028
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004029 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004031 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004032 /* let's try again later from this session. We add ourselves into
4033 * this session's users so that it can remove us upon termination.
4034 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004035 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004036 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004037 }
4038
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004039 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01004040 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004041 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004042
Willy Tarreau76153662012-11-26 01:16:39 +01004043 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004044 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01004045 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004046 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004047 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004048 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004049
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004050 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004051 return 0;
4052
Willy Tarreau295a8372011-03-10 11:25:07 +01004053 si->applet.ctx.sess.target = NULL;
4054 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004055 return 1;
4056 }
4057
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004058 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004059 /* fall through */
4060
4061 default:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004062 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004063 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004064 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004065}
4066
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004067/* This is called when the stream interface is closed. For instance, upon an
4068 * external abort, we won't call the i/o handler anymore so we may need to
4069 * remove back references to the session currently being dumped.
4070 */
Simon Horman74d88312013-02-12 10:45:50 +09004071static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004072{
4073 if (si->applet.st0 == STAT_CLI_O_SESS && si->conn->xprt_st == STAT_ST_LIST) {
4074 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
4075 LIST_DEL(&si->applet.ctx.sess.bref.users);
4076 }
4077}
4078
Willy Tarreau20e99322013-04-13 09:22:25 +02004079/* This function is used to either dump tables states (when action is set
4080 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
4081 * The xprt_ctx must have been zeroed first, and the flags properly set.
4082 * It returns 0 if the output buffer is full and it needs to be called
4083 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004084 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004085static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004086{
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004087 struct session *s = si->conn->xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004088 struct ebmb_node *eb;
4089 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004090 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004091 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004092
4093 /*
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004094 * We have 3 possible states in si->conn->xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01004095 * - STAT_ST_INIT : the first call
4096 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004097 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004098 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004099 * and the entry pointer points to the next entry to be dumped,
4100 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004101 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004102 * data though.
4103 */
4104
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004105 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004106 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004107 if (si->conn->xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004108 si->applet.ctx.table.entry->ref_cnt--;
4109 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004110 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004111 return 1;
4112 }
4113
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004114 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004115
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004116 while (si->conn->xprt_st != STAT_ST_FIN) {
4117 switch (si->conn->xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004118 case STAT_ST_INIT:
4119 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4120 if (!si->applet.ctx.table.proxy)
4121 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004122
Willy Tarreau295a8372011-03-10 11:25:07 +01004123 si->applet.ctx.table.entry = NULL;
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_INFO:
4128 if (!si->applet.ctx.table.proxy ||
4129 (si->applet.ctx.table.target &&
4130 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004131 si->conn->xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004132 break;
4133 }
4134
Willy Tarreau295a8372011-03-10 11:25:07 +01004135 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004136 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004137 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004138 return 0;
4139
Willy Tarreau295a8372011-03-10 11:25:07 +01004140 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004141 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004142 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004143 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004144 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004145 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4146 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004147 si->conn->xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004148 break;
4149 }
4150 }
4151 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004152 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004153 break;
4154
Willy Tarreau295a8372011-03-10 11:25:07 +01004155 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004156 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004157
Willy Tarreau295a8372011-03-10 11:25:07 +01004158 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004159 /* we're filtering on some data contents */
4160 void *ptr;
4161 long long data;
4162
Willy Tarreau295a8372011-03-10 11:25:07 +01004163 dt = si->applet.ctx.table.data_type;
4164 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4165 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004166 dt);
4167
4168 data = 0;
4169 switch (stktable_data_types[dt].std_type) {
4170 case STD_T_SINT:
4171 data = stktable_data_cast(ptr, std_t_sint);
4172 break;
4173 case STD_T_UINT:
4174 data = stktable_data_cast(ptr, std_t_uint);
4175 break;
4176 case STD_T_ULL:
4177 data = stktable_data_cast(ptr, std_t_ull);
4178 break;
4179 case STD_T_FRQP:
4180 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004181 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004182 break;
4183 }
4184
4185 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004186 if ((data < si->applet.ctx.table.value &&
4187 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4188 si->applet.ctx.table.data_op == STD_OP_GT ||
4189 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4190 (data == si->applet.ctx.table.value &&
4191 (si->applet.ctx.table.data_op == STD_OP_NE ||
4192 si->applet.ctx.table.data_op == STD_OP_GT ||
4193 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4194 (data > si->applet.ctx.table.value &&
4195 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4196 si->applet.ctx.table.data_op == STD_OP_LT ||
4197 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004198 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004199 }
4200
Simon Hormanc88b8872011-06-15 15:18:49 +09004201 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004202 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004203 si->applet.ctx.table.entry))
4204 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004205
Willy Tarreau295a8372011-03-10 11:25:07 +01004206 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004207
Willy Tarreau295a8372011-03-10 11:25:07 +01004208 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004209 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004210 struct stksess *old = si->applet.ctx.table.entry;
4211 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004212 if (show)
4213 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau33fba6f2013-08-13 16:44:40 +02004214 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004215 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004216 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004217 break;
4218 }
4219
Simon Hormanc88b8872011-06-15 15:18:49 +09004220
4221 if (show)
4222 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4223 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4224 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4225
Willy Tarreau295a8372011-03-10 11:25:07 +01004226 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004227 si->conn->xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004228 break;
4229
Willy Tarreau295a8372011-03-10 11:25:07 +01004230 case STAT_ST_END:
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004231 si->conn->xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004232 break;
4233 }
4234 }
4235 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004236}
4237
Willy Tarreaud426a182010-03-05 14:58:26 +01004238/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004239 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4240 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4241 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4242 * lines are respected within the limit of 70 output chars. Lines that are
4243 * continuation of a previous truncated line begin with "+" instead of " "
4244 * after the offset. The new pointer is returned.
4245 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004246static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4247 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004248{
4249 int end;
4250 unsigned char c;
4251
4252 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004253 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004254 return ptr;
4255
Willy Tarreau77804732012-10-29 16:14:26 +01004256 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004257
Willy Tarreaud426a182010-03-05 14:58:26 +01004258 while (ptr < len && ptr < bsize) {
4259 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004260 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004261 if (out->len > end - 2)
4262 break;
4263 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004264 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004265 if (out->len > end - 3)
4266 break;
4267 out->str[out->len++] = '\\';
4268 switch (c) {
4269 case '\t': c = 't'; break;
4270 case '\n': c = 'n'; break;
4271 case '\r': c = 'r'; break;
4272 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004273 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004274 }
4275 out->str[out->len++] = c;
4276 } else {
4277 if (out->len > end - 5)
4278 break;
4279 out->str[out->len++] = '\\';
4280 out->str[out->len++] = 'x';
4281 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4282 out->str[out->len++] = hextab[c & 0xF];
4283 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004284 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004285 /* we had a line break, let's return now */
4286 out->str[out->len++] = '\n';
4287 *line = ptr;
4288 return ptr;
4289 }
4290 }
4291 /* we have an incomplete line, we return it as-is */
4292 out->str[out->len++] = '\n';
4293 return ptr;
4294}
4295
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004296/* This function dumps all captured errors onto the stream interface's
4297 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004298 * properly set. It returns 0 if the output buffer is full and it needs
4299 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004300 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004301static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004302{
4303 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004304
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004305 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004306 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004307
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004308 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004309
Willy Tarreau295a8372011-03-10 11:25:07 +01004310 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004311 /* the function had not been called yet, let's prepare the
4312 * buffer for a response.
4313 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004314 struct tm tm;
4315
4316 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004317 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004318 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4319 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4320 error_snapshot_id);
4321
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004322 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004323 /* Socket buffer full. Let's try again later from the same point */
4324 return 0;
4325 }
4326
Willy Tarreau295a8372011-03-10 11:25:07 +01004327 si->applet.ctx.errors.px = proxy;
4328 si->applet.ctx.errors.buf = 0;
4329 si->applet.ctx.errors.bol = 0;
4330 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004331 }
4332
4333 /* we have two inner loops here, one for the proxy, the other one for
4334 * the buffer.
4335 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004336 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004337 struct error_snapshot *es;
4338
Willy Tarreau295a8372011-03-10 11:25:07 +01004339 if (si->applet.ctx.errors.buf == 0)
4340 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004341 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004342 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004343
4344 if (!es->when.tv_sec)
4345 goto next;
4346
Willy Tarreau295a8372011-03-10 11:25:07 +01004347 if (si->applet.ctx.errors.iid >= 0 &&
4348 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4349 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004350 goto next;
4351
Willy Tarreau295a8372011-03-10 11:25:07 +01004352 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004353 /* just print headers now */
4354
4355 char pn[INET6_ADDRSTRLEN];
4356 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004357 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004358
4359 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004360 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004361 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004362 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004363
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004364 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4365 case AF_INET:
4366 case AF_INET6:
4367 port = get_host_port(&es->src);
4368 break;
4369 default:
4370 port = 0;
4371 }
4372
Willy Tarreau295a8372011-03-10 11:25:07 +01004373 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004374 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004375 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004376 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004377 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004378 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004379 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4380 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004381 break;
4382 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004383 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004384 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004385 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004386 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004387 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004388 break;
4389 }
4390
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004391 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004392 ", server %s (#%d), event #%u\n"
4393 " src %s:%d, session #%d, session flags 0x%08x\n"
4394 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4395 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4396 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4397 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4398 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4399 es->ev_id,
4400 pn, port, es->sid, es->s_flags,
4401 es->state, es->m_flags, es->t_flags,
4402 es->m_clen, es->m_blen,
4403 es->b_flags, es->b_out, es->b_tot,
4404 es->len, es->b_wrap, es->pos);
4405
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004406 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004407 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004408 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004409 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004410 si->applet.ctx.errors.ptr = 0;
4411 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004412 }
4413
Willy Tarreau295a8372011-03-10 11:25:07 +01004414 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004415 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004416 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004417 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004418 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004419 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004420 goto next;
4421 }
4422
4423 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004424 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004425 int newptr;
4426 int newline;
4427
Willy Tarreau295a8372011-03-10 11:25:07 +01004428 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004429 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 +01004430 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004431 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004432
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004433 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004434 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004435 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004436 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004437 si->applet.ctx.errors.ptr = newptr;
4438 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004439 };
4440 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004441 si->applet.ctx.errors.bol = 0;
4442 si->applet.ctx.errors.ptr = -1;
4443 si->applet.ctx.errors.buf++;
4444 if (si->applet.ctx.errors.buf > 1) {
4445 si->applet.ctx.errors.buf = 0;
4446 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004447 }
4448 }
4449
4450 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004451 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004452}
4453
Willy Tarreaud5781202012-09-22 19:32:35 +02004454/* parse the "level" argument on the bind lines */
4455static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4456{
4457 if (!*args[cur_arg + 1]) {
4458 memprintf(err, "'%s' : missing level", args[cur_arg]);
4459 return ERR_ALERT | ERR_FATAL;
4460 }
4461
4462 if (!strcmp(args[cur_arg+1], "user"))
4463 conf->level = ACCESS_LVL_USER;
4464 else if (!strcmp(args[cur_arg+1], "operator"))
4465 conf->level = ACCESS_LVL_OPER;
4466 else if (!strcmp(args[cur_arg+1], "admin"))
4467 conf->level = ACCESS_LVL_ADMIN;
4468 else {
4469 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4470 args[cur_arg], args[cur_arg+1]);
4471 return ERR_ALERT | ERR_FATAL;
4472 }
4473
4474 return 0;
4475}
4476
Willy Tarreaub24281b2011-02-13 13:16:36 +01004477struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004478 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004479 .name = "<STATS>", /* used for logging */
4480 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004481 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004482};
4483
Simon Horman9bd2c732011-06-15 15:18:44 +09004484static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004485 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004486 .name = "<CLI>", /* used for logging */
4487 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004488 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004489};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004490
Willy Tarreaudc13c112013-06-21 23:16:39 +02004491static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004492 { CFG_GLOBAL, "stats", stats_parse_global },
4493 { 0, NULL, NULL },
4494}};
4495
Willy Tarreaud5781202012-09-22 19:32:35 +02004496static struct bind_kw_list bind_kws = { "STAT", { }, {
4497 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4498 { NULL, NULL, 0 },
4499}};
4500
Willy Tarreau10522fd2008-07-09 20:12:41 +02004501__attribute__((constructor))
4502static void __dumpstats_module_init(void)
4503{
4504 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004505 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004506}
4507
Willy Tarreau91861262007-10-17 17:06:05 +02004508/*
4509 * Local variables:
4510 * c-indent-level: 8
4511 * c-basic-offset: 8
4512 * End:
4513 */