blob: 350b11103525056a6bf23e74329d6ceccebbfc92 [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>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010049#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010053#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020054#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020055#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020056#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020058#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010059#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010060#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020061
Willy Tarreau7a0169a2012-11-19 17:13:16 +010062#ifdef USE_OPENSSL
63#include <proto/ssl_sock.h>
64#endif
65
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010066static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010067static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090068static int stats_dump_sess_to_buffer(struct stream_interface *si);
69static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010070static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010071static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
72static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010073static int stats_maps_list(struct stream_interface *si);
74static int stats_map_list(struct stream_interface *si);
75static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +090076
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010077/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010078 * cli_io_handler()
79 * -> stats_dump_sess_to_buffer() // "show sess"
80 * -> stats_dump_errors_to_buffer() // "show errors"
81 * -> stats_dump_info_to_buffer() // "show info"
82 * -> stats_dump_stat_to_buffer() // "show stat"
83 * -> stats_dump_csv_header()
84 * -> stats_dump_proxy_to_buffer()
85 * -> stats_dump_fe_stats()
86 * -> stats_dump_li_stats()
87 * -> stats_dump_sv_stats()
88 * -> stats_dump_be_stats()
89 *
90 * http_stats_io_handler()
91 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
92 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
93 * -> stats_dump_html_head() // emits the HTML headers
94 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
95 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
96 * -> stats_dump_html_px_hdr()
97 * -> stats_dump_fe_stats()
98 * -> stats_dump_li_stats()
99 * -> stats_dump_sv_stats()
100 * -> stats_dump_be_stats()
101 * -> stats_dump_html_px_end()
102 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100103 */
104
Simon Horman9bd2c732011-06-15 15:18:44 +0900105static struct si_applet cli_applet;
106
107static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200108 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200109 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200110 " clear table : remove an entry from a table\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100111 " clear map [id] : clear the content of this map\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200112 " help : this message\n"
113 " prompt : toggle interactive mode with prompt\n"
114 " quit : disconnect\n"
115 " show info : report information about the running process\n"
116 " show stat : report counters for each proxy and server\n"
117 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100118 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200119 " show table [id]: report table usage stats or dump this table's contents\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100120 " show map [id] : report avalaible maps or dump this map's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200121 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200122 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200123 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100124 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200125 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200126 " set rate-limit : change a rate limiting value\n"
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100127 " set map [id] [key] [value] : modify map entry\n"
128 " add map [id] [key] [value] : add map entry\n"
129 " del map [id] [key] : delete map entry\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200130 " disable : put a server or frontend in maintenance mode\n"
131 " enable : re-enable a server or frontend which is in maintenance mode\n"
132 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200133 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200134
Simon Horman9bd2c732011-06-15 15:18:44 +0900135static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200136 "Permission denied\n"
137 "";
138
Willy Tarreau295a8372011-03-10 11:25:07 +0100139/* data transmission states for the stats responses */
140enum {
141 STAT_ST_INIT = 0,
142 STAT_ST_HEAD,
143 STAT_ST_INFO,
144 STAT_ST_LIST,
145 STAT_ST_END,
146 STAT_ST_FIN,
147};
148
149/* data transmission states for the stats responses inside a proxy */
150enum {
151 STAT_PX_ST_INIT = 0,
152 STAT_PX_ST_TH,
153 STAT_PX_ST_FE,
154 STAT_PX_ST_LI,
155 STAT_PX_ST_SV,
156 STAT_PX_ST_BE,
157 STAT_PX_ST_END,
158 STAT_PX_ST_FIN,
159};
160
Cyril Bonté19979e12012-04-04 12:57:21 +0200161extern const char *stat_status_codes[];
162
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200163/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200164 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200165 * needs to be closed and ignored, or a negative value upon critical failure.
166 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900167static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200168{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100169 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100170 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200171
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200172 tv_zero(&s->logs.tv_request);
173 s->logs.t_queue = 0;
174 s->logs.t_connect = 0;
175 s->logs.t_data = 0;
176 s->logs.t_close = 0;
177 s->logs.bytes_in = s->logs.bytes_out = 0;
178 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
179 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200180
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200181 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200182
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200183 if (s->listener->timeout) {
184 s->req->rto = *s->listener->timeout;
185 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200186 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200187 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200188}
189
Willy Tarreau07e9e642010-08-17 21:48:17 +0200190/* allocate a new stats frontend named <name>, and return it
191 * (or NULL in case of lack of memory).
192 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200193static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200194{
195 struct proxy *fe;
196
197 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
198 if (!fe)
199 return NULL;
200
Willy Tarreau237250c2011-07-29 01:49:03 +0200201 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200202 fe->next = proxy;
203 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200204 fe->last_change = now.tv_sec;
205 fe->id = strdup("GLOBAL");
206 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200207 fe->maxconn = 10; /* default to 10 concurrent connections */
208 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200209 fe->conf.file = strdup(file);
210 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200211 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200212
213 /* the stats frontend is the only one able to assign ID #0 */
214 fe->conf.id.key = fe->uuid = 0;
215 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200216 return fe;
217}
218
Willy Tarreaufbee7132007-10-18 13:53:22 +0200219/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200220 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
221 * error message into the <err> buffer which will be preallocated. The trailing
222 * '\n' must not be written. The function must be called with <args> pointing to
223 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200224 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200225static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200226 struct proxy *defpx, const char *file, int line,
227 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200228{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200229 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200230 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200231
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200232 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200233 int cur_arg;
234
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200235 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200236 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 +0200237 return -1;
238 }
239
Willy Tarreau89a63132009-08-16 17:41:45 +0200240 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200241 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200242 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200243 return -1;
244 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200245 }
246
Willy Tarreau4348fad2012-09-20 16:48:07 +0200247 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200248 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200249
Willy Tarreauc53d4222012-09-20 20:19:28 +0200250 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
251 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
252 file, line, args[0], args[1], err && *err ? *err : "error");
253 return -1;
254 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200255
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200256 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200257 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200258 static int bind_dumped;
259 struct bind_kw *kw;
260
261 kw = bind_find_kw(args[cur_arg]);
262 if (kw) {
263 if (!kw->parse) {
264 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
265 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200266 return -1;
267 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200268
269 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
270 if (err && *err)
271 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
272 else
273 memprintf(err, "'%s %s' : error encountered while processing '%s'",
274 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200275 return -1;
276 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200277
278 cur_arg += 1 + kw->skip;
279 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200280 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200281
282 if (!bind_dumped) {
283 bind_dump_kws(err);
284 indent_msg(err, 4);
285 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200287
288 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
289 args[0], args[1], args[cur_arg],
290 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
291 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100293
Willy Tarreauc53d4222012-09-20 20:19:28 +0200294 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
295 l->maxconn = global.stats_fe->maxconn;
296 l->backlog = global.stats_fe->backlog;
297 l->timeout = &global.stats_fe->timeout.client;
298 l->accept = session_accept;
299 l->handler = process_session;
300 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
301 l->nice = -64; /* we want to boost priority for local stats */
302 global.maxsock += l->maxconn;
303 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200304 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200305 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100306 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200307 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100308
309 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200310 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100311 return -1;
312 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200313
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100314 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200315 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 return -1;
317 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200318 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200319 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200321 return -1;
322 }
323 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200324 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200325 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200326 else if (!strcmp(args[1], "maxconn")) {
327 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200328
329 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200330 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 return -1;
332 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200333
334 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200335 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200336 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200337 return -1;
338 }
339 }
340 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200341 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200342 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
343 int cur_arg = 2;
344 unsigned int set = 0;
345
Willy Tarreau91319572013-04-20 09:48:50 +0200346 if (!global.stats_fe) {
347 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
348 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
349 return -1;
350 }
351 }
352
Willy Tarreau35b7b162012-10-22 23:17:18 +0200353 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100354 unsigned int low, high;
355
Willy Tarreau35b7b162012-10-22 23:17:18 +0200356 if (strcmp(args[cur_arg], "all") == 0) {
357 set = 0;
358 break;
359 }
360 else if (strcmp(args[cur_arg], "odd") == 0) {
361 set |= 0x55555555;
362 }
363 else if (strcmp(args[cur_arg], "even") == 0) {
364 set |= 0xAAAAAAAA;
365 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100366 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100367 char *dash = strchr(args[cur_arg], '-');
368
369 low = high = str2uic(args[cur_arg]);
370 if (dash)
371 high = str2uic(dash + 1);
372
373 if (high < low) {
374 unsigned int swap = low;
375 low = high;
376 high = swap;
377 }
378
379 if (low < 1 || high > 32) {
380 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
381 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200382 return -1;
383 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100384
385 while (low <= high)
386 set |= 1 << (low++ - 1);
387 }
388 else {
389 memprintf(err,
390 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
391 args[0], args[1]);
392 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200393 }
394 cur_arg++;
395 }
396 global.stats_fe->bind_proc = set;
397 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200398 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200399 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200400 return -1;
401 }
402 return 0;
403}
404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100405/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
406 * for clearing it if needed.
407 */
408static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100409{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100410 chunk_appendf(&trash,
411 "# pxname,svname,"
412 "qcur,qmax,"
413 "scur,smax,slim,stot,"
414 "bin,bout,"
415 "dreq,dresp,"
416 "ereq,econ,eresp,"
417 "wretr,wredis,"
418 "status,weight,act,bck,"
419 "chkfail,chkdown,lastchg,downtime,qlimit,"
420 "pid,iid,sid,throttle,lbtot,tracked,type,"
421 "rate,rate_lim,rate_max,"
422 "check_status,check_code,check_duration,"
423 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
424 "req_rate,req_rate_max,req_tot,"
425 "cli_abrt,srv_abrt,"
426 "comp_in,comp_out,comp_byp,comp_rsp,"
427 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100428}
429
Simon Hormand9366582011-06-15 15:18:45 +0900430/* print a string of text buffer to <out>. The format is :
431 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
432 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
433 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
434 */
435static int dump_text(struct chunk *out, const char *buf, int bsize)
436{
437 unsigned char c;
438 int ptr = 0;
439
440 while (buf[ptr] && ptr < bsize) {
441 c = buf[ptr];
442 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
443 if (out->len > out->size - 1)
444 break;
445 out->str[out->len++] = c;
446 }
447 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
448 if (out->len > out->size - 2)
449 break;
450 out->str[out->len++] = '\\';
451 switch (c) {
452 case ' ': c = ' '; break;
453 case '\t': c = 't'; break;
454 case '\n': c = 'n'; break;
455 case '\r': c = 'r'; break;
456 case '\e': c = 'e'; break;
457 case '\\': c = '\\'; break;
458 }
459 out->str[out->len++] = c;
460 }
461 else {
462 if (out->len > out->size - 4)
463 break;
464 out->str[out->len++] = '\\';
465 out->str[out->len++] = 'x';
466 out->str[out->len++] = hextab[(c >> 4) & 0xF];
467 out->str[out->len++] = hextab[c & 0xF];
468 }
469 ptr++;
470 }
471
472 return ptr;
473}
474
475/* print a buffer in hexa.
476 * Print stopped if <bsize> is reached, or if no more place in the chunk.
477 */
478static int dump_binary(struct chunk *out, const char *buf, int bsize)
479{
480 unsigned char c;
481 int ptr = 0;
482
483 while (ptr < bsize) {
484 c = buf[ptr];
485
486 if (out->len > out->size - 2)
487 break;
488 out->str[out->len++] = hextab[(c >> 4) & 0xF];
489 out->str[out->len++] = hextab[c & 0xF];
490
491 ptr++;
492 }
493 return ptr;
494}
495
496/* Dump the status of a table to a stream interface's
497 * read buffer. It returns 0 if the output buffer is full
498 * and needs to be called again, otherwise non-zero.
499 */
500static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
501 struct proxy *proxy, struct proxy *target)
502{
Willy Tarreau306f8302013-07-08 15:53:06 +0200503 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900504
Willy Tarreau77804732012-10-29 16:14:26 +0100505 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900506 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
507
508 /* any other information should be dumped here */
509
Willy Tarreau290e63a2012-09-20 18:07:14 +0200510 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100511 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900512
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200513 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900514 return 0;
515
516 return 1;
517}
518
519/* Dump the a table entry to a stream interface's
520 * read buffer. It returns 0 if the output buffer is full
521 * and needs to be called again, otherwise non-zero.
522 */
523static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
524 struct proxy *proxy, struct stksess *entry)
525{
526 int dt;
527
Willy Tarreau77804732012-10-29 16:14:26 +0100528 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900529
530 if (proxy->table.type == STKTABLE_TYPE_IP) {
531 char addr[INET_ADDRSTRLEN];
532 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100533 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900534 }
535 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
536 char addr[INET6_ADDRSTRLEN];
537 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100538 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900539 }
540 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100541 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900542 }
543 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100544 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900545 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
546 }
547 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100548 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900549 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
550 }
551
Willy Tarreau77804732012-10-29 16:14:26 +0100552 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900553
554 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
555 void *ptr;
556
557 if (proxy->table.data_ofs[dt] == 0)
558 continue;
559 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900561 else
Willy Tarreau77804732012-10-29 16:14:26 +0100562 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900563
564 ptr = stktable_data_ptr(&proxy->table, entry, dt);
565 switch (stktable_data_types[dt].std_type) {
566 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100567 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900568 break;
569 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100570 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900571 break;
572 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100573 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900574 break;
575 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900577 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
578 proxy->table.data_arg[dt].u));
579 break;
580 }
581 }
Willy Tarreau77804732012-10-29 16:14:26 +0100582 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900583
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200584 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900585 return 0;
586
587 return 1;
588}
589
Willy Tarreaudec98142012-06-06 23:37:08 +0200590static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900591{
Willy Tarreau306f8302013-07-08 15:53:06 +0200592 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100593 struct appctx *appctx = __objt_appctx(si->end);
594 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900595 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900596 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900597 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200598 long long value;
599 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200600 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200601 void *ptr;
602 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900603
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100604 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900605
606 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100607 appctx->ctx.cli.msg = "Key value expected\n";
608 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900609 return;
610 }
611
Simon Hormanc5b89f62011-06-15 15:18:50 +0900612 switch (px->table.type) {
613 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900614 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100615 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900616 break;
617 case STKTABLE_TYPE_IPV6:
618 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100619 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900620 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900621 case STKTABLE_TYPE_INTEGER:
622 {
623 char *endptr;
624 unsigned long val;
625 errno = 0;
626 val = strtoul(args[4], &endptr, 10);
627 if ((errno == ERANGE && val == ULONG_MAX) ||
628 (errno != 0 && val == 0) || endptr == args[4] ||
629 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100630 appctx->ctx.cli.msg = "Invalid key\n";
631 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900632 return;
633 }
634 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100635 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900636 break;
637 }
638 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900639 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100640 static_table_key->key = args[4];
641 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900642 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900643 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200644 switch (action) {
645 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100646 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200647 break;
648 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100649 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200650 break;
651 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100652 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200653 break;
654 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100655 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900656 return;
657 }
658
659 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200660 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100661 appctx->ctx.cli.msg = stats_permission_denied_msg;
662 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900663 return;
664 }
665
Willy Tarreau07115412012-10-29 21:56:59 +0100666 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900667
Willy Tarreaudec98142012-06-06 23:37:08 +0200668 switch (action) {
669 case STAT_CLI_O_TAB:
670 if (!ts)
671 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100672 chunk_reset(&trash);
673 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900674 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100675 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900676 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200677
678 case STAT_CLI_O_CLR:
679 if (!ts)
680 return;
681 if (ts->ref_cnt) {
682 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100683 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
684 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200685 return;
686 }
687 stksess_kill(&px->table, ts);
688 break;
Simon Horman17bce342011-06-15 15:18:47 +0900689
Willy Tarreau654694e2012-06-07 01:03:16 +0200690 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200691 if (ts)
692 stktable_touch(&px->table, ts, 1);
693 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100694 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200695 if (!ts) {
696 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100697 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
698 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200699 return;
700 }
701 stktable_store(&px->table, ts, 1);
702 }
703
Willy Tarreau47060b62013-08-01 21:11:42 +0200704 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
705 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100706 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
707 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200708 return;
709 }
710
711 data_type = stktable_get_data_type(args[cur_arg] + 5);
712 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100713 appctx->ctx.cli.msg = "Unknown data type\n";
714 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200715 return;
716 }
717
718 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100719 appctx->ctx.cli.msg = "Data type not stored in this table\n";
720 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200721 return;
722 }
723
724 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100725 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
726 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200727 return;
728 }
729
730 ptr = stktable_data_ptr(&px->table, ts, data_type);
731
732 switch (stktable_data_types[data_type].std_type) {
733 case STD_T_SINT:
734 stktable_data_cast(ptr, std_t_sint) = value;
735 break;
736 case STD_T_UINT:
737 stktable_data_cast(ptr, std_t_uint) = value;
738 break;
739 case STD_T_ULL:
740 stktable_data_cast(ptr, std_t_ull) = value;
741 break;
742 case STD_T_FRQP:
743 /* We set both the current and previous values. That way
744 * the reported frequency is stable during all the period
745 * then slowly fades out. This allows external tools to
746 * push measures without having to update them too often.
747 */
748 frqp = &stktable_data_cast(ptr, std_t_frqp);
749 frqp->curr_tick = now_ms;
750 frqp->prev_ctr = 0;
751 frqp->curr_ctr = value;
752 break;
753 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200754 }
755 break;
756
Willy Tarreaudec98142012-06-06 23:37:08 +0200757 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100758 appctx->ctx.cli.msg = "Unknown action\n";
759 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200760 break;
Simon Horman121f3052011-06-15 15:18:46 +0900761 }
Simon Horman121f3052011-06-15 15:18:46 +0900762}
763
Willy Tarreau654694e2012-06-07 01:03:16 +0200764static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900765{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100766 struct appctx *appctx = __objt_appctx(si->end);
767
Willy Tarreau04b3a192013-04-13 09:41:37 +0200768 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100769 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
770 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200771 return;
772 }
773
Simon Hormand5b9fd92011-06-15 15:18:48 +0900774 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100775 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
776 if (appctx->ctx.table.data_type < 0) {
777 appctx->ctx.cli.msg = "Unknown data type\n";
778 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900779 return;
780 }
781
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100782 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
783 appctx->ctx.cli.msg = "Data type not stored in this table\n";
784 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900785 return;
786 }
787
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100788 appctx->ctx.table.data_op = get_std_op(args[4]);
789 if (appctx->ctx.table.data_op < 0) {
790 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
791 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900792 return;
793 }
794
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100795 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
796 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
797 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900798 return;
799 }
800}
801
Willy Tarreaudec98142012-06-06 23:37:08 +0200802static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900803{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100804 struct appctx *appctx = __objt_appctx(si->end);
805
806 appctx->ctx.table.data_type = -1;
807 appctx->st2 = STAT_ST_INIT;
808 appctx->ctx.table.target = NULL;
809 appctx->ctx.table.proxy = NULL;
810 appctx->ctx.table.entry = NULL;
811 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812
813 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100814 appctx->ctx.table.target = find_stktable(args[2]);
815 if (!appctx->ctx.table.target) {
816 appctx->ctx.cli.msg = "No such table\n";
817 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900818 return;
819 }
820 }
821 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200822 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900823 goto err_args;
824 return;
825 }
826
827 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200828 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900829 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200830 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900831 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900832 goto err_args;
833
834 return;
835
836err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200837 switch (action) {
838 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100839 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200840 break;
841 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100842 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200843 break;
844 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100845 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200846 break;
847 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100848 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900849}
850
Willy Tarreau532a4502011-09-07 22:37:44 +0200851/* Expects to find a frontend named <arg> and returns it, otherwise displays various
852 * adequate error messages and returns NULL. This function also expects the session
853 * level to be admin.
854 */
855static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
856{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200858 struct proxy *px;
859
Willy Tarreau290e63a2012-09-20 18:07:14 +0200860 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100861 appctx->ctx.cli.msg = stats_permission_denied_msg;
862 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200863 return NULL;
864 }
865
866 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100867 appctx->ctx.cli.msg = "A frontend name is expected.\n";
868 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200869 return NULL;
870 }
871
872 px = findproxy(arg, PR_CAP_FE);
873 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100874 appctx->ctx.cli.msg = "No such frontend.\n";
875 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200876 return NULL;
877 }
878 return px;
879}
880
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200881/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
882 * and returns the pointer to the server. Otherwise, display adequate error messages
883 * and returns NULL. This function also expects the session level to be admin. Note:
884 * the <arg> is modified to remove the '/'.
885 */
886static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
887{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100888 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200889 struct proxy *px;
890 struct server *sv;
891 char *line;
892
Willy Tarreau290e63a2012-09-20 18:07:14 +0200893 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = stats_permission_denied_msg;
895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200896 return NULL;
897 }
898
899 /* split "backend/server" and make <line> point to server */
900 for (line = arg; *line; line++)
901 if (*line == '/') {
902 *line++ = '\0';
903 break;
904 }
905
906 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
908 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200909 return NULL;
910 }
911
912 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100913 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
914 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200915 return NULL;
916 }
917
918 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100919 appctx->ctx.cli.msg = "Proxy is disabled.\n";
920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200921 return NULL;
922 }
923
924 return sv;
925}
926
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100927/* This function is used with map management. It permits to browse each
928 * really allocated descriptors of one map reference. The variable
929 * <appctx->ctx.map.ref> must contain the map reference to browse.
930 * The variable <appctx->ctx.map.desc> contain the descriptor of the
931 * current allocated map descriptor. This variable must be initialized
932 * to NULL.
933 */
934static inline void stats_map_lookup_next(struct stream_interface *si)
935{
936 struct appctx *appctx = __objt_appctx(si->end);
937
938 /* search the next allocated map */
939 while (1) {
940 /* get next descriptor */
941 if (!appctx->ctx.map.desc)
942 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.ref->maps,
943 struct map_descriptor *, list);
944 else
945 appctx->ctx.map.desc = LIST_NEXT(&appctx->ctx.map.desc->list,
946 struct map_descriptor *, list);
947
948 /* detect end of list */
949 if (&appctx->ctx.map.desc->list == &appctx->ctx.map.ref->maps) {
950 appctx->ctx.map.desc = NULL;
951 return;
952 }
953
954 /* do not lookup this entry */
955 if (!appctx->ctx.map.desc->do_free)
956 continue;
957
958 /* avalaible descriptor */
959 return;
960 }
961}
962
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200963/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200964 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100965 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200966 * designating the function which will have to process the request, which can
967 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200968 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900969static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200970{
Willy Tarreau306f8302013-07-08 15:53:06 +0200971 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100972 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200973 char *args[MAX_STATS_ARGS + 1];
974 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100975 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200976
977 while (isspace((unsigned char)*line))
978 line++;
979
980 arg = 0;
981 args[arg] = line;
982
983 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +0100984 if (*line == '\\') {
985 line++;
986 if (*line == '\0')
987 break;
988 }
989 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200990 *line++ = '\0';
991
992 while (isspace((unsigned char)*line))
993 line++;
994
995 args[++arg] = line;
996 continue;
997 }
998
999 line++;
1000 }
1001
1002 while (++arg <= MAX_STATS_ARGS)
1003 args[arg] = line;
1004
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001005 /* remove \ */
1006 arg = 0;
1007 while (*args[arg] != '\0') {
1008 j = 0;
1009 for (i=0; args[arg][i] != '\0'; i++) {
1010 if (args[arg][i] == '\\')
1011 continue;
1012 args[arg][j] = args[arg][i];
1013 j++;
1014 }
1015 args[arg][j] = '\0';
1016 arg++;
1017 }
1018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001019 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001020 if (strcmp(args[0], "show") == 0) {
1021 if (strcmp(args[1], "stat") == 0) {
1022 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001023 appctx->ctx.stats.flags |= STAT_BOUND;
1024 appctx->ctx.stats.iid = atoi(args[2]);
1025 appctx->ctx.stats.type = atoi(args[3]);
1026 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001027 }
1028
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001029 appctx->st2 = STAT_ST_INIT;
1030 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001031 }
1032 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001033 appctx->st2 = STAT_ST_INIT;
1034 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001035 }
1036 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001037 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001038 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001039 appctx->ctx.cli.msg = stats_permission_denied_msg;
1040 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001041 return 1;
1042 }
Willy Tarreau76153662012-11-26 01:16:39 +01001043 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001044 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001045 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001046 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001047 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001048 appctx->ctx.sess.target = NULL;
1049 appctx->ctx.sess.section = 0; /* start with session status */
1050 appctx->ctx.sess.pos = 0;
1051 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001052 }
1053 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001054 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001055 appctx->ctx.cli.msg = stats_permission_denied_msg;
1056 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001057 return 1;
1058 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001059 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001060 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001061 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001062 appctx->ctx.errors.iid = -1;
1063 appctx->ctx.errors.px = NULL;
1064 appctx->st2 = STAT_ST_INIT;
1065 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001066 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001067 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001068 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001069 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001070 else if (strcmp(args[1], "map") == 0) {
1071
1072 /* no parameter: display all map avalaible */
1073 if (!*args[2]) {
1074 appctx->st2 = STAT_ST_INIT;
1075 appctx->st0 = STAT_CLI_O_MAPS;
1076 return 1;
1077 }
1078
1079 /* lookup into the maps */
1080 appctx->ctx.map.ref = map_get_reference(args[2]);
1081 if (!appctx->ctx.map.ref) {
1082 appctx->ctx.cli.msg = "Unknown map reference.\n";
1083 appctx->st0 = STAT_CLI_PRINT;
1084 return 1;
1085 }
1086 appctx->st2 = STAT_ST_INIT;
1087 appctx->st0 = STAT_CLI_O_MAP;
1088 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001089 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001090 return 0;
1091 }
1092 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001093 else if (strcmp(args[0], "clear") == 0) {
1094 if (strcmp(args[1], "counters") == 0) {
1095 struct proxy *px;
1096 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001097 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001098 int clrall = 0;
1099
1100 if (strcmp(args[2], "all") == 0)
1101 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001102
Willy Tarreau6162db22009-10-10 17:13:00 +02001103 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001104 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1105 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001106 appctx->ctx.cli.msg = stats_permission_denied_msg;
1107 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001108 return 1;
1109 }
1110
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001111 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001112 if (clrall) {
1113 memset(&px->be_counters, 0, sizeof(px->be_counters));
1114 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1115 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001116 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001117 px->be_counters.conn_max = 0;
1118 px->be_counters.p.http.rps_max = 0;
1119 px->be_counters.sps_max = 0;
1120 px->be_counters.cps_max = 0;
1121 px->be_counters.nbpend_max = 0;
1122
1123 px->fe_counters.conn_max = 0;
1124 px->fe_counters.p.http.rps_max = 0;
1125 px->fe_counters.sps_max = 0;
1126 px->fe_counters.cps_max = 0;
1127 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001128 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001129
1130 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001131 if (clrall)
1132 memset(&sv->counters, 0, sizeof(sv->counters));
1133 else {
1134 sv->counters.cur_sess_max = 0;
1135 sv->counters.nbpend_max = 0;
1136 sv->counters.sps_max = 0;
1137 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001138
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001139 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001140 if (li->counters) {
1141 if (clrall)
1142 memset(li->counters, 0, sizeof(*li->counters));
1143 else
1144 li->counters->conn_max = 0;
1145 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001146 }
1147
Willy Tarreau81c25d02011-09-07 15:17:21 +02001148 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001149 return 1;
1150 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001151 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001152 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001153 /* end of processing */
1154 return 1;
1155 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001156 else if (strcmp(args[1], "map") == 0) {
1157 struct map_reference *mref;
1158 struct map_descriptor *mdesc;
1159 struct map_entry *ent, *nent;
1160
1161 /* no parameter */
1162 if (!*args[2]) {
1163 appctx->ctx.cli.msg = "Expect map reference.\n";
1164 appctx->st0 = STAT_CLI_PRINT;
1165 return 1;
1166 }
1167
1168 /* lookup into the maps */
1169 mref = map_get_reference(args[2]);
1170 if (!mref) {
1171 appctx->ctx.cli.msg = "Unknown map reference.\n";
1172 appctx->st0 = STAT_CLI_PRINT;
1173 return 1;
1174 }
1175
1176 /* clear all maps */
1177 list_for_each_entry(mdesc, &mref->maps, list)
1178 if (mdesc->do_free)
1179 pattern_prune_expr(mdesc->pat);
1180
1181 /* clear map reference */
1182 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1183 LIST_DEL(&ent->list);
1184 free(ent->key);
1185 free(ent->value);
1186 free(ent);
1187 }
1188
1189 /* return response */
1190 appctx->ctx.cli.msg = "Done.\n";
1191 appctx->st0 = STAT_CLI_PRINT;
1192 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001193 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001194 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001195 return 0;
1196 }
1197 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001198 else if (strcmp(args[0], "get") == 0) {
1199 if (strcmp(args[1], "weight") == 0) {
1200 struct proxy *px;
1201 struct server *sv;
1202
1203 /* split "backend/server" and make <line> point to server */
1204 for (line = args[2]; *line; line++)
1205 if (*line == '/') {
1206 *line++ = '\0';
1207 break;
1208 }
1209
1210 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001211 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1212 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001213 return 1;
1214 }
1215
1216 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001217 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1218 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001219 return 1;
1220 }
1221
1222 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001223 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1224 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001225 return 1;
1226 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001227 else if (strcmp(args[1], "map") == 0) {
1228
1229 /* no parameter */
1230 if (!*args[2] || !*args[3]) {
1231 appctx->ctx.cli.msg = "Expect map reference and required key.\n";
1232 appctx->st0 = STAT_CLI_PRINT;
1233 return 1;
1234 }
1235
1236 /* lookup into the maps */
1237 appctx->ctx.map.ref = map_get_reference(args[2]);
1238 if (!appctx->ctx.map.ref) {
1239 appctx->ctx.cli.msg = "Unknown map reference.\n";
1240 appctx->st0 = STAT_CLI_PRINT;
1241 return 1;
1242 }
1243
1244 /* copy input string */
1245 appctx->ctx.map.chunk.len = strlen(args[3]);
1246 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1247 appctx->ctx.map.chunk.str = strdup(args[3]);
1248 if (!appctx->ctx.map.chunk.str) {
1249 appctx->ctx.cli.msg = "Out of memory error.\n";
1250 appctx->st0 = STAT_CLI_PRINT;
1251 return 1;
1252 }
1253
1254 /* prepare response */
1255 appctx->st2 = STAT_ST_INIT;
1256 appctx->st0 = STAT_CLI_O_MLOOK;
1257 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001258 else { /* not "get weight" */
1259 return 0;
1260 }
1261 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001262 else if (strcmp(args[0], "set") == 0) {
1263 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001264 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001265 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001266
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001267 sv = expect_server_admin(s, si, args[2]);
1268 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001269 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001270
Simon Horman7d09b9a2013-02-12 10:45:51 +09001271 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001272 /*
1273 * The user-weight may now be zero and thus
1274 * the server considered to be draining.
1275 * Update the server's drain state as necessary.
1276 */
1277 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001278 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001279 appctx->ctx.cli.msg = warning;
1280 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001281 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001282 return 1;
1283 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001284 else if (strcmp(args[1], "timeout") == 0) {
1285 if (strcmp(args[2], "cli") == 0) {
1286 unsigned timeout;
1287 const char *res;
1288
1289 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001290 appctx->ctx.cli.msg = "Expects an integer value.\n";
1291 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001292 return 1;
1293 }
1294
1295 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1296 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001297 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1298 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001299 return 1;
1300 }
1301
1302 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1303 return 1;
1304 }
1305 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001306 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1307 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001308 return 1;
1309 }
1310 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001311 else if (strcmp(args[1], "maxconn") == 0) {
1312 if (strcmp(args[2], "frontend") == 0) {
1313 struct proxy *px;
1314 struct listener *l;
1315 int v;
1316
Willy Tarreau532a4502011-09-07 22:37:44 +02001317 px = expect_frontend_admin(s, si, args[3]);
1318 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001319 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001320
1321 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001322 appctx->ctx.cli.msg = "Integer value expected.\n";
1323 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001324 return 1;
1325 }
1326
1327 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001328 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001329 appctx->ctx.cli.msg = "Value out of range.\n";
1330 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001331 return 1;
1332 }
1333
1334 /* OK, the value is fine, so we assign it to the proxy and to all of
1335 * its listeners. The blocked ones will be dequeued.
1336 */
1337 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001338 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001339 l->maxconn = v;
1340 if (l->state == LI_FULL)
1341 resume_listener(l);
1342 }
1343
1344 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1345 dequeue_all_listeners(&s->fe->listener_queue);
1346
1347 return 1;
1348 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001349 else if (strcmp(args[2], "global") == 0) {
1350 int v;
1351
Willy Tarreau290e63a2012-09-20 18:07:14 +02001352 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001353 appctx->ctx.cli.msg = stats_permission_denied_msg;
1354 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001355 return 1;
1356 }
1357
1358 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001359 appctx->ctx.cli.msg = "Expects an integer value.\n";
1360 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001361 return 1;
1362 }
1363
1364 v = atoi(args[3]);
1365 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001366 appctx->ctx.cli.msg = "Value out of range.\n";
1367 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001368 return 1;
1369 }
1370
1371 /* check for unlimited values */
1372 if (v <= 0)
1373 v = global.hardmaxconn;
1374
1375 global.maxconn = v;
1376
1377 /* Dequeues all of the listeners waiting for a resource */
1378 if (!LIST_ISEMPTY(&global_listener_queue))
1379 dequeue_all_listeners(&global_listener_queue);
1380
1381 return 1;
1382 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001383 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001384 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1385 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001386 return 1;
1387 }
1388 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001389 else if (strcmp(args[1], "rate-limit") == 0) {
1390 if (strcmp(args[2], "connections") == 0) {
1391 if (strcmp(args[3], "global") == 0) {
1392 int v;
1393
Willy Tarreau290e63a2012-09-20 18:07:14 +02001394 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001395 appctx->ctx.cli.msg = stats_permission_denied_msg;
1396 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001397 return 1;
1398 }
1399
1400 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001401 appctx->ctx.cli.msg = "Expects an integer value.\n";
1402 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001403 return 1;
1404 }
1405
1406 v = atoi(args[4]);
1407 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001408 appctx->ctx.cli.msg = "Value out of range.\n";
1409 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001410 return 1;
1411 }
1412
1413 global.cps_lim = v;
1414
1415 /* Dequeues all of the listeners waiting for a resource */
1416 if (!LIST_ISEMPTY(&global_listener_queue))
1417 dequeue_all_listeners(&global_listener_queue);
1418
1419 return 1;
1420 }
1421 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001422 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1423 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001424 return 1;
1425 }
1426 }
William Lallemandd85f9172012-11-09 17:05:39 +01001427 else if (strcmp(args[2], "http-compression") == 0) {
1428 if (strcmp(args[3], "global") == 0) {
1429 int v;
1430
Willy Tarreau85d47f92012-11-21 00:29:50 +01001431 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001432 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1433 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001434 return 1;
1435 }
1436
William Lallemandd85f9172012-11-09 17:05:39 +01001437 v = atoi(args[4]);
1438 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1439 }
1440 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001441 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1442 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001443 return 1;
1444 }
1445 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001446 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001447 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
1448 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001449 return 1;
1450 }
1451 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001452 else if (strcmp(args[1], "table") == 0) {
1453 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1454 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001455 else if (strcmp(args[1], "map") == 0) {
1456 struct pattern *pat_elt;
1457 struct pat_idx_elt *idx_elt;
1458 char *value;
1459
1460 /* Expect three parameters: map name, key and new value. */
1461 if (!*args[2] || !*args[3] || !*args[4]) {
1462 appctx->ctx.cli.msg = "'set map' expect three parameters: map name, key and value.\n";
1463 appctx->st0 = STAT_CLI_PRINT;
1464 return 1;
1465 }
1466
1467 /* Lookup the reference in the maps. */
1468 appctx->ctx.map.ref = map_get_reference(args[2]);
1469 if (!appctx->ctx.map.ref) {
1470 appctx->ctx.cli.msg = "Unknown map reference.\n";
1471 appctx->st0 = STAT_CLI_PRINT;
1472 return 1;
1473 }
1474
1475 /* Lookup the entry in the reference values. */
1476 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1477 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1478 break;
1479
1480 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1481 appctx->ctx.cli.msg = "Entry not found.\n";
1482 appctx->st0 = STAT_CLI_PRINT;
1483 return 1;
1484 }
1485
1486 /* Update each reference entries. */
1487 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1488 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1489 value = strdup(args[4]);
1490 if (!value) {
1491 appctx->ctx.cli.msg = "Out of memory error.\n";
1492 appctx->st0 = STAT_CLI_PRINT;
1493 return 1;
1494 }
1495 free(appctx->ctx.map.ent->value);
1496 appctx->ctx.map.ent->value = value;
1497 }
1498 }
1499
1500 /* Change the sample. The lookup juste return the first entry, other
1501 * entries are not changed, but are never matched.
1502 */
1503 appctx->ctx.map.desc = NULL;
1504 for (stats_map_lookup_next(si);
1505 appctx->ctx.map.desc;
1506 stats_map_lookup_next(si)) {
1507 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1508 if (pat_elt != NULL)
1509 appctx->ctx.map.desc->parse(appctx->ctx.map.ent->value, pat_elt->smp);
1510 if (idx_elt != NULL)
1511 appctx->ctx.map.desc->parse(appctx->ctx.map.ent->value, idx_elt->smp);
1512 }
1513
1514 /* The set is done, send message. */
1515 appctx->ctx.cli.msg = "Done.\n";
1516 appctx->st0 = STAT_CLI_PRINT;
1517 return 1;
1518 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001519 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001520 return 0;
1521 }
1522 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001523 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001524 if (strcmp(args[1], "agent") == 0) {
1525 struct server *sv;
1526
1527 sv = expect_server_admin(s, si, args[2]);
1528 if (!sv)
1529 return 1;
1530
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001531 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1532 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1533 appctx->st0 = STAT_CLI_PRINT;
1534 return 1;
1535 }
1536
1537 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001538 return 1;
1539 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001540 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001541 struct server *sv;
1542
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001543 sv = expect_server_admin(s, si, args[2]);
1544 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001545 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001546
Cyril Bontécd19e512010-01-31 22:34:03 +01001547 if (sv->state & SRV_MAINTAIN) {
1548 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001549 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001550 /* If this server tracks the status of another one,
1551 * we must restore the good status.
1552 */
Willy Tarreau44267702011-10-28 15:35:33 +02001553 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001554 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001555 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001556 } else {
1557 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001558 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001559 }
1560 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001561 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001562 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001563 }
1564 }
1565
Willy Tarreau532a4502011-09-07 22:37:44 +02001566 return 1;
1567 }
1568 else if (strcmp(args[1], "frontend") == 0) {
1569 struct proxy *px;
1570
1571 px = expect_frontend_admin(s, si, args[2]);
1572 if (!px)
1573 return 1;
1574
1575 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001576 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1577 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001578 return 1;
1579 }
1580
1581 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001582 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1583 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001584 return 1;
1585 }
1586
1587 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1589 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001590 return 1;
1591 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001592 return 1;
1593 }
1594 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001595 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1596 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001597 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001598 }
1599 }
1600 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001601 if (strcmp(args[1], "agent") == 0) {
1602 struct server *sv;
1603
1604 sv = expect_server_admin(s, si, args[2]);
1605 if (!sv)
1606 return 1;
1607
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001608 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001609 return 1;
1610 }
1611 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001612 struct server *sv;
1613
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001614 sv = expect_server_admin(s, si, args[2]);
1615 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001616 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001617
Cyril Bontécd19e512010-01-31 22:34:03 +01001618 if (! (sv->state & SRV_MAINTAIN)) {
1619 /* Not already in maintenance, we can change the server state */
1620 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001621 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001622 }
1623
Willy Tarreau532a4502011-09-07 22:37:44 +02001624 return 1;
1625 }
1626 else if (strcmp(args[1], "frontend") == 0) {
1627 struct proxy *px;
1628
1629 px = expect_frontend_admin(s, si, args[2]);
1630 if (!px)
1631 return 1;
1632
1633 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001634 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1635 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001636 return 1;
1637 }
1638
1639 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001640 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1641 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001642 return 1;
1643 }
1644
1645 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001646 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1647 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001648 return 1;
1649 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001650 return 1;
1651 }
1652 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001653 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1654 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001655 return 1;
1656 }
1657 }
1658 else if (strcmp(args[0], "shutdown") == 0) {
1659 if (strcmp(args[1], "frontend") == 0) {
1660 struct proxy *px;
1661
1662 px = expect_frontend_admin(s, si, args[2]);
1663 if (!px)
1664 return 1;
1665
1666 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001667 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1668 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001669 return 1;
1670 }
1671
1672 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1673 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1674 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1675 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1676 stop_proxy(px);
1677 return 1;
1678 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001679 else if (strcmp(args[1], "session") == 0) {
1680 struct session *sess, *ptr;
1681
Willy Tarreau290e63a2012-09-20 18:07:14 +02001682 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001683 appctx->ctx.cli.msg = stats_permission_denied_msg;
1684 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001685 return 1;
1686 }
1687
1688 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001689 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1690 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001691 return 1;
1692 }
1693
1694 ptr = (void *)strtoul(args[2], NULL, 0);
1695
1696 /* first, look for the requested session in the session table */
1697 list_for_each_entry(sess, &sessions, list) {
1698 if (sess == ptr)
1699 break;
1700 }
1701
1702 /* do we have the session ? */
1703 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001704 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1705 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001706 return 1;
1707 }
1708
1709 session_shutdown(sess, SN_ERR_KILLED);
1710 return 1;
1711 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001712 else if (strcmp(args[1], "sessions") == 0) {
1713 if (strcmp(args[2], "server") == 0) {
1714 struct server *sv;
1715 struct session *sess, *sess_bck;
1716
1717 sv = expect_server_admin(s, si, args[3]);
1718 if (!sv)
1719 return 1;
1720
1721 /* kill all the session that are on this server */
1722 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1723 if (sess->srv_conn == sv)
1724 session_shutdown(sess, SN_ERR_KILLED);
1725
1726 return 1;
1727 }
1728 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001729 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1730 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001731 return 1;
1732 }
1733 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001734 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001735 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1736 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001737 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001738 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001739 }
1740 else if (strcmp(args[0], "del") == 0) {
1741 if (strcmp(args[1], "map") == 0) {
1742 struct pattern *pat_elt;
1743 struct pat_idx_elt *idx_elt;
1744 struct map_entry *ent;
1745
1746 /* Expect two parameters: map name and key. */
1747 if (!*args[2] || !*args[3]) {
1748 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1749 appctx->st0 = STAT_CLI_PRINT;
1750 return 1;
1751 }
1752
1753 /* Lookup the reference in the maps. */
1754 appctx->ctx.map.ref = map_get_reference(args[2]);
1755 if (!appctx->ctx.map.ref) {
1756 appctx->ctx.cli.msg = "Unknown map reference.\n";
1757 appctx->st0 = STAT_CLI_PRINT;
1758 return 1;
1759 }
1760
1761 /* Lookup the entry in the reference values.
1762 * If the entry is not found in the reference, return error message.
1763 */
1764 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1765 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1766 break;
1767
1768 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1769 appctx->ctx.cli.msg = "Entry not found.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
1771 return 1;
1772 }
1773
1774 /* Delete each enties from reference. */
1775 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1776 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1777 LIST_DEL(&appctx->ctx.map.ent->list);
1778 free(appctx->ctx.map.ent->key);
1779 free(appctx->ctx.map.ent->value);
1780 free(appctx->ctx.map.ent);
1781 }
1782 }
1783
1784 /* Delete all matching entries for each map descritor. */
1785 appctx->ctx.map.desc = NULL;
1786 stats_map_lookup_next(si);
1787 while (appctx->ctx.map.desc) {
1788 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1789 if (pat_elt != NULL) {
1790 LIST_DEL(&pat_elt->list);
1791 pattern_free(pat_elt);
1792 }
1793 if (idx_elt != NULL) {
1794 ebmb_delete(&idx_elt->node);
1795 free(idx_elt);
1796 }
1797 }
1798 stats_map_lookup_next(si);
1799 }
1800
1801 /* The deletion is done, send message. */
1802 appctx->ctx.cli.msg = "Done.\n";
1803 appctx->st0 = STAT_CLI_PRINT;
1804 return 1;
1805 }
1806 else { /* unknown "del" parameter */
1807 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1808 appctx->st0 = STAT_CLI_PRINT;
1809 return 1;
1810 }
1811 }
1812 else if (strcmp(args[0], "add") == 0) {
1813 if (strcmp(args[1], "map") == 0) {
1814 const char *params[2];
1815 struct pattern *pat;
1816 struct map_entry *ent;
1817 struct sample_storage *smp;
1818
1819 /* Expect three parameters: map name, key and new value. */
1820 if (!*args[2] || !*args[3] || !*args[4]) {
1821 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1822 appctx->st0 = STAT_CLI_PRINT;
1823 return 1;
1824 }
1825
1826 params[0] = args[3];
1827 params[1] = "";
1828
1829 /* Lookup the reference in the maps. */
1830 appctx->ctx.map.ref = map_get_reference(args[2]);
1831 if (!appctx->ctx.map.ref) {
1832 appctx->ctx.cli.msg = "Unknown map reference.\n";
1833 appctx->st0 = STAT_CLI_PRINT;
1834 return 1;
1835 }
1836
1837 /* Prepare and link the new map_entry element. If out of memory
1838 * error the action is cancelled and the descriptor are left
1839 * coherents.
1840 */
1841 ent = malloc(sizeof(*ent));
1842 if (!ent) {
1843 appctx->ctx.cli.msg = "Out of memory error.\n";
1844 appctx->st0 = STAT_CLI_PRINT;
1845 return 1;
1846 }
1847 ent->key = strdup(args[3]);
1848 if (!ent->key) {
1849 free(ent);
1850 appctx->ctx.cli.msg = "Out of memory error.\n";
1851 appctx->st0 = STAT_CLI_PRINT;
1852 return 1;
1853 }
1854 ent->value = strdup(args[4]);
1855 if (!ent->value) {
1856 free(ent->key);
1857 free(ent);
1858 appctx->ctx.cli.msg = "Out of memory error.\n";
1859 appctx->st0 = STAT_CLI_PRINT;
1860 return 1;
1861 }
1862 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1863
1864 /* Browse each map descritor and try to insert this new value. */
1865 appctx->ctx.map.desc = NULL;
1866 for (stats_map_lookup_next(si);
1867 appctx->ctx.map.desc;
1868 stats_map_lookup_next(si)) {
1869
1870 /* Create new sample. Return out of memory error
1871 * if the memory cannot be allocated. The 'add' process
1872 * is aborted, but the already inserted entries are not
1873 * deleted.
1874 */
1875 smp = calloc(1, sizeof(*smp));
1876 if (!smp) {
1877 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1878 appctx->st0 = STAT_CLI_PRINT;
1879 return 1;
1880 }
1881
1882 /* Create sample. If this function fails, the insertion
1883 * is canceled for this 'descriptor', but continue, for
1884 * the other descriptors.
1885 */
1886 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1887 free(smp);
1888 continue;
1889 }
1890
1891 /* If the value can be indexed, get the first pattern. If
1892 * the return entry is not the indexed entry, new 'pattern' is
1893 * created by the function pattern_register(). If the 'pattern'
1894 * is NULL, new entry is created. This is ugly because the
1895 * following code interfers with the own code of the function
1896 * pattern_register().
1897 */
1898 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
1899 appctx->ctx.map.desc->pat->match == pat_match_ip) {
1900 pat = LIST_NEXT(&appctx->ctx.map.desc->pat->patterns, struct pattern *, list);
1901 if (&pat->list == &appctx->ctx.map.desc->pat->patterns)
1902 pat = NULL;
1903 }
1904 else
1905 pat = NULL;
1906
1907 if (!pattern_register(appctx->ctx.map.desc->pat, params, smp, &pat, 0, NULL)) {
1908 free(smp);
1909 continue;
1910 }
1911 }
1912
1913 /* The add is done, send message. */
1914 appctx->ctx.cli.msg = "Done.\n";
1915 appctx->st0 = STAT_CLI_PRINT;
1916 return 1;
1917 }
1918 else { /* unknown "del" parameter */
1919 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
1920 appctx->st0 = STAT_CLI_PRINT;
1921 return 1;
1922 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001923 }
1924 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001925 return 0;
1926 }
1927 return 1;
1928}
1929
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001930/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001931 * used to processes I/O from/to the stats unix socket. The system relies on a
1932 * state machine handling requests and various responses. We read a request,
1933 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001934 * Then we can read again. The state is stored in appctx->st0 and is one of the
1935 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001936 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001937 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001938static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001939{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001940 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001941 struct channel *req = si->ob;
1942 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001943 int reql;
1944 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001945
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001946 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1947 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001948
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001949 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001950 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001951 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001952 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1953 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001954 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001955 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001956 /* Let's close for real now. We just close the request
1957 * side, the conditions below will complete if needed.
1958 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001959 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001960 break;
1961 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001962 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001963 /* ensure we have some output room left in the event we
1964 * would want to return some info right after parsing.
1965 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001966 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001967 break;
1968
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001969 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001970 if (reql <= 0) { /* closed or EOL not found */
1971 if (reql == 0)
1972 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001973 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001974 continue;
1975 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001976
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001977 /* seek for a possible semi-colon. If we find one, we
1978 * replace it with an LF and skip only this part.
1979 */
1980 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001981 if (trash.str[len] == ';') {
1982 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001983 reql = len + 1;
1984 break;
1985 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001986
Willy Tarreau816fc222009-10-04 07:36:58 +02001987 /* now it is time to check that we have a full line,
1988 * remove the trailing \n and possibly \r, then cut the
1989 * line.
1990 */
1991 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001992 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001993 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001994 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001995 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001996
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001997 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001998 len--;
1999
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002000 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002001
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002002 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002003 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002004 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002005 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002006 continue;
2007 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002008 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002009 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002010 else if (strcmp(trash.str, "help") == 0 ||
2011 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002012 appctx->ctx.cli.msg = stats_sock_usage_msg;
2013 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002014 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002015 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002016 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002017 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002018 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002019 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002020 /* if prompt is disabled, print help on empty lines,
2021 * so that the user at least knows how to enable
2022 * prompt and find help.
2023 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002024 appctx->ctx.cli.msg = stats_sock_usage_msg;
2025 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002026 }
2027
2028 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002029 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002030 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002031 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002032 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002033 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002034 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002035 continue;
2036 }
2037
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002038 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002039 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2041 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002042 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002043 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002044 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002045 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002046 break;
2047 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002048 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002049 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002050 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002051 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002052 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002053 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002054 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002055 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002056 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002057 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002058 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002059 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002060 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002061 if (stats_table_request(si, appctx->st0))
2062 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002063 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002064 case STAT_CLI_O_MAPS:
2065 if (stats_maps_list(si))
2066 appctx->st0 = STAT_CLI_PROMPT;
2067 break;
2068 case STAT_CLI_O_MAP:
2069 if (stats_map_list(si))
2070 appctx->st0 = STAT_CLI_PROMPT;
2071 break;
2072 case STAT_CLI_O_MLOOK:
2073 if (stats_map_lookup(si))
2074 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002075 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002076 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002077 break;
2078 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002079
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002080 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002081 if (appctx->st0 == STAT_CLI_PROMPT) {
2082 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2083 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 }
2085
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002086 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002087 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002088 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002089
2090 /* Now we close the output if one of the writers did so,
2091 * or if we're not in interactive mode and the request
2092 * buffer is empty. This still allows pipelined requests
2093 * to be sent in non-interactive mode.
2094 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002095 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2096 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002097 continue;
2098 }
2099
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002100 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002101 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002102 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002103 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002105 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002106 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2107 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002108 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002109 * and nothing more to consume. This is comparable to a broken pipe, so
2110 * we forward the close to the request side so that it flows upstream to
2111 * the client.
2112 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002113 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002114 }
2115
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002116 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002117 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2118 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2119 /* We have no more processing to do, and nothing more to send, and
2120 * the client side has closed. So we'll forward this state downstream
2121 * on the response buffer.
2122 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002123 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002124 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002125 }
2126
2127 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002128 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002129
2130 /* we don't want to expire timeouts while we're processing requests */
2131 si->ib->rex = TICK_ETERNITY;
2132 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002133
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002134 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002135 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 +02002136 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002137 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 +02002138
2139 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2140 /* check that we have released everything then unregister */
2141 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002142 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002143}
2144
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002145/* This function dumps information onto the stream interface's read buffer.
2146 * It returns 0 as long as it does not complete, non-zero upon completion.
2147 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002148 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002149static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002150{
2151 unsigned int up = (now.tv_sec - start_date.tv_sec);
2152
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002153 chunk_printf(&trash,
2154 "Name: " PRODUCT_NAME "\n"
2155 "Version: " HAPROXY_VERSION "\n"
2156 "Release_date: " HAPROXY_DATE "\n"
2157 "Nbproc: %d\n"
2158 "Process_num: %d\n"
2159 "Pid: %d\n"
2160 "Uptime: %dd %dh%02dm%02ds\n"
2161 "Uptime_sec: %d\n"
2162 "Memmax_MB: %d\n"
2163 "Ulimit-n: %d\n"
2164 "Maxsock: %d\n"
2165 "Maxconn: %d\n"
2166 "Hard_maxconn: %d\n"
2167 "Maxpipes: %d\n"
2168 "CurrConns: %d\n"
2169 "PipesUsed: %d\n"
2170 "PipesFree: %d\n"
2171 "ConnRate: %d\n"
2172 "ConnRateLimit: %d\n"
2173 "MaxConnRate: %d\n"
2174 "CompressBpsIn: %u\n"
2175 "CompressBpsOut: %u\n"
2176 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002177#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002178 "ZlibMemUsage: %ld\n"
2179 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002180#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002181 "Tasks: %d\n"
2182 "Run_queue: %d\n"
2183 "Idle_pct: %d\n"
2184 "node: %s\n"
2185 "description: %s\n"
2186 "",
2187 global.nbproc,
2188 relative_pid,
2189 pid,
2190 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2191 up,
2192 global.rlimit_memmax,
2193 global.rlimit_nofile,
2194 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
2195 actconn, pipes_used, pipes_free,
2196 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
2197 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2198 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002199#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002200 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002201#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002202 nb_tasks_cur, run_queue_cur, idle_pct,
2203 global.node, global.desc ? global.desc : ""
2204 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002205
2206 if (bi_putchk(si->ib, &trash) == -1)
2207 return 0;
2208
2209 return 1;
2210}
2211
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002212/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2213 * the state from stream interface <si>. The caller is responsible for clearing
2214 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002215 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002216static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002217{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002218 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002219 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002220
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002221 if (!(px->cap & PR_CAP_FE))
2222 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002223
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002224 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002225 return 0;
2226
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002227 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002228 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002229 /* name, queue */
2230 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002231
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002233 /* Column sub-heading for Enable or Disable server */
2234 chunk_appendf(&trash, "<td></td>");
2235 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002236
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002237 chunk_appendf(&trash,
2238 "<td class=ac>"
2239 "<a name=\"%s/Frontend\"></a>"
2240 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2241 "<td colspan=3></td>"
2242 "",
2243 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002244
Willy Tarreau466c9b52012-12-23 02:25:03 +01002245 chunk_appendf(&trash,
2246 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002247 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002248 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2249 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2250 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002251 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2252 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2253 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002254
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002255 if (px->mode == PR_MODE_HTTP)
2256 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002257 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002258 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002259
2260 chunk_appendf(&trash,
2261 "</table></div></u></td>"
2262 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002263 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002264 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2265 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2266 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002267 U2H(px->fe_counters.sps_max),
2268 U2H(px->fe_counters.cps_max),
2269 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002270
2271 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002272 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002273 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002274 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002275
2276 chunk_appendf(&trash,
2277 "</table></div></u></td>"
2278 /* sessions rate : limit */
2279 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002280 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002281
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002282 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002283 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002284 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002285 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002286 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2287 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002288 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002289 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2290 U2H(px->fe_counters.cum_sess),
2291 U2H(px->fe_counters.cum_conn),
2292 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002293
Willy Tarreau466c9b52012-12-23 02:25:03 +01002294 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002295 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002296 chunk_appendf(&trash,
2297 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2298 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2299 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2300 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2301 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2302 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2303 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2304 "<tr><th>- other responses:</th><td>%s</td></tr>"
2305 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2306 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002307 U2H(px->fe_counters.p.http.cum_req),
2308 U2H(px->fe_counters.p.http.rsp[1]),
2309 U2H(px->fe_counters.p.http.rsp[2]),
2310 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002311 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002312 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002313 U2H(px->fe_counters.p.http.rsp[3]),
2314 U2H(px->fe_counters.p.http.rsp[4]),
2315 U2H(px->fe_counters.p.http.rsp[5]),
2316 U2H(px->fe_counters.p.http.rsp[0]),
2317 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002318 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002319
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002320 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002321 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002322 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002323 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002324 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002325 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002326 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002327 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002328
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002329 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002330 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002331 "<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 +01002332 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002333 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002334 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2335 px->fe_counters.comp_in ?
2336 (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 +01002337 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002338
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002339 chunk_appendf(&trash,
2340 /* denied: req, resp */
2341 "<td>%s</td><td>%s</td>"
2342 /* errors : request, connect, response */
2343 "<td>%s</td><td></td><td></td>"
2344 /* warnings: retries, redispatches */
2345 "<td></td><td></td>"
2346 /* server status : reflect frontend status */
2347 "<td class=ac>%s</td>"
2348 /* rest of server: nothing */
2349 "<td class=ac colspan=8></td></tr>"
2350 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002351 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2352 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002353 px->state == PR_STREADY ? "OPEN" :
2354 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002355 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002356 else { /* CSV mode */
2357 chunk_appendf(&trash,
2358 /* pxid, name, queue cur, queue max, */
2359 "%s,FRONTEND,,,"
2360 /* sessions : current, max, limit, total */
2361 "%d,%d,%d,%lld,"
2362 /* bytes : in, out */
2363 "%lld,%lld,"
2364 /* denied: req, resp */
2365 "%lld,%lld,"
2366 /* errors : request, connect, response */
2367 "%lld,,,"
2368 /* warnings: retries, redispatches */
2369 ",,"
2370 /* server status : reflect frontend status */
2371 "%s,"
2372 /* rest of server: nothing */
2373 ",,,,,,,,"
2374 /* pid, iid, sid, throttle, lbtot, tracked, type */
2375 "%d,%d,0,,,,%d,"
2376 /* rate, rate_lim, rate_max */
2377 "%u,%u,%u,"
2378 /* check_status, check_code, check_duration */
2379 ",,,",
2380 px->id,
2381 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2382 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2383 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2384 px->fe_counters.failed_req,
2385 px->state == PR_STREADY ? "OPEN" :
2386 px->state == PR_STFULL ? "FULL" : "STOP",
2387 relative_pid, px->uuid, STATS_TYPE_FE,
2388 read_freq_ctr(&px->fe_sess_per_sec),
2389 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002390
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002391 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2392 if (px->mode == PR_MODE_HTTP) {
2393 for (i=1; i<6; i++)
2394 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2395 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2396 }
2397 else
2398 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002399
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002400 /* failed health analyses */
2401 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002403 /* requests : req_rate, req_rate_max, req_tot, */
2404 chunk_appendf(&trash, "%u,%u,%lld,",
2405 read_freq_ctr(&px->fe_req_per_sec),
2406 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2407
2408 /* errors: cli_aborts, srv_aborts */
2409 chunk_appendf(&trash, ",,");
2410
2411 /* compression: in, out, bypassed */
2412 chunk_appendf(&trash, "%lld,%lld,%lld,",
2413 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2414
2415 /* compression: comp_rsp */
2416 chunk_appendf(&trash, "%lld,",
2417 px->fe_counters.p.http.comp_rsp);
2418
2419 /* finish with EOL */
2420 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002421 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002422 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002423}
2424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002425/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2426 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2427 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2428 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002429 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002430static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002431{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002432 struct appctx *appctx = __objt_appctx(si->end);
2433
2434 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002435 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002436 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002437 /* Column sub-heading for Enable or Disable server */
2438 chunk_appendf(&trash, "<td></td>");
2439 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002440 chunk_appendf(&trash,
2441 /* frontend name, listener name */
2442 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2443 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2444 "",
2445 px->id, l->name,
2446 (flags & ST_SHLGNDS)?"<u>":"",
2447 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002448
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002449 if (flags & ST_SHLGNDS) {
2450 char str[INET6_ADDRSTRLEN];
2451 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002452
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002453 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002454
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002455 port = get_host_port(&l->addr);
2456 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2457 case AF_INET:
2458 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2459 break;
2460 case AF_INET6:
2461 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2462 break;
2463 case AF_UNIX:
2464 chunk_appendf(&trash, "unix, ");
2465 break;
2466 case -1:
2467 chunk_appendf(&trash, "(%s), ", strerror(errno));
2468 break;
2469 }
Willy Tarreau91861262007-10-17 17:06:05 +02002470
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002471 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002472 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002473 }
Willy Tarreau91861262007-10-17 17:06:05 +02002474
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002475 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002476 /* queue */
2477 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002478 /* sessions rate: current, max, limit */
2479 "<td colspan=3>&nbsp;</td>"
2480 /* sessions: current, max, limit, total, lbtot */
2481 "<td>%s</td><td>%s</td><td>%s</td>"
2482 "<td>%s</td><td>&nbsp;</td>"
2483 /* bytes: in, out */
2484 "<td>%s</td><td>%s</td>"
2485 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002486 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002487 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2488 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002489
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002490 chunk_appendf(&trash,
2491 /* denied: req, resp */
2492 "<td>%s</td><td>%s</td>"
2493 /* errors: request, connect, response */
2494 "<td>%s</td><td></td><td></td>"
2495 /* warnings: retries, redispatches */
2496 "<td></td><td></td>"
2497 /* server status: reflect listener status */
2498 "<td class=ac>%s</td>"
2499 /* rest of server: nothing */
2500 "<td class=ac colspan=8></td></tr>"
2501 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002502 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2503 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002504 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2505 }
2506 else { /* CSV mode */
2507 chunk_appendf(&trash,
2508 /* pxid, name, queue cur, queue max, */
2509 "%s,%s,,,"
2510 /* sessions: current, max, limit, total */
2511 "%d,%d,%d,%lld,"
2512 /* bytes: in, out */
2513 "%lld,%lld,"
2514 /* denied: req, resp */
2515 "%lld,%lld,"
2516 /* errors: request, connect, response */
2517 "%lld,,,"
2518 /* warnings: retries, redispatches */
2519 ",,"
2520 /* server status: reflect listener status */
2521 "%s,"
2522 /* rest of server: nothing */
2523 ",,,,,,,,"
2524 /* pid, iid, sid, throttle, lbtot, tracked, type */
2525 "%d,%d,%d,,,,%d,"
2526 /* rate, rate_lim, rate_max */
2527 ",,,"
2528 /* check_status, check_code, check_duration */
2529 ",,,"
2530 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2531 ",,,,,,"
2532 /* failed health analyses */
2533 ","
2534 /* requests : req_rate, req_rate_max, req_tot, */
2535 ",,,"
2536 /* errors: cli_aborts, srv_aborts */
2537 ",,"
2538 /* compression: in, out, bypassed, comp_rsp */
2539 ",,,,"
2540 "\n",
2541 px->id, l->name,
2542 l->nbconn, l->counters->conn_max,
2543 l->maxconn, l->counters->cum_conn,
2544 l->counters->bytes_in, l->counters->bytes_out,
2545 l->counters->denied_req, l->counters->denied_resp,
2546 l->counters->failed_req,
2547 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2548 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2549 }
2550 return 1;
2551}
Willy Tarreau91861262007-10-17 17:06:05 +02002552
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002553/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2554 * from stream interface <si>, stats flags <flags>, and server state <state>.
2555 * The caller is responsible for clearing the trash if needed. Returns non-zero
2556 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002557 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2558 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002559 */
2560static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2561{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002562 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 struct server *ref = sv->track ? sv->track : sv;
2564 char str[INET6_ADDRSTRLEN];
2565 struct chunk src;
2566 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002568 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002569 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002570 "DOWN",
2571 "DN %d/%d &uarr;",
2572 "UP %d/%d &darr;",
2573 "UP",
2574 "NOLB %d/%d &darr;",
2575 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002576 "DRAIN %d/%d &darr;",
2577 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 "<i>no check</i>"
2579 };
Willy Tarreau91861262007-10-17 17:06:05 +02002580
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002581 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2582 chunk_appendf(&trash, "<tr class=\"maintain\">");
2583 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002584 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002585 "<tr class=\"%s%d\">",
2586 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002588 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002589 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002590 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2591 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002592
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002593 chunk_appendf(&trash,
2594 "<td class=ac><a name=\"%s/%s\"></a>%s"
2595 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2596 "",
2597 px->id, sv->id,
2598 (flags & ST_SHLGNDS) ? "<u>" : "",
2599 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002600
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002601 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002602 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002603
2604 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2605 case AF_INET:
2606 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2607 break;
2608 case AF_INET6:
2609 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2610 break;
2611 case AF_UNIX:
2612 chunk_appendf(&trash, "unix, ");
2613 break;
2614 case -1:
2615 chunk_appendf(&trash, "(%s), ", strerror(errno));
2616 break;
2617 default: /* address family not supported */
2618 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002619 }
Willy Tarreau91861262007-10-17 17:06:05 +02002620
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002621 /* id */
2622 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 /* cookie */
2625 if (sv->cookie) {
2626 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002627
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002628 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2629 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002630
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002631 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002632 }
2633
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002634 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002635 }
Willy Tarreau91861262007-10-17 17:06:05 +02002636
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002638 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002639 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002640 /* sessions rate : current, max, limit */
2641 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002642 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002643 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002644 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2645 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002646
Willy Tarreau466c9b52012-12-23 02:25:03 +01002647
2648 chunk_appendf(&trash,
2649 /* sessions: current, max, limit, total */
2650 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002651 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002652 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2653 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002654 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2655 U2H(sv->counters.cum_sess),
2656 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002657
Willy Tarreau466c9b52012-12-23 02:25:03 +01002658 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2659 if (px->mode == PR_MODE_HTTP) {
2660 unsigned long long tot;
2661 for (tot = i = 0; i < 6; i++)
2662 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002663
Willy Tarreau466c9b52012-12-23 02:25:03 +01002664 chunk_appendf(&trash,
2665 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2666 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2667 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2668 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2669 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2670 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2671 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2672 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002673 U2H(tot),
2674 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2675 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2676 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2677 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2678 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2679 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 +02002680 }
Willy Tarreau91861262007-10-17 17:06:05 +02002681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002683 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002684 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002685 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002686 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688 chunk_appendf(&trash,
2689 /* bytes : in, out */
2690 "<td>%s</td><td>%s</td>"
2691 /* denied: req, resp */
2692 "<td></td><td>%s</td>"
2693 /* errors : request, connect */
2694 "<td></td><td>%s</td>"
2695 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002696 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002697 /* warnings: retries, redispatches */
2698 "<td>%lld</td><td>%lld</td>"
2699 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002700 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2701 U2H(sv->counters.failed_secu),
2702 U2H(sv->counters.failed_conns),
2703 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002704 sv->counters.cli_aborts,
2705 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002706 sv->counters.retries, sv->counters.redispatches);
2707
2708 /* status, lest check */
2709 chunk_appendf(&trash, "<td class=ac>");
2710
2711 if (sv->state & SRV_MAINTAIN) {
2712 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2713 chunk_appendf(&trash, "MAINT");
2714 }
2715 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2716 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2717 chunk_appendf(&trash, "MAINT(via)");
2718 }
2719 else if (ref->state & SRV_CHECKED) {
2720 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2721 chunk_appendf(&trash,
2722 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002723 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2724 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002725 }
Willy Tarreau91861262007-10-17 17:06:05 +02002726
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002727 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002728 chunk_appendf(&trash,
2729 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002730 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002731 get_check_status_info(sv->check.status));
2732
2733 if (sv->check.status >= HCHK_STATUS_L57DATA)
2734 chunk_appendf(&trash, "/%d", sv->check.code);
2735
2736 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002737 chunk_appendf(&trash, " in %lums", sv->check.duration);
2738
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002739 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002740 get_check_status_description(sv->check.status));
2741 if (*sv->check.desc) {
2742 chunk_appendf(&trash, ": ");
2743 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2744 chunk_htmlencode(&trash, &src);
2745 }
2746 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 }
2748 else
2749 chunk_appendf(&trash, "</td><td>");
2750
2751 chunk_appendf(&trash,
2752 /* weight */
2753 "</td><td class=ac>%d</td>"
2754 /* act, bck */
2755 "<td class=ac>%s</td><td class=ac>%s</td>"
2756 "",
2757 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2758 (sv->state & SRV_BACKUP) ? "-" : "Y",
2759 (sv->state & SRV_BACKUP) ? "Y" : "-");
2760
2761 /* check failures: unique, fatal, down time */
2762 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002763 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764
2765 if (ref->observe)
2766 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2767
2768 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002769 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002770 "<td>%lld</td><td>%s</td>"
2771 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002772 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002773 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2774 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002775 else if (sv != ref) {
2776 if (sv->state & SRV_MAINTAIN)
2777 chunk_appendf(&trash,
2778 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2779 ref->proxy->id, ref->id);
2780 else
2781 chunk_appendf(&trash,
2782 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2783 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2784 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 else
2786 chunk_appendf(&trash, "<td colspan=3></td>");
2787
2788 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002789 if (sv->state & SRV_WARMINGUP)
2790 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002791 else
2792 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2793 }
2794 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002795 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796 "DOWN,",
2797 "DOWN %d/%d,",
2798 "UP %d/%d,",
2799 "UP,",
2800 "NOLB %d/%d,",
2801 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002802 "DRAIN %d/%d,",
2803 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002804 "no check,"
2805 };
2806
2807 chunk_appendf(&trash,
2808 /* pxid, name */
2809 "%s,%s,"
2810 /* queue : current, max */
2811 "%d,%d,"
2812 /* sessions : current, max, limit, total */
2813 "%d,%d,%s,%lld,"
2814 /* bytes : in, out */
2815 "%lld,%lld,"
2816 /* denied: req, resp */
2817 ",%lld,"
2818 /* errors : request, connect, response */
2819 ",%lld,%lld,"
2820 /* warnings: retries, redispatches */
2821 "%lld,%lld,"
2822 "",
2823 px->id, sv->id,
2824 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002825 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002826 sv->counters.bytes_in, sv->counters.bytes_out,
2827 sv->counters.failed_secu,
2828 sv->counters.failed_conns, sv->counters.failed_resp,
2829 sv->counters.retries, sv->counters.redispatches);
2830
2831 /* status */
2832 if (sv->state & SRV_MAINTAIN)
2833 chunk_appendf(&trash, "MAINT,");
2834 else if (ref != sv && ref->state & SRV_MAINTAIN)
2835 chunk_appendf(&trash, "MAINT(via),");
2836 else
2837 chunk_appendf(&trash,
2838 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002839 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2840 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841
2842 chunk_appendf(&trash,
2843 /* weight, active, backup */
2844 "%d,%d,%d,"
2845 "",
2846 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2847 (sv->state & SRV_BACKUP) ? 0 : 1,
2848 (sv->state & SRV_BACKUP) ? 1 : 0);
2849
2850 /* check failures: unique, fatal; last change, total downtime */
2851 if (sv->state & SRV_CHECKED)
2852 chunk_appendf(&trash,
2853 "%lld,%lld,%d,%d,",
2854 sv->counters.failed_checks, sv->counters.down_trans,
2855 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2856 else
2857 chunk_appendf(&trash, ",,,,");
2858
2859 /* queue limit, pid, iid, sid, */
2860 chunk_appendf(&trash,
2861 "%s,"
2862 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002863 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002864 relative_pid, px->uuid, sv->puid);
2865
2866 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002867 if (sv->state & SRV_WARMINGUP)
2868 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002869
2870 /* sessions: lbtot */
2871 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2872
2873 /* tracked */
2874 if (sv->track)
2875 chunk_appendf(&trash, "%s/%s,",
2876 sv->track->proxy->id, sv->track->id);
2877 else
2878 chunk_appendf(&trash, ",");
2879
2880 /* type */
2881 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2882
2883 /* rate */
2884 chunk_appendf(&trash, "%u,,%u,",
2885 read_freq_ctr(&sv->sess_per_sec),
2886 sv->counters.sps_max);
2887
2888 if (sv->state & SRV_CHECKED) {
2889 /* check_status */
2890 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2891
2892 /* check_code */
2893 if (sv->check.status >= HCHK_STATUS_L57DATA)
2894 chunk_appendf(&trash, "%u,", sv->check.code);
2895 else
2896 chunk_appendf(&trash, ",");
2897
2898 /* check_duration */
2899 if (sv->check.status >= HCHK_STATUS_CHECKED)
2900 chunk_appendf(&trash, "%lu,", sv->check.duration);
2901 else
2902 chunk_appendf(&trash, ",");
2903
2904 }
2905 else
2906 chunk_appendf(&trash, ",,,");
2907
2908 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2909 if (px->mode == PR_MODE_HTTP) {
2910 for (i=1; i<6; i++)
2911 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2912
2913 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2914 }
2915 else
2916 chunk_appendf(&trash, ",,,,,,");
2917
2918 /* failed health analyses */
2919 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2920
2921 /* requests : req_rate, req_rate_max, req_tot, */
2922 chunk_appendf(&trash, ",,,");
2923
2924 /* errors: cli_aborts, srv_aborts */
2925 chunk_appendf(&trash, "%lld,%lld,",
2926 sv->counters.cli_aborts, sv->counters.srv_aborts);
2927
2928 /* compression: in, out, bypassed, comp_rsp */
2929 chunk_appendf(&trash, ",,,,");
2930
2931 /* finish with EOL */
2932 chunk_appendf(&trash, "\n");
2933 }
2934 return 1;
2935}
2936
2937/* Dumps a line for backend <px> to the trash for and uses the state from stream
2938 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2939 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2940 */
2941static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2942{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002943 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002944 struct chunk src;
2945 int i;
2946
2947 if (!(px->cap & PR_CAP_BE))
2948 return 0;
2949
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002950 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 return 0;
2952
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002953 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002954 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002955 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956 /* Column sub-heading for Enable or Disable server */
2957 chunk_appendf(&trash, "<td></td>");
2958 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002959 chunk_appendf(&trash,
2960 "<td class=ac>"
2961 /* name */
2962 "%s<a name=\"%s/Backend\"></a>"
2963 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2964 "",
2965 (flags & ST_SHLGNDS)?"<u>":"",
2966 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002967
2968 if (flags & ST_SHLGNDS) {
2969 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002970 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002971 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2972
2973 /* cookie */
2974 if (px->cookie_name) {
2975 chunk_appendf(&trash, ", cookie: '");
2976 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2977 chunk_htmlencode(&trash, &src);
2978 chunk_appendf(&trash, "'");
2979 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002980 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002981 }
2982
2983 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002984 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002985 /* queue : current, max */
2986 "<td>%s</td><td>%s</td><td></td>"
2987 /* sessions rate : current, max, limit */
2988 "<td>%s</td><td>%s</td><td></td>"
2989 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002991 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2992 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993
2994 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002995 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002996 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002997 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002998 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002999 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003000 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3001 U2H(px->be_counters.cum_conn),
3002 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003
Willy Tarreau466c9b52012-12-23 02:25:03 +01003004 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003006 chunk_appendf(&trash,
3007 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3008 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3009 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3010 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3011 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3012 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3013 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3014 "<tr><th>- other responses:</th><td>%s</td></tr>"
3015 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3016 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003017 U2H(px->be_counters.p.http.cum_req),
3018 U2H(px->be_counters.p.http.rsp[1]),
3019 U2H(px->be_counters.p.http.rsp[2]),
3020 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003021 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003022 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003023 U2H(px->be_counters.p.http.rsp[3]),
3024 U2H(px->be_counters.p.http.rsp[4]),
3025 U2H(px->be_counters.p.http.rsp[5]),
3026 U2H(px->be_counters.p.http.rsp[0]),
3027 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003028 }
3029
3030 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003031 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003032 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01003033 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003034 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003035 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003036 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003037 U2H(px->be_counters.cum_lbconn),
3038 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003039
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003040 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003041 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003042 "<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 +01003043 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003044 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003045 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3046 px->be_counters.comp_in ?
3047 (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 +01003048 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3049
3050 chunk_appendf(&trash,
3051 /* denied: req, resp */
3052 "<td>%s</td><td>%s</td>"
3053 /* errors : request, connect */
3054 "<td></td><td>%s</td>"
3055 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003056 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003057 /* warnings: retries, redispatches */
3058 "<td>%lld</td><td>%lld</td>"
3059 /* backend status: reflect backend status (up/down): we display UP
3060 * if the backend has known working servers or if it has no server at
3061 * all (eg: for stats). Then we display the total weight, number of
3062 * active and backups. */
3063 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3064 "<td class=ac>%d</td><td class=ac>%d</td>"
3065 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003066 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3067 U2H(px->be_counters.failed_conns),
3068 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003069 px->be_counters.cli_aborts,
3070 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003071 px->be_counters.retries, px->be_counters.redispatches,
3072 human_time(now.tv_sec - px->last_change, 1),
3073 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3074 "<font color=\"red\"><b>DOWN</b></font>",
3075 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3076 px->srv_act, px->srv_bck);
3077
3078 chunk_appendf(&trash,
3079 /* rest of backend: nothing, down transitions, total downtime, throttle */
3080 "<td class=ac>&nbsp;</td><td>%d</td>"
3081 "<td>%s</td>"
3082 "<td></td>"
3083 "</tr>",
3084 px->down_trans,
3085 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3086 }
3087 else { /* CSV mode */
3088 chunk_appendf(&trash,
3089 /* pxid, name */
3090 "%s,BACKEND,"
3091 /* queue : current, max */
3092 "%d,%d,"
3093 /* sessions : current, max, limit, total */
3094 "%d,%d,%d,%lld,"
3095 /* bytes : in, out */
3096 "%lld,%lld,"
3097 /* denied: req, resp */
3098 "%lld,%lld,"
3099 /* errors : request, connect, response */
3100 ",%lld,%lld,"
3101 /* warnings: retries, redispatches */
3102 "%lld,%lld,"
3103 /* backend status: reflect backend status (up/down): we display UP
3104 * if the backend has known working servers or if it has no server at
3105 * all (eg: for stats). Then we display the total weight, number of
3106 * active and backups. */
3107 "%s,"
3108 "%d,%d,%d,"
3109 /* rest of backend: nothing, down transitions, last change, total downtime */
3110 ",%d,%d,%d,,"
3111 /* pid, iid, sid, throttle, lbtot, tracked, type */
3112 "%d,%d,0,,%lld,,%d,"
3113 /* rate, rate_lim, rate_max, */
3114 "%u,,%u,"
3115 /* check_status, check_code, check_duration */
3116 ",,,",
3117 px->id,
3118 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3119 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3120 px->be_counters.bytes_in, px->be_counters.bytes_out,
3121 px->be_counters.denied_req, px->be_counters.denied_resp,
3122 px->be_counters.failed_conns, px->be_counters.failed_resp,
3123 px->be_counters.retries, px->be_counters.redispatches,
3124 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3125 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3126 px->srv_act, px->srv_bck,
3127 px->down_trans, (int)(now.tv_sec - px->last_change),
3128 px->srv?be_downtime(px):0,
3129 relative_pid, px->uuid,
3130 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3131 read_freq_ctr(&px->be_sess_per_sec),
3132 px->be_counters.sps_max);
3133
3134 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3135 if (px->mode == PR_MODE_HTTP) {
3136 for (i=1; i<6; i++)
3137 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3138 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3139 }
3140 else
3141 chunk_appendf(&trash, ",,,,,,");
3142
3143 /* failed health analyses */
3144 chunk_appendf(&trash, ",");
3145
3146 /* requests : req_rate, req_rate_max, req_tot, */
3147 chunk_appendf(&trash, ",,,");
3148
3149 /* errors: cli_aborts, srv_aborts */
3150 chunk_appendf(&trash, "%lld,%lld,",
3151 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3152
3153 /* compression: in, out, bypassed */
3154 chunk_appendf(&trash, "%lld,%lld,%lld,",
3155 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3156
3157 /* compression: comp_rsp */
3158 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3159
3160 /* finish with EOL */
3161 chunk_appendf(&trash, "\n");
3162 }
3163 return 1;
3164}
3165
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003166/* Dumps the HTML table header for proxy <px> 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_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003173 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3174
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003175 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003177
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003178 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003179 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003181 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003182 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3183 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003184 }
3185
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003186 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003187 "<form action=\"%s%s%s%s\" method=\"post\">",
3188 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003189 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3190 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003191 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003192 }
3193
3194 /* print a new table */
3195 chunk_appendf(&trash,
3196 "<table class=\"tbl\" width=\"100%%\">\n"
3197 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003198 "<th class=\"pxname\" width=\"10%%\">");
3199
3200 chunk_appendf(&trash,
3201 "<a name=\"%s\"></a>%s"
3202 "<a class=px href=\"#%s\">%s</a>",
3203 px->id,
3204 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3205 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206
3207 if (uri->flags & ST_SHLGNDS) {
3208 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003209 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003210 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3211 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003212 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003213 }
3214
3215 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003216 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 "<th class=\"%s\" width=\"90%%\">%s</th>"
3218 "</tr>\n"
3219 "</table>\n"
3220 "<table class=\"tbl\" width=\"100%%\">\n"
3221 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3223 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3224
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003225 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003226 /* Column heading for Enable or Disable server */
3227 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003228 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229
3230 chunk_appendf(&trash,
3231 "<th rowspan=2></th>"
3232 "<th colspan=3>Queue</th>"
3233 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
3234 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3235 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3236 "<th colspan=9>Server</th>"
3237 "</tr>\n"
3238 "<tr class=\"titre\">"
3239 "<th>Cur</th><th>Max</th><th>Limit</th>"
3240 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3241 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
3242 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3243 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3244 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3245 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3246 "<th>Thrtle</th>\n"
3247 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003248}
3249
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003250/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003252 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003253static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003255 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 chunk_appendf(&trash, "</table>");
3257
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003258 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003259 /* close the form used to enable/disable this proxy servers */
3260 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003261 "Choose the action to perform on the checked servers : "
3262 "<select name=action>"
3263 "<option value=\"\"></option>"
3264 "<option value=\"disable\">Disable</option>"
3265 "<option value=\"enable\">Enable</option>"
3266 "<option value=\"stop\">Soft Stop</option>"
3267 "<option value=\"start\">Soft Start</option>"
3268 "<option value=\"shutdown\">Kill Sessions</option>"
3269 "</select>"
3270 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3271 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3272 "</form>",
3273 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274 }
3275
3276 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003277}
Willy Tarreau91861262007-10-17 17:06:05 +02003278
3279/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003280 * Dumps statistics for a proxy. The output is sent to the stream interface's
3281 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3282 * buffer space, or non-zero if everything completed. This function is used
3283 * both by the CLI and the HTTP entry points, and is able to dump the output
3284 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003285 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003286static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003287{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003288 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003289 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003290 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003291 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003292 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003293
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003294 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003295
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003296 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003297 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003298 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003299 if (uri && uri->scope) {
3300 /* we have a limited scope, we have to check the proxy name */
3301 struct stat_scope *scope;
3302 int len;
3303
3304 len = strlen(px->id);
3305 scope = uri->scope;
3306
3307 while (scope) {
3308 /* match exact proxy name */
3309 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3310 break;
3311
3312 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003313 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003314 break;
3315 scope = scope->next;
3316 }
3317
3318 /* proxy name not found : don't dump anything */
3319 if (scope == NULL)
3320 return 1;
3321 }
3322
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003323 /* if the user has requested a limited output and the proxy
3324 * name does not match, skip it.
3325 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 if (appctx->ctx.stats.scope_len &&
3327 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003328 return 1;
3329
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003330 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3331 (appctx->ctx.stats.iid != -1) &&
3332 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003333 return 1;
3334
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003335 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003336 /* fall through */
3337
Willy Tarreau295a8372011-03-10 11:25:07 +01003338 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003339 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003340 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003341 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003342 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003343 }
Willy Tarreau91861262007-10-17 17:06:05 +02003344
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003345 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003346 /* fall through */
3347
Willy Tarreau295a8372011-03-10 11:25:07 +01003348 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003349 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003351 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003352 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003353
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003354 appctx->ctx.stats.l = px->conf.listeners.n;
3355 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003356 /* fall through */
3357
Willy Tarreau295a8372011-03-10 11:25:07 +01003358 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003359 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003360 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003361 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02003362 return 0;
3363
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003364 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003365 if (!l->counters)
3366 continue;
3367
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003368 if (appctx->ctx.stats.flags & STAT_BOUND) {
3369 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003370 break;
3371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003372 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003373 continue;
3374 }
3375
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 /* print the frontend */
3377 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3378 if (bi_putchk(rep, &trash) == -1)
3379 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003380 }
3381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3383 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003384 /* fall through */
3385
Willy Tarreau295a8372011-03-10 11:25:07 +01003386 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003387 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003388 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003389 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 +02003390
Willy Tarreau9b28e032012-10-12 23:49:43 +02003391 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02003392 return 0;
3393
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003394 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003395
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003396 if (appctx->ctx.stats.flags & STAT_BOUND) {
3397 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003398 break;
3399
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003400 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003401 continue;
3402 }
3403
Willy Tarreau44267702011-10-28 15:35:33 +02003404 if (sv->track)
3405 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003406 else
3407 svs = sv;
3408
Willy Tarreau91861262007-10-17 17:06:05 +02003409 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003410 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003411 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003412 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003413 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003414 sv_state = 3; /* UP */
3415 else
3416 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003417
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003418 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003419 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003420 else if (svs->state & SRV_GOINGDOWN)
3421 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003422 }
Willy Tarreau91861262007-10-17 17:06:05 +02003423 else
Simon Horman125d0992013-02-24 17:23:38 +09003424 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003425 sv_state = 1; /* going up */
3426 else
3427 sv_state = 0; /* DOWN */
3428
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003429 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003430 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003431 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003432 continue;
3433 }
3434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3436 if (bi_putchk(rep, &trash) == -1)
3437 return 0;
3438 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003439
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003440 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003442
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 case STAT_PX_ST_BE:
3444 /* print the backend */
3445 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3446 if (bi_putchk(rep, &trash) == -1)
3447 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003448
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003449 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003451
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003453 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003454 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 if (bi_putchk(rep, &trash) == -1)
3456 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003457 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003458
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003459 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003460 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003461
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 case STAT_PX_ST_FIN:
3463 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003464
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003465 default:
3466 /* unknown state, we should put an abort() here ! */
3467 return 1;
3468 }
3469}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003470
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003471/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3472 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003474static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475{
3476 /* WARNING! This must fit in the first buffer !!! */
3477 chunk_appendf(&trash,
3478 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3479 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3480 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3481 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3482 "<style type=\"text/css\"><!--\n"
3483 "body {"
3484 " font-family: arial, helvetica, sans-serif;"
3485 " font-size: 12px;"
3486 " font-weight: normal;"
3487 " color: black;"
3488 " background: white;"
3489 "}\n"
3490 "th,td {"
3491 " font-size: 10px;"
3492 "}\n"
3493 "h1 {"
3494 " font-size: x-large;"
3495 " margin-bottom: 0.5em;"
3496 "}\n"
3497 "h2 {"
3498 " font-family: helvetica, arial;"
3499 " font-size: x-large;"
3500 " font-weight: bold;"
3501 " font-style: italic;"
3502 " color: #6020a0;"
3503 " margin-top: 0em;"
3504 " margin-bottom: 0em;"
3505 "}\n"
3506 "h3 {"
3507 " font-family: helvetica, arial;"
3508 " font-size: 16px;"
3509 " font-weight: bold;"
3510 " color: #b00040;"
3511 " background: #e8e8d0;"
3512 " margin-top: 0em;"
3513 " margin-bottom: 0em;"
3514 "}\n"
3515 "li {"
3516 " margin-top: 0.25em;"
3517 " margin-right: 2em;"
3518 "}\n"
3519 ".hr {margin-top: 0.25em;"
3520 " border-color: black;"
3521 " border-bottom-style: solid;"
3522 "}\n"
3523 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3524 ".total {background: #20D0D0;color: #ffff80;}\n"
3525 ".frontend {background: #e8e8d0;}\n"
3526 ".socket {background: #d0d0d0;}\n"
3527 ".backend {background: #e8e8d0;}\n"
3528 ".active0 {background: #ff9090;}\n"
3529 ".active1 {background: #ffd020;}\n"
3530 ".active2 {background: #ffffa0;}\n"
3531 ".active3 {background: #c0ffc0;}\n"
3532 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003533 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3534 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3535 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003536 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003537 ".backup0 {background: #ff9090;}\n"
3538 ".backup1 {background: #ff80ff;}\n"
3539 ".backup2 {background: #c060ff;}\n"
3540 ".backup3 {background: #b0d0ff;}\n"
3541 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3542 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003543 ".backup6 {background: #c060ff;}\n"
3544 ".backup7 {background: #cc9900;}\n"
3545 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003546 ".maintain {background: #c07820;}\n"
3547 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3548 "\n"
3549 "a.px:link {color: #ffff40; text-decoration: none;}"
3550 "a.px:visited {color: #ffff40; text-decoration: none;}"
3551 "a.px:hover {color: #ffffff; text-decoration: none;}"
3552 "a.lfsb:link {color: #000000; text-decoration: none;}"
3553 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3554 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3555 "\n"
3556 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3557 "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"
3558 "table.tbl td.ac { text-align: center;}\n"
3559 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3560 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3561 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3562 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3563 "\n"
3564 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3565 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3566 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003567 "table.det { border-collapse: collapse; border-style: none; }\n"
3568 "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"
3569 "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 +01003570 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003571 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003572 " display:block;\n"
3573 " visibility:hidden;\n"
3574 " z-index:2147483647;\n"
3575 " position:absolute;\n"
3576 " padding:2px 4px 3px;\n"
3577 " background:#f0f060; color:#000000;\n"
3578 " border:1px solid #7040c0;\n"
3579 " white-space:nowrap;\n"
3580 " font-style:normal;font-size:11px;font-weight:normal;\n"
3581 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3582 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3583 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003584 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003585 "-->\n"
3586 "</style></head>\n",
3587 (uri->flags & ST_SHNODE) ? " on " : "",
3588 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3589 );
3590}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003591
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003592/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003593 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003594 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003596static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003597{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003598 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003599 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003600 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003602 /* WARNING! this has to fit the first packet too.
3603 * We are around 3.5 kB, add adding entries will
3604 * become tricky if we want to support 4kB buffers !
3605 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003606 chunk_appendf(&trash,
3607 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3608 PRODUCT_NAME "%s</a></h1>\n"
3609 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3610 "<hr width=\"100%%\" class=\"hr\">\n"
3611 "<h3>&gt; General process information</h3>\n"
3612 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3613 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3614 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3615 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3616 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3617 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3618 "Running tasks: %d/%d; idle = %d %%<br>\n"
3619 "</td><td align=\"center\" nowrap>\n"
3620 "<table class=\"lgd\"><tr>\n"
3621 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3622 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3623 "</tr><tr>\n"
3624 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3625 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3626 "</tr><tr>\n"
3627 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3628 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3629 "</tr><tr>\n"
3630 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003631 "</tr><tr>\n"
3632 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003633 "</tr><tr>\n"
3634 "<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 -07003635 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003636 "<td class=\"active7\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003637 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003638 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003639 "</td>"
3640 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3641 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3642 "",
3643 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3644 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3645 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3646 (uri->flags & ST_SHDESC) ? ": " : "",
3647 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3648 pid, relative_pid, global.nbproc,
3649 up / 86400, (up % 86400) / 3600,
3650 (up % 3600) / 60, (up % 60),
3651 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3652 global.rlimit_memmax ? " MB" : "",
3653 global.rlimit_nofile,
3654 global.maxsock, global.maxconn, global.maxpipes,
3655 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3656 run_queue_cur, nb_tasks_cur, idle_pct
3657 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003658
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003659 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3660 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3661 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003662
3663 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003664 "<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 +02003665 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003666 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3667 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3668 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003669 STAT_SCOPE_TXT_MAXLEN);
3670
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003671 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003672 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003673 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003674 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003675 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3676 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003677 }
3678
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003679 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003680 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003681 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003682 uri->uri_prefix,
3683 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003684 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003685 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003686 else
3687 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003688 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003689 uri->uri_prefix,
3690 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003691 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003692 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003693
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003695 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003697 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003698 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003699 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003700 "",
3701 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003702 else
3703 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003704 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003705 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003706 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003707 ";norefresh",
3708 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003709 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003710
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003711 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003712 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003713 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003714 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3715 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003716 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003717
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003718 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003719 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003720 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003721 (uri->refresh > 0) ? ";norefresh" : "",
3722 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003723
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003724 chunk_appendf(&trash,
3725 "</ul></td>"
3726 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3727 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3728 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3729 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3730 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3731 "</ul>"
3732 "</td>"
3733 "</tr></table>\n"
3734 ""
3735 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003736
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003737 if (appctx->ctx.stats.st_code) {
3738 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003739 case STAT_STATUS_DONE:
3740 chunk_appendf(&trash,
3741 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003742 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003743 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003744 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003745 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3746 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003747 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003748 break;
3749 case STAT_STATUS_NONE:
3750 chunk_appendf(&trash,
3751 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003752 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003753 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003754 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003755 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3756 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003757 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003758 break;
3759 case STAT_STATUS_PART:
3760 chunk_appendf(&trash,
3761 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003762 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003763 "Action partially processed.<br>"
3764 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003765 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003766 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3767 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003768 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003769 break;
3770 case STAT_STATUS_ERRP:
3771 chunk_appendf(&trash,
3772 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003773 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003774 "Action not processed because of invalid parameters."
3775 "<ul>"
3776 "<li>The action is maybe unknown.</li>"
3777 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3778 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3779 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003780 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003781 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3782 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003783 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003784 break;
3785 case STAT_STATUS_EXCD:
3786 chunk_appendf(&trash,
3787 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003788 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003789 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3790 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003791 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003792 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3793 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003794 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003795 break;
3796 case STAT_STATUS_DENY:
3797 chunk_appendf(&trash,
3798 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003799 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003800 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003801 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003802 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3803 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003804 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003805 break;
3806 default:
3807 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003808 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003809 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003811 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003812 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3813 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003814 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003815 }
3816 chunk_appendf(&trash, "<p>\n");
3817 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003818}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003819
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003820/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3821 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003822 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003823static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003824{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003825 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003827
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003828/* This function dumps statistics onto the stream interface's read buffer in
3829 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003830 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3831 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3832 * and the session must be closed, or -1 in case of any error. This function is
3833 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003835static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003836{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003837 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838 struct channel *rep = si->ib;
3839 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003841 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003842
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003843 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003844 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003845 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003848 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003849 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003850 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003851 else
3852 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003853
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003854 if (bi_putchk(rep, &trash) == -1)
3855 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003856
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003857 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003858 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003859
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003861 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003862 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003863 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003864 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003865 }
Willy Tarreau91861262007-10-17 17:06:05 +02003866
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003867 appctx->ctx.stats.px = proxy;
3868 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3869 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003870 /* fall through */
3871
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003872 case STAT_ST_LIST:
3873 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003874 while (appctx->ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003875 if (buffer_almost_full(rep->buf))
3876 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003877
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003878 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003879 /* skip the disabled proxies, global frontend and non-networked ones */
3880 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003881 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003882 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003883
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003884 appctx->ctx.stats.px = px->next;
3885 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 }
3887 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003888
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003889 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003890 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003891
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003892 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003893 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003894 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003895 if (bi_putchk(rep, &trash) == -1)
3896 return 0;
3897 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003898
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003899 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003900 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003901
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003902 case STAT_ST_FIN:
3903 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003904
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003905 default:
3906 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003907 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003908 return -1;
3909 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910}
Willy Tarreauae526782010-03-04 20:34:23 +01003911
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003912/* We reached the stats page through a POST request. The appctx is
3913 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01003914 * Parse the posted data and enable/disable servers if necessary.
3915 * Returns 1 if request was parsed or zero if it needs more data.
3916 */
3917static int stats_process_http_post(struct stream_interface *si)
3918{
3919 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003920 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003921
3922 struct proxy *px = NULL;
3923 struct server *sv = NULL;
3924
3925 char key[LINESIZE];
3926 int action = ST_ADM_ACTION_NONE;
3927 int reprocess = 0;
3928
3929 int total_servers = 0;
3930 int altered_servers = 0;
3931
3932 char *first_param, *cur_param, *next_param, *end_params;
3933 char *st_cur_param = NULL;
3934 char *st_next_param = NULL;
3935
3936 struct chunk *temp;
3937 int reql;
3938
3939 temp = get_trash_chunk();
3940 if (temp->size < s->txn.req.body_len) {
3941 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003942 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003943 goto out;
3944 }
3945
3946 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3947 if (reql <= 0) {
3948 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003949 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003950 return 0;
3951 }
3952
3953 first_param = temp->str;
3954 end_params = temp->str + reql;
3955 cur_param = next_param = end_params;
3956 *end_params = '\0';
3957
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003958 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003959
3960 /*
3961 * Parse the parameters in reverse order to only store the last value.
3962 * From the html form, the backend and the action are at the end.
3963 */
3964 while (cur_param > first_param) {
3965 char *value;
3966 int poffset, plen;
3967
3968 cur_param--;
3969
3970 if ((*cur_param == '&') || (cur_param == first_param)) {
3971 reprocess_servers:
3972 /* Parse the key */
3973 poffset = (cur_param != first_param ? 1 : 0);
3974 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3975 if ((plen > 0) && (plen <= sizeof(key))) {
3976 strncpy(key, cur_param + poffset, plen);
3977 key[plen - 1] = '\0';
3978 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003979 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003980 goto out;
3981 }
3982
3983 /* Parse the value */
3984 value = key;
3985 while (*value != '\0' && *value != '=') {
3986 value++;
3987 }
3988 if (*value == '=') {
3989 /* Ok, a value is found, we can mark the end of the key */
3990 *value++ = '\0';
3991 }
3992 if (url_decode(key) < 0 || url_decode(value) < 0)
3993 break;
3994
3995 /* Now we can check the key to see what to do */
3996 if (!px && (strcmp(key, "b") == 0)) {
3997 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
3998 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003999 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004000 goto out;
4001 }
4002 }
4003 else if (!action && (strcmp(key, "action") == 0)) {
4004 if (strcmp(value, "disable") == 0) {
4005 action = ST_ADM_ACTION_DISABLE;
4006 }
4007 else if (strcmp(value, "enable") == 0) {
4008 action = ST_ADM_ACTION_ENABLE;
4009 }
4010 else if (strcmp(value, "stop") == 0) {
4011 action = ST_ADM_ACTION_STOP;
4012 }
4013 else if (strcmp(value, "start") == 0) {
4014 action = ST_ADM_ACTION_START;
4015 }
4016 else if (strcmp(value, "shutdown") == 0) {
4017 action = ST_ADM_ACTION_SHUTDOWN;
4018 }
4019 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004020 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004021 goto out;
4022 }
4023 }
4024 else if (strcmp(key, "s") == 0) {
4025 if (!(px && action)) {
4026 /*
4027 * Indicates that we'll need to reprocess the parameters
4028 * as soon as backend and action are known
4029 */
4030 if (!reprocess) {
4031 st_cur_param = cur_param;
4032 st_next_param = next_param;
4033 }
4034 reprocess = 1;
4035 }
4036 else if ((sv = findserver(px, value)) != NULL) {
4037 switch (action) {
4038 case ST_ADM_ACTION_DISABLE:
4039 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4040 /* Not already in maintenance, we can change the server state */
4041 sv->state |= SRV_MAINTAIN;
4042 set_server_down(&sv->check);
4043 altered_servers++;
4044 total_servers++;
4045 }
4046 break;
4047 case ST_ADM_ACTION_ENABLE:
4048 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
4049 /* Already in maintenance, we can change the server state */
4050 set_server_up(&sv->check);
4051 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4052 altered_servers++;
4053 total_servers++;
4054 }
4055 break;
4056 case ST_ADM_ACTION_STOP:
4057 case ST_ADM_ACTION_START:
4058 if (action == ST_ADM_ACTION_START)
4059 sv->uweight = sv->iweight;
4060 else
4061 sv->uweight = 0;
4062
4063 server_recalc_eweight(sv);
4064 set_server_drain_state(sv);
4065
4066 altered_servers++;
4067 total_servers++;
4068 break;
4069 case ST_ADM_ACTION_SHUTDOWN:
4070 if (px->state != PR_STSTOPPED) {
4071 struct session *sess, *sess_bck;
4072
4073 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4074 if (sess->srv_conn == sv)
4075 session_shutdown(sess, SN_ERR_KILLED);
4076
4077 altered_servers++;
4078 total_servers++;
4079 }
4080 break;
4081 }
4082 } else {
4083 /* the server name is unknown or ambiguous (duplicate names) */
4084 total_servers++;
4085 }
4086 }
4087 if (reprocess && px && action) {
4088 /* Now, we know the backend and the action chosen by the user.
4089 * We can safely restart from the first server parameter
4090 * to reprocess them
4091 */
4092 cur_param = st_cur_param;
4093 next_param = st_next_param;
4094 reprocess = 0;
4095 goto reprocess_servers;
4096 }
4097
4098 next_param = cur_param;
4099 }
4100 }
4101
4102 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004103 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004104 }
4105 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004106 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004107 }
4108 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004109 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004110 }
4111 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004112 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004113 }
4114 out:
4115 return 1;
4116}
4117
4118
4119static int stats_send_http_headers(struct stream_interface *si)
4120{
4121 struct session *s = session_from_task(si->owner);
4122 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004123 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004124
4125 chunk_printf(&trash,
4126 "HTTP/1.0 200 OK\r\n"
4127 "Cache-Control: no-cache\r\n"
4128 "Connection: close\r\n"
4129 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004130 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004131
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004132 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004133 chunk_appendf(&trash, "Refresh: %d\r\n",
4134 uri->refresh);
4135
4136 chunk_appendf(&trash, "\r\n");
4137
4138 s->txn.status = 200;
4139 s->logs.tv_request = now;
4140
4141 if (bi_putchk(si->ib, &trash) == -1)
4142 return 0;
4143
4144 return 1;
4145}
4146
4147static int stats_send_http_redirect(struct stream_interface *si)
4148{
4149 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4150 struct session *s = session_from_task(si->owner);
4151 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004152 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004154 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004155 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004156 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004157 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004158 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4159 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004160 }
4161
4162 /* We don't want to land on the posted stats page because a refresh will
4163 * repost the data. We don't want this to happen on accident so we redirect
4164 * the browse to the stats page with a GET.
4165 */
4166 chunk_printf(&trash,
4167 "HTTP/1.1 303 See Other\r\n"
4168 "Cache-Control: no-cache\r\n"
4169 "Content-Type: text/plain\r\n"
4170 "Connection: close\r\n"
4171 "Location: %s;st=%s%s%s%s\r\n"
4172 "\r\n",
4173 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004174 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4175 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4176 stat_status_codes[appctx->ctx.stats.st_code]) ?
4177 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004178 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004179 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4180 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004181 scope_txt);
4182
4183 s->txn.status = 303;
4184 s->logs.tv_request = now;
4185
4186 if (bi_putchk(si->ib, &trash) == -1)
4187 return 0;
4188
4189 return 1;
4190}
4191
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004192/* This I/O handler runs as an applet embedded in a stream interface. It is
4193 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004194 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004195 * automatically unregisters itself once transfer is complete.
4196 */
4197static void http_stats_io_handler(struct stream_interface *si)
4198{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004199 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004200 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 struct channel *req = si->ob;
4202 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004204 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4205 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004206
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004207 /* check that the output is not closed */
4208 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004209 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004210
Willy Tarreau347a35d2013-11-22 17:51:09 +01004211 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004212 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004213 if (stats_send_http_headers(si)) {
4214 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004215 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004216 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004217 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004218 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004219 }
4220
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004221 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004222 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004223 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004224 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004225
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004226 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004227 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004228 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004229 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004231 }
4232
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004233 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004234 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004235 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004236 }
4237
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004238 if (appctx->st0 == STAT_HTTP_DONE)
Willy Tarreau347a35d2013-11-22 17:51:09 +01004239 si_shutw(si);
4240
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004241 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4242 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004244 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004245 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4246 si_shutr(si);
4247 res->flags |= CF_READ_NULL;
4248 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004249 }
Willy Tarreau91861262007-10-17 17:06:05 +02004250
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004251 /* update all other flags and resync with the other side */
4252 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004253
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004254 /* we don't want to expire timeouts while we're processing requests */
4255 si->ib->rex = TICK_ETERNITY;
4256 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004257
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004258 out:
4259 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4260 /* check that we have released everything then unregister */
4261 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004262 }
4263}
4264
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004265
Willy Tarreau909d5172012-11-26 03:04:41 +01004266static inline const char *get_conn_ctrl_name(const struct connection *conn)
4267{
Willy Tarreauf79c8172013-10-21 16:30:56 +02004268 if (!(conn->flags & CO_FL_CTRL_READY) || !conn->ctrl)
Willy Tarreau909d5172012-11-26 03:04:41 +01004269 return "NONE";
4270 return conn->ctrl->name;
4271}
4272
4273static inline const char *get_conn_xprt_name(const struct connection *conn)
4274{
4275 static char ptr[17];
4276
Willy Tarreauf79c8172013-10-21 16:30:56 +02004277 if (!(conn->flags & CO_FL_XPRT_READY) || !conn->xprt)
Willy Tarreau909d5172012-11-26 03:04:41 +01004278 return "NONE";
4279
4280 if (conn->xprt == &raw_sock)
4281 return "RAW";
4282
4283#ifdef USE_OPENSSL
4284 if (conn->xprt == &ssl_sock)
4285 return "SSL";
4286#endif
4287 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4288 return ptr;
4289}
4290
4291static inline const char *get_conn_data_name(const struct connection *conn)
4292{
4293 static char ptr[17];
4294
4295 if (!conn->data)
4296 return "NONE";
4297
4298 if (conn->data == &sess_conn_cb)
4299 return "SESS";
4300
4301 if (conn->data == &si_conn_cb)
4302 return "STRM";
4303
4304 if (conn->data == &check_conn_cb)
4305 return "CHCK";
4306
4307 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4308 return ptr;
4309}
4310
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004311/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004312 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004313 * 0 if the output buffer is full and it needs to be called again, otherwise
4314 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004315 */
Willy Tarreau76153662012-11-26 01:16:39 +01004316static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004317{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004318 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004319 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004320 extern const char *monthname[12];
4321 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004322 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004323 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004324
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004325 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004326
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004327 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004328 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004329 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4330 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004331 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004332 appctx->ctx.sess.uid = 0;
4333 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004334 return 1;
4335 }
4336
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004337 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004338 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004339 appctx->ctx.sess.uid = sess->uniq_id;
4340 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004341 /* fall through */
4342
4343 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004344 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004345 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004346 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004347 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004348 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4349 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004350 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004351 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004352
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004353 conn = objt_conn(sess->si[0].end);
4354 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004355 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004356 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004357 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004358 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004359 break;
4360 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004361 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004362 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004363 default:
4364 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004365 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004366 break;
4367 }
4368
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004369 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004370 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004371 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004372
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004374 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004375 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4376 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4377 sess->listener ? sess->listener->luid : 0);
4378
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004379 if (conn)
4380 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004381
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004382 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004383 case AF_INET:
4384 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004385 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004386 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004387 break;
4388 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004389 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004390 break;
4391 default:
4392 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004393 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004394 break;
4395 }
4396
Willy Tarreau50943332011-09-02 17:33:05 +02004397 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004398 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004399 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004400 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004401 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4402 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004403 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004404
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004405 conn = objt_conn(sess->si[1].end);
4406 if (conn)
4407 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004408
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004409 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004410 case AF_INET:
4411 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004412 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004413 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004414 break;
4415 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004416 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004417 break;
4418 default:
4419 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004420 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004421 break;
4422 }
4423
4424 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004425 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004426 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004427 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4428 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004429 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004430 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004431
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004432 if (conn)
4433 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004434
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004435 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004436 case AF_INET:
4437 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004438 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004439 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004440 break;
4441 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004442 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004443 break;
4444 default:
4445 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004446 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004447 break;
4448 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004449
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004450 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004451 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004452 sess->task,
4453 sess->task->state,
4454 sess->task->nice, sess->task->calls,
4455 sess->task->expire ?
4456 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4457 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4458 TICKS_TO_MS(1000)) : "<NEVER>",
4459 task_in_rq(sess->task) ? ", running" : "");
4460
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004461 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004462 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004463 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4464
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004465 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004466 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4467 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4468 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4469
4470 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004471 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004472 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004473 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004474 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004475 obj_type_name(sess->si[0].end),
4476 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004477 sess->si[0].exp ?
4478 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4479 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4480 TICKS_TO_MS(1000)) : "<NEVER>",
4481 sess->si[0].err_type);
4482
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004483 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004484 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004485 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004486 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004487 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004488 obj_type_name(sess->si[1].end),
4489 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004490 sess->si[1].exp ?
4491 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4492 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4493 TICKS_TO_MS(1000)) : "<NEVER>",
4494 sess->si[1].err_type);
4495
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004496 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004497 chunk_appendf(&trash,
4498 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004499 conn,
4500 get_conn_ctrl_name(conn),
4501 get_conn_xprt_name(conn),
4502 get_conn_data_name(conn),
4503 obj_type_name(conn->target),
4504 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004505
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004506 chunk_appendf(&trash,
4507 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004508 conn->flags,
4509 conn->t.sock.fd,
4510 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4511 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4512 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004513 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004514 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4515 chunk_appendf(&trash,
4516 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4517 tmpctx,
4518 tmpctx->st0,
4519 tmpctx->st1,
4520 tmpctx->st2,
4521 tmpctx->applet->name);
4522 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004523
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004524 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004525 chunk_appendf(&trash,
4526 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004527 conn,
4528 get_conn_ctrl_name(conn),
4529 get_conn_xprt_name(conn),
4530 get_conn_data_name(conn),
4531 obj_type_name(conn->target),
4532 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004533
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004534 chunk_appendf(&trash,
4535 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004536 conn->flags,
4537 conn->t.sock.fd,
4538 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4539 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4540 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004541 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004542 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4543 chunk_appendf(&trash,
4544 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4545 tmpctx,
4546 tmpctx->st0,
4547 tmpctx->st1,
4548 tmpctx->st2,
4549 tmpctx->applet->name);
4550 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004551
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004552 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004553 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004554 " an_exp=%s",
4555 sess->req,
4556 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004557 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004558 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004559 sess->req->analyse_exp ?
4560 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4561 TICKS_TO_MS(1000)) : "<NEVER>");
4562
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004563 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004564 " rex=%s",
4565 sess->req->rex ?
4566 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4567 TICKS_TO_MS(1000)) : "<NEVER>");
4568
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004569 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004570 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004571 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004572 sess->req->wex ?
4573 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4574 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004575 sess->req->buf,
4576 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004577 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004578 sess->txn.req.next, sess->req->buf->i,
4579 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004580
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004581 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004582 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004583 " an_exp=%s",
4584 sess->rep,
4585 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004586 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004587 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004588 sess->rep->analyse_exp ?
4589 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4590 TICKS_TO_MS(1000)) : "<NEVER>");
4591
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004592 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004593 " rex=%s",
4594 sess->rep->rex ?
4595 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4596 TICKS_TO_MS(1000)) : "<NEVER>");
4597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004598 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004599 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004600 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004601 sess->rep->wex ?
4602 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4603 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004604 sess->rep->buf,
4605 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004606 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004607 sess->txn.rsp.next, sess->rep->buf->i,
4608 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004610 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004611 return 0;
4612
4613 /* use other states to dump the contents */
4614 }
4615 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004616 appctx->ctx.sess.uid = 0;
4617 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004618 return 1;
4619}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004620
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004621static int stats_maps_list(struct stream_interface *si)
4622{
4623 struct appctx *appctx = __objt_appctx(si->end);
4624
4625 switch (appctx->st2) {
4626 case STAT_ST_INIT:
4627 /* Init to the first entry. The list cannot be change */
4628 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4629 appctx->st2 = STAT_ST_LIST;
4630 /* fall through */
4631
4632 case STAT_ST_LIST:
4633 while (appctx->ctx.map.ref) {
4634
4635 chunk_reset(&trash);
4636
4637 /* build messages */
4638 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4639
4640 if (bi_putchk(si->ib, &trash) == -1) {
4641 /* let's try again later from this session. We add ourselves into
4642 * this session's users so that it can remove us upon termination.
4643 */
4644 return 0;
4645 }
4646
4647 /* get next list entry and check the end of the list */
4648 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4649 struct map_reference *, list);
4650 if (&appctx->ctx.map.ref->list == &maps)
4651 break;
4652 }
4653
4654 appctx->st2 = STAT_ST_FIN;
4655 /* fall through */
4656
4657 default:
4658 appctx->st2 = STAT_ST_FIN;
4659 return 1;
4660 }
4661}
4662
4663static const char *smp_to_type[SMP_TYPES] = {
4664 [SMP_T_BOOL] = "bool",
4665 [SMP_T_UINT] = "uint",
4666 [SMP_T_SINT] = "sint",
4667 [SMP_T_ADDR] = "addr",
4668 [SMP_T_IPV4] = "ipv4",
4669 [SMP_T_IPV6] = "ipv6",
4670 [SMP_T_STR] = "str",
4671 [SMP_T_BIN] = "bin",
4672 [SMP_T_CSTR] = "cstr",
4673 [SMP_T_CBIN] = "cbin",
4674};
4675
4676static int stats_map_lookup(struct stream_interface *si)
4677{
4678 struct appctx *appctx = __objt_appctx(si->end);
4679 struct sample_storage *smp;
4680 struct sample sample;
4681 struct pattern *pat;
4682 struct pat_idx_elt *elt;
4683 enum pat_match_res res;
4684 struct sockaddr_in addr;
4685 char s_addr[INET_ADDRSTRLEN];
4686
4687 switch (appctx->st2) {
4688 case STAT_ST_INIT:
4689 appctx->ctx.map.desc = NULL;
4690 stats_map_lookup_next(si);
4691 appctx->st2 = STAT_ST_LIST;
4692 /* fall through */
4693
4694 case STAT_ST_LIST:
4695 /* for each lookup type */
4696 while (appctx->ctx.map.desc) {
4697 /* initialise chunk to build new message */
4698 chunk_reset(&trash);
4699
4700 /* execute pattern matching */
4701 sample.type = SMP_T_CSTR;
4702 sample.data.str.len = appctx->ctx.map.chunk.len;
4703 sample.data.str.str = appctx->ctx.map.chunk.str;
4704 pat = NULL;
4705 elt = NULL;
4706 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4707
4708 /* build return message: set type of match */
4709 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4710 chunk_appendf(&trash, "found, ");
4711 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4712 chunk_appendf(&trash, "bool, ");
4713 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4714 chunk_appendf(&trash, "int, ");
4715 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4716 chunk_appendf(&trash, "ip, ");
4717 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4718 chunk_appendf(&trash, "bin, ");
4719 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4720 chunk_appendf(&trash, "len, ");
4721 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4722 chunk_appendf(&trash, "str, ");
4723 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4724 chunk_appendf(&trash, "beg, ");
4725 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4726 chunk_appendf(&trash, "sub, ");
4727 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4728 chunk_appendf(&trash, "dir, ");
4729 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4730 chunk_appendf(&trash, "dom, ");
4731 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4732 chunk_appendf(&trash, "end, ");
4733 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4734 chunk_appendf(&trash, "reg, ");
4735 else /* The never appens case */
4736 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4737
4738 /* Display no match, and set default value */
4739 if (res == PAT_NOMATCH) {
4740 chunk_appendf(&trash, "no-match, ");
4741 smp = appctx->ctx.map.desc->def;
4742 }
4743
4744 /* Display match and match info */
4745 else {
4746 /* display match */
4747 chunk_appendf(&trash, "match, ");
4748
4749 /* display search mode */
4750 if (elt)
4751 chunk_appendf(&trash, "tree, ");
4752 else
4753 chunk_appendf(&trash, "list, ");
4754
4755 /* display search options */
4756 if (pat) {
4757 /* case sensitive */
4758 if (pat->flags & PAT_F_IGNORE_CASE)
4759 chunk_appendf(&trash, "case-insensitive, ");
4760 else
4761 chunk_appendf(&trash, "case-sensitive, ");
4762
4763 /* display source */
4764 if (pat->flags & PAT_F_FROM_FILE)
4765 chunk_appendf(&trash, "from-file, ");
4766 }
4767
4768 /* display match expresion */
4769 if (elt) {
4770 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4771 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4772 }
4773 /* only IPv4 */
4774 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4775 /* convert ip */
4776 memcpy(&addr.sin_addr, elt->node.key, 4);
4777 addr.sin_family = AF_INET;
4778 if (addr_to_str((struct sockaddr_storage *)&addr, s_addr, INET_ADDRSTRLEN))
4779 chunk_appendf(&trash, "match=\"%s/%d\", ", s_addr, elt->node.node.pfx);
4780 }
4781 }
4782 }
4783
4784 /* display return value */
4785 if (!smp) {
4786 chunk_appendf(&trash, "return=nothing\n");
4787 }
4788 else {
4789 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4790 sample.type = smp->type;
4791 if (sample_casts[sample.type][SMP_T_CSTR] &&
4792 sample_casts[sample.type][SMP_T_CSTR](&sample))
4793 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4794 sample.data.str.str, smp_to_type[smp->type]);
4795 else
4796 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4797 smp_to_type[smp->type]);
4798 }
4799
4800 /* display response */
4801 if (bi_putchk(si->ib, &trash) == -1) {
4802 /* let's try again later from this session. We add ourselves into
4803 * this session's users so that it can remove us upon termination.
4804 */
4805 return 0;
4806 }
4807
4808 /* get next entry */
4809 stats_map_lookup_next(si);
4810 }
4811
4812 appctx->st2 = STAT_ST_FIN;
4813 /* fall through */
4814
4815 default:
4816 appctx->st2 = STAT_ST_FIN;
4817 free(appctx->ctx.map.chunk.str);
4818 return 1;
4819 }
4820}
4821
4822static int stats_map_list(struct stream_interface *si)
4823{
4824 struct appctx *appctx = __objt_appctx(si->end);
4825
4826 switch (appctx->st2) {
4827
4828 case STAT_ST_INIT:
4829 /* Init to the first entry. The list cannot be change */
4830 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
4831 struct map_entry *, list);
4832 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4833 appctx->ctx.map.ent = NULL;
4834 appctx->st2 = STAT_ST_LIST;
4835 /* fall through */
4836
4837 case STAT_ST_LIST:
4838 while (appctx->ctx.map.ent) {
4839 chunk_reset(&trash);
4840
4841 /* build messages */
4842 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
4843
4844 if (bi_putchk(si->ib, &trash) == -1) {
4845 /* let's try again later from this session. We add ourselves into
4846 * this session's users so that it can remove us upon termination.
4847 */
4848 return 0;
4849 }
4850
4851 /* get next list entry and check the end of the list */
4852 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
4853 struct map_entry *, list);
4854 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4855 break;
4856 }
4857
4858 appctx->st2 = STAT_ST_FIN;
4859 /* fall through */
4860
4861 default:
4862 appctx->st2 = STAT_ST_FIN;
4863 return 1;
4864 }
4865}
4866
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004867/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004868 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004869 * to be called again, otherwise non-zero. It is designed to be called
4870 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004871 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004872static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004873{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004874 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004875 struct connection *conn;
4876
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004877 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004878 /* If we're forced to shut down, we might have to remove our
4879 * reference to the last session being dumped.
4880 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004881 if (appctx->st2 == STAT_ST_LIST) {
4882 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4883 LIST_DEL(&appctx->ctx.sess.bref.users);
4884 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004885 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004886 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004887 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004888 }
4889
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004890 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004891
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004892 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004893 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004894 /* the function had not been called yet, let's prepare the
4895 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004896 * pointer to the first in the global list. When a target
4897 * session is being destroyed, it is responsible for updating
4898 * this pointer. We know we have reached the end when this
4899 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004900 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004901 LIST_INIT(&appctx->ctx.sess.bref.users);
4902 appctx->ctx.sess.bref.ref = sessions.n;
4903 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004904 /* fall through */
4905
Willy Tarreau295a8372011-03-10 11:25:07 +01004906 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004907 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004908 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4909 LIST_DEL(&appctx->ctx.sess.bref.users);
4910 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004911 }
4912
4913 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004914 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004915 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004916 struct session *curr_sess;
4917
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004918 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004919
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004920 if (appctx->ctx.sess.target) {
4921 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004922 goto next_sess;
4923
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004924 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004925 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01004926 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004927 return 0;
4928
4929 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004930 LIST_DEL(&appctx->ctx.sess.bref.users);
4931 LIST_INIT(&appctx->ctx.sess.bref.users);
4932 if (appctx->ctx.sess.target != (void *)-1) {
4933 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01004934 break;
4935 }
4936 else
4937 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004938 }
4939
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004940 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004941 "%p: proto=%s",
4942 curr_sess,
4943 curr_sess->listener->proto->name);
4944
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004945
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004946 conn = objt_conn(curr_sess->si[0].end);
4947 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02004948 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004949 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004950 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004951 " src=%s:%d fe=%s be=%s srv=%s",
4952 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004953 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004954 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004955 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004956 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004957 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004958 break;
4959 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004960 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02004961 " src=unix:%d fe=%s be=%s srv=%s",
4962 curr_sess->listener->luid,
4963 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02004964 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004965 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02004966 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004967 break;
4968 }
4969
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004970 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02004971 " ts=%02x age=%s calls=%d",
4972 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01004973 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
4974 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004975
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004976 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004977 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004978 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004979 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004980 curr_sess->req->analysers,
4981 curr_sess->req->rex ?
4982 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
4983 TICKS_TO_MS(1000)) : "");
4984
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004985 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004986 ",wx=%s",
4987 curr_sess->req->wex ?
4988 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
4989 TICKS_TO_MS(1000)) : "");
4990
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004991 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004992 ",ax=%s]",
4993 curr_sess->req->analyse_exp ?
4994 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
4995 TICKS_TO_MS(1000)) : "");
4996
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004997 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004998 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004999 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005000 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005001 curr_sess->rep->analysers,
5002 curr_sess->rep->rex ?
5003 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5004 TICKS_TO_MS(1000)) : "");
5005
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005006 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005007 ",wx=%s",
5008 curr_sess->rep->wex ?
5009 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5010 TICKS_TO_MS(1000)) : "");
5011
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005012 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005013 ",ax=%s]",
5014 curr_sess->rep->analyse_exp ?
5015 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5016 TICKS_TO_MS(1000)) : "");
5017
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005018 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005019 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005020 " s0=[%d,%1xh,fd=%d,ex=%s]",
5021 curr_sess->si[0].state,
5022 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005023 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005024 curr_sess->si[0].exp ?
5025 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5026 TICKS_TO_MS(1000)) : "");
5027
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005028 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005029 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005030 " s1=[%d,%1xh,fd=%d,ex=%s]",
5031 curr_sess->si[1].state,
5032 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005033 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005034 curr_sess->si[1].exp ?
5035 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5036 TICKS_TO_MS(1000)) : "");
5037
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005038 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005039 " exp=%s",
5040 curr_sess->task->expire ?
5041 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5042 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005043 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005044 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005046 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005047
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005048 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005049 /* let's try again later from this session. We add ourselves into
5050 * this session's users so that it can remove us upon termination.
5051 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005052 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005053 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005054 }
5055
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005056 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005057 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005058 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005059
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005060 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005061 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005062 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005063 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005064 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005065 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005066
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005067 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005068 return 0;
5069
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005070 appctx->ctx.sess.target = NULL;
5071 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005072 return 1;
5073 }
5074
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005075 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005076 /* fall through */
5077
5078 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005079 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005080 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005081 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005082}
5083
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005084/* This is called when the stream interface is closed. For instance, upon an
5085 * external abort, we won't call the i/o handler anymore so we may need to
5086 * remove back references to the session currently being dumped.
5087 */
Simon Horman74d88312013-02-12 10:45:50 +09005088static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005089{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005090 struct appctx *appctx = __objt_appctx(si->end);
5091
5092 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5093 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5094 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005095 }
5096}
5097
Willy Tarreau20e99322013-04-13 09:22:25 +02005098/* This function is used to either dump tables states (when action is set
5099 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005100 * It returns 0 if the output buffer is full and it needs to be called
5101 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005102 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005103static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005104{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005105 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005106 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005107 struct ebmb_node *eb;
5108 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005109 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005110 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005111
5112 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005113 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005114 * - STAT_ST_INIT : the first call
5115 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005116 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005117 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005118 * and the entry pointer points to the next entry to be dumped,
5119 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005120 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005121 * data though.
5122 */
5123
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005124 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005125 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005126 if (appctx->st2 == STAT_ST_LIST) {
5127 appctx->ctx.table.entry->ref_cnt--;
5128 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005129 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005130 return 1;
5131 }
5132
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005133 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005134
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005135 while (appctx->st2 != STAT_ST_FIN) {
5136 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005137 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 appctx->ctx.table.proxy = appctx->ctx.table.target;
5139 if (!appctx->ctx.table.proxy)
5140 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005141
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005142 appctx->ctx.table.entry = NULL;
5143 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005144 break;
5145
Willy Tarreau295a8372011-03-10 11:25:07 +01005146 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005147 if (!appctx->ctx.table.proxy ||
5148 (appctx->ctx.table.target &&
5149 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5150 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005151 break;
5152 }
5153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005154 if (appctx->ctx.table.proxy->table.size) {
5155 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5156 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005157 return 0;
5158
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005159 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005160 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005161 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005162 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005163 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005164 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5165 appctx->ctx.table.entry->ref_cnt++;
5166 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005167 break;
5168 }
5169 }
5170 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005171 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005172 break;
5173
Willy Tarreau295a8372011-03-10 11:25:07 +01005174 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005175 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005177 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005178 /* we're filtering on some data contents */
5179 void *ptr;
5180 long long data;
5181
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005182 dt = appctx->ctx.table.data_type;
5183 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5184 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005185 dt);
5186
5187 data = 0;
5188 switch (stktable_data_types[dt].std_type) {
5189 case STD_T_SINT:
5190 data = stktable_data_cast(ptr, std_t_sint);
5191 break;
5192 case STD_T_UINT:
5193 data = stktable_data_cast(ptr, std_t_uint);
5194 break;
5195 case STD_T_ULL:
5196 data = stktable_data_cast(ptr, std_t_ull);
5197 break;
5198 case STD_T_FRQP:
5199 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005200 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005201 break;
5202 }
5203
5204 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005205 if ((data < appctx->ctx.table.value &&
5206 (appctx->ctx.table.data_op == STD_OP_EQ ||
5207 appctx->ctx.table.data_op == STD_OP_GT ||
5208 appctx->ctx.table.data_op == STD_OP_GE)) ||
5209 (data == appctx->ctx.table.value &&
5210 (appctx->ctx.table.data_op == STD_OP_NE ||
5211 appctx->ctx.table.data_op == STD_OP_GT ||
5212 appctx->ctx.table.data_op == STD_OP_LT)) ||
5213 (data > appctx->ctx.table.value &&
5214 (appctx->ctx.table.data_op == STD_OP_EQ ||
5215 appctx->ctx.table.data_op == STD_OP_LT ||
5216 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005217 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005218 }
5219
Simon Hormanc88b8872011-06-15 15:18:49 +09005220 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005221 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5222 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005223 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005224
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005225 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005226
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005227 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005228 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005229 struct stksess *old = appctx->ctx.table.entry;
5230 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005231 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005232 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5233 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5234 stksess_kill(&appctx->ctx.table.proxy->table, old);
5235 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005236 break;
5237 }
5238
Simon Hormanc88b8872011-06-15 15:18:49 +09005239
5240 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005241 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5242 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5243 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005244
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005245 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5246 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005247 break;
5248
Willy Tarreau295a8372011-03-10 11:25:07 +01005249 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005250 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005251 break;
5252 }
5253 }
5254 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005255}
5256
Willy Tarreaud426a182010-03-05 14:58:26 +01005257/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005258 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5259 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5260 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5261 * lines are respected within the limit of 70 output chars. Lines that are
5262 * continuation of a previous truncated line begin with "+" instead of " "
5263 * after the offset. The new pointer is returned.
5264 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005265static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5266 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005267{
5268 int end;
5269 unsigned char c;
5270
5271 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005272 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005273 return ptr;
5274
Willy Tarreau77804732012-10-29 16:14:26 +01005275 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005276
Willy Tarreaud426a182010-03-05 14:58:26 +01005277 while (ptr < len && ptr < bsize) {
5278 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005279 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005280 if (out->len > end - 2)
5281 break;
5282 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005283 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005284 if (out->len > end - 3)
5285 break;
5286 out->str[out->len++] = '\\';
5287 switch (c) {
5288 case '\t': c = 't'; break;
5289 case '\n': c = 'n'; break;
5290 case '\r': c = 'r'; break;
5291 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005292 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005293 }
5294 out->str[out->len++] = c;
5295 } else {
5296 if (out->len > end - 5)
5297 break;
5298 out->str[out->len++] = '\\';
5299 out->str[out->len++] = 'x';
5300 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5301 out->str[out->len++] = hextab[c & 0xF];
5302 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005303 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005304 /* we had a line break, let's return now */
5305 out->str[out->len++] = '\n';
5306 *line = ptr;
5307 return ptr;
5308 }
5309 }
5310 /* we have an incomplete line, we return it as-is */
5311 out->str[out->len++] = '\n';
5312 return ptr;
5313}
5314
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005315/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005316 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005317 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005318 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005319static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005320{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005321 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005322 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005323
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005324 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005325 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005326
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005327 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005328
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005329 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005330 /* the function had not been called yet, let's prepare the
5331 * buffer for a response.
5332 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005333 struct tm tm;
5334
5335 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005336 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005337 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5338 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5339 error_snapshot_id);
5340
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005341 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005342 /* Socket buffer full. Let's try again later from the same point */
5343 return 0;
5344 }
5345
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005346 appctx->ctx.errors.px = proxy;
5347 appctx->ctx.errors.buf = 0;
5348 appctx->ctx.errors.bol = 0;
5349 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005350 }
5351
5352 /* we have two inner loops here, one for the proxy, the other one for
5353 * the buffer.
5354 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005355 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005356 struct error_snapshot *es;
5357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005358 if (appctx->ctx.errors.buf == 0)
5359 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005360 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005361 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005362
5363 if (!es->when.tv_sec)
5364 goto next;
5365
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005366 if (appctx->ctx.errors.iid >= 0 &&
5367 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5368 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005369 goto next;
5370
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005371 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005372 /* just print headers now */
5373
5374 char pn[INET6_ADDRSTRLEN];
5375 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005376 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005377
5378 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005379 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005380 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005381 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005382
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005383 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5384 case AF_INET:
5385 case AF_INET6:
5386 port = get_host_port(&es->src);
5387 break;
5388 default:
5389 port = 0;
5390 }
5391
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005392 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005393 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005394 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005395 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005396 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005397 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005398 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5399 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005400 break;
5401 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005402 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005403 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005404 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005405 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005406 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005407 break;
5408 }
5409
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005410 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005411 ", server %s (#%d), event #%u\n"
5412 " src %s:%d, session #%d, session flags 0x%08x\n"
5413 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5414 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5415 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5416 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5417 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5418 es->ev_id,
5419 pn, port, es->sid, es->s_flags,
5420 es->state, es->m_flags, es->t_flags,
5421 es->m_clen, es->m_blen,
5422 es->b_flags, es->b_out, es->b_tot,
5423 es->len, es->b_wrap, es->pos);
5424
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005425 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005426 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005427 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005428 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005429 appctx->ctx.errors.ptr = 0;
5430 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005431 }
5432
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005433 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005434 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005435 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005436 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005437 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005438 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005439 goto next;
5440 }
5441
5442 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005443 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005444 int newptr;
5445 int newline;
5446
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005447 newline = appctx->ctx.errors.bol;
5448 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5449 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005450 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005451
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005452 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005453 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005454 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005455 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005456 appctx->ctx.errors.ptr = newptr;
5457 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005458 };
5459 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005460 appctx->ctx.errors.bol = 0;
5461 appctx->ctx.errors.ptr = -1;
5462 appctx->ctx.errors.buf++;
5463 if (appctx->ctx.errors.buf > 1) {
5464 appctx->ctx.errors.buf = 0;
5465 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005466 }
5467 }
5468
5469 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005470 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005471}
5472
Willy Tarreaud5781202012-09-22 19:32:35 +02005473/* parse the "level" argument on the bind lines */
5474static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5475{
5476 if (!*args[cur_arg + 1]) {
5477 memprintf(err, "'%s' : missing level", args[cur_arg]);
5478 return ERR_ALERT | ERR_FATAL;
5479 }
5480
5481 if (!strcmp(args[cur_arg+1], "user"))
5482 conf->level = ACCESS_LVL_USER;
5483 else if (!strcmp(args[cur_arg+1], "operator"))
5484 conf->level = ACCESS_LVL_OPER;
5485 else if (!strcmp(args[cur_arg+1], "admin"))
5486 conf->level = ACCESS_LVL_ADMIN;
5487 else {
5488 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5489 args[cur_arg], args[cur_arg+1]);
5490 return ERR_ALERT | ERR_FATAL;
5491 }
5492
5493 return 0;
5494}
5495
Willy Tarreaub24281b2011-02-13 13:16:36 +01005496struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005497 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005498 .name = "<STATS>", /* used for logging */
5499 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005500 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005501};
5502
Simon Horman9bd2c732011-06-15 15:18:44 +09005503static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005504 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005505 .name = "<CLI>", /* used for logging */
5506 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005507 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005508};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005509
Willy Tarreaudc13c112013-06-21 23:16:39 +02005510static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005511 { CFG_GLOBAL, "stats", stats_parse_global },
5512 { 0, NULL, NULL },
5513}};
5514
Willy Tarreaud5781202012-09-22 19:32:35 +02005515static struct bind_kw_list bind_kws = { "STAT", { }, {
5516 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5517 { NULL, NULL, 0 },
5518}};
5519
Willy Tarreau10522fd2008-07-09 20:12:41 +02005520__attribute__((constructor))
5521static void __dumpstats_module_init(void)
5522{
5523 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005524 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005525}
5526
Willy Tarreau91861262007-10-17 17:06:05 +02005527/*
5528 * Local variables:
5529 * c-indent-level: 8
5530 * c-basic-offset: 8
5531 * End:
5532 */