blob: bded9079aa25fbe0413326d54e7387409a7fc28a [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;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001558 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001559 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001560 }
1561 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001562 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001563 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001564 }
1565 }
1566
Willy Tarreau532a4502011-09-07 22:37:44 +02001567 return 1;
1568 }
1569 else if (strcmp(args[1], "frontend") == 0) {
1570 struct proxy *px;
1571
1572 px = expect_frontend_admin(s, si, args[2]);
1573 if (!px)
1574 return 1;
1575
1576 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001577 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1578 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001579 return 1;
1580 }
1581
1582 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001583 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1584 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001585 return 1;
1586 }
1587
1588 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001589 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1590 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001591 return 1;
1592 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001593 return 1;
1594 }
1595 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001596 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1597 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001598 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001599 }
1600 }
1601 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001602 if (strcmp(args[1], "agent") == 0) {
1603 struct server *sv;
1604
1605 sv = expect_server_admin(s, si, args[2]);
1606 if (!sv)
1607 return 1;
1608
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001609 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001610 return 1;
1611 }
1612 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001613 struct server *sv;
1614
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001615 sv = expect_server_admin(s, si, args[2]);
1616 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001617 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001618
Cyril Bontécd19e512010-01-31 22:34:03 +01001619 if (! (sv->state & SRV_MAINTAIN)) {
1620 /* Not already in maintenance, we can change the server state */
1621 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001622 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001623 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001624 }
1625
Willy Tarreau532a4502011-09-07 22:37:44 +02001626 return 1;
1627 }
1628 else if (strcmp(args[1], "frontend") == 0) {
1629 struct proxy *px;
1630
1631 px = expect_frontend_admin(s, si, args[2]);
1632 if (!px)
1633 return 1;
1634
1635 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001636 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1637 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001638 return 1;
1639 }
1640
1641 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001642 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1643 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001644 return 1;
1645 }
1646
1647 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001648 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1649 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001650 return 1;
1651 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001652 return 1;
1653 }
1654 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001655 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1656 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001657 return 1;
1658 }
1659 }
1660 else if (strcmp(args[0], "shutdown") == 0) {
1661 if (strcmp(args[1], "frontend") == 0) {
1662 struct proxy *px;
1663
1664 px = expect_frontend_admin(s, si, args[2]);
1665 if (!px)
1666 return 1;
1667
1668 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001669 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1670 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001671 return 1;
1672 }
1673
1674 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 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1677 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1678 stop_proxy(px);
1679 return 1;
1680 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001681 else if (strcmp(args[1], "session") == 0) {
1682 struct session *sess, *ptr;
1683
Willy Tarreau290e63a2012-09-20 18:07:14 +02001684 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->ctx.cli.msg = stats_permission_denied_msg;
1686 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001687 return 1;
1688 }
1689
1690 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001691 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001693 return 1;
1694 }
1695
1696 ptr = (void *)strtoul(args[2], NULL, 0);
1697
1698 /* first, look for the requested session in the session table */
1699 list_for_each_entry(sess, &sessions, list) {
1700 if (sess == ptr)
1701 break;
1702 }
1703
1704 /* do we have the session ? */
1705 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001706 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1707 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001708 return 1;
1709 }
1710
1711 session_shutdown(sess, SN_ERR_KILLED);
1712 return 1;
1713 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001714 else if (strcmp(args[1], "sessions") == 0) {
1715 if (strcmp(args[2], "server") == 0) {
1716 struct server *sv;
1717 struct session *sess, *sess_bck;
1718
1719 sv = expect_server_admin(s, si, args[3]);
1720 if (!sv)
1721 return 1;
1722
1723 /* kill all the session that are on this server */
1724 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1725 if (sess->srv_conn == sv)
1726 session_shutdown(sess, SN_ERR_KILLED);
1727
1728 return 1;
1729 }
1730 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001731 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1732 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001733 return 1;
1734 }
1735 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001736 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001737 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1738 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001739 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001740 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001741 }
1742 else if (strcmp(args[0], "del") == 0) {
1743 if (strcmp(args[1], "map") == 0) {
1744 struct pattern *pat_elt;
1745 struct pat_idx_elt *idx_elt;
1746 struct map_entry *ent;
1747
1748 /* Expect two parameters: map name and key. */
1749 if (!*args[2] || !*args[3]) {
1750 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1751 appctx->st0 = STAT_CLI_PRINT;
1752 return 1;
1753 }
1754
1755 /* Lookup the reference in the maps. */
1756 appctx->ctx.map.ref = map_get_reference(args[2]);
1757 if (!appctx->ctx.map.ref) {
1758 appctx->ctx.cli.msg = "Unknown map reference.\n";
1759 appctx->st0 = STAT_CLI_PRINT;
1760 return 1;
1761 }
1762
1763 /* Lookup the entry in the reference values.
1764 * If the entry is not found in the reference, return error message.
1765 */
1766 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1767 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1768 break;
1769
1770 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1771 appctx->ctx.cli.msg = "Entry not found.\n";
1772 appctx->st0 = STAT_CLI_PRINT;
1773 return 1;
1774 }
1775
1776 /* Delete each enties from reference. */
1777 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1778 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1779 LIST_DEL(&appctx->ctx.map.ent->list);
1780 free(appctx->ctx.map.ent->key);
1781 free(appctx->ctx.map.ent->value);
1782 free(appctx->ctx.map.ent);
1783 }
1784 }
1785
1786 /* Delete all matching entries for each map descritor. */
1787 appctx->ctx.map.desc = NULL;
1788 stats_map_lookup_next(si);
1789 while (appctx->ctx.map.desc) {
1790 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1791 if (pat_elt != NULL) {
1792 LIST_DEL(&pat_elt->list);
1793 pattern_free(pat_elt);
1794 }
1795 if (idx_elt != NULL) {
1796 ebmb_delete(&idx_elt->node);
1797 free(idx_elt);
1798 }
1799 }
1800 stats_map_lookup_next(si);
1801 }
1802
1803 /* The deletion is done, send message. */
1804 appctx->ctx.cli.msg = "Done.\n";
1805 appctx->st0 = STAT_CLI_PRINT;
1806 return 1;
1807 }
1808 else { /* unknown "del" parameter */
1809 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1810 appctx->st0 = STAT_CLI_PRINT;
1811 return 1;
1812 }
1813 }
1814 else if (strcmp(args[0], "add") == 0) {
1815 if (strcmp(args[1], "map") == 0) {
1816 const char *params[2];
1817 struct pattern *pat;
1818 struct map_entry *ent;
1819 struct sample_storage *smp;
1820
1821 /* Expect three parameters: map name, key and new value. */
1822 if (!*args[2] || !*args[3] || !*args[4]) {
1823 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1824 appctx->st0 = STAT_CLI_PRINT;
1825 return 1;
1826 }
1827
1828 params[0] = args[3];
1829 params[1] = "";
1830
1831 /* Lookup the reference in the maps. */
1832 appctx->ctx.map.ref = map_get_reference(args[2]);
1833 if (!appctx->ctx.map.ref) {
1834 appctx->ctx.cli.msg = "Unknown map reference.\n";
1835 appctx->st0 = STAT_CLI_PRINT;
1836 return 1;
1837 }
1838
1839 /* Prepare and link the new map_entry element. If out of memory
1840 * error the action is cancelled and the descriptor are left
1841 * coherents.
1842 */
1843 ent = malloc(sizeof(*ent));
1844 if (!ent) {
1845 appctx->ctx.cli.msg = "Out of memory error.\n";
1846 appctx->st0 = STAT_CLI_PRINT;
1847 return 1;
1848 }
1849 ent->key = strdup(args[3]);
1850 if (!ent->key) {
1851 free(ent);
1852 appctx->ctx.cli.msg = "Out of memory error.\n";
1853 appctx->st0 = STAT_CLI_PRINT;
1854 return 1;
1855 }
1856 ent->value = strdup(args[4]);
1857 if (!ent->value) {
1858 free(ent->key);
1859 free(ent);
1860 appctx->ctx.cli.msg = "Out of memory error.\n";
1861 appctx->st0 = STAT_CLI_PRINT;
1862 return 1;
1863 }
1864 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1865
1866 /* Browse each map descritor and try to insert this new value. */
1867 appctx->ctx.map.desc = NULL;
1868 for (stats_map_lookup_next(si);
1869 appctx->ctx.map.desc;
1870 stats_map_lookup_next(si)) {
1871
1872 /* Create new sample. Return out of memory error
1873 * if the memory cannot be allocated. The 'add' process
1874 * is aborted, but the already inserted entries are not
1875 * deleted.
1876 */
1877 smp = calloc(1, sizeof(*smp));
1878 if (!smp) {
1879 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1880 appctx->st0 = STAT_CLI_PRINT;
1881 return 1;
1882 }
1883
1884 /* Create sample. If this function fails, the insertion
1885 * is canceled for this 'descriptor', but continue, for
1886 * the other descriptors.
1887 */
1888 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1889 free(smp);
1890 continue;
1891 }
1892
1893 /* If the value can be indexed, get the first pattern. If
1894 * the return entry is not the indexed entry, new 'pattern' is
1895 * created by the function pattern_register(). If the 'pattern'
1896 * is NULL, new entry is created. This is ugly because the
1897 * following code interfers with the own code of the function
1898 * pattern_register().
1899 */
1900 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
1901 appctx->ctx.map.desc->pat->match == pat_match_ip) {
1902 pat = LIST_NEXT(&appctx->ctx.map.desc->pat->patterns, struct pattern *, list);
1903 if (&pat->list == &appctx->ctx.map.desc->pat->patterns)
1904 pat = NULL;
1905 }
1906 else
1907 pat = NULL;
1908
1909 if (!pattern_register(appctx->ctx.map.desc->pat, params, smp, &pat, 0, NULL)) {
1910 free(smp);
1911 continue;
1912 }
1913 }
1914
1915 /* The add is done, send message. */
1916 appctx->ctx.cli.msg = "Done.\n";
1917 appctx->st0 = STAT_CLI_PRINT;
1918 return 1;
1919 }
1920 else { /* unknown "del" parameter */
1921 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
1922 appctx->st0 = STAT_CLI_PRINT;
1923 return 1;
1924 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001925 }
1926 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001927 return 0;
1928 }
1929 return 1;
1930}
1931
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001932/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001933 * used to processes I/O from/to the stats unix socket. The system relies on a
1934 * state machine handling requests and various responses. We read a request,
1935 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001936 * Then we can read again. The state is stored in appctx->st0 and is one of the
1937 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001938 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001939 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001940static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001941{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001942 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001943 struct channel *req = si->ob;
1944 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001945 int reql;
1946 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001947
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001948 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1949 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001950
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001951 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001952 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001953 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001954 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1955 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001956 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001957 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001958 /* Let's close for real now. We just close the request
1959 * side, the conditions below will complete if needed.
1960 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001961 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001962 break;
1963 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001964 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001965 /* ensure we have some output room left in the event we
1966 * would want to return some info right after parsing.
1967 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001968 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001969 break;
1970
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001971 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001972 if (reql <= 0) { /* closed or EOL not found */
1973 if (reql == 0)
1974 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001975 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001976 continue;
1977 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001978
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001979 /* seek for a possible semi-colon. If we find one, we
1980 * replace it with an LF and skip only this part.
1981 */
1982 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001983 if (trash.str[len] == ';') {
1984 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001985 reql = len + 1;
1986 break;
1987 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001988
Willy Tarreau816fc222009-10-04 07:36:58 +02001989 /* now it is time to check that we have a full line,
1990 * remove the trailing \n and possibly \r, then cut the
1991 * line.
1992 */
1993 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001994 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001995 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001996 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001997 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001998
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001999 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002000 len--;
2001
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002002 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002003
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002004 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002005 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002006 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002007 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002008 continue;
2009 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002010 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002011 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002012 else if (strcmp(trash.str, "help") == 0 ||
2013 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002014 appctx->ctx.cli.msg = stats_sock_usage_msg;
2015 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002016 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002017 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002018 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002019 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002020 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002021 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002022 /* if prompt is disabled, print help on empty lines,
2023 * so that the user at least knows how to enable
2024 * prompt and find help.
2025 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002026 appctx->ctx.cli.msg = stats_sock_usage_msg;
2027 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002028 }
2029
2030 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002031 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002032 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002033 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002034 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002035 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002036 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002037 continue;
2038 }
2039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002040 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002041 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002042 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2043 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002044 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002045 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002046 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002047 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002048 break;
2049 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002050 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002051 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002052 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002053 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002054 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002055 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002056 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002057 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002058 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002059 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002060 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002061 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002062 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002063 if (stats_table_request(si, appctx->st0))
2064 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002065 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002066 case STAT_CLI_O_MAPS:
2067 if (stats_maps_list(si))
2068 appctx->st0 = STAT_CLI_PROMPT;
2069 break;
2070 case STAT_CLI_O_MAP:
2071 if (stats_map_list(si))
2072 appctx->st0 = STAT_CLI_PROMPT;
2073 break;
2074 case STAT_CLI_O_MLOOK:
2075 if (stats_map_lookup(si))
2076 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002077 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002078 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002079 break;
2080 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002081
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002082 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002083 if (appctx->st0 == STAT_CLI_PROMPT) {
2084 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2085 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002086 }
2087
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002088 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002089 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002090 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002091
2092 /* Now we close the output if one of the writers did so,
2093 * or if we're not in interactive mode and the request
2094 * buffer is empty. This still allows pipelined requests
2095 * to be sent in non-interactive mode.
2096 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002097 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2098 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002099 continue;
2100 }
2101
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002102 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002103 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002104 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002105 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002107 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002108 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2109 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002110 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002111 * and nothing more to consume. This is comparable to a broken pipe, so
2112 * we forward the close to the request side so that it flows upstream to
2113 * the client.
2114 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002115 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002116 }
2117
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002118 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002119 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2120 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2121 /* We have no more processing to do, and nothing more to send, and
2122 * the client side has closed. So we'll forward this state downstream
2123 * on the response buffer.
2124 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002125 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002126 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002127 }
2128
2129 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002130 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002131
2132 /* we don't want to expire timeouts while we're processing requests */
2133 si->ib->rex = TICK_ETERNITY;
2134 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002135
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002136 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002137 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 +02002138 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002139 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 +02002140
2141 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2142 /* check that we have released everything then unregister */
2143 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002144 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002145}
2146
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002147/* This function dumps information onto the stream interface's read buffer.
2148 * It returns 0 as long as it does not complete, non-zero upon completion.
2149 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002150 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002151static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002152{
2153 unsigned int up = (now.tv_sec - start_date.tv_sec);
2154
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002155 chunk_printf(&trash,
2156 "Name: " PRODUCT_NAME "\n"
2157 "Version: " HAPROXY_VERSION "\n"
2158 "Release_date: " HAPROXY_DATE "\n"
2159 "Nbproc: %d\n"
2160 "Process_num: %d\n"
2161 "Pid: %d\n"
2162 "Uptime: %dd %dh%02dm%02ds\n"
2163 "Uptime_sec: %d\n"
2164 "Memmax_MB: %d\n"
2165 "Ulimit-n: %d\n"
2166 "Maxsock: %d\n"
2167 "Maxconn: %d\n"
2168 "Hard_maxconn: %d\n"
2169 "Maxpipes: %d\n"
2170 "CurrConns: %d\n"
2171 "PipesUsed: %d\n"
2172 "PipesFree: %d\n"
2173 "ConnRate: %d\n"
2174 "ConnRateLimit: %d\n"
2175 "MaxConnRate: %d\n"
2176 "CompressBpsIn: %u\n"
2177 "CompressBpsOut: %u\n"
2178 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002179#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002180 "ZlibMemUsage: %ld\n"
2181 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002182#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002183 "Tasks: %d\n"
2184 "Run_queue: %d\n"
2185 "Idle_pct: %d\n"
2186 "node: %s\n"
2187 "description: %s\n"
2188 "",
2189 global.nbproc,
2190 relative_pid,
2191 pid,
2192 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2193 up,
2194 global.rlimit_memmax,
2195 global.rlimit_nofile,
2196 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
2197 actconn, pipes_used, pipes_free,
2198 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
2199 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2200 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002201#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002202 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002203#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002204 nb_tasks_cur, run_queue_cur, idle_pct,
2205 global.node, global.desc ? global.desc : ""
2206 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002207
2208 if (bi_putchk(si->ib, &trash) == -1)
2209 return 0;
2210
2211 return 1;
2212}
2213
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002214/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2215 * the state from stream interface <si>. The caller is responsible for clearing
2216 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002217 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002218static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002219{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002220 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002221 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002222
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002223 if (!(px->cap & PR_CAP_FE))
2224 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002225
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002227 return 0;
2228
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002229 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002230 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002231 /* name, queue */
2232 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002233
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002234 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002235 /* Column sub-heading for Enable or Disable server */
2236 chunk_appendf(&trash, "<td></td>");
2237 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002238
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002239 chunk_appendf(&trash,
2240 "<td class=ac>"
2241 "<a name=\"%s/Frontend\"></a>"
2242 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2243 "<td colspan=3></td>"
2244 "",
2245 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002246
Willy Tarreau466c9b52012-12-23 02:25:03 +01002247 chunk_appendf(&trash,
2248 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002249 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002250 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2251 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2252 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002253 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2254 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2255 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002256
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002257 if (px->mode == PR_MODE_HTTP)
2258 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002259 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002260 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002261
2262 chunk_appendf(&trash,
2263 "</table></div></u></td>"
2264 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002265 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002266 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2267 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2268 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002269 U2H(px->fe_counters.sps_max),
2270 U2H(px->fe_counters.cps_max),
2271 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002272
2273 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002274 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002275 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002276 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002277
2278 chunk_appendf(&trash,
2279 "</table></div></u></td>"
2280 /* sessions rate : limit */
2281 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002282 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002283
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002284 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002285 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002286 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002287 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002288 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2289 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002290 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002291 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2292 U2H(px->fe_counters.cum_sess),
2293 U2H(px->fe_counters.cum_conn),
2294 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002295
Willy Tarreau466c9b52012-12-23 02:25:03 +01002296 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002297 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002298 chunk_appendf(&trash,
2299 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2300 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2301 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2302 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2303 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2304 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2305 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2306 "<tr><th>- other responses:</th><td>%s</td></tr>"
2307 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2308 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002309 U2H(px->fe_counters.p.http.cum_req),
2310 U2H(px->fe_counters.p.http.rsp[1]),
2311 U2H(px->fe_counters.p.http.rsp[2]),
2312 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002313 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002314 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002315 U2H(px->fe_counters.p.http.rsp[3]),
2316 U2H(px->fe_counters.p.http.rsp[4]),
2317 U2H(px->fe_counters.p.http.rsp[5]),
2318 U2H(px->fe_counters.p.http.rsp[0]),
2319 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002320 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002321
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002322 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002323 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002324 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002325 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002326 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002327 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002328 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002329 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002330
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002331 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002332 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002333 "<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 +01002334 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002335 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002336 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2337 px->fe_counters.comp_in ?
2338 (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 +01002339 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002340
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002341 chunk_appendf(&trash,
2342 /* denied: req, resp */
2343 "<td>%s</td><td>%s</td>"
2344 /* errors : request, connect, response */
2345 "<td>%s</td><td></td><td></td>"
2346 /* warnings: retries, redispatches */
2347 "<td></td><td></td>"
2348 /* server status : reflect frontend status */
2349 "<td class=ac>%s</td>"
2350 /* rest of server: nothing */
2351 "<td class=ac colspan=8></td></tr>"
2352 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002353 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2354 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002355 px->state == PR_STREADY ? "OPEN" :
2356 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002357 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002358 else { /* CSV mode */
2359 chunk_appendf(&trash,
2360 /* pxid, name, queue cur, queue max, */
2361 "%s,FRONTEND,,,"
2362 /* sessions : current, max, limit, total */
2363 "%d,%d,%d,%lld,"
2364 /* bytes : in, out */
2365 "%lld,%lld,"
2366 /* denied: req, resp */
2367 "%lld,%lld,"
2368 /* errors : request, connect, response */
2369 "%lld,,,"
2370 /* warnings: retries, redispatches */
2371 ",,"
2372 /* server status : reflect frontend status */
2373 "%s,"
2374 /* rest of server: nothing */
2375 ",,,,,,,,"
2376 /* pid, iid, sid, throttle, lbtot, tracked, type */
2377 "%d,%d,0,,,,%d,"
2378 /* rate, rate_lim, rate_max */
2379 "%u,%u,%u,"
2380 /* check_status, check_code, check_duration */
2381 ",,,",
2382 px->id,
2383 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2384 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2385 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2386 px->fe_counters.failed_req,
2387 px->state == PR_STREADY ? "OPEN" :
2388 px->state == PR_STFULL ? "FULL" : "STOP",
2389 relative_pid, px->uuid, STATS_TYPE_FE,
2390 read_freq_ctr(&px->fe_sess_per_sec),
2391 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002392
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002393 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2394 if (px->mode == PR_MODE_HTTP) {
2395 for (i=1; i<6; i++)
2396 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2397 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2398 }
2399 else
2400 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002401
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002402 /* failed health analyses */
2403 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002404
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002405 /* requests : req_rate, req_rate_max, req_tot, */
2406 chunk_appendf(&trash, "%u,%u,%lld,",
2407 read_freq_ctr(&px->fe_req_per_sec),
2408 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2409
2410 /* errors: cli_aborts, srv_aborts */
2411 chunk_appendf(&trash, ",,");
2412
2413 /* compression: in, out, bypassed */
2414 chunk_appendf(&trash, "%lld,%lld,%lld,",
2415 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2416
2417 /* compression: comp_rsp */
2418 chunk_appendf(&trash, "%lld,",
2419 px->fe_counters.p.http.comp_rsp);
2420
2421 /* finish with EOL */
2422 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002423 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002424 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002425}
2426
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002427/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2428 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2429 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2430 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002431 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002432static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002433{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002434 struct appctx *appctx = __objt_appctx(si->end);
2435
2436 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002437 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002438 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002439 /* Column sub-heading for Enable or Disable server */
2440 chunk_appendf(&trash, "<td></td>");
2441 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002442 chunk_appendf(&trash,
2443 /* frontend name, listener name */
2444 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2445 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2446 "",
2447 px->id, l->name,
2448 (flags & ST_SHLGNDS)?"<u>":"",
2449 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002450
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002451 if (flags & ST_SHLGNDS) {
2452 char str[INET6_ADDRSTRLEN];
2453 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002454
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002455 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002456
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002457 port = get_host_port(&l->addr);
2458 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2459 case AF_INET:
2460 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2461 break;
2462 case AF_INET6:
2463 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2464 break;
2465 case AF_UNIX:
2466 chunk_appendf(&trash, "unix, ");
2467 break;
2468 case -1:
2469 chunk_appendf(&trash, "(%s), ", strerror(errno));
2470 break;
2471 }
Willy Tarreau91861262007-10-17 17:06:05 +02002472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002473 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002474 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002475 }
Willy Tarreau91861262007-10-17 17:06:05 +02002476
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002477 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002478 /* queue */
2479 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002480 /* sessions rate: current, max, limit */
2481 "<td colspan=3>&nbsp;</td>"
2482 /* sessions: current, max, limit, total, lbtot */
2483 "<td>%s</td><td>%s</td><td>%s</td>"
2484 "<td>%s</td><td>&nbsp;</td>"
2485 /* bytes: in, out */
2486 "<td>%s</td><td>%s</td>"
2487 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002488 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002489 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2490 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002492 chunk_appendf(&trash,
2493 /* denied: req, resp */
2494 "<td>%s</td><td>%s</td>"
2495 /* errors: request, connect, response */
2496 "<td>%s</td><td></td><td></td>"
2497 /* warnings: retries, redispatches */
2498 "<td></td><td></td>"
2499 /* server status: reflect listener status */
2500 "<td class=ac>%s</td>"
2501 /* rest of server: nothing */
2502 "<td class=ac colspan=8></td></tr>"
2503 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002504 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2505 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002506 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2507 }
2508 else { /* CSV mode */
2509 chunk_appendf(&trash,
2510 /* pxid, name, queue cur, queue max, */
2511 "%s,%s,,,"
2512 /* sessions: current, max, limit, total */
2513 "%d,%d,%d,%lld,"
2514 /* bytes: in, out */
2515 "%lld,%lld,"
2516 /* denied: req, resp */
2517 "%lld,%lld,"
2518 /* errors: request, connect, response */
2519 "%lld,,,"
2520 /* warnings: retries, redispatches */
2521 ",,"
2522 /* server status: reflect listener status */
2523 "%s,"
2524 /* rest of server: nothing */
2525 ",,,,,,,,"
2526 /* pid, iid, sid, throttle, lbtot, tracked, type */
2527 "%d,%d,%d,,,,%d,"
2528 /* rate, rate_lim, rate_max */
2529 ",,,"
2530 /* check_status, check_code, check_duration */
2531 ",,,"
2532 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2533 ",,,,,,"
2534 /* failed health analyses */
2535 ","
2536 /* requests : req_rate, req_rate_max, req_tot, */
2537 ",,,"
2538 /* errors: cli_aborts, srv_aborts */
2539 ",,"
2540 /* compression: in, out, bypassed, comp_rsp */
2541 ",,,,"
2542 "\n",
2543 px->id, l->name,
2544 l->nbconn, l->counters->conn_max,
2545 l->maxconn, l->counters->cum_conn,
2546 l->counters->bytes_in, l->counters->bytes_out,
2547 l->counters->denied_req, l->counters->denied_resp,
2548 l->counters->failed_req,
2549 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2550 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2551 }
2552 return 1;
2553}
Willy Tarreau91861262007-10-17 17:06:05 +02002554
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2556 * from stream interface <si>, stats flags <flags>, and server state <state>.
2557 * The caller is responsible for clearing the trash if needed. Returns non-zero
2558 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002559 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2560 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002561 */
2562static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2563{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002564 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002565 struct server *ref = sv->track ? sv->track : sv;
2566 char str[INET6_ADDRSTRLEN];
2567 struct chunk src;
2568 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002569
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002570 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002571 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 "DOWN",
2573 "DN %d/%d &uarr;",
2574 "UP %d/%d &darr;",
2575 "UP",
2576 "NOLB %d/%d &darr;",
2577 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002578 "DRAIN %d/%d &darr;",
2579 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580 "<i>no check</i>"
2581 };
Willy Tarreau91861262007-10-17 17:06:05 +02002582
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2584 chunk_appendf(&trash, "<tr class=\"maintain\">");
2585 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002586 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002587 "<tr class=\"%s%d\">",
2588 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002589
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002590 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002591 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002592 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2593 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002594
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002595 chunk_appendf(&trash,
2596 "<td class=ac><a name=\"%s/%s\"></a>%s"
2597 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2598 "",
2599 px->id, sv->id,
2600 (flags & ST_SHLGNDS) ? "<u>" : "",
2601 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002602
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002603 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002604 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002605
2606 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2607 case AF_INET:
2608 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2609 break;
2610 case AF_INET6:
2611 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2612 break;
2613 case AF_UNIX:
2614 chunk_appendf(&trash, "unix, ");
2615 break;
2616 case -1:
2617 chunk_appendf(&trash, "(%s), ", strerror(errno));
2618 break;
2619 default: /* address family not supported */
2620 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002621 }
Willy Tarreau91861262007-10-17 17:06:05 +02002622
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002623 /* id */
2624 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002625
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 /* cookie */
2627 if (sv->cookie) {
2628 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002629
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002630 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2631 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002632
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002633 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634 }
2635
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002636 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002637 }
Willy Tarreau91861262007-10-17 17:06:05 +02002638
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002639 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002640 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002641 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002642 /* sessions rate : current, max, limit */
2643 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002644 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002646 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2647 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002648
Willy Tarreau466c9b52012-12-23 02:25:03 +01002649
2650 chunk_appendf(&trash,
2651 /* sessions: current, max, limit, total */
2652 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002653 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002654 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2655 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002656 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2657 U2H(sv->counters.cum_sess),
2658 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002659
Willy Tarreau466c9b52012-12-23 02:25:03 +01002660 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2661 if (px->mode == PR_MODE_HTTP) {
2662 unsigned long long tot;
2663 for (tot = i = 0; i < 6; i++)
2664 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002665
Willy Tarreau466c9b52012-12-23 02:25:03 +01002666 chunk_appendf(&trash,
2667 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2668 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2669 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2670 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2671 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2672 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2673 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2674 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002675 U2H(tot),
2676 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2677 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2678 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2679 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2680 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2681 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 +02002682 }
Willy Tarreau91861262007-10-17 17:06:05 +02002683
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002684 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002685 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002686 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002687 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002688 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002689
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002690 chunk_appendf(&trash,
2691 /* bytes : in, out */
2692 "<td>%s</td><td>%s</td>"
2693 /* denied: req, resp */
2694 "<td></td><td>%s</td>"
2695 /* errors : request, connect */
2696 "<td></td><td>%s</td>"
2697 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002698 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002699 /* warnings: retries, redispatches */
2700 "<td>%lld</td><td>%lld</td>"
2701 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002702 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2703 U2H(sv->counters.failed_secu),
2704 U2H(sv->counters.failed_conns),
2705 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002706 sv->counters.cli_aborts,
2707 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002708 sv->counters.retries, sv->counters.redispatches);
2709
2710 /* status, lest check */
2711 chunk_appendf(&trash, "<td class=ac>");
2712
2713 if (sv->state & SRV_MAINTAIN) {
2714 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2715 chunk_appendf(&trash, "MAINT");
2716 }
2717 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2718 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2719 chunk_appendf(&trash, "MAINT(via)");
2720 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002721 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002722 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2723 chunk_appendf(&trash,
2724 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002725 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2726 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002727 }
Willy Tarreau91861262007-10-17 17:06:05 +02002728
Willy Tarreauff5ae352013-12-11 20:36:34 +01002729 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002730 chunk_appendf(&trash,
2731 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002732 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002733 get_check_status_info(sv->check.status));
2734
2735 if (sv->check.status >= HCHK_STATUS_L57DATA)
2736 chunk_appendf(&trash, "/%d", sv->check.code);
2737
2738 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002739 chunk_appendf(&trash, " in %lums", sv->check.duration);
2740
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002741 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002742 get_check_status_description(sv->check.status));
2743 if (*sv->check.desc) {
2744 chunk_appendf(&trash, ": ");
2745 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2746 chunk_htmlencode(&trash, &src);
2747 }
2748 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 }
2750 else
2751 chunk_appendf(&trash, "</td><td>");
2752
2753 chunk_appendf(&trash,
2754 /* weight */
2755 "</td><td class=ac>%d</td>"
2756 /* act, bck */
2757 "<td class=ac>%s</td><td class=ac>%s</td>"
2758 "",
2759 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2760 (sv->state & SRV_BACKUP) ? "-" : "Y",
2761 (sv->state & SRV_BACKUP) ? "Y" : "-");
2762
2763 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002764 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002765 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002766
2767 if (ref->observe)
2768 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2769
2770 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002771 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002772 "<td>%lld</td><td>%s</td>"
2773 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002774 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2776 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002777 else if (sv != ref) {
2778 if (sv->state & SRV_MAINTAIN)
2779 chunk_appendf(&trash,
2780 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2781 ref->proxy->id, ref->id);
2782 else
2783 chunk_appendf(&trash,
2784 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2785 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2786 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 else
2788 chunk_appendf(&trash, "<td colspan=3></td>");
2789
2790 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002791 if (sv->state & SRV_WARMINGUP)
2792 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002793 else
2794 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2795 }
2796 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002797 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798 "DOWN,",
2799 "DOWN %d/%d,",
2800 "UP %d/%d,",
2801 "UP,",
2802 "NOLB %d/%d,",
2803 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002804 "DRAIN %d/%d,",
2805 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002806 "no check,"
2807 };
2808
2809 chunk_appendf(&trash,
2810 /* pxid, name */
2811 "%s,%s,"
2812 /* queue : current, max */
2813 "%d,%d,"
2814 /* sessions : current, max, limit, total */
2815 "%d,%d,%s,%lld,"
2816 /* bytes : in, out */
2817 "%lld,%lld,"
2818 /* denied: req, resp */
2819 ",%lld,"
2820 /* errors : request, connect, response */
2821 ",%lld,%lld,"
2822 /* warnings: retries, redispatches */
2823 "%lld,%lld,"
2824 "",
2825 px->id, sv->id,
2826 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002827 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002828 sv->counters.bytes_in, sv->counters.bytes_out,
2829 sv->counters.failed_secu,
2830 sv->counters.failed_conns, sv->counters.failed_resp,
2831 sv->counters.retries, sv->counters.redispatches);
2832
2833 /* status */
2834 if (sv->state & SRV_MAINTAIN)
2835 chunk_appendf(&trash, "MAINT,");
2836 else if (ref != sv && ref->state & SRV_MAINTAIN)
2837 chunk_appendf(&trash, "MAINT(via),");
2838 else
2839 chunk_appendf(&trash,
2840 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002841 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2842 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843
2844 chunk_appendf(&trash,
2845 /* weight, active, backup */
2846 "%d,%d,%d,"
2847 "",
2848 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2849 (sv->state & SRV_BACKUP) ? 0 : 1,
2850 (sv->state & SRV_BACKUP) ? 1 : 0);
2851
2852 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002853 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002854 chunk_appendf(&trash,
2855 "%lld,%lld,%d,%d,",
2856 sv->counters.failed_checks, sv->counters.down_trans,
2857 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2858 else
2859 chunk_appendf(&trash, ",,,,");
2860
2861 /* queue limit, pid, iid, sid, */
2862 chunk_appendf(&trash,
2863 "%s,"
2864 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002865 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002866 relative_pid, px->uuid, sv->puid);
2867
2868 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002869 if (sv->state & SRV_WARMINGUP)
2870 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002871
2872 /* sessions: lbtot */
2873 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2874
2875 /* tracked */
2876 if (sv->track)
2877 chunk_appendf(&trash, "%s/%s,",
2878 sv->track->proxy->id, sv->track->id);
2879 else
2880 chunk_appendf(&trash, ",");
2881
2882 /* type */
2883 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2884
2885 /* rate */
2886 chunk_appendf(&trash, "%u,,%u,",
2887 read_freq_ctr(&sv->sess_per_sec),
2888 sv->counters.sps_max);
2889
Willy Tarreauff5ae352013-12-11 20:36:34 +01002890 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002891 /* check_status */
2892 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2893
2894 /* check_code */
2895 if (sv->check.status >= HCHK_STATUS_L57DATA)
2896 chunk_appendf(&trash, "%u,", sv->check.code);
2897 else
2898 chunk_appendf(&trash, ",");
2899
2900 /* check_duration */
2901 if (sv->check.status >= HCHK_STATUS_CHECKED)
2902 chunk_appendf(&trash, "%lu,", sv->check.duration);
2903 else
2904 chunk_appendf(&trash, ",");
2905
2906 }
2907 else
2908 chunk_appendf(&trash, ",,,");
2909
2910 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2911 if (px->mode == PR_MODE_HTTP) {
2912 for (i=1; i<6; i++)
2913 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2914
2915 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2916 }
2917 else
2918 chunk_appendf(&trash, ",,,,,,");
2919
2920 /* failed health analyses */
2921 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2922
2923 /* requests : req_rate, req_rate_max, req_tot, */
2924 chunk_appendf(&trash, ",,,");
2925
2926 /* errors: cli_aborts, srv_aborts */
2927 chunk_appendf(&trash, "%lld,%lld,",
2928 sv->counters.cli_aborts, sv->counters.srv_aborts);
2929
2930 /* compression: in, out, bypassed, comp_rsp */
2931 chunk_appendf(&trash, ",,,,");
2932
2933 /* finish with EOL */
2934 chunk_appendf(&trash, "\n");
2935 }
2936 return 1;
2937}
2938
2939/* Dumps a line for backend <px> to the trash for and uses the state from stream
2940 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2941 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2942 */
2943static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2944{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002945 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946 struct chunk src;
2947 int i;
2948
2949 if (!(px->cap & PR_CAP_BE))
2950 return 0;
2951
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002952 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002953 return 0;
2954
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002955 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002957 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002958 /* Column sub-heading for Enable or Disable server */
2959 chunk_appendf(&trash, "<td></td>");
2960 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002961 chunk_appendf(&trash,
2962 "<td class=ac>"
2963 /* name */
2964 "%s<a name=\"%s/Backend\"></a>"
2965 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2966 "",
2967 (flags & ST_SHLGNDS)?"<u>":"",
2968 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002969
2970 if (flags & ST_SHLGNDS) {
2971 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002972 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2974
2975 /* cookie */
2976 if (px->cookie_name) {
2977 chunk_appendf(&trash, ", cookie: '");
2978 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2979 chunk_htmlencode(&trash, &src);
2980 chunk_appendf(&trash, "'");
2981 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002982 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 }
2984
2985 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002986 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002987 /* queue : current, max */
2988 "<td>%s</td><td>%s</td><td></td>"
2989 /* sessions rate : current, max, limit */
2990 "<td>%s</td><td>%s</td><td></td>"
2991 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002992 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002993 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2994 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995
2996 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002997 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002998 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002999 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003000 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003001 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003002 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3003 U2H(px->be_counters.cum_conn),
3004 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005
Willy Tarreau466c9b52012-12-23 02:25:03 +01003006 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003007 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003008 chunk_appendf(&trash,
3009 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3010 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3011 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3012 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3013 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3014 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3015 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3016 "<tr><th>- other responses:</th><td>%s</td></tr>"
3017 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3018 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003019 U2H(px->be_counters.p.http.cum_req),
3020 U2H(px->be_counters.p.http.rsp[1]),
3021 U2H(px->be_counters.p.http.rsp[2]),
3022 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003023 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003024 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003025 U2H(px->be_counters.p.http.rsp[3]),
3026 U2H(px->be_counters.p.http.rsp[4]),
3027 U2H(px->be_counters.p.http.rsp[5]),
3028 U2H(px->be_counters.p.http.rsp[0]),
3029 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 }
3031
3032 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003033 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003034 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01003035 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003036 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003037 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003038 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003039 U2H(px->be_counters.cum_lbconn),
3040 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003043 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003044 "<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 +01003045 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003046 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003047 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3048 px->be_counters.comp_in ?
3049 (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 +01003050 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3051
3052 chunk_appendf(&trash,
3053 /* denied: req, resp */
3054 "<td>%s</td><td>%s</td>"
3055 /* errors : request, connect */
3056 "<td></td><td>%s</td>"
3057 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003058 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003059 /* warnings: retries, redispatches */
3060 "<td>%lld</td><td>%lld</td>"
3061 /* backend status: reflect backend status (up/down): we display UP
3062 * if the backend has known working servers or if it has no server at
3063 * all (eg: for stats). Then we display the total weight, number of
3064 * active and backups. */
3065 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3066 "<td class=ac>%d</td><td class=ac>%d</td>"
3067 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003068 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3069 U2H(px->be_counters.failed_conns),
3070 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003071 px->be_counters.cli_aborts,
3072 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003073 px->be_counters.retries, px->be_counters.redispatches,
3074 human_time(now.tv_sec - px->last_change, 1),
3075 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3076 "<font color=\"red\"><b>DOWN</b></font>",
3077 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3078 px->srv_act, px->srv_bck);
3079
3080 chunk_appendf(&trash,
3081 /* rest of backend: nothing, down transitions, total downtime, throttle */
3082 "<td class=ac>&nbsp;</td><td>%d</td>"
3083 "<td>%s</td>"
3084 "<td></td>"
3085 "</tr>",
3086 px->down_trans,
3087 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3088 }
3089 else { /* CSV mode */
3090 chunk_appendf(&trash,
3091 /* pxid, name */
3092 "%s,BACKEND,"
3093 /* queue : current, max */
3094 "%d,%d,"
3095 /* sessions : current, max, limit, total */
3096 "%d,%d,%d,%lld,"
3097 /* bytes : in, out */
3098 "%lld,%lld,"
3099 /* denied: req, resp */
3100 "%lld,%lld,"
3101 /* errors : request, connect, response */
3102 ",%lld,%lld,"
3103 /* warnings: retries, redispatches */
3104 "%lld,%lld,"
3105 /* backend status: reflect backend status (up/down): we display UP
3106 * if the backend has known working servers or if it has no server at
3107 * all (eg: for stats). Then we display the total weight, number of
3108 * active and backups. */
3109 "%s,"
3110 "%d,%d,%d,"
3111 /* rest of backend: nothing, down transitions, last change, total downtime */
3112 ",%d,%d,%d,,"
3113 /* pid, iid, sid, throttle, lbtot, tracked, type */
3114 "%d,%d,0,,%lld,,%d,"
3115 /* rate, rate_lim, rate_max, */
3116 "%u,,%u,"
3117 /* check_status, check_code, check_duration */
3118 ",,,",
3119 px->id,
3120 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3121 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3122 px->be_counters.bytes_in, px->be_counters.bytes_out,
3123 px->be_counters.denied_req, px->be_counters.denied_resp,
3124 px->be_counters.failed_conns, px->be_counters.failed_resp,
3125 px->be_counters.retries, px->be_counters.redispatches,
3126 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3127 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3128 px->srv_act, px->srv_bck,
3129 px->down_trans, (int)(now.tv_sec - px->last_change),
3130 px->srv?be_downtime(px):0,
3131 relative_pid, px->uuid,
3132 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3133 read_freq_ctr(&px->be_sess_per_sec),
3134 px->be_counters.sps_max);
3135
3136 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3137 if (px->mode == PR_MODE_HTTP) {
3138 for (i=1; i<6; i++)
3139 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3140 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3141 }
3142 else
3143 chunk_appendf(&trash, ",,,,,,");
3144
3145 /* failed health analyses */
3146 chunk_appendf(&trash, ",");
3147
3148 /* requests : req_rate, req_rate_max, req_tot, */
3149 chunk_appendf(&trash, ",,,");
3150
3151 /* errors: cli_aborts, srv_aborts */
3152 chunk_appendf(&trash, "%lld,%lld,",
3153 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3154
3155 /* compression: in, out, bypassed */
3156 chunk_appendf(&trash, "%lld,%lld,%lld,",
3157 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3158
3159 /* compression: comp_rsp */
3160 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3161
3162 /* finish with EOL */
3163 chunk_appendf(&trash, "\n");
3164 }
3165 return 1;
3166}
3167
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003168/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003169 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003170 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003172static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003175 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003177 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003178 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003179
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003180 /* 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 +02003181 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003182 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003183 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003184 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3185 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003186 }
3187
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003188 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003189 "<form action=\"%s%s%s%s\" method=\"post\">",
3190 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003191 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3192 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003193 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003194 }
3195
3196 /* print a new table */
3197 chunk_appendf(&trash,
3198 "<table class=\"tbl\" width=\"100%%\">\n"
3199 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003200 "<th class=\"pxname\" width=\"10%%\">");
3201
3202 chunk_appendf(&trash,
3203 "<a name=\"%s\"></a>%s"
3204 "<a class=px href=\"#%s\">%s</a>",
3205 px->id,
3206 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3207 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003208
3209 if (uri->flags & ST_SHLGNDS) {
3210 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003211 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3213 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003214 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 }
3216
3217 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003218 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 "<th class=\"%s\" width=\"90%%\">%s</th>"
3220 "</tr>\n"
3221 "</table>\n"
3222 "<table class=\"tbl\" width=\"100%%\">\n"
3223 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003224 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3225 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3226
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003227 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003228 /* Column heading for Enable or Disable server */
3229 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003230 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231
3232 chunk_appendf(&trash,
3233 "<th rowspan=2></th>"
3234 "<th colspan=3>Queue</th>"
3235 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
3236 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3237 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3238 "<th colspan=9>Server</th>"
3239 "</tr>\n"
3240 "<tr class=\"titre\">"
3241 "<th>Cur</th><th>Max</th><th>Limit</th>"
3242 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3243 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
3244 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3245 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3246 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3247 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3248 "<th>Thrtle</th>\n"
3249 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003250}
3251
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003252/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003255static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003257 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003258 chunk_appendf(&trash, "</table>");
3259
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003260 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003261 /* close the form used to enable/disable this proxy servers */
3262 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003263 "Choose the action to perform on the checked servers : "
3264 "<select name=action>"
3265 "<option value=\"\"></option>"
3266 "<option value=\"disable\">Disable</option>"
3267 "<option value=\"enable\">Enable</option>"
3268 "<option value=\"stop\">Soft Stop</option>"
3269 "<option value=\"start\">Soft Start</option>"
3270 "<option value=\"shutdown\">Kill Sessions</option>"
3271 "</select>"
3272 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3273 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3274 "</form>",
3275 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003276 }
3277
3278 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279}
Willy Tarreau91861262007-10-17 17:06:05 +02003280
3281/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003282 * Dumps statistics for a proxy. The output is sent to the stream interface's
3283 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3284 * buffer space, or non-zero if everything completed. This function is used
3285 * both by the CLI and the HTTP entry points, and is able to dump the output
3286 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003287 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003288static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003289{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003290 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003291 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003292 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003293 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003294 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003295
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003296 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003298 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003299 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003300 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003301 if (uri && uri->scope) {
3302 /* we have a limited scope, we have to check the proxy name */
3303 struct stat_scope *scope;
3304 int len;
3305
3306 len = strlen(px->id);
3307 scope = uri->scope;
3308
3309 while (scope) {
3310 /* match exact proxy name */
3311 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3312 break;
3313
3314 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003315 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003316 break;
3317 scope = scope->next;
3318 }
3319
3320 /* proxy name not found : don't dump anything */
3321 if (scope == NULL)
3322 return 1;
3323 }
3324
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003325 /* if the user has requested a limited output and the proxy
3326 * name does not match, skip it.
3327 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003328 if (appctx->ctx.stats.scope_len &&
3329 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 +02003330 return 1;
3331
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003332 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3333 (appctx->ctx.stats.iid != -1) &&
3334 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003335 return 1;
3336
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003337 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003338 /* fall through */
3339
Willy Tarreau295a8372011-03-10 11:25:07 +01003340 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003341 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003342 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003343 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003344 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003345 }
Willy Tarreau91861262007-10-17 17:06:05 +02003346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003347 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003348 /* fall through */
3349
Willy Tarreau295a8372011-03-10 11:25:07 +01003350 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003351 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003353 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003354 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003355
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003356 appctx->ctx.stats.l = px->conf.listeners.n;
3357 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003358 /* fall through */
3359
Willy Tarreau295a8372011-03-10 11:25:07 +01003360 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003361 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003362 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003363 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02003364 return 0;
3365
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003366 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003367 if (!l->counters)
3368 continue;
3369
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003370 if (appctx->ctx.stats.flags & STAT_BOUND) {
3371 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003372 break;
3373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003374 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003375 continue;
3376 }
3377
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 /* print the frontend */
3379 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3380 if (bi_putchk(rep, &trash) == -1)
3381 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003382 }
3383
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003384 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3385 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003386 /* fall through */
3387
Willy Tarreau295a8372011-03-10 11:25:07 +01003388 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003389 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003390 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003391 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 +02003392
Willy Tarreau9b28e032012-10-12 23:49:43 +02003393 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02003394 return 0;
3395
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003396 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003397
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003398 if (appctx->ctx.stats.flags & STAT_BOUND) {
3399 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003400 break;
3401
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003402 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003403 continue;
3404 }
3405
Willy Tarreau44267702011-10-28 15:35:33 +02003406 if (sv->track)
3407 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003408 else
3409 svs = sv;
3410
Willy Tarreau91861262007-10-17 17:06:05 +02003411 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003412 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003413 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003414 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003415 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003416 sv_state = 3; /* UP */
3417 else
3418 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003419
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003420 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003421 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003422 else if (svs->state & SRV_GOINGDOWN)
3423 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003424 }
Willy Tarreau91861262007-10-17 17:06:05 +02003425 else
Simon Horman125d0992013-02-24 17:23:38 +09003426 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003427 sv_state = 1; /* going up */
3428 else
3429 sv_state = 0; /* DOWN */
3430
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003431 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003432 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003433 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003434 continue;
3435 }
3436
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3438 if (bi_putchk(rep, &trash) == -1)
3439 return 0;
3440 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003441
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003442 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 case STAT_PX_ST_BE:
3446 /* print the backend */
3447 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3448 if (bi_putchk(rep, &trash) == -1)
3449 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003450
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003451 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003453
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003455 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003456 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003457 if (bi_putchk(rep, &trash) == -1)
3458 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003459 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003460
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003461 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003463
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003464 case STAT_PX_ST_FIN:
3465 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003466
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003467 default:
3468 /* unknown state, we should put an abort() here ! */
3469 return 1;
3470 }
3471}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003472
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003473/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3474 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003476static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003477{
3478 /* WARNING! This must fit in the first buffer !!! */
3479 chunk_appendf(&trash,
3480 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3481 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3482 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3483 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3484 "<style type=\"text/css\"><!--\n"
3485 "body {"
3486 " font-family: arial, helvetica, sans-serif;"
3487 " font-size: 12px;"
3488 " font-weight: normal;"
3489 " color: black;"
3490 " background: white;"
3491 "}\n"
3492 "th,td {"
3493 " font-size: 10px;"
3494 "}\n"
3495 "h1 {"
3496 " font-size: x-large;"
3497 " margin-bottom: 0.5em;"
3498 "}\n"
3499 "h2 {"
3500 " font-family: helvetica, arial;"
3501 " font-size: x-large;"
3502 " font-weight: bold;"
3503 " font-style: italic;"
3504 " color: #6020a0;"
3505 " margin-top: 0em;"
3506 " margin-bottom: 0em;"
3507 "}\n"
3508 "h3 {"
3509 " font-family: helvetica, arial;"
3510 " font-size: 16px;"
3511 " font-weight: bold;"
3512 " color: #b00040;"
3513 " background: #e8e8d0;"
3514 " margin-top: 0em;"
3515 " margin-bottom: 0em;"
3516 "}\n"
3517 "li {"
3518 " margin-top: 0.25em;"
3519 " margin-right: 2em;"
3520 "}\n"
3521 ".hr {margin-top: 0.25em;"
3522 " border-color: black;"
3523 " border-bottom-style: solid;"
3524 "}\n"
3525 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3526 ".total {background: #20D0D0;color: #ffff80;}\n"
3527 ".frontend {background: #e8e8d0;}\n"
3528 ".socket {background: #d0d0d0;}\n"
3529 ".backend {background: #e8e8d0;}\n"
3530 ".active0 {background: #ff9090;}\n"
3531 ".active1 {background: #ffd020;}\n"
3532 ".active2 {background: #ffffa0;}\n"
3533 ".active3 {background: #c0ffc0;}\n"
3534 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003535 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3536 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3537 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003538 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003539 ".backup0 {background: #ff9090;}\n"
3540 ".backup1 {background: #ff80ff;}\n"
3541 ".backup2 {background: #c060ff;}\n"
3542 ".backup3 {background: #b0d0ff;}\n"
3543 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3544 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003545 ".backup6 {background: #c060ff;}\n"
3546 ".backup7 {background: #cc9900;}\n"
3547 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003548 ".maintain {background: #c07820;}\n"
3549 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3550 "\n"
3551 "a.px:link {color: #ffff40; text-decoration: none;}"
3552 "a.px:visited {color: #ffff40; text-decoration: none;}"
3553 "a.px:hover {color: #ffffff; text-decoration: none;}"
3554 "a.lfsb:link {color: #000000; text-decoration: none;}"
3555 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3556 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3557 "\n"
3558 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3559 "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"
3560 "table.tbl td.ac { text-align: center;}\n"
3561 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3562 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3563 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3564 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3565 "\n"
3566 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3567 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3568 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003569 "table.det { border-collapse: collapse; border-style: none; }\n"
3570 "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"
Willy Tarreau6b9d3a82013-12-16 09:00:35 +01003571 "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;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003572 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003573 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003574 " display:block;\n"
3575 " visibility:hidden;\n"
3576 " z-index:2147483647;\n"
3577 " position:absolute;\n"
3578 " padding:2px 4px 3px;\n"
3579 " background:#f0f060; color:#000000;\n"
3580 " border:1px solid #7040c0;\n"
3581 " white-space:nowrap;\n"
3582 " font-style:normal;font-size:11px;font-weight:normal;\n"
3583 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3584 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3585 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003586 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003587 "-->\n"
3588 "</style></head>\n",
3589 (uri->flags & ST_SHNODE) ? " on " : "",
3590 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3591 );
3592}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003593
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003594/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003596 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003597 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003598static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003599{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003600 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003602 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003603
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003604 /* WARNING! this has to fit the first packet too.
3605 * We are around 3.5 kB, add adding entries will
3606 * become tricky if we want to support 4kB buffers !
3607 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608 chunk_appendf(&trash,
3609 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3610 PRODUCT_NAME "%s</a></h1>\n"
3611 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3612 "<hr width=\"100%%\" class=\"hr\">\n"
3613 "<h3>&gt; General process information</h3>\n"
3614 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3615 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3616 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3617 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3618 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3619 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3620 "Running tasks: %d/%d; idle = %d %%<br>\n"
3621 "</td><td align=\"center\" nowrap>\n"
3622 "<table class=\"lgd\"><tr>\n"
3623 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3624 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3625 "</tr><tr>\n"
3626 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3627 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3628 "</tr><tr>\n"
3629 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3630 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3631 "</tr><tr>\n"
3632 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003633 "</tr><tr>\n"
3634 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003635 "</tr><tr>\n"
3636 "<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 -07003637 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003638 "<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 +01003639 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003640 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003641 "</td>"
3642 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3643 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3644 "",
3645 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3646 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3647 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3648 (uri->flags & ST_SHDESC) ? ": " : "",
3649 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3650 pid, relative_pid, global.nbproc,
3651 up / 86400, (up % 86400) / 3600,
3652 (up % 3600) / 60, (up % 60),
3653 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3654 global.rlimit_memmax ? " MB" : "",
3655 global.rlimit_nofile,
3656 global.maxsock, global.maxconn, global.maxpipes,
3657 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3658 run_queue_cur, nb_tasks_cur, idle_pct
3659 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003660
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003661 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3662 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3663 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003664
3665 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003666 "<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 +02003667 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003668 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3669 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3670 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003671 STAT_SCOPE_TXT_MAXLEN);
3672
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003673 /* 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 +02003674 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003675 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003676 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003677 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3678 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003679 }
3680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003681 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003682 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003683 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 uri->uri_prefix,
3685 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003686 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003687 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003688 else
3689 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003690 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003691 uri->uri_prefix,
3692 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003693 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003694 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003695
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003697 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003698 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003699 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003700 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003701 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003702 "",
3703 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003704 else
3705 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003706 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003707 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003708 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003709 ";norefresh",
3710 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003711 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003712
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003713 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003714 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003715 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003716 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3717 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003718 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003719
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003720 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003721 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003722 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003723 (uri->refresh > 0) ? ";norefresh" : "",
3724 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003725
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003726 chunk_appendf(&trash,
3727 "</ul></td>"
3728 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3729 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3730 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3731 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3732 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3733 "</ul>"
3734 "</td>"
3735 "</tr></table>\n"
3736 ""
3737 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003738
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003739 if (appctx->ctx.stats.st_code) {
3740 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003741 case STAT_STATUS_DONE:
3742 chunk_appendf(&trash,
3743 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003744 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003745 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003746 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003747 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3748 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003749 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003750 break;
3751 case STAT_STATUS_NONE:
3752 chunk_appendf(&trash,
3753 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003754 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003755 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003756 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003757 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3758 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003759 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003760 break;
3761 case STAT_STATUS_PART:
3762 chunk_appendf(&trash,
3763 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003764 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003765 "Action partially processed.<br>"
3766 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003767 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003768 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3769 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003770 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003771 break;
3772 case STAT_STATUS_ERRP:
3773 chunk_appendf(&trash,
3774 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003775 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003776 "Action not processed because of invalid parameters."
3777 "<ul>"
3778 "<li>The action is maybe unknown.</li>"
3779 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3780 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3781 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003782 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003783 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3784 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003785 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003786 break;
3787 case STAT_STATUS_EXCD:
3788 chunk_appendf(&trash,
3789 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003790 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003791 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3792 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003793 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003794 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3795 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003796 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 break;
3798 case STAT_STATUS_DENY:
3799 chunk_appendf(&trash,
3800 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003801 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003802 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003803 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003804 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3805 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003806 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003807 break;
3808 default:
3809 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003810 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003811 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003812 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003813 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003814 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3815 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003816 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 }
3818 chunk_appendf(&trash, "<p>\n");
3819 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003820}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003821
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003822/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3823 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003824 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003825static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003827 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003829
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003830/* This function dumps statistics onto the stream interface's read buffer in
3831 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003832 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3833 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3834 * and the session must be closed, or -1 in case of any error. This function is
3835 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003836 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003837static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003839 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003840 struct channel *rep = si->ib;
3841 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003842
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003843 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003844
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003845 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003847 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003848 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003849
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003850 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003851 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003852 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003853 else
3854 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003855
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003856 if (bi_putchk(rep, &trash) == -1)
3857 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003858
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003859 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003861
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003862 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003863 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003864 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003865 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003866 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003867 }
Willy Tarreau91861262007-10-17 17:06:05 +02003868
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003869 appctx->ctx.stats.px = proxy;
3870 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3871 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003872 /* fall through */
3873
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003874 case STAT_ST_LIST:
3875 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003876 while (appctx->ctx.stats.px) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003877 if (buffer_almost_full(rep->buf))
3878 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003879
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003880 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003881 /* skip the disabled proxies, global frontend and non-networked ones */
3882 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003883 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003884 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003885
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003886 appctx->ctx.stats.px = px->next;
3887 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003888 }
3889 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003891 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003892 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003893
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003894 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003895 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003896 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003897 if (bi_putchk(rep, &trash) == -1)
3898 return 0;
3899 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003900
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003901 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003902 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003903
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003904 case STAT_ST_FIN:
3905 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003906
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003907 default:
3908 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003909 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910 return -1;
3911 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912}
Willy Tarreauae526782010-03-04 20:34:23 +01003913
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003914/* We reached the stats page through a POST request. The appctx is
3915 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01003916 * Parse the posted data and enable/disable servers if necessary.
3917 * Returns 1 if request was parsed or zero if it needs more data.
3918 */
3919static int stats_process_http_post(struct stream_interface *si)
3920{
3921 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003922 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003923
3924 struct proxy *px = NULL;
3925 struct server *sv = NULL;
3926
3927 char key[LINESIZE];
3928 int action = ST_ADM_ACTION_NONE;
3929 int reprocess = 0;
3930
3931 int total_servers = 0;
3932 int altered_servers = 0;
3933
3934 char *first_param, *cur_param, *next_param, *end_params;
3935 char *st_cur_param = NULL;
3936 char *st_next_param = NULL;
3937
3938 struct chunk *temp;
3939 int reql;
3940
3941 temp = get_trash_chunk();
3942 if (temp->size < s->txn.req.body_len) {
3943 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003944 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003945 goto out;
3946 }
3947
3948 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
3949 if (reql <= 0) {
3950 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003951 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003952 return 0;
3953 }
3954
3955 first_param = temp->str;
3956 end_params = temp->str + reql;
3957 cur_param = next_param = end_params;
3958 *end_params = '\0';
3959
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003960 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003961
3962 /*
3963 * Parse the parameters in reverse order to only store the last value.
3964 * From the html form, the backend and the action are at the end.
3965 */
3966 while (cur_param > first_param) {
3967 char *value;
3968 int poffset, plen;
3969
3970 cur_param--;
3971
3972 if ((*cur_param == '&') || (cur_param == first_param)) {
3973 reprocess_servers:
3974 /* Parse the key */
3975 poffset = (cur_param != first_param ? 1 : 0);
3976 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
3977 if ((plen > 0) && (plen <= sizeof(key))) {
3978 strncpy(key, cur_param + poffset, plen);
3979 key[plen - 1] = '\0';
3980 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003981 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003982 goto out;
3983 }
3984
3985 /* Parse the value */
3986 value = key;
3987 while (*value != '\0' && *value != '=') {
3988 value++;
3989 }
3990 if (*value == '=') {
3991 /* Ok, a value is found, we can mark the end of the key */
3992 *value++ = '\0';
3993 }
3994 if (url_decode(key) < 0 || url_decode(value) < 0)
3995 break;
3996
3997 /* Now we can check the key to see what to do */
3998 if (!px && (strcmp(key, "b") == 0)) {
3999 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4000 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004001 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004002 goto out;
4003 }
4004 }
4005 else if (!action && (strcmp(key, "action") == 0)) {
4006 if (strcmp(value, "disable") == 0) {
4007 action = ST_ADM_ACTION_DISABLE;
4008 }
4009 else if (strcmp(value, "enable") == 0) {
4010 action = ST_ADM_ACTION_ENABLE;
4011 }
4012 else if (strcmp(value, "stop") == 0) {
4013 action = ST_ADM_ACTION_STOP;
4014 }
4015 else if (strcmp(value, "start") == 0) {
4016 action = ST_ADM_ACTION_START;
4017 }
4018 else if (strcmp(value, "shutdown") == 0) {
4019 action = ST_ADM_ACTION_SHUTDOWN;
4020 }
4021 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004022 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004023 goto out;
4024 }
4025 }
4026 else if (strcmp(key, "s") == 0) {
4027 if (!(px && action)) {
4028 /*
4029 * Indicates that we'll need to reprocess the parameters
4030 * as soon as backend and action are known
4031 */
4032 if (!reprocess) {
4033 st_cur_param = cur_param;
4034 st_next_param = next_param;
4035 }
4036 reprocess = 1;
4037 }
4038 else if ((sv = findserver(px, value)) != NULL) {
4039 switch (action) {
4040 case ST_ADM_ACTION_DISABLE:
4041 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4042 /* Not already in maintenance, we can change the server state */
4043 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004044 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004045 set_server_down(&sv->check);
4046 altered_servers++;
4047 total_servers++;
4048 }
4049 break;
4050 case ST_ADM_ACTION_ENABLE:
4051 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004052 /* Already in maintenance, we can change the server state.
4053 * If this server tracks the status of another one,
4054 * we must restore the good status.
4055 */
4056 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4057 set_server_up(&sv->check);
4058 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4059 }
4060 else {
4061 sv->state &= ~SRV_MAINTAIN;
4062 sv->check.state &= ~CHK_ST_PAUSED;
4063 set_server_down(&sv->check);
4064 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004065 altered_servers++;
4066 total_servers++;
4067 }
4068 break;
4069 case ST_ADM_ACTION_STOP:
4070 case ST_ADM_ACTION_START:
4071 if (action == ST_ADM_ACTION_START)
4072 sv->uweight = sv->iweight;
4073 else
4074 sv->uweight = 0;
4075
4076 server_recalc_eweight(sv);
4077 set_server_drain_state(sv);
4078
4079 altered_servers++;
4080 total_servers++;
4081 break;
4082 case ST_ADM_ACTION_SHUTDOWN:
4083 if (px->state != PR_STSTOPPED) {
4084 struct session *sess, *sess_bck;
4085
4086 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4087 if (sess->srv_conn == sv)
4088 session_shutdown(sess, SN_ERR_KILLED);
4089
4090 altered_servers++;
4091 total_servers++;
4092 }
4093 break;
4094 }
4095 } else {
4096 /* the server name is unknown or ambiguous (duplicate names) */
4097 total_servers++;
4098 }
4099 }
4100 if (reprocess && px && action) {
4101 /* Now, we know the backend and the action chosen by the user.
4102 * We can safely restart from the first server parameter
4103 * to reprocess them
4104 */
4105 cur_param = st_cur_param;
4106 next_param = st_next_param;
4107 reprocess = 0;
4108 goto reprocess_servers;
4109 }
4110
4111 next_param = cur_param;
4112 }
4113 }
4114
4115 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004116 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004117 }
4118 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004119 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004120 }
4121 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004122 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004123 }
4124 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004125 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004126 }
4127 out:
4128 return 1;
4129}
4130
4131
4132static int stats_send_http_headers(struct stream_interface *si)
4133{
4134 struct session *s = session_from_task(si->owner);
4135 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004136 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004137
4138 chunk_printf(&trash,
Willy Tarreauf3221f92013-12-28 21:11:35 +01004139 "HTTP/1.%c 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004140 "Cache-Control: no-cache\r\n"
4141 "Connection: close\r\n"
4142 "Content-Type: %s\r\n",
Willy Tarreauf3221f92013-12-28 21:11:35 +01004143 (appctx->ctx.stats.flags & STAT_CHUNKED) ? '1' : '0',
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004144 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004145
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004146 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004147 chunk_appendf(&trash, "Refresh: %d\r\n",
4148 uri->refresh);
4149
Willy Tarreauf3221f92013-12-28 21:11:35 +01004150 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4151
4152 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4153 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
Willy Tarreaub8006232013-12-28 21:48:56 +01004154 else
Willy Tarreauf3221f92013-12-28 21:11:35 +01004155 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004156
4157 s->txn.status = 200;
4158 s->logs.tv_request = now;
4159
4160 if (bi_putchk(si->ib, &trash) == -1)
4161 return 0;
4162
4163 return 1;
4164}
4165
4166static int stats_send_http_redirect(struct stream_interface *si)
4167{
4168 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4169 struct session *s = session_from_task(si->owner);
4170 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004171 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004172
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004173 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004174 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004175 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004176 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004177 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4178 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004179 }
4180
4181 /* We don't want to land on the posted stats page because a refresh will
4182 * repost the data. We don't want this to happen on accident so we redirect
4183 * the browse to the stats page with a GET.
4184 */
4185 chunk_printf(&trash,
4186 "HTTP/1.1 303 See Other\r\n"
4187 "Cache-Control: no-cache\r\n"
4188 "Content-Type: text/plain\r\n"
4189 "Connection: close\r\n"
4190 "Location: %s;st=%s%s%s%s\r\n"
4191 "\r\n",
4192 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004193 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4194 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4195 stat_status_codes[appctx->ctx.stats.st_code]) ?
4196 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004197 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004198 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4199 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004200 scope_txt);
4201
4202 s->txn.status = 303;
4203 s->logs.tv_request = now;
4204
4205 if (bi_putchk(si->ib, &trash) == -1)
4206 return 0;
4207
4208 return 1;
4209}
4210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004211/* This I/O handler runs as an applet embedded in a stream interface. It is
4212 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004213 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004214 * automatically unregisters itself once transfer is complete.
4215 */
4216static void http_stats_io_handler(struct stream_interface *si)
4217{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004218 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004219 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004220 struct channel *req = si->ob;
4221 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004222
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004223 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4224 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004225
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004226 /* check that the output is not closed */
4227 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004228 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004229
Willy Tarreau347a35d2013-11-22 17:51:09 +01004230 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004231 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004232 if (stats_send_http_headers(si)) {
4233 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004234 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004235 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004236 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004237 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004238 }
4239
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004240 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauf3221f92013-12-28 21:11:35 +01004241 unsigned int prev_len = si->ib->buf->i;
4242 unsigned int data_len;
4243 unsigned int last_len;
4244 unsigned int last_fwd;
4245
4246 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4247 /* One difficulty we're facing is that we must prevent
4248 * the input data from being automatically forwarded to
4249 * the output area. For this, we temporarily disable
4250 * forwarding on the channel.
4251 */
4252 last_fwd = si->ib->to_forward;
4253 si->ib->to_forward = 0;
4254 chunk_printf(&trash, "\r\n000000\r\n");
4255 if (bi_putchk(si->ib, &trash) == -1) {
4256 si->ib->to_forward = last_fwd;
4257 goto fail;
4258 }
4259 }
4260
4261 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004262 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004263 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauf3221f92013-12-28 21:11:35 +01004264
4265 last_len = si->ib->buf->i;
4266
4267 /* Now we must either adjust or remove the chunk size. This is
4268 * not easy because the chunk size might wrap at the end of the
4269 * buffer, so we pretend we have nothing in the buffer, we write
4270 * the size, then restore the buffer's contents. Note that we can
4271 * only do that because no forwarding is scheduled on the stats
4272 * applet.
4273 */
4274 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4275 si->ib->total -= (last_len - prev_len);
4276 si->ib->buf->i -= (last_len - prev_len);
4277
4278 if (last_len != data_len) {
4279 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4280 bi_putchk(si->ib, &trash);
4281
4282 si->ib->total += (last_len - data_len);
4283 si->ib->buf->i += (last_len - data_len);
4284 }
4285 /* now re-enable forwarding */
4286 channel_forward(si->ib, last_fwd);
4287 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004288 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004289
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004290 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004291 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004292 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004293 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004294 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004295 }
4296
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004297 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004298 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004299 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004300 }
4301
Willy Tarreauf3221f92013-12-28 21:11:35 +01004302 if (appctx->st0 == STAT_HTTP_DONE) {
4303 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4304 chunk_printf(&trash, "\r\n0\r\n\r\n");
4305 if (bi_putchk(si->ib, &trash) == -1)
4306 goto fail;
4307 }
4308 /* eat the whole request */
4309 bo_skip(si->ob, si->ob->buf->o);
4310 res->flags |= CF_READ_NULL;
4311 si_shutr(si);
4312 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004313
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004314 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4315 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004316
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004317 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004318 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4319 si_shutr(si);
4320 res->flags |= CF_READ_NULL;
4321 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 }
Willy Tarreau91861262007-10-17 17:06:05 +02004323
Willy Tarreauf3221f92013-12-28 21:11:35 +01004324 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 /* update all other flags and resync with the other side */
4326 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004327
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004328 /* we don't want to expire timeouts while we're processing requests */
4329 si->ib->rex = TICK_ETERNITY;
4330 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004331
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332 out:
4333 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4334 /* check that we have released everything then unregister */
4335 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004336 }
4337}
4338
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339
Willy Tarreau909d5172012-11-26 03:04:41 +01004340static inline const char *get_conn_ctrl_name(const struct connection *conn)
4341{
Willy Tarreauf79c8172013-10-21 16:30:56 +02004342 if (!(conn->flags & CO_FL_CTRL_READY) || !conn->ctrl)
Willy Tarreau909d5172012-11-26 03:04:41 +01004343 return "NONE";
4344 return conn->ctrl->name;
4345}
4346
4347static inline const char *get_conn_xprt_name(const struct connection *conn)
4348{
4349 static char ptr[17];
4350
Willy Tarreauf79c8172013-10-21 16:30:56 +02004351 if (!(conn->flags & CO_FL_XPRT_READY) || !conn->xprt)
Willy Tarreau909d5172012-11-26 03:04:41 +01004352 return "NONE";
4353
4354 if (conn->xprt == &raw_sock)
4355 return "RAW";
4356
4357#ifdef USE_OPENSSL
4358 if (conn->xprt == &ssl_sock)
4359 return "SSL";
4360#endif
4361 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4362 return ptr;
4363}
4364
4365static inline const char *get_conn_data_name(const struct connection *conn)
4366{
4367 static char ptr[17];
4368
4369 if (!conn->data)
4370 return "NONE";
4371
4372 if (conn->data == &sess_conn_cb)
4373 return "SESS";
4374
4375 if (conn->data == &si_conn_cb)
4376 return "STRM";
4377
4378 if (conn->data == &check_conn_cb)
4379 return "CHCK";
4380
4381 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4382 return ptr;
4383}
4384
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004385/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004386 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004387 * 0 if the output buffer is full and it needs to be called again, otherwise
4388 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004389 */
Willy Tarreau76153662012-11-26 01:16:39 +01004390static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004391{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004393 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004394 extern const char *monthname[12];
4395 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004396 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004397 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004398
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004399 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004400
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004401 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004402 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004403 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4404 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004405 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004406 appctx->ctx.sess.uid = 0;
4407 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004408 return 1;
4409 }
4410
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004411 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004412 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004413 appctx->ctx.sess.uid = sess->uniq_id;
4414 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004415 /* fall through */
4416
4417 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004418 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004419 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004420 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004421 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004422 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4423 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004424 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004425 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004426
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004427 conn = objt_conn(sess->si[0].end);
4428 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004429 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004430 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004431 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004432 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004433 break;
4434 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004435 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004436 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004437 default:
4438 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004439 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004440 break;
4441 }
4442
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004443 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004444 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004445 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004446
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004447 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004448 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004449 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4450 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4451 sess->listener ? sess->listener->luid : 0);
4452
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004453 if (conn)
4454 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004455
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004456 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004457 case AF_INET:
4458 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004459 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004460 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004461 break;
4462 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004463 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004464 break;
4465 default:
4466 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004467 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004468 break;
4469 }
4470
Willy Tarreau50943332011-09-02 17:33:05 +02004471 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004472 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004473 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004474 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004475 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4476 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004477 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004478
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004479 conn = objt_conn(sess->si[1].end);
4480 if (conn)
4481 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004482
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004483 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004484 case AF_INET:
4485 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004486 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004487 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004488 break;
4489 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004490 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004491 break;
4492 default:
4493 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004494 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004495 break;
4496 }
4497
4498 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004499 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004500 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004501 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4502 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004503 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004504 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004505
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004506 if (conn)
4507 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004508
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004509 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004510 case AF_INET:
4511 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004512 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004513 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004514 break;
4515 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004516 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004517 break;
4518 default:
4519 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004520 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004521 break;
4522 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004523
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004524 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004525 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004526 sess->task,
4527 sess->task->state,
4528 sess->task->nice, sess->task->calls,
4529 sess->task->expire ?
4530 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4531 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4532 TICKS_TO_MS(1000)) : "<NEVER>",
4533 task_in_rq(sess->task) ? ", running" : "");
4534
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004535 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004536 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004537 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4538
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004539 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004540 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4541 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4542 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4543
4544 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004545 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004546 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004547 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004548 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004549 obj_type_name(sess->si[0].end),
4550 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004551 sess->si[0].exp ?
4552 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4553 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4554 TICKS_TO_MS(1000)) : "<NEVER>",
4555 sess->si[0].err_type);
4556
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004557 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004558 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004559 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004560 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004561 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004562 obj_type_name(sess->si[1].end),
4563 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004564 sess->si[1].exp ?
4565 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4566 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4567 TICKS_TO_MS(1000)) : "<NEVER>",
4568 sess->si[1].err_type);
4569
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004570 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004571 chunk_appendf(&trash,
4572 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004573 conn,
4574 get_conn_ctrl_name(conn),
4575 get_conn_xprt_name(conn),
4576 get_conn_data_name(conn),
4577 obj_type_name(conn->target),
4578 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004579
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004580 chunk_appendf(&trash,
4581 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004582 conn->flags,
4583 conn->t.sock.fd,
4584 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4585 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4586 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004587 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004588 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4589 chunk_appendf(&trash,
4590 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4591 tmpctx,
4592 tmpctx->st0,
4593 tmpctx->st1,
4594 tmpctx->st2,
4595 tmpctx->applet->name);
4596 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004597
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004598 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004599 chunk_appendf(&trash,
4600 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004601 conn,
4602 get_conn_ctrl_name(conn),
4603 get_conn_xprt_name(conn),
4604 get_conn_data_name(conn),
4605 obj_type_name(conn->target),
4606 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004607
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004608 chunk_appendf(&trash,
4609 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004610 conn->flags,
4611 conn->t.sock.fd,
4612 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_e : 0,
4613 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].spec_p : 0,
4614 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004615 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004616 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4617 chunk_appendf(&trash,
4618 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4619 tmpctx,
4620 tmpctx->st0,
4621 tmpctx->st1,
4622 tmpctx->st2,
4623 tmpctx->applet->name);
4624 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004626 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004627 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004628 " an_exp=%s",
4629 sess->req,
4630 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004631 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004632 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004633 sess->req->analyse_exp ?
4634 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4635 TICKS_TO_MS(1000)) : "<NEVER>");
4636
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004637 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004638 " rex=%s",
4639 sess->req->rex ?
4640 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4641 TICKS_TO_MS(1000)) : "<NEVER>");
4642
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004643 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004644 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004645 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004646 sess->req->wex ?
4647 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4648 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004649 sess->req->buf,
4650 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004651 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004652 sess->txn.req.next, sess->req->buf->i,
4653 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004654
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004655 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004656 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004657 " an_exp=%s",
4658 sess->rep,
4659 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004660 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004661 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004662 sess->rep->analyse_exp ?
4663 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4664 TICKS_TO_MS(1000)) : "<NEVER>");
4665
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004666 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004667 " rex=%s",
4668 sess->rep->rex ?
4669 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4670 TICKS_TO_MS(1000)) : "<NEVER>");
4671
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004672 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004673 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004674 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004675 sess->rep->wex ?
4676 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4677 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004678 sess->rep->buf,
4679 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004680 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004681 sess->txn.rsp.next, sess->rep->buf->i,
4682 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004685 return 0;
4686
4687 /* use other states to dump the contents */
4688 }
4689 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004690 appctx->ctx.sess.uid = 0;
4691 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004692 return 1;
4693}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004694
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004695static int stats_maps_list(struct stream_interface *si)
4696{
4697 struct appctx *appctx = __objt_appctx(si->end);
4698
4699 switch (appctx->st2) {
4700 case STAT_ST_INIT:
4701 /* Init to the first entry. The list cannot be change */
4702 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4703 appctx->st2 = STAT_ST_LIST;
4704 /* fall through */
4705
4706 case STAT_ST_LIST:
4707 while (appctx->ctx.map.ref) {
4708
4709 chunk_reset(&trash);
4710
4711 /* build messages */
4712 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4713
4714 if (bi_putchk(si->ib, &trash) == -1) {
4715 /* let's try again later from this session. We add ourselves into
4716 * this session's users so that it can remove us upon termination.
4717 */
4718 return 0;
4719 }
4720
4721 /* get next list entry and check the end of the list */
4722 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4723 struct map_reference *, list);
4724 if (&appctx->ctx.map.ref->list == &maps)
4725 break;
4726 }
4727
4728 appctx->st2 = STAT_ST_FIN;
4729 /* fall through */
4730
4731 default:
4732 appctx->st2 = STAT_ST_FIN;
4733 return 1;
4734 }
4735}
4736
4737static const char *smp_to_type[SMP_TYPES] = {
4738 [SMP_T_BOOL] = "bool",
4739 [SMP_T_UINT] = "uint",
4740 [SMP_T_SINT] = "sint",
4741 [SMP_T_ADDR] = "addr",
4742 [SMP_T_IPV4] = "ipv4",
4743 [SMP_T_IPV6] = "ipv6",
4744 [SMP_T_STR] = "str",
4745 [SMP_T_BIN] = "bin",
4746 [SMP_T_CSTR] = "cstr",
4747 [SMP_T_CBIN] = "cbin",
4748};
4749
4750static int stats_map_lookup(struct stream_interface *si)
4751{
4752 struct appctx *appctx = __objt_appctx(si->end);
4753 struct sample_storage *smp;
4754 struct sample sample;
4755 struct pattern *pat;
4756 struct pat_idx_elt *elt;
4757 enum pat_match_res res;
4758 struct sockaddr_in addr;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004759 char addr_str[INET_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004760
4761 switch (appctx->st2) {
4762 case STAT_ST_INIT:
4763 appctx->ctx.map.desc = NULL;
4764 stats_map_lookup_next(si);
4765 appctx->st2 = STAT_ST_LIST;
4766 /* fall through */
4767
4768 case STAT_ST_LIST:
4769 /* for each lookup type */
4770 while (appctx->ctx.map.desc) {
4771 /* initialise chunk to build new message */
4772 chunk_reset(&trash);
4773
4774 /* execute pattern matching */
4775 sample.type = SMP_T_CSTR;
4776 sample.data.str.len = appctx->ctx.map.chunk.len;
4777 sample.data.str.str = appctx->ctx.map.chunk.str;
4778 pat = NULL;
4779 elt = NULL;
4780 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4781
4782 /* build return message: set type of match */
4783 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4784 chunk_appendf(&trash, "found, ");
4785 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4786 chunk_appendf(&trash, "bool, ");
4787 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4788 chunk_appendf(&trash, "int, ");
4789 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4790 chunk_appendf(&trash, "ip, ");
4791 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4792 chunk_appendf(&trash, "bin, ");
4793 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4794 chunk_appendf(&trash, "len, ");
4795 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4796 chunk_appendf(&trash, "str, ");
4797 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4798 chunk_appendf(&trash, "beg, ");
4799 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4800 chunk_appendf(&trash, "sub, ");
4801 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4802 chunk_appendf(&trash, "dir, ");
4803 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4804 chunk_appendf(&trash, "dom, ");
4805 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4806 chunk_appendf(&trash, "end, ");
4807 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4808 chunk_appendf(&trash, "reg, ");
4809 else /* The never appens case */
4810 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4811
4812 /* Display no match, and set default value */
4813 if (res == PAT_NOMATCH) {
4814 chunk_appendf(&trash, "no-match, ");
4815 smp = appctx->ctx.map.desc->def;
4816 }
4817
4818 /* Display match and match info */
4819 else {
4820 /* display match */
4821 chunk_appendf(&trash, "match, ");
4822
4823 /* display search mode */
4824 if (elt)
4825 chunk_appendf(&trash, "tree, ");
4826 else
4827 chunk_appendf(&trash, "list, ");
4828
4829 /* display search options */
4830 if (pat) {
4831 /* case sensitive */
4832 if (pat->flags & PAT_F_IGNORE_CASE)
4833 chunk_appendf(&trash, "case-insensitive, ");
4834 else
4835 chunk_appendf(&trash, "case-sensitive, ");
4836
4837 /* display source */
4838 if (pat->flags & PAT_F_FROM_FILE)
4839 chunk_appendf(&trash, "from-file, ");
4840 }
4841
4842 /* display match expresion */
4843 if (elt) {
4844 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4845 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4846 }
4847 /* only IPv4 */
4848 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4849 /* convert ip */
4850 memcpy(&addr.sin_addr, elt->node.key, 4);
4851 addr.sin_family = AF_INET;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004852 if (addr_to_str((struct sockaddr_storage *)&addr, addr_str, INET_ADDRSTRLEN))
4853 chunk_appendf(&trash, "match=\"%s/%d\", ", addr_str, elt->node.node.pfx);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004854 }
4855 }
4856 }
4857
4858 /* display return value */
4859 if (!smp) {
4860 chunk_appendf(&trash, "return=nothing\n");
4861 }
4862 else {
4863 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4864 sample.type = smp->type;
4865 if (sample_casts[sample.type][SMP_T_CSTR] &&
4866 sample_casts[sample.type][SMP_T_CSTR](&sample))
4867 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4868 sample.data.str.str, smp_to_type[smp->type]);
4869 else
4870 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4871 smp_to_type[smp->type]);
4872 }
4873
4874 /* display response */
4875 if (bi_putchk(si->ib, &trash) == -1) {
4876 /* let's try again later from this session. We add ourselves into
4877 * this session's users so that it can remove us upon termination.
4878 */
4879 return 0;
4880 }
4881
4882 /* get next entry */
4883 stats_map_lookup_next(si);
4884 }
4885
4886 appctx->st2 = STAT_ST_FIN;
4887 /* fall through */
4888
4889 default:
4890 appctx->st2 = STAT_ST_FIN;
4891 free(appctx->ctx.map.chunk.str);
4892 return 1;
4893 }
4894}
4895
4896static int stats_map_list(struct stream_interface *si)
4897{
4898 struct appctx *appctx = __objt_appctx(si->end);
4899
4900 switch (appctx->st2) {
4901
4902 case STAT_ST_INIT:
4903 /* Init to the first entry. The list cannot be change */
4904 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
4905 struct map_entry *, list);
4906 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4907 appctx->ctx.map.ent = NULL;
4908 appctx->st2 = STAT_ST_LIST;
4909 /* fall through */
4910
4911 case STAT_ST_LIST:
4912 while (appctx->ctx.map.ent) {
4913 chunk_reset(&trash);
4914
4915 /* build messages */
4916 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
4917
4918 if (bi_putchk(si->ib, &trash) == -1) {
4919 /* let's try again later from this session. We add ourselves into
4920 * this session's users so that it can remove us upon termination.
4921 */
4922 return 0;
4923 }
4924
4925 /* get next list entry and check the end of the list */
4926 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
4927 struct map_entry *, list);
4928 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4929 break;
4930 }
4931
4932 appctx->st2 = STAT_ST_FIN;
4933 /* fall through */
4934
4935 default:
4936 appctx->st2 = STAT_ST_FIN;
4937 return 1;
4938 }
4939}
4940
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004941/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004942 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004943 * to be called again, otherwise non-zero. It is designed to be called
4944 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004945 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004946static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004947{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004948 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004949 struct connection *conn;
4950
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004951 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004952 /* If we're forced to shut down, we might have to remove our
4953 * reference to the last session being dumped.
4954 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004955 if (appctx->st2 == STAT_ST_LIST) {
4956 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4957 LIST_DEL(&appctx->ctx.sess.bref.users);
4958 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004959 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004960 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004961 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004962 }
4963
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004964 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004966 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004967 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004968 /* the function had not been called yet, let's prepare the
4969 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004970 * pointer to the first in the global list. When a target
4971 * session is being destroyed, it is responsible for updating
4972 * this pointer. We know we have reached the end when this
4973 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004974 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004975 LIST_INIT(&appctx->ctx.sess.bref.users);
4976 appctx->ctx.sess.bref.ref = sessions.n;
4977 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004978 /* fall through */
4979
Willy Tarreau295a8372011-03-10 11:25:07 +01004980 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004981 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004982 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
4983 LIST_DEL(&appctx->ctx.sess.bref.users);
4984 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004985 }
4986
4987 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004988 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01004989 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004990 struct session *curr_sess;
4991
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004992 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004994 if (appctx->ctx.sess.target) {
4995 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004996 goto next_sess;
4997
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004998 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004999 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005000 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005001 return 0;
5002
5003 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005004 LIST_DEL(&appctx->ctx.sess.bref.users);
5005 LIST_INIT(&appctx->ctx.sess.bref.users);
5006 if (appctx->ctx.sess.target != (void *)-1) {
5007 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005008 break;
5009 }
5010 else
5011 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005012 }
5013
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005014 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005015 "%p: proto=%s",
5016 curr_sess,
5017 curr_sess->listener->proto->name);
5018
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005019
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005020 conn = objt_conn(curr_sess->si[0].end);
5021 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005022 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005023 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005024 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005025 " src=%s:%d fe=%s be=%s srv=%s",
5026 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005027 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005028 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005029 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005030 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005031 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005032 break;
5033 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005034 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005035 " src=unix:%d fe=%s be=%s srv=%s",
5036 curr_sess->listener->luid,
5037 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005038 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005039 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005040 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005041 break;
5042 }
5043
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005044 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005045 " ts=%02x age=%s calls=%d",
5046 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005047 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5048 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005049
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005050 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005051 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005052 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005053 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005054 curr_sess->req->analysers,
5055 curr_sess->req->rex ?
5056 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5057 TICKS_TO_MS(1000)) : "");
5058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005059 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005060 ",wx=%s",
5061 curr_sess->req->wex ?
5062 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5063 TICKS_TO_MS(1000)) : "");
5064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005065 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005066 ",ax=%s]",
5067 curr_sess->req->analyse_exp ?
5068 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5069 TICKS_TO_MS(1000)) : "");
5070
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005071 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005072 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005073 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005074 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005075 curr_sess->rep->analysers,
5076 curr_sess->rep->rex ?
5077 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5078 TICKS_TO_MS(1000)) : "");
5079
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005080 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005081 ",wx=%s",
5082 curr_sess->rep->wex ?
5083 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5084 TICKS_TO_MS(1000)) : "");
5085
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005086 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005087 ",ax=%s]",
5088 curr_sess->rep->analyse_exp ?
5089 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5090 TICKS_TO_MS(1000)) : "");
5091
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005092 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005093 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005094 " s0=[%d,%1xh,fd=%d,ex=%s]",
5095 curr_sess->si[0].state,
5096 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005097 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005098 curr_sess->si[0].exp ?
5099 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5100 TICKS_TO_MS(1000)) : "");
5101
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005102 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005103 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005104 " s1=[%d,%1xh,fd=%d,ex=%s]",
5105 curr_sess->si[1].state,
5106 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005107 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005108 curr_sess->si[1].exp ?
5109 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5110 TICKS_TO_MS(1000)) : "");
5111
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005112 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005113 " exp=%s",
5114 curr_sess->task->expire ?
5115 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5116 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005117 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005118 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005119
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005120 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005122 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005123 /* let's try again later from this session. We add ourselves into
5124 * this session's users so that it can remove us upon termination.
5125 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005126 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005127 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005128 }
5129
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005130 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005131 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005132 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005133
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005134 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005135 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005136 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005137 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005138 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005139 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005140
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005141 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005142 return 0;
5143
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005144 appctx->ctx.sess.target = NULL;
5145 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005146 return 1;
5147 }
5148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005149 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005150 /* fall through */
5151
5152 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005153 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005154 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005155 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005156}
5157
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005158/* This is called when the stream interface is closed. For instance, upon an
5159 * external abort, we won't call the i/o handler anymore so we may need to
5160 * remove back references to the session currently being dumped.
5161 */
Simon Horman74d88312013-02-12 10:45:50 +09005162static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005163{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005164 struct appctx *appctx = __objt_appctx(si->end);
5165
5166 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5167 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5168 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005169 }
5170}
5171
Willy Tarreau20e99322013-04-13 09:22:25 +02005172/* This function is used to either dump tables states (when action is set
5173 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005174 * It returns 0 if the output buffer is full and it needs to be called
5175 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005176 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005177static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005178{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005179 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005180 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005181 struct ebmb_node *eb;
5182 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005183 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005184 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005185
5186 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005187 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005188 * - STAT_ST_INIT : the first call
5189 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005190 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005191 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005192 * and the entry pointer points to the next entry to be dumped,
5193 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005194 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005195 * data though.
5196 */
5197
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005198 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005199 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005200 if (appctx->st2 == STAT_ST_LIST) {
5201 appctx->ctx.table.entry->ref_cnt--;
5202 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005203 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005204 return 1;
5205 }
5206
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005207 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005208
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005209 while (appctx->st2 != STAT_ST_FIN) {
5210 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005211 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005212 appctx->ctx.table.proxy = appctx->ctx.table.target;
5213 if (!appctx->ctx.table.proxy)
5214 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005215
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005216 appctx->ctx.table.entry = NULL;
5217 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005218 break;
5219
Willy Tarreau295a8372011-03-10 11:25:07 +01005220 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005221 if (!appctx->ctx.table.proxy ||
5222 (appctx->ctx.table.target &&
5223 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5224 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005225 break;
5226 }
5227
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005228 if (appctx->ctx.table.proxy->table.size) {
5229 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5230 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005231 return 0;
5232
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005233 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005234 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005235 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005236 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005237 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005238 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5239 appctx->ctx.table.entry->ref_cnt++;
5240 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005241 break;
5242 }
5243 }
5244 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005245 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005246 break;
5247
Willy Tarreau295a8372011-03-10 11:25:07 +01005248 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005249 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005250
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005251 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005252 /* we're filtering on some data contents */
5253 void *ptr;
5254 long long data;
5255
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005256 dt = appctx->ctx.table.data_type;
5257 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5258 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005259 dt);
5260
5261 data = 0;
5262 switch (stktable_data_types[dt].std_type) {
5263 case STD_T_SINT:
5264 data = stktable_data_cast(ptr, std_t_sint);
5265 break;
5266 case STD_T_UINT:
5267 data = stktable_data_cast(ptr, std_t_uint);
5268 break;
5269 case STD_T_ULL:
5270 data = stktable_data_cast(ptr, std_t_ull);
5271 break;
5272 case STD_T_FRQP:
5273 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005274 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005275 break;
5276 }
5277
5278 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005279 if ((data < appctx->ctx.table.value &&
5280 (appctx->ctx.table.data_op == STD_OP_EQ ||
5281 appctx->ctx.table.data_op == STD_OP_GT ||
5282 appctx->ctx.table.data_op == STD_OP_GE)) ||
5283 (data == appctx->ctx.table.value &&
5284 (appctx->ctx.table.data_op == STD_OP_NE ||
5285 appctx->ctx.table.data_op == STD_OP_GT ||
5286 appctx->ctx.table.data_op == STD_OP_LT)) ||
5287 (data > appctx->ctx.table.value &&
5288 (appctx->ctx.table.data_op == STD_OP_EQ ||
5289 appctx->ctx.table.data_op == STD_OP_LT ||
5290 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005291 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005292 }
5293
Simon Hormanc88b8872011-06-15 15:18:49 +09005294 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005295 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5296 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005297 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005299 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005300
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005301 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005302 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005303 struct stksess *old = appctx->ctx.table.entry;
5304 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005305 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005306 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5307 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5308 stksess_kill(&appctx->ctx.table.proxy->table, old);
5309 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005310 break;
5311 }
5312
Simon Hormanc88b8872011-06-15 15:18:49 +09005313
5314 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005315 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5316 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5317 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005318
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005319 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5320 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005321 break;
5322
Willy Tarreau295a8372011-03-10 11:25:07 +01005323 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005324 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005325 break;
5326 }
5327 }
5328 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005329}
5330
Willy Tarreaud426a182010-03-05 14:58:26 +01005331/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005332 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5333 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5334 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5335 * lines are respected within the limit of 70 output chars. Lines that are
5336 * continuation of a previous truncated line begin with "+" instead of " "
5337 * after the offset. The new pointer is returned.
5338 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005339static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5340 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005341{
5342 int end;
5343 unsigned char c;
5344
5345 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005346 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005347 return ptr;
5348
Willy Tarreau77804732012-10-29 16:14:26 +01005349 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005350
Willy Tarreaud426a182010-03-05 14:58:26 +01005351 while (ptr < len && ptr < bsize) {
5352 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005353 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005354 if (out->len > end - 2)
5355 break;
5356 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005357 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005358 if (out->len > end - 3)
5359 break;
5360 out->str[out->len++] = '\\';
5361 switch (c) {
5362 case '\t': c = 't'; break;
5363 case '\n': c = 'n'; break;
5364 case '\r': c = 'r'; break;
5365 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005366 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005367 }
5368 out->str[out->len++] = c;
5369 } else {
5370 if (out->len > end - 5)
5371 break;
5372 out->str[out->len++] = '\\';
5373 out->str[out->len++] = 'x';
5374 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5375 out->str[out->len++] = hextab[c & 0xF];
5376 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005377 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005378 /* we had a line break, let's return now */
5379 out->str[out->len++] = '\n';
5380 *line = ptr;
5381 return ptr;
5382 }
5383 }
5384 /* we have an incomplete line, we return it as-is */
5385 out->str[out->len++] = '\n';
5386 return ptr;
5387}
5388
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005389/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005390 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005391 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005392 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005393static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005394{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005395 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005396 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005397
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005398 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005399 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005400
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005401 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005402
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005403 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005404 /* the function had not been called yet, let's prepare the
5405 * buffer for a response.
5406 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005407 struct tm tm;
5408
5409 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005410 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005411 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5412 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5413 error_snapshot_id);
5414
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005415 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005416 /* Socket buffer full. Let's try again later from the same point */
5417 return 0;
5418 }
5419
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005420 appctx->ctx.errors.px = proxy;
5421 appctx->ctx.errors.buf = 0;
5422 appctx->ctx.errors.bol = 0;
5423 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005424 }
5425
5426 /* we have two inner loops here, one for the proxy, the other one for
5427 * the buffer.
5428 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005429 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005430 struct error_snapshot *es;
5431
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005432 if (appctx->ctx.errors.buf == 0)
5433 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005434 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005435 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005436
5437 if (!es->when.tv_sec)
5438 goto next;
5439
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005440 if (appctx->ctx.errors.iid >= 0 &&
5441 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5442 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005443 goto next;
5444
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005445 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005446 /* just print headers now */
5447
5448 char pn[INET6_ADDRSTRLEN];
5449 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005450 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005451
5452 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005453 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005454 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005455 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005456
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005457 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5458 case AF_INET:
5459 case AF_INET6:
5460 port = get_host_port(&es->src);
5461 break;
5462 default:
5463 port = 0;
5464 }
5465
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005466 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005467 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005468 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005469 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005470 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005471 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005472 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5473 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005474 break;
5475 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005476 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005477 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005478 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005479 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005480 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005481 break;
5482 }
5483
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005484 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005485 ", server %s (#%d), event #%u\n"
5486 " src %s:%d, session #%d, session flags 0x%08x\n"
5487 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5488 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5489 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5490 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5491 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5492 es->ev_id,
5493 pn, port, es->sid, es->s_flags,
5494 es->state, es->m_flags, es->t_flags,
5495 es->m_clen, es->m_blen,
5496 es->b_flags, es->b_out, es->b_tot,
5497 es->len, es->b_wrap, es->pos);
5498
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005499 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005500 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005501 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005502 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005503 appctx->ctx.errors.ptr = 0;
5504 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005505 }
5506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005507 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005508 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005509 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005510 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005511 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005512 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005513 goto next;
5514 }
5515
5516 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005517 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005518 int newptr;
5519 int newline;
5520
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005521 newline = appctx->ctx.errors.bol;
5522 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5523 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005524 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005525
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005526 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005527 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005528 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005529 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005530 appctx->ctx.errors.ptr = newptr;
5531 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005532 };
5533 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005534 appctx->ctx.errors.bol = 0;
5535 appctx->ctx.errors.ptr = -1;
5536 appctx->ctx.errors.buf++;
5537 if (appctx->ctx.errors.buf > 1) {
5538 appctx->ctx.errors.buf = 0;
5539 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005540 }
5541 }
5542
5543 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005544 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005545}
5546
Willy Tarreaud5781202012-09-22 19:32:35 +02005547/* parse the "level" argument on the bind lines */
5548static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5549{
5550 if (!*args[cur_arg + 1]) {
5551 memprintf(err, "'%s' : missing level", args[cur_arg]);
5552 return ERR_ALERT | ERR_FATAL;
5553 }
5554
5555 if (!strcmp(args[cur_arg+1], "user"))
5556 conf->level = ACCESS_LVL_USER;
5557 else if (!strcmp(args[cur_arg+1], "operator"))
5558 conf->level = ACCESS_LVL_OPER;
5559 else if (!strcmp(args[cur_arg+1], "admin"))
5560 conf->level = ACCESS_LVL_ADMIN;
5561 else {
5562 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5563 args[cur_arg], args[cur_arg+1]);
5564 return ERR_ALERT | ERR_FATAL;
5565 }
5566
5567 return 0;
5568}
5569
Willy Tarreaub24281b2011-02-13 13:16:36 +01005570struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005571 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005572 .name = "<STATS>", /* used for logging */
5573 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005574 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005575};
5576
Simon Horman9bd2c732011-06-15 15:18:44 +09005577static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005578 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005579 .name = "<CLI>", /* used for logging */
5580 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005581 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005582};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005583
Willy Tarreaudc13c112013-06-21 23:16:39 +02005584static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005585 { CFG_GLOBAL, "stats", stats_parse_global },
5586 { 0, NULL, NULL },
5587}};
5588
Willy Tarreaud5781202012-09-22 19:32:35 +02005589static struct bind_kw_list bind_kws = { "STAT", { }, {
5590 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5591 { NULL, NULL, 0 },
5592}};
5593
Willy Tarreau10522fd2008-07-09 20:12:41 +02005594__attribute__((constructor))
5595static void __dumpstats_module_init(void)
5596{
5597 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005598 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005599}
5600
Willy Tarreau91861262007-10-17 17:06:05 +02005601/*
5602 * Local variables:
5603 * c-indent-level: 8
5604 * c-basic-offset: 8
5605 * End:
5606 */