blob: 51b47ca9e9c2df0871c339fff0606a5fd7a46117 [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;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001149 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001150 return 1;
1151 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001152 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001153 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001154 /* end of processing */
1155 return 1;
1156 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001157 else if (strcmp(args[1], "map") == 0) {
1158 struct map_reference *mref;
1159 struct map_descriptor *mdesc;
1160 struct map_entry *ent, *nent;
1161
1162 /* no parameter */
1163 if (!*args[2]) {
1164 appctx->ctx.cli.msg = "Expect map reference.\n";
1165 appctx->st0 = STAT_CLI_PRINT;
1166 return 1;
1167 }
1168
1169 /* lookup into the maps */
1170 mref = map_get_reference(args[2]);
1171 if (!mref) {
1172 appctx->ctx.cli.msg = "Unknown map reference.\n";
1173 appctx->st0 = STAT_CLI_PRINT;
1174 return 1;
1175 }
1176
1177 /* clear all maps */
1178 list_for_each_entry(mdesc, &mref->maps, list)
1179 if (mdesc->do_free)
1180 pattern_prune_expr(mdesc->pat);
1181
1182 /* clear map reference */
1183 list_for_each_entry_safe(ent, nent, &mref->entries, list) {
1184 LIST_DEL(&ent->list);
1185 free(ent->key);
1186 free(ent->value);
1187 free(ent);
1188 }
1189
1190 /* return response */
1191 appctx->ctx.cli.msg = "Done.\n";
1192 appctx->st0 = STAT_CLI_PRINT;
1193 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001194 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001195 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001196 return 0;
1197 }
1198 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001199 else if (strcmp(args[0], "get") == 0) {
1200 if (strcmp(args[1], "weight") == 0) {
1201 struct proxy *px;
1202 struct server *sv;
1203
1204 /* split "backend/server" and make <line> point to server */
1205 for (line = args[2]; *line; line++)
1206 if (*line == '/') {
1207 *line++ = '\0';
1208 break;
1209 }
1210
1211 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001212 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1213 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001214 return 1;
1215 }
1216
1217 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001218 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1219 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001220 return 1;
1221 }
1222
1223 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001224 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1225 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001226 return 1;
1227 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001228 else if (strcmp(args[1], "map") == 0) {
1229
1230 /* no parameter */
1231 if (!*args[2] || !*args[3]) {
1232 appctx->ctx.cli.msg = "Expect map reference and required key.\n";
1233 appctx->st0 = STAT_CLI_PRINT;
1234 return 1;
1235 }
1236
1237 /* lookup into the maps */
1238 appctx->ctx.map.ref = map_get_reference(args[2]);
1239 if (!appctx->ctx.map.ref) {
1240 appctx->ctx.cli.msg = "Unknown map reference.\n";
1241 appctx->st0 = STAT_CLI_PRINT;
1242 return 1;
1243 }
1244
1245 /* copy input string */
1246 appctx->ctx.map.chunk.len = strlen(args[3]);
1247 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1248 appctx->ctx.map.chunk.str = strdup(args[3]);
1249 if (!appctx->ctx.map.chunk.str) {
1250 appctx->ctx.cli.msg = "Out of memory error.\n";
1251 appctx->st0 = STAT_CLI_PRINT;
1252 return 1;
1253 }
1254
1255 /* prepare response */
1256 appctx->st2 = STAT_ST_INIT;
1257 appctx->st0 = STAT_CLI_O_MLOOK;
1258 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001259 else { /* not "get weight" */
1260 return 0;
1261 }
1262 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001263 else if (strcmp(args[0], "set") == 0) {
1264 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001265 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001266 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001267
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001268 sv = expect_server_admin(s, si, args[2]);
1269 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001270 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001271
Simon Horman7d09b9a2013-02-12 10:45:51 +09001272 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001273 /*
1274 * The user-weight may now be zero and thus
1275 * the server considered to be draining.
1276 * Update the server's drain state as necessary.
1277 */
1278 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001279 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001280 appctx->ctx.cli.msg = warning;
1281 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001282 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001283 return 1;
1284 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001285 else if (strcmp(args[1], "timeout") == 0) {
1286 if (strcmp(args[2], "cli") == 0) {
1287 unsigned timeout;
1288 const char *res;
1289
1290 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001291 appctx->ctx.cli.msg = "Expects an integer value.\n";
1292 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001293 return 1;
1294 }
1295
1296 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1297 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001298 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1299 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001300 return 1;
1301 }
1302
1303 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1304 return 1;
1305 }
1306 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001307 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1308 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001309 return 1;
1310 }
1311 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001312 else if (strcmp(args[1], "maxconn") == 0) {
1313 if (strcmp(args[2], "frontend") == 0) {
1314 struct proxy *px;
1315 struct listener *l;
1316 int v;
1317
Willy Tarreau532a4502011-09-07 22:37:44 +02001318 px = expect_frontend_admin(s, si, args[3]);
1319 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001320 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001321
1322 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001323 appctx->ctx.cli.msg = "Integer value expected.\n";
1324 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001325 return 1;
1326 }
1327
1328 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001329 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001330 appctx->ctx.cli.msg = "Value out of range.\n";
1331 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001332 return 1;
1333 }
1334
1335 /* OK, the value is fine, so we assign it to the proxy and to all of
1336 * its listeners. The blocked ones will be dequeued.
1337 */
1338 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001339 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001340 l->maxconn = v;
1341 if (l->state == LI_FULL)
1342 resume_listener(l);
1343 }
1344
1345 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1346 dequeue_all_listeners(&s->fe->listener_queue);
1347
1348 return 1;
1349 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001350 else if (strcmp(args[2], "global") == 0) {
1351 int v;
1352
Willy Tarreau290e63a2012-09-20 18:07:14 +02001353 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001354 appctx->ctx.cli.msg = stats_permission_denied_msg;
1355 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001356 return 1;
1357 }
1358
1359 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001360 appctx->ctx.cli.msg = "Expects an integer value.\n";
1361 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001362 return 1;
1363 }
1364
1365 v = atoi(args[3]);
1366 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001367 appctx->ctx.cli.msg = "Value out of range.\n";
1368 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001369 return 1;
1370 }
1371
1372 /* check for unlimited values */
1373 if (v <= 0)
1374 v = global.hardmaxconn;
1375
1376 global.maxconn = v;
1377
1378 /* Dequeues all of the listeners waiting for a resource */
1379 if (!LIST_ISEMPTY(&global_listener_queue))
1380 dequeue_all_listeners(&global_listener_queue);
1381
1382 return 1;
1383 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001384 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001385 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1386 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001387 return 1;
1388 }
1389 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001390 else if (strcmp(args[1], "rate-limit") == 0) {
1391 if (strcmp(args[2], "connections") == 0) {
1392 if (strcmp(args[3], "global") == 0) {
1393 int v;
1394
Willy Tarreau290e63a2012-09-20 18:07:14 +02001395 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001396 appctx->ctx.cli.msg = stats_permission_denied_msg;
1397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001398 return 1;
1399 }
1400
1401 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001402 appctx->ctx.cli.msg = "Expects an integer value.\n";
1403 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001404 return 1;
1405 }
1406
1407 v = atoi(args[4]);
1408 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001409 appctx->ctx.cli.msg = "Value out of range.\n";
1410 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001411 return 1;
1412 }
1413
1414 global.cps_lim = v;
1415
1416 /* Dequeues all of the listeners waiting for a resource */
1417 if (!LIST_ISEMPTY(&global_listener_queue))
1418 dequeue_all_listeners(&global_listener_queue);
1419
1420 return 1;
1421 }
1422 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1424 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001425 return 1;
1426 }
1427 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001428 else if (strcmp(args[2], "sessions") == 0) {
1429 if (strcmp(args[3], "global") == 0) {
1430 int v;
1431
1432 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1433 appctx->ctx.cli.msg = stats_permission_denied_msg;
1434 appctx->st0 = STAT_CLI_PRINT;
1435 return 1;
1436 }
1437
1438 if (!*args[4]) {
1439 appctx->ctx.cli.msg = "Expects an integer value.\n";
1440 appctx->st0 = STAT_CLI_PRINT;
1441 return 1;
1442 }
1443
1444 v = atoi(args[4]);
1445 if (v < 0) {
1446 appctx->ctx.cli.msg = "Value out of range.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
1448 return 1;
1449 }
1450
1451 global.sps_lim = v;
1452
1453 /* Dequeues all of the listeners waiting for a resource */
1454 if (!LIST_ISEMPTY(&global_listener_queue))
1455 dequeue_all_listeners(&global_listener_queue);
1456
1457 return 1;
1458 }
1459 else {
1460 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1461 appctx->st0 = STAT_CLI_PRINT;
1462 return 1;
1463 }
1464 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001465#ifdef USE_OPENSSL
1466 else if (strcmp(args[2], "ssl-sessions") == 0) {
1467 if (strcmp(args[3], "global") == 0) {
1468 int v;
1469
1470 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1471 appctx->ctx.cli.msg = stats_permission_denied_msg;
1472 appctx->st0 = STAT_CLI_PRINT;
1473 return 1;
1474 }
1475
1476 if (!*args[4]) {
1477 appctx->ctx.cli.msg = "Expects an integer value.\n";
1478 appctx->st0 = STAT_CLI_PRINT;
1479 return 1;
1480 }
1481
1482 v = atoi(args[4]);
1483 if (v < 0) {
1484 appctx->ctx.cli.msg = "Value out of range.\n";
1485 appctx->st0 = STAT_CLI_PRINT;
1486 return 1;
1487 }
1488
1489 global.ssl_lim = v;
1490
1491 /* Dequeues all of the listeners waiting for a resource */
1492 if (!LIST_ISEMPTY(&global_listener_queue))
1493 dequeue_all_listeners(&global_listener_queue);
1494
1495 return 1;
1496 }
1497 else {
1498 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1499 appctx->st0 = STAT_CLI_PRINT;
1500 return 1;
1501 }
1502 }
1503#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001504 else if (strcmp(args[2], "http-compression") == 0) {
1505 if (strcmp(args[3], "global") == 0) {
1506 int v;
1507
Willy Tarreau85d47f92012-11-21 00:29:50 +01001508 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1510 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001511 return 1;
1512 }
1513
William Lallemandd85f9172012-11-09 17:05:39 +01001514 v = atoi(args[4]);
1515 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1516 }
1517 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001518 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1519 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001520 return 1;
1521 }
1522 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001523 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001524 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001526 return 1;
1527 }
1528 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001529 else if (strcmp(args[1], "table") == 0) {
1530 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1531 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001532 else if (strcmp(args[1], "map") == 0) {
1533 struct pattern *pat_elt;
1534 struct pat_idx_elt *idx_elt;
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001535 char *value = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001536
1537 /* Expect three parameters: map name, key and new value. */
1538 if (!*args[2] || !*args[3] || !*args[4]) {
1539 appctx->ctx.cli.msg = "'set map' expect three parameters: map name, key and value.\n";
1540 appctx->st0 = STAT_CLI_PRINT;
1541 return 1;
1542 }
1543
1544 /* Lookup the reference in the maps. */
1545 appctx->ctx.map.ref = map_get_reference(args[2]);
1546 if (!appctx->ctx.map.ref) {
1547 appctx->ctx.cli.msg = "Unknown map reference.\n";
1548 appctx->st0 = STAT_CLI_PRINT;
1549 return 1;
1550 }
1551
1552 /* Lookup the entry in the reference values. */
1553 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1554 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1555 break;
1556
1557 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1558 appctx->ctx.cli.msg = "Entry not found.\n";
1559 appctx->st0 = STAT_CLI_PRINT;
1560 return 1;
1561 }
1562
1563 /* Update each reference entries. */
1564 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list) {
1565 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1566 value = strdup(args[4]);
1567 if (!value) {
1568 appctx->ctx.cli.msg = "Out of memory error.\n";
1569 appctx->st0 = STAT_CLI_PRINT;
1570 return 1;
1571 }
1572 free(appctx->ctx.map.ent->value);
1573 appctx->ctx.map.ent->value = value;
1574 }
1575 }
1576
1577 /* Change the sample. The lookup juste return the first entry, other
1578 * entries are not changed, but are never matched.
1579 */
1580 appctx->ctx.map.desc = NULL;
1581 for (stats_map_lookup_next(si);
1582 appctx->ctx.map.desc;
1583 stats_map_lookup_next(si)) {
1584 pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL);
1585 if (pat_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001586 appctx->ctx.map.desc->parse(value, pat_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001587 if (idx_elt != NULL)
Thierry FOURNIER410f8102014-01-15 18:09:36 +01001588 appctx->ctx.map.desc->parse(value, idx_elt->smp);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001589 }
1590
1591 /* The set is done, send message. */
1592 appctx->ctx.cli.msg = "Done.\n";
1593 appctx->st0 = STAT_CLI_PRINT;
1594 return 1;
1595 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001596 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001597 return 0;
1598 }
1599 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001600 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001601 if (strcmp(args[1], "agent") == 0) {
1602 struct server *sv;
1603
1604 sv = expect_server_admin(s, si, args[2]);
1605 if (!sv)
1606 return 1;
1607
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001608 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1609 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1610 appctx->st0 = STAT_CLI_PRINT;
1611 return 1;
1612 }
1613
1614 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001615 return 1;
1616 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001617 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001618 struct server *sv;
1619
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001620 sv = expect_server_admin(s, si, args[2]);
1621 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001622 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001623
Cyril Bontécd19e512010-01-31 22:34:03 +01001624 if (sv->state & SRV_MAINTAIN) {
1625 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001626 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001627 /* If this server tracks the status of another one,
1628 * we must restore the good status.
1629 */
Willy Tarreau44267702011-10-28 15:35:33 +02001630 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001631 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001632 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001633 } else {
1634 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001635 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001636 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001637 }
1638 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001639 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001640 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001641 }
1642 }
1643
Willy Tarreau532a4502011-09-07 22:37:44 +02001644 return 1;
1645 }
1646 else if (strcmp(args[1], "frontend") == 0) {
1647 struct proxy *px;
1648
1649 px = expect_frontend_admin(s, si, args[2]);
1650 if (!px)
1651 return 1;
1652
1653 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001654 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1655 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001656 return 1;
1657 }
1658
1659 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001660 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1661 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001662 return 1;
1663 }
1664
1665 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001666 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1667 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001668 return 1;
1669 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001670 return 1;
1671 }
1672 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001673 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1674 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001675 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001676 }
1677 }
1678 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001679 if (strcmp(args[1], "agent") == 0) {
1680 struct server *sv;
1681
1682 sv = expect_server_admin(s, si, args[2]);
1683 if (!sv)
1684 return 1;
1685
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001686 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001687 return 1;
1688 }
1689 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001690 struct server *sv;
1691
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001692 sv = expect_server_admin(s, si, args[2]);
1693 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001694 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001695
Cyril Bontécd19e512010-01-31 22:34:03 +01001696 if (! (sv->state & SRV_MAINTAIN)) {
1697 /* Not already in maintenance, we can change the server state */
1698 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001699 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001700 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001701 }
1702
Willy Tarreau532a4502011-09-07 22:37:44 +02001703 return 1;
1704 }
1705 else if (strcmp(args[1], "frontend") == 0) {
1706 struct proxy *px;
1707
1708 px = expect_frontend_admin(s, si, args[2]);
1709 if (!px)
1710 return 1;
1711
1712 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001713 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1714 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001715 return 1;
1716 }
1717
1718 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001719 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1720 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001721 return 1;
1722 }
1723
1724 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001725 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1726 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001727 return 1;
1728 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001729 return 1;
1730 }
1731 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001732 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1733 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001734 return 1;
1735 }
1736 }
1737 else if (strcmp(args[0], "shutdown") == 0) {
1738 if (strcmp(args[1], "frontend") == 0) {
1739 struct proxy *px;
1740
1741 px = expect_frontend_admin(s, si, args[2]);
1742 if (!px)
1743 return 1;
1744
1745 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001746 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1747 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001748 return 1;
1749 }
1750
1751 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1752 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1753 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1754 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1755 stop_proxy(px);
1756 return 1;
1757 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001758 else if (strcmp(args[1], "session") == 0) {
1759 struct session *sess, *ptr;
1760
Willy Tarreau290e63a2012-09-20 18:07:14 +02001761 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001762 appctx->ctx.cli.msg = stats_permission_denied_msg;
1763 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001764 return 1;
1765 }
1766
1767 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001768 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1769 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001770 return 1;
1771 }
1772
1773 ptr = (void *)strtoul(args[2], NULL, 0);
1774
1775 /* first, look for the requested session in the session table */
1776 list_for_each_entry(sess, &sessions, list) {
1777 if (sess == ptr)
1778 break;
1779 }
1780
1781 /* do we have the session ? */
1782 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001783 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1784 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001785 return 1;
1786 }
1787
1788 session_shutdown(sess, SN_ERR_KILLED);
1789 return 1;
1790 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001791 else if (strcmp(args[1], "sessions") == 0) {
1792 if (strcmp(args[2], "server") == 0) {
1793 struct server *sv;
1794 struct session *sess, *sess_bck;
1795
1796 sv = expect_server_admin(s, si, args[3]);
1797 if (!sv)
1798 return 1;
1799
1800 /* kill all the session that are on this server */
1801 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1802 if (sess->srv_conn == sv)
1803 session_shutdown(sess, SN_ERR_KILLED);
1804
1805 return 1;
1806 }
1807 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001808 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001810 return 1;
1811 }
1812 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001813 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001814 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1815 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001816 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001817 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001818 }
1819 else if (strcmp(args[0], "del") == 0) {
1820 if (strcmp(args[1], "map") == 0) {
1821 struct pattern *pat_elt;
1822 struct pat_idx_elt *idx_elt;
1823 struct map_entry *ent;
1824
1825 /* Expect two parameters: map name and key. */
1826 if (!*args[2] || !*args[3]) {
1827 appctx->ctx.cli.msg = "'del map' expect two parameters: map name and key.\n";
1828 appctx->st0 = STAT_CLI_PRINT;
1829 return 1;
1830 }
1831
1832 /* Lookup the reference in the maps. */
1833 appctx->ctx.map.ref = map_get_reference(args[2]);
1834 if (!appctx->ctx.map.ref) {
1835 appctx->ctx.cli.msg = "Unknown map reference.\n";
1836 appctx->st0 = STAT_CLI_PRINT;
1837 return 1;
1838 }
1839
1840 /* Lookup the entry in the reference values.
1841 * If the entry is not found in the reference, return error message.
1842 */
1843 list_for_each_entry(appctx->ctx.map.ent, &appctx->ctx.map.ref->entries, list)
1844 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0)
1845 break;
1846
1847 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries) {
1848 appctx->ctx.cli.msg = "Entry not found.\n";
1849 appctx->st0 = STAT_CLI_PRINT;
1850 return 1;
1851 }
1852
1853 /* Delete each enties from reference. */
1854 list_for_each_entry_safe(appctx->ctx.map.ent, ent, &appctx->ctx.map.ref->entries, list) {
1855 if (strcmp(args[3], appctx->ctx.map.ent->key) == 0) {
1856 LIST_DEL(&appctx->ctx.map.ent->list);
1857 free(appctx->ctx.map.ent->key);
1858 free(appctx->ctx.map.ent->value);
1859 free(appctx->ctx.map.ent);
1860 }
1861 }
1862
1863 /* Delete all matching entries for each map descritor. */
1864 appctx->ctx.map.desc = NULL;
1865 stats_map_lookup_next(si);
1866 while (appctx->ctx.map.desc) {
1867 while (pattern_lookup(args[3], appctx->ctx.map.desc->pat, &pat_elt, &idx_elt, NULL)) {
1868 if (pat_elt != NULL) {
1869 LIST_DEL(&pat_elt->list);
1870 pattern_free(pat_elt);
1871 }
1872 if (idx_elt != NULL) {
1873 ebmb_delete(&idx_elt->node);
1874 free(idx_elt);
1875 }
1876 }
1877 stats_map_lookup_next(si);
1878 }
1879
1880 /* The deletion is done, send message. */
1881 appctx->ctx.cli.msg = "Done.\n";
1882 appctx->st0 = STAT_CLI_PRINT;
1883 return 1;
1884 }
1885 else { /* unknown "del" parameter */
1886 appctx->ctx.cli.msg = "'del' only supports 'map'.\n";
1887 appctx->st0 = STAT_CLI_PRINT;
1888 return 1;
1889 }
1890 }
1891 else if (strcmp(args[0], "add") == 0) {
1892 if (strcmp(args[1], "map") == 0) {
1893 const char *params[2];
1894 struct pattern *pat;
1895 struct map_entry *ent;
1896 struct sample_storage *smp;
1897
1898 /* Expect three parameters: map name, key and new value. */
1899 if (!*args[2] || !*args[3] || !*args[4]) {
1900 appctx->ctx.cli.msg = "'add map' expect three parameters: map name, key and value.\n";
1901 appctx->st0 = STAT_CLI_PRINT;
1902 return 1;
1903 }
1904
1905 params[0] = args[3];
1906 params[1] = "";
1907
1908 /* Lookup the reference in the maps. */
1909 appctx->ctx.map.ref = map_get_reference(args[2]);
1910 if (!appctx->ctx.map.ref) {
1911 appctx->ctx.cli.msg = "Unknown map reference.\n";
1912 appctx->st0 = STAT_CLI_PRINT;
1913 return 1;
1914 }
1915
1916 /* Prepare and link the new map_entry element. If out of memory
1917 * error the action is cancelled and the descriptor are left
1918 * coherents.
1919 */
1920 ent = malloc(sizeof(*ent));
1921 if (!ent) {
1922 appctx->ctx.cli.msg = "Out of memory error.\n";
1923 appctx->st0 = STAT_CLI_PRINT;
1924 return 1;
1925 }
1926 ent->key = strdup(args[3]);
1927 if (!ent->key) {
1928 free(ent);
1929 appctx->ctx.cli.msg = "Out of memory error.\n";
1930 appctx->st0 = STAT_CLI_PRINT;
1931 return 1;
1932 }
1933 ent->value = strdup(args[4]);
1934 if (!ent->value) {
1935 free(ent->key);
1936 free(ent);
1937 appctx->ctx.cli.msg = "Out of memory error.\n";
1938 appctx->st0 = STAT_CLI_PRINT;
1939 return 1;
1940 }
1941 LIST_ADDQ(&appctx->ctx.map.ref->entries, &ent->list);
1942
1943 /* Browse each map descritor and try to insert this new value. */
1944 appctx->ctx.map.desc = NULL;
1945 for (stats_map_lookup_next(si);
1946 appctx->ctx.map.desc;
1947 stats_map_lookup_next(si)) {
1948
1949 /* Create new sample. Return out of memory error
1950 * if the memory cannot be allocated. The 'add' process
1951 * is aborted, but the already inserted entries are not
1952 * deleted.
1953 */
1954 smp = calloc(1, sizeof(*smp));
1955 if (!smp) {
1956 appctx->ctx.cli.msg = "Out of memory error. The value is not added in all maps.\n";
1957 appctx->st0 = STAT_CLI_PRINT;
1958 return 1;
1959 }
1960
1961 /* Create sample. If this function fails, the insertion
1962 * is canceled for this 'descriptor', but continue, for
1963 * the other descriptors.
1964 */
1965 if (!appctx->ctx.map.desc->parse(ent->value, smp)) {
1966 free(smp);
1967 continue;
1968 }
1969
1970 /* If the value can be indexed, get the first pattern. If
1971 * the return entry is not the indexed entry, new 'pattern' is
1972 * created by the function pattern_register(). If the 'pattern'
1973 * is NULL, new entry is created. This is ugly because the
1974 * following code interfers with the own code of the function
1975 * pattern_register().
1976 */
1977 if (appctx->ctx.map.desc->pat->match == pat_match_str ||
1978 appctx->ctx.map.desc->pat->match == pat_match_ip) {
1979 pat = LIST_NEXT(&appctx->ctx.map.desc->pat->patterns, struct pattern *, list);
1980 if (&pat->list == &appctx->ctx.map.desc->pat->patterns)
1981 pat = NULL;
1982 }
1983 else
1984 pat = NULL;
1985
1986 if (!pattern_register(appctx->ctx.map.desc->pat, params, smp, &pat, 0, NULL)) {
1987 free(smp);
1988 continue;
1989 }
1990 }
1991
1992 /* The add is done, send message. */
1993 appctx->ctx.cli.msg = "Done.\n";
1994 appctx->st0 = STAT_CLI_PRINT;
1995 return 1;
1996 }
1997 else { /* unknown "del" parameter */
1998 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
1999 appctx->st0 = STAT_CLI_PRINT;
2000 return 1;
2001 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002002 }
2003 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002004 return 0;
2005 }
2006 return 1;
2007}
2008
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002009/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002010 * used to processes I/O from/to the stats unix socket. The system relies on a
2011 * state machine handling requests and various responses. We read a request,
2012 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002013 * Then we can read again. The state is stored in appctx->st0 and is one of the
2014 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002015 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002016 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002017static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002018{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002019 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002020 struct channel *req = si->ob;
2021 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002022 int reql;
2023 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002024
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002025 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2026 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002027
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002028 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002029 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002030 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002031 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2032 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002033 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002034 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002035 /* Let's close for real now. We just close the request
2036 * side, the conditions below will complete if needed.
2037 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002038 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002039 break;
2040 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002041 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002042 /* ensure we have some output room left in the event we
2043 * would want to return some info right after parsing.
2044 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002045 if (buffer_almost_full(si->ib->buf)) {
2046 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002047 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002048 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002049
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002050 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002051 if (reql <= 0) { /* closed or EOL not found */
2052 if (reql == 0)
2053 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002054 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002055 continue;
2056 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002057
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002058 /* seek for a possible semi-colon. If we find one, we
2059 * replace it with an LF and skip only this part.
2060 */
2061 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002062 if (trash.str[len] == ';') {
2063 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002064 reql = len + 1;
2065 break;
2066 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002067
Willy Tarreau816fc222009-10-04 07:36:58 +02002068 /* now it is time to check that we have a full line,
2069 * remove the trailing \n and possibly \r, then cut the
2070 * line.
2071 */
2072 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002073 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002074 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002075 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002076 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002077
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002078 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002079 len--;
2080
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002081 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002083 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002085 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002086 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002087 continue;
2088 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002089 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002090 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002091 else if (strcmp(trash.str, "help") == 0 ||
2092 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002093 appctx->ctx.cli.msg = stats_sock_usage_msg;
2094 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002095 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002096 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002097 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002098 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002099 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002100 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002101 /* if prompt is disabled, print help on empty lines,
2102 * so that the user at least knows how to enable
2103 * prompt and find help.
2104 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002105 appctx->ctx.cli.msg = stats_sock_usage_msg;
2106 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002107 }
2108
2109 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002110 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002111 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002112 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002113 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002114 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002115 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002116 continue;
2117 }
2118
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002119 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002120 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002121 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2122 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002123 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002124 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002125 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002126 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002127 break;
2128 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002129 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002130 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002131 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002132 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002133 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002134 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002135 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002136 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002137 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002138 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002139 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002140 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002141 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002142 if (stats_table_request(si, appctx->st0))
2143 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002144 break;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002145 case STAT_CLI_O_MAPS:
2146 if (stats_maps_list(si))
2147 appctx->st0 = STAT_CLI_PROMPT;
2148 break;
2149 case STAT_CLI_O_MAP:
2150 if (stats_map_list(si))
2151 appctx->st0 = STAT_CLI_PROMPT;
2152 break;
2153 case STAT_CLI_O_MLOOK:
2154 if (stats_map_lookup(si))
2155 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002156 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002157 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002158 break;
2159 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002160
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002161 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002162 if (appctx->st0 == STAT_CLI_PROMPT) {
2163 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2164 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002165 }
2166
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002167 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002168 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002169 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002170
2171 /* Now we close the output if one of the writers did so,
2172 * or if we're not in interactive mode and the request
2173 * buffer is empty. This still allows pipelined requests
2174 * to be sent in non-interactive mode.
2175 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002176 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2177 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002178 continue;
2179 }
2180
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002181 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002182 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002183 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002185
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002186 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002187 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2188 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002189 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002190 * and nothing more to consume. This is comparable to a broken pipe, so
2191 * we forward the close to the request side so that it flows upstream to
2192 * the client.
2193 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002194 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002195 }
2196
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002197 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002198 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2199 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2200 /* We have no more processing to do, and nothing more to send, and
2201 * the client side has closed. So we'll forward this state downstream
2202 * on the response buffer.
2203 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002204 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002205 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002206 }
2207
2208 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002209 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002210
2211 /* we don't want to expire timeouts while we're processing requests */
2212 si->ib->rex = TICK_ETERNITY;
2213 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002214
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002215 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002216 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 +02002217 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002218 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 +02002219
2220 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2221 /* check that we have released everything then unregister */
2222 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002223 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002224}
2225
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002226/* This function dumps information onto the stream interface's read buffer.
2227 * It returns 0 as long as it does not complete, non-zero upon completion.
2228 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002229 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002230static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002231{
2232 unsigned int up = (now.tv_sec - start_date.tv_sec);
2233
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002234 chunk_printf(&trash,
2235 "Name: " PRODUCT_NAME "\n"
2236 "Version: " HAPROXY_VERSION "\n"
2237 "Release_date: " HAPROXY_DATE "\n"
2238 "Nbproc: %d\n"
2239 "Process_num: %d\n"
2240 "Pid: %d\n"
2241 "Uptime: %dd %dh%02dm%02ds\n"
2242 "Uptime_sec: %d\n"
2243 "Memmax_MB: %d\n"
2244 "Ulimit-n: %d\n"
2245 "Maxsock: %d\n"
2246 "Maxconn: %d\n"
2247 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002248 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002249 "CumConns: %d\n"
2250 "CumReq: %d\n"
2251#ifdef USE_OPENSSL
2252 "MaxSslConns: %d\n"
2253 "CurrSslConns: %d\n"
2254 "CumSslConns: %d\n"
2255#endif
2256 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002257 "PipesUsed: %d\n"
2258 "PipesFree: %d\n"
2259 "ConnRate: %d\n"
2260 "ConnRateLimit: %d\n"
2261 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002262 "SessRate: %d\n"
2263 "SessRateLimit: %d\n"
2264 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002265#ifdef USE_OPENSSL
2266 "SslRate: %d\n"
2267 "SslRateLimit: %d\n"
2268 "MaxSslRate: %d\n"
2269#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002270 "CompressBpsIn: %u\n"
2271 "CompressBpsOut: %u\n"
2272 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002273#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002274 "ZlibMemUsage: %ld\n"
2275 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002276#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002277 "Tasks: %d\n"
2278 "Run_queue: %d\n"
2279 "Idle_pct: %d\n"
2280 "node: %s\n"
2281 "description: %s\n"
2282 "",
2283 global.nbproc,
2284 relative_pid,
2285 pid,
2286 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2287 up,
2288 global.rlimit_memmax,
2289 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002290 global.maxsock, global.maxconn, global.hardmaxconn,
2291 actconn, totalconn, global.req_count,
2292#ifdef USE_OPENSSL
2293 global.maxsslconn, sslconns, totalsslconns,
2294#endif
2295 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002296 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002297 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002298#ifdef USE_OPENSSL
2299 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2300#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002301 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2302 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002303#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002304 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002305#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002306 nb_tasks_cur, run_queue_cur, idle_pct,
2307 global.node, global.desc ? global.desc : ""
2308 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002309
2310 if (bi_putchk(si->ib, &trash) == -1)
2311 return 0;
2312
2313 return 1;
2314}
2315
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002316/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2317 * the state from stream interface <si>. The caller is responsible for clearing
2318 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002319 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002320static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002321{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002322 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002323 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002324
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002325 if (!(px->cap & PR_CAP_FE))
2326 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002327
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002328 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002329 return 0;
2330
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002331 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002332 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002333 /* name, queue */
2334 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002335
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002336 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002337 /* Column sub-heading for Enable or Disable server */
2338 chunk_appendf(&trash, "<td></td>");
2339 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002340
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002341 chunk_appendf(&trash,
2342 "<td class=ac>"
2343 "<a name=\"%s/Frontend\"></a>"
2344 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2345 "<td colspan=3></td>"
2346 "",
2347 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002348
Willy Tarreau466c9b52012-12-23 02:25:03 +01002349 chunk_appendf(&trash,
2350 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002351 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002352 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2353 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2354 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002355 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2356 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2357 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002358
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359 if (px->mode == PR_MODE_HTTP)
2360 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002361 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002362 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002363
2364 chunk_appendf(&trash,
2365 "</table></div></u></td>"
2366 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002367 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002368 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2369 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2370 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002371 U2H(px->fe_counters.sps_max),
2372 U2H(px->fe_counters.cps_max),
2373 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002374
2375 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002376 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002377 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002378 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002379
2380 chunk_appendf(&trash,
2381 "</table></div></u></td>"
2382 /* sessions rate : limit */
2383 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002384 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002385
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002386 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002387 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002388 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002389 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002390 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2391 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002392 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002393 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2394 U2H(px->fe_counters.cum_sess),
2395 U2H(px->fe_counters.cum_conn),
2396 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002397
Willy Tarreau466c9b52012-12-23 02:25:03 +01002398 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002399 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002400 chunk_appendf(&trash,
2401 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2402 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2403 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2404 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2405 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2406 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2407 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2408 "<tr><th>- other responses:</th><td>%s</td></tr>"
2409 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2410 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002411 U2H(px->fe_counters.p.http.cum_req),
2412 U2H(px->fe_counters.p.http.rsp[1]),
2413 U2H(px->fe_counters.p.http.rsp[2]),
2414 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002415 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002416 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002417 U2H(px->fe_counters.p.http.rsp[3]),
2418 U2H(px->fe_counters.p.http.rsp[4]),
2419 U2H(px->fe_counters.p.http.rsp[5]),
2420 U2H(px->fe_counters.p.http.rsp[0]),
2421 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002422 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002423
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002424 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002425 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002426 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002427 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002428 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002429 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002430 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002431 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002432
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002433 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002434 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002435 "<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 +01002436 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002437 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002438 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2439 px->fe_counters.comp_in ?
2440 (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 +01002441 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002442
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002443 chunk_appendf(&trash,
2444 /* denied: req, resp */
2445 "<td>%s</td><td>%s</td>"
2446 /* errors : request, connect, response */
2447 "<td>%s</td><td></td><td></td>"
2448 /* warnings: retries, redispatches */
2449 "<td></td><td></td>"
2450 /* server status : reflect frontend status */
2451 "<td class=ac>%s</td>"
2452 /* rest of server: nothing */
2453 "<td class=ac colspan=8></td></tr>"
2454 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002455 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2456 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002457 px->state == PR_STREADY ? "OPEN" :
2458 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002459 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002460 else { /* CSV mode */
2461 chunk_appendf(&trash,
2462 /* pxid, name, queue cur, queue max, */
2463 "%s,FRONTEND,,,"
2464 /* sessions : current, max, limit, total */
2465 "%d,%d,%d,%lld,"
2466 /* bytes : in, out */
2467 "%lld,%lld,"
2468 /* denied: req, resp */
2469 "%lld,%lld,"
2470 /* errors : request, connect, response */
2471 "%lld,,,"
2472 /* warnings: retries, redispatches */
2473 ",,"
2474 /* server status : reflect frontend status */
2475 "%s,"
2476 /* rest of server: nothing */
2477 ",,,,,,,,"
2478 /* pid, iid, sid, throttle, lbtot, tracked, type */
2479 "%d,%d,0,,,,%d,"
2480 /* rate, rate_lim, rate_max */
2481 "%u,%u,%u,"
2482 /* check_status, check_code, check_duration */
2483 ",,,",
2484 px->id,
2485 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2486 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2487 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2488 px->fe_counters.failed_req,
2489 px->state == PR_STREADY ? "OPEN" :
2490 px->state == PR_STFULL ? "FULL" : "STOP",
2491 relative_pid, px->uuid, STATS_TYPE_FE,
2492 read_freq_ctr(&px->fe_sess_per_sec),
2493 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002494
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002495 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2496 if (px->mode == PR_MODE_HTTP) {
2497 for (i=1; i<6; i++)
2498 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2499 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2500 }
2501 else
2502 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002504 /* failed health analyses */
2505 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002506
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002507 /* requests : req_rate, req_rate_max, req_tot, */
2508 chunk_appendf(&trash, "%u,%u,%lld,",
2509 read_freq_ctr(&px->fe_req_per_sec),
2510 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2511
2512 /* errors: cli_aborts, srv_aborts */
2513 chunk_appendf(&trash, ",,");
2514
2515 /* compression: in, out, bypassed */
2516 chunk_appendf(&trash, "%lld,%lld,%lld,",
2517 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2518
2519 /* compression: comp_rsp */
2520 chunk_appendf(&trash, "%lld,",
2521 px->fe_counters.p.http.comp_rsp);
2522
2523 /* finish with EOL */
2524 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002525 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002526 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002527}
2528
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002529/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2530 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2531 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2532 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002533 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002535{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002536 struct appctx *appctx = __objt_appctx(si->end);
2537
2538 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002539 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002540 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002541 /* Column sub-heading for Enable or Disable server */
2542 chunk_appendf(&trash, "<td></td>");
2543 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002544 chunk_appendf(&trash,
2545 /* frontend name, listener name */
2546 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2547 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2548 "",
2549 px->id, l->name,
2550 (flags & ST_SHLGNDS)?"<u>":"",
2551 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002552
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002553 if (flags & ST_SHLGNDS) {
2554 char str[INET6_ADDRSTRLEN];
2555 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002556
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002557 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002558
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002559 port = get_host_port(&l->addr);
2560 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2561 case AF_INET:
2562 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2563 break;
2564 case AF_INET6:
2565 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2566 break;
2567 case AF_UNIX:
2568 chunk_appendf(&trash, "unix, ");
2569 break;
2570 case -1:
2571 chunk_appendf(&trash, "(%s), ", strerror(errno));
2572 break;
2573 }
Willy Tarreau91861262007-10-17 17:06:05 +02002574
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002576 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577 }
Willy Tarreau91861262007-10-17 17:06:05 +02002578
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002580 /* queue */
2581 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582 /* sessions rate: current, max, limit */
2583 "<td colspan=3>&nbsp;</td>"
2584 /* sessions: current, max, limit, total, lbtot */
2585 "<td>%s</td><td>%s</td><td>%s</td>"
2586 "<td>%s</td><td>&nbsp;</td>"
2587 /* bytes: in, out */
2588 "<td>%s</td><td>%s</td>"
2589 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002590 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002591 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2592 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002593
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 chunk_appendf(&trash,
2595 /* denied: req, resp */
2596 "<td>%s</td><td>%s</td>"
2597 /* errors: request, connect, response */
2598 "<td>%s</td><td></td><td></td>"
2599 /* warnings: retries, redispatches */
2600 "<td></td><td></td>"
2601 /* server status: reflect listener status */
2602 "<td class=ac>%s</td>"
2603 /* rest of server: nothing */
2604 "<td class=ac colspan=8></td></tr>"
2605 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002606 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2607 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002608 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2609 }
2610 else { /* CSV mode */
2611 chunk_appendf(&trash,
2612 /* pxid, name, queue cur, queue max, */
2613 "%s,%s,,,"
2614 /* sessions: current, max, limit, total */
2615 "%d,%d,%d,%lld,"
2616 /* bytes: in, out */
2617 "%lld,%lld,"
2618 /* denied: req, resp */
2619 "%lld,%lld,"
2620 /* errors: request, connect, response */
2621 "%lld,,,"
2622 /* warnings: retries, redispatches */
2623 ",,"
2624 /* server status: reflect listener status */
2625 "%s,"
2626 /* rest of server: nothing */
2627 ",,,,,,,,"
2628 /* pid, iid, sid, throttle, lbtot, tracked, type */
2629 "%d,%d,%d,,,,%d,"
2630 /* rate, rate_lim, rate_max */
2631 ",,,"
2632 /* check_status, check_code, check_duration */
2633 ",,,"
2634 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2635 ",,,,,,"
2636 /* failed health analyses */
2637 ","
2638 /* requests : req_rate, req_rate_max, req_tot, */
2639 ",,,"
2640 /* errors: cli_aborts, srv_aborts */
2641 ",,"
2642 /* compression: in, out, bypassed, comp_rsp */
2643 ",,,,"
2644 "\n",
2645 px->id, l->name,
2646 l->nbconn, l->counters->conn_max,
2647 l->maxconn, l->counters->cum_conn,
2648 l->counters->bytes_in, l->counters->bytes_out,
2649 l->counters->denied_req, l->counters->denied_resp,
2650 l->counters->failed_req,
2651 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2652 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2653 }
2654 return 1;
2655}
Willy Tarreau91861262007-10-17 17:06:05 +02002656
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002657/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2658 * from stream interface <si>, stats flags <flags>, and server state <state>.
2659 * The caller is responsible for clearing the trash if needed. Returns non-zero
2660 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002661 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2662 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002663 */
2664static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2665{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002666 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002667 struct server *ref = sv->track ? sv->track : sv;
2668 char str[INET6_ADDRSTRLEN];
2669 struct chunk src;
2670 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002671
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002672 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002673 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002674 "DOWN",
2675 "DN %d/%d &uarr;",
2676 "UP %d/%d &darr;",
2677 "UP",
2678 "NOLB %d/%d &darr;",
2679 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002680 "DRAIN %d/%d &darr;",
2681 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 "<i>no check</i>"
2683 };
Willy Tarreau91861262007-10-17 17:06:05 +02002684
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002685 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2686 chunk_appendf(&trash, "<tr class=\"maintain\">");
2687 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002688 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002689 "<tr class=\"%s%d\">",
2690 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002691
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002692 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002693 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002694 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2695 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002696
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002697 chunk_appendf(&trash,
2698 "<td class=ac><a name=\"%s/%s\"></a>%s"
2699 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2700 "",
2701 px->id, sv->id,
2702 (flags & ST_SHLGNDS) ? "<u>" : "",
2703 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002704
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002705 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002706 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002707
2708 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2709 case AF_INET:
2710 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2711 break;
2712 case AF_INET6:
2713 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2714 break;
2715 case AF_UNIX:
2716 chunk_appendf(&trash, "unix, ");
2717 break;
2718 case -1:
2719 chunk_appendf(&trash, "(%s), ", strerror(errno));
2720 break;
2721 default: /* address family not supported */
2722 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002723 }
Willy Tarreau91861262007-10-17 17:06:05 +02002724
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 /* id */
2726 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002727
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002728 /* cookie */
2729 if (sv->cookie) {
2730 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002731
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002732 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2733 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002734
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002735 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002736 }
2737
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002738 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002739 }
Willy Tarreau91861262007-10-17 17:06:05 +02002740
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002741 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002742 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002743 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 /* sessions rate : current, max, limit */
2745 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002746 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002748 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2749 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002750
Willy Tarreau466c9b52012-12-23 02:25:03 +01002751
2752 chunk_appendf(&trash,
2753 /* sessions: current, max, limit, total */
2754 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002755 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002756 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2757 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002758 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2759 U2H(sv->counters.cum_sess),
2760 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002761
Willy Tarreau466c9b52012-12-23 02:25:03 +01002762 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2763 if (px->mode == PR_MODE_HTTP) {
2764 unsigned long long tot;
2765 for (tot = i = 0; i < 6; i++)
2766 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767
Willy Tarreau466c9b52012-12-23 02:25:03 +01002768 chunk_appendf(&trash,
2769 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2770 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2771 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2772 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2773 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2774 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2775 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2776 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002777 U2H(tot),
2778 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2779 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2780 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2781 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2782 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2783 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 +02002784 }
Willy Tarreau91861262007-10-17 17:06:05 +02002785
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002786 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002787 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002788 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002789 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002790 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002791
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792 chunk_appendf(&trash,
2793 /* bytes : in, out */
2794 "<td>%s</td><td>%s</td>"
2795 /* denied: req, resp */
2796 "<td></td><td>%s</td>"
2797 /* errors : request, connect */
2798 "<td></td><td>%s</td>"
2799 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002800 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801 /* warnings: retries, redispatches */
2802 "<td>%lld</td><td>%lld</td>"
2803 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002804 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2805 U2H(sv->counters.failed_secu),
2806 U2H(sv->counters.failed_conns),
2807 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002808 sv->counters.cli_aborts,
2809 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810 sv->counters.retries, sv->counters.redispatches);
2811
2812 /* status, lest check */
2813 chunk_appendf(&trash, "<td class=ac>");
2814
2815 if (sv->state & SRV_MAINTAIN) {
2816 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2817 chunk_appendf(&trash, "MAINT");
2818 }
2819 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2820 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2821 chunk_appendf(&trash, "MAINT(via)");
2822 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002823 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002824 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2825 chunk_appendf(&trash,
2826 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002827 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2828 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002829 }
Willy Tarreau91861262007-10-17 17:06:05 +02002830
Willy Tarreauff5ae352013-12-11 20:36:34 +01002831 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002832 chunk_appendf(&trash,
2833 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002834 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002835 get_check_status_info(sv->check.status));
2836
2837 if (sv->check.status >= HCHK_STATUS_L57DATA)
2838 chunk_appendf(&trash, "/%d", sv->check.code);
2839
2840 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002841 chunk_appendf(&trash, " in %lums", sv->check.duration);
2842
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002843 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002844 get_check_status_description(sv->check.status));
2845 if (*sv->check.desc) {
2846 chunk_appendf(&trash, ": ");
2847 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2848 chunk_htmlencode(&trash, &src);
2849 }
2850 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 }
2852 else
2853 chunk_appendf(&trash, "</td><td>");
2854
2855 chunk_appendf(&trash,
2856 /* weight */
2857 "</td><td class=ac>%d</td>"
2858 /* act, bck */
2859 "<td class=ac>%s</td><td class=ac>%s</td>"
2860 "",
2861 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2862 (sv->state & SRV_BACKUP) ? "-" : "Y",
2863 (sv->state & SRV_BACKUP) ? "Y" : "-");
2864
2865 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002866 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002867 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002868
2869 if (ref->observe)
2870 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2871
2872 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002873 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002874 "<td>%lld</td><td>%s</td>"
2875 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002876 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002877 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2878 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002879 else if (sv != ref) {
2880 if (sv->state & SRV_MAINTAIN)
2881 chunk_appendf(&trash,
2882 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2883 ref->proxy->id, ref->id);
2884 else
2885 chunk_appendf(&trash,
2886 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2887 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2888 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002889 else
2890 chunk_appendf(&trash, "<td colspan=3></td>");
2891
2892 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002893 if (sv->state & SRV_WARMINGUP)
2894 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002895 else
2896 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2897 }
2898 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002899 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002900 "DOWN,",
2901 "DOWN %d/%d,",
2902 "UP %d/%d,",
2903 "UP,",
2904 "NOLB %d/%d,",
2905 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002906 "DRAIN %d/%d,",
2907 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002908 "no check,"
2909 };
2910
2911 chunk_appendf(&trash,
2912 /* pxid, name */
2913 "%s,%s,"
2914 /* queue : current, max */
2915 "%d,%d,"
2916 /* sessions : current, max, limit, total */
2917 "%d,%d,%s,%lld,"
2918 /* bytes : in, out */
2919 "%lld,%lld,"
2920 /* denied: req, resp */
2921 ",%lld,"
2922 /* errors : request, connect, response */
2923 ",%lld,%lld,"
2924 /* warnings: retries, redispatches */
2925 "%lld,%lld,"
2926 "",
2927 px->id, sv->id,
2928 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002929 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002930 sv->counters.bytes_in, sv->counters.bytes_out,
2931 sv->counters.failed_secu,
2932 sv->counters.failed_conns, sv->counters.failed_resp,
2933 sv->counters.retries, sv->counters.redispatches);
2934
2935 /* status */
2936 if (sv->state & SRV_MAINTAIN)
2937 chunk_appendf(&trash, "MAINT,");
2938 else if (ref != sv && ref->state & SRV_MAINTAIN)
2939 chunk_appendf(&trash, "MAINT(via),");
2940 else
2941 chunk_appendf(&trash,
2942 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002943 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2944 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002945
2946 chunk_appendf(&trash,
2947 /* weight, active, backup */
2948 "%d,%d,%d,"
2949 "",
2950 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2951 (sv->state & SRV_BACKUP) ? 0 : 1,
2952 (sv->state & SRV_BACKUP) ? 1 : 0);
2953
2954 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002955 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956 chunk_appendf(&trash,
2957 "%lld,%lld,%d,%d,",
2958 sv->counters.failed_checks, sv->counters.down_trans,
2959 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2960 else
2961 chunk_appendf(&trash, ",,,,");
2962
2963 /* queue limit, pid, iid, sid, */
2964 chunk_appendf(&trash,
2965 "%s,"
2966 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002967 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002968 relative_pid, px->uuid, sv->puid);
2969
2970 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002971 if (sv->state & SRV_WARMINGUP)
2972 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973
2974 /* sessions: lbtot */
2975 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2976
2977 /* tracked */
2978 if (sv->track)
2979 chunk_appendf(&trash, "%s/%s,",
2980 sv->track->proxy->id, sv->track->id);
2981 else
2982 chunk_appendf(&trash, ",");
2983
2984 /* type */
2985 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2986
2987 /* rate */
2988 chunk_appendf(&trash, "%u,,%u,",
2989 read_freq_ctr(&sv->sess_per_sec),
2990 sv->counters.sps_max);
2991
Willy Tarreauff5ae352013-12-11 20:36:34 +01002992 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993 /* check_status */
2994 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2995
2996 /* check_code */
2997 if (sv->check.status >= HCHK_STATUS_L57DATA)
2998 chunk_appendf(&trash, "%u,", sv->check.code);
2999 else
3000 chunk_appendf(&trash, ",");
3001
3002 /* check_duration */
3003 if (sv->check.status >= HCHK_STATUS_CHECKED)
3004 chunk_appendf(&trash, "%lu,", sv->check.duration);
3005 else
3006 chunk_appendf(&trash, ",");
3007
3008 }
3009 else
3010 chunk_appendf(&trash, ",,,");
3011
3012 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3013 if (px->mode == PR_MODE_HTTP) {
3014 for (i=1; i<6; i++)
3015 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3016
3017 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3018 }
3019 else
3020 chunk_appendf(&trash, ",,,,,,");
3021
3022 /* failed health analyses */
3023 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3024
3025 /* requests : req_rate, req_rate_max, req_tot, */
3026 chunk_appendf(&trash, ",,,");
3027
3028 /* errors: cli_aborts, srv_aborts */
3029 chunk_appendf(&trash, "%lld,%lld,",
3030 sv->counters.cli_aborts, sv->counters.srv_aborts);
3031
3032 /* compression: in, out, bypassed, comp_rsp */
3033 chunk_appendf(&trash, ",,,,");
3034
3035 /* finish with EOL */
3036 chunk_appendf(&trash, "\n");
3037 }
3038 return 1;
3039}
3040
3041/* Dumps a line for backend <px> to the trash for and uses the state from stream
3042 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3043 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3044 */
3045static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3046{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003047 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003048 struct chunk src;
3049 int i;
3050
3051 if (!(px->cap & PR_CAP_BE))
3052 return 0;
3053
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003054 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003055 return 0;
3056
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003057 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003058 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003059 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003060 /* Column sub-heading for Enable or Disable server */
3061 chunk_appendf(&trash, "<td></td>");
3062 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003063 chunk_appendf(&trash,
3064 "<td class=ac>"
3065 /* name */
3066 "%s<a name=\"%s/Backend\"></a>"
3067 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3068 "",
3069 (flags & ST_SHLGNDS)?"<u>":"",
3070 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003071
3072 if (flags & ST_SHLGNDS) {
3073 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003074 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003075 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3076
3077 /* cookie */
3078 if (px->cookie_name) {
3079 chunk_appendf(&trash, ", cookie: '");
3080 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3081 chunk_htmlencode(&trash, &src);
3082 chunk_appendf(&trash, "'");
3083 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003084 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003085 }
3086
3087 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003088 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003089 /* queue : current, max */
3090 "<td>%s</td><td>%s</td><td></td>"
3091 /* sessions rate : current, max, limit */
3092 "<td>%s</td><td>%s</td><td></td>"
3093 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003094 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003095 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3096 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003097
3098 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003099 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003100 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003101 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003102 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003103 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003104 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3105 U2H(px->be_counters.cum_conn),
3106 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107
Willy Tarreau466c9b52012-12-23 02:25:03 +01003108 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003109 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003110 chunk_appendf(&trash,
3111 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3112 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3113 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3114 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3115 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3116 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3117 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3118 "<tr><th>- other responses:</th><td>%s</td></tr>"
3119 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3120 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003121 U2H(px->be_counters.p.http.cum_req),
3122 U2H(px->be_counters.p.http.rsp[1]),
3123 U2H(px->be_counters.p.http.rsp[2]),
3124 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003126 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003127 U2H(px->be_counters.p.http.rsp[3]),
3128 U2H(px->be_counters.p.http.rsp[4]),
3129 U2H(px->be_counters.p.http.rsp[5]),
3130 U2H(px->be_counters.p.http.rsp[0]),
3131 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132 }
3133
3134 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003135 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003136 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01003137 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003139 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003141 U2H(px->be_counters.cum_lbconn),
3142 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003144 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003145 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003146 "<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 +01003147 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003148 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003149 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3150 px->be_counters.comp_in ?
3151 (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 +01003152 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3153
3154 chunk_appendf(&trash,
3155 /* denied: req, resp */
3156 "<td>%s</td><td>%s</td>"
3157 /* errors : request, connect */
3158 "<td></td><td>%s</td>"
3159 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003160 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003161 /* warnings: retries, redispatches */
3162 "<td>%lld</td><td>%lld</td>"
3163 /* backend status: reflect backend status (up/down): we display UP
3164 * if the backend has known working servers or if it has no server at
3165 * all (eg: for stats). Then we display the total weight, number of
3166 * active and backups. */
3167 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3168 "<td class=ac>%d</td><td class=ac>%d</td>"
3169 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003170 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3171 U2H(px->be_counters.failed_conns),
3172 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173 px->be_counters.cli_aborts,
3174 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 px->be_counters.retries, px->be_counters.redispatches,
3176 human_time(now.tv_sec - px->last_change, 1),
3177 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3178 "<font color=\"red\"><b>DOWN</b></font>",
3179 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3180 px->srv_act, px->srv_bck);
3181
3182 chunk_appendf(&trash,
3183 /* rest of backend: nothing, down transitions, total downtime, throttle */
3184 "<td class=ac>&nbsp;</td><td>%d</td>"
3185 "<td>%s</td>"
3186 "<td></td>"
3187 "</tr>",
3188 px->down_trans,
3189 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3190 }
3191 else { /* CSV mode */
3192 chunk_appendf(&trash,
3193 /* pxid, name */
3194 "%s,BACKEND,"
3195 /* queue : current, max */
3196 "%d,%d,"
3197 /* sessions : current, max, limit, total */
3198 "%d,%d,%d,%lld,"
3199 /* bytes : in, out */
3200 "%lld,%lld,"
3201 /* denied: req, resp */
3202 "%lld,%lld,"
3203 /* errors : request, connect, response */
3204 ",%lld,%lld,"
3205 /* warnings: retries, redispatches */
3206 "%lld,%lld,"
3207 /* backend status: reflect backend status (up/down): we display UP
3208 * if the backend has known working servers or if it has no server at
3209 * all (eg: for stats). Then we display the total weight, number of
3210 * active and backups. */
3211 "%s,"
3212 "%d,%d,%d,"
3213 /* rest of backend: nothing, down transitions, last change, total downtime */
3214 ",%d,%d,%d,,"
3215 /* pid, iid, sid, throttle, lbtot, tracked, type */
3216 "%d,%d,0,,%lld,,%d,"
3217 /* rate, rate_lim, rate_max, */
3218 "%u,,%u,"
3219 /* check_status, check_code, check_duration */
3220 ",,,",
3221 px->id,
3222 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3223 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3224 px->be_counters.bytes_in, px->be_counters.bytes_out,
3225 px->be_counters.denied_req, px->be_counters.denied_resp,
3226 px->be_counters.failed_conns, px->be_counters.failed_resp,
3227 px->be_counters.retries, px->be_counters.redispatches,
3228 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3229 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3230 px->srv_act, px->srv_bck,
3231 px->down_trans, (int)(now.tv_sec - px->last_change),
3232 px->srv?be_downtime(px):0,
3233 relative_pid, px->uuid,
3234 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3235 read_freq_ctr(&px->be_sess_per_sec),
3236 px->be_counters.sps_max);
3237
3238 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3239 if (px->mode == PR_MODE_HTTP) {
3240 for (i=1; i<6; i++)
3241 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3242 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3243 }
3244 else
3245 chunk_appendf(&trash, ",,,,,,");
3246
3247 /* failed health analyses */
3248 chunk_appendf(&trash, ",");
3249
3250 /* requests : req_rate, req_rate_max, req_tot, */
3251 chunk_appendf(&trash, ",,,");
3252
3253 /* errors: cli_aborts, srv_aborts */
3254 chunk_appendf(&trash, "%lld,%lld,",
3255 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3256
3257 /* compression: in, out, bypassed */
3258 chunk_appendf(&trash, "%lld,%lld,%lld,",
3259 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3260
3261 /* compression: comp_rsp */
3262 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3263
3264 /* finish with EOL */
3265 chunk_appendf(&trash, "\n");
3266 }
3267 return 1;
3268}
3269
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003270/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003271 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003272 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003273 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003274static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003275{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003276 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003277 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3278
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003279 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003280 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003282 /* 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 +02003283 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003284 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003285 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003286 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3287 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003288 }
3289
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003290 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003291 "<form action=\"%s%s%s%s\" method=\"post\">",
3292 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3294 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003295 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003296 }
3297
3298 /* print a new table */
3299 chunk_appendf(&trash,
3300 "<table class=\"tbl\" width=\"100%%\">\n"
3301 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003302 "<th class=\"pxname\" width=\"10%%\">");
3303
3304 chunk_appendf(&trash,
3305 "<a name=\"%s\"></a>%s"
3306 "<a class=px href=\"#%s\">%s</a>",
3307 px->id,
3308 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3309 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310
3311 if (uri->flags & ST_SHLGNDS) {
3312 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003313 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3315 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003316 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 }
3318
3319 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003320 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003321 "<th class=\"%s\" width=\"90%%\">%s</th>"
3322 "</tr>\n"
3323 "</table>\n"
3324 "<table class=\"tbl\" width=\"100%%\">\n"
3325 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003326 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3327 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3328
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003329 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003330 /* Column heading for Enable or Disable server */
3331 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003332 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333
3334 chunk_appendf(&trash,
3335 "<th rowspan=2></th>"
3336 "<th colspan=3>Queue</th>"
3337 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
3338 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3339 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3340 "<th colspan=9>Server</th>"
3341 "</tr>\n"
3342 "<tr class=\"titre\">"
3343 "<th>Cur</th><th>Max</th><th>Limit</th>"
3344 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
3345 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
3346 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3347 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3348 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3349 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3350 "<th>Thrtle</th>\n"
3351 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003352}
3353
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003354/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003357static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003359 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 chunk_appendf(&trash, "</table>");
3361
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003362 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 /* close the form used to enable/disable this proxy servers */
3364 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003365 "Choose the action to perform on the checked servers : "
3366 "<select name=action>"
3367 "<option value=\"\"></option>"
3368 "<option value=\"disable\">Disable</option>"
3369 "<option value=\"enable\">Enable</option>"
3370 "<option value=\"stop\">Soft Stop</option>"
3371 "<option value=\"start\">Soft Start</option>"
3372 "<option value=\"shutdown\">Kill Sessions</option>"
3373 "</select>"
3374 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3375 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3376 "</form>",
3377 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003378 }
3379
3380 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003381}
Willy Tarreau91861262007-10-17 17:06:05 +02003382
3383/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003384 * Dumps statistics for a proxy. The output is sent to the stream interface's
3385 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3386 * buffer space, or non-zero if everything completed. This function is used
3387 * both by the CLI and the HTTP entry points, and is able to dump the output
3388 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003389 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003390static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003391{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003393 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003394 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003395 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003396 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003397
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003398 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003399
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003400 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003401 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003402 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003403 if (uri && uri->scope) {
3404 /* we have a limited scope, we have to check the proxy name */
3405 struct stat_scope *scope;
3406 int len;
3407
3408 len = strlen(px->id);
3409 scope = uri->scope;
3410
3411 while (scope) {
3412 /* match exact proxy name */
3413 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3414 break;
3415
3416 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003417 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003418 break;
3419 scope = scope->next;
3420 }
3421
3422 /* proxy name not found : don't dump anything */
3423 if (scope == NULL)
3424 return 1;
3425 }
3426
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003427 /* if the user has requested a limited output and the proxy
3428 * name does not match, skip it.
3429 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003430 if (appctx->ctx.stats.scope_len &&
3431 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 +02003432 return 1;
3433
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003434 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3435 (appctx->ctx.stats.iid != -1) &&
3436 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003437 return 1;
3438
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003439 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003440 /* fall through */
3441
Willy Tarreau295a8372011-03-10 11:25:07 +01003442 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003443 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003444 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003445 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003446 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003447 }
Willy Tarreau91861262007-10-17 17:06:05 +02003448
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003449 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003450 /* fall through */
3451
Willy Tarreau295a8372011-03-10 11:25:07 +01003452 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003453 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003455 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003456 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003457
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003458 appctx->ctx.stats.l = px->conf.listeners.n;
3459 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003460 /* fall through */
3461
Willy Tarreau295a8372011-03-10 11:25:07 +01003462 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003463 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003464 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003465 if (buffer_almost_full(rep->buf)) {
3466 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003467 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003468 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003469
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003470 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003471 if (!l->counters)
3472 continue;
3473
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003474 if (appctx->ctx.stats.flags & STAT_BOUND) {
3475 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003476 break;
3477
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003478 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003479 continue;
3480 }
3481
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003482 /* print the frontend */
3483 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3484 if (bi_putchk(rep, &trash) == -1)
3485 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003486 }
3487
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003488 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3489 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003490 /* fall through */
3491
Willy Tarreau295a8372011-03-10 11:25:07 +01003492 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003493 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003494 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003495 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 +02003496
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003497 if (buffer_almost_full(rep->buf)) {
3498 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003499 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003500 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003501
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003502 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003503
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003504 if (appctx->ctx.stats.flags & STAT_BOUND) {
3505 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003506 break;
3507
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003508 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003509 continue;
3510 }
3511
Willy Tarreau44267702011-10-28 15:35:33 +02003512 if (sv->track)
3513 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003514 else
3515 svs = sv;
3516
Willy Tarreau91861262007-10-17 17:06:05 +02003517 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003518 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003519 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003520 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003521 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003522 sv_state = 3; /* UP */
3523 else
3524 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003525
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003526 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003527 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003528 else if (svs->state & SRV_GOINGDOWN)
3529 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003530 }
Willy Tarreau91861262007-10-17 17:06:05 +02003531 else
Simon Horman125d0992013-02-24 17:23:38 +09003532 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003533 sv_state = 1; /* going up */
3534 else
3535 sv_state = 0; /* DOWN */
3536
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003537 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003538 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003539 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003540 continue;
3541 }
3542
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003543 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3544 if (bi_putchk(rep, &trash) == -1)
3545 return 0;
3546 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003548 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003549 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003550
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551 case STAT_PX_ST_BE:
3552 /* print the backend */
3553 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3554 if (bi_putchk(rep, &trash) == -1)
3555 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003557 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003558 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003559
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003560 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003561 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003562 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003563 if (bi_putchk(rep, &trash) == -1)
3564 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003565 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003566
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003567 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003568 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003569
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003570 case STAT_PX_ST_FIN:
3571 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003572
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003573 default:
3574 /* unknown state, we should put an abort() here ! */
3575 return 1;
3576 }
3577}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003578
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003579/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3580 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003581 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003582static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003583{
3584 /* WARNING! This must fit in the first buffer !!! */
3585 chunk_appendf(&trash,
3586 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3587 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3588 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3589 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3590 "<style type=\"text/css\"><!--\n"
3591 "body {"
3592 " font-family: arial, helvetica, sans-serif;"
3593 " font-size: 12px;"
3594 " font-weight: normal;"
3595 " color: black;"
3596 " background: white;"
3597 "}\n"
3598 "th,td {"
3599 " font-size: 10px;"
3600 "}\n"
3601 "h1 {"
3602 " font-size: x-large;"
3603 " margin-bottom: 0.5em;"
3604 "}\n"
3605 "h2 {"
3606 " font-family: helvetica, arial;"
3607 " font-size: x-large;"
3608 " font-weight: bold;"
3609 " font-style: italic;"
3610 " color: #6020a0;"
3611 " margin-top: 0em;"
3612 " margin-bottom: 0em;"
3613 "}\n"
3614 "h3 {"
3615 " font-family: helvetica, arial;"
3616 " font-size: 16px;"
3617 " font-weight: bold;"
3618 " color: #b00040;"
3619 " background: #e8e8d0;"
3620 " margin-top: 0em;"
3621 " margin-bottom: 0em;"
3622 "}\n"
3623 "li {"
3624 " margin-top: 0.25em;"
3625 " margin-right: 2em;"
3626 "}\n"
3627 ".hr {margin-top: 0.25em;"
3628 " border-color: black;"
3629 " border-bottom-style: solid;"
3630 "}\n"
3631 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3632 ".total {background: #20D0D0;color: #ffff80;}\n"
3633 ".frontend {background: #e8e8d0;}\n"
3634 ".socket {background: #d0d0d0;}\n"
3635 ".backend {background: #e8e8d0;}\n"
3636 ".active0 {background: #ff9090;}\n"
3637 ".active1 {background: #ffd020;}\n"
3638 ".active2 {background: #ffffa0;}\n"
3639 ".active3 {background: #c0ffc0;}\n"
3640 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003641 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3642 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3643 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003644 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003645 ".backup0 {background: #ff9090;}\n"
3646 ".backup1 {background: #ff80ff;}\n"
3647 ".backup2 {background: #c060ff;}\n"
3648 ".backup3 {background: #b0d0ff;}\n"
3649 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3650 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003651 ".backup6 {background: #c060ff;}\n"
3652 ".backup7 {background: #cc9900;}\n"
3653 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654 ".maintain {background: #c07820;}\n"
3655 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3656 "\n"
3657 "a.px:link {color: #ffff40; text-decoration: none;}"
3658 "a.px:visited {color: #ffff40; text-decoration: none;}"
3659 "a.px:hover {color: #ffffff; text-decoration: none;}"
3660 "a.lfsb:link {color: #000000; text-decoration: none;}"
3661 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3662 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3663 "\n"
3664 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3665 "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"
3666 "table.tbl td.ac { text-align: center;}\n"
3667 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3668 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3669 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3670 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3671 "\n"
3672 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3673 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3674 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003675 "table.det { border-collapse: collapse; border-style: none; }\n"
3676 "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 +01003677 "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 +01003678 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003679 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003680 " display:block;\n"
3681 " visibility:hidden;\n"
3682 " z-index:2147483647;\n"
3683 " position:absolute;\n"
3684 " padding:2px 4px 3px;\n"
3685 " background:#f0f060; color:#000000;\n"
3686 " border:1px solid #7040c0;\n"
3687 " white-space:nowrap;\n"
3688 " font-style:normal;font-size:11px;font-weight:normal;\n"
3689 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3690 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3691 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003692 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003693 "-->\n"
3694 "</style></head>\n",
3695 (uri->flags & ST_SHNODE) ? " on " : "",
3696 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3697 );
3698}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003699
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003700/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003701 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003702 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003703 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003704static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003705{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003706 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003707 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003708 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003709
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003710 /* WARNING! this has to fit the first packet too.
3711 * We are around 3.5 kB, add adding entries will
3712 * become tricky if we want to support 4kB buffers !
3713 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003714 chunk_appendf(&trash,
3715 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3716 PRODUCT_NAME "%s</a></h1>\n"
3717 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3718 "<hr width=\"100%%\" class=\"hr\">\n"
3719 "<h3>&gt; General process information</h3>\n"
3720 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3721 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3722 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3723 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3724 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3725 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3726 "Running tasks: %d/%d; idle = %d %%<br>\n"
3727 "</td><td align=\"center\" nowrap>\n"
3728 "<table class=\"lgd\"><tr>\n"
3729 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3730 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3731 "</tr><tr>\n"
3732 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3733 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3734 "</tr><tr>\n"
3735 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3736 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3737 "</tr><tr>\n"
3738 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003739 "</tr><tr>\n"
3740 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003741 "</tr><tr>\n"
3742 "<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 -07003743 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003744 "<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 +01003745 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003746 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003747 "</td>"
3748 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3749 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3750 "",
3751 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3752 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3753 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3754 (uri->flags & ST_SHDESC) ? ": " : "",
3755 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3756 pid, relative_pid, global.nbproc,
3757 up / 86400, (up % 86400) / 3600,
3758 (up % 3600) / 60, (up % 60),
3759 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3760 global.rlimit_memmax ? " MB" : "",
3761 global.rlimit_nofile,
3762 global.maxsock, global.maxconn, global.maxpipes,
3763 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3764 run_queue_cur, nb_tasks_cur, idle_pct
3765 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003766
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003767 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3768 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3769 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003770
3771 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003772 "<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 +02003773 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003774 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3775 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3776 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003777 STAT_SCOPE_TXT_MAXLEN);
3778
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003779 /* 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 +02003780 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003781 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003782 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003783 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3784 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003785 }
3786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003787 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003788 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003789 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003790 uri->uri_prefix,
3791 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003792 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003793 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003794 else
3795 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003796 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 uri->uri_prefix,
3798 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003799 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003800 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003801
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003802 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003803 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003805 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003806 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003807 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003808 "",
3809 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 else
3811 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003812 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003813 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003814 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003815 ";norefresh",
3816 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003820 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003821 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003822 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3823 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003824 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003825
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003827 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003829 (uri->refresh > 0) ? ";norefresh" : "",
3830 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003831
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003832 chunk_appendf(&trash,
3833 "</ul></td>"
3834 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3835 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3836 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3837 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3838 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3839 "</ul>"
3840 "</td>"
3841 "</tr></table>\n"
3842 ""
3843 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003844
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003845 if (appctx->ctx.stats.st_code) {
3846 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003847 case STAT_STATUS_DONE:
3848 chunk_appendf(&trash,
3849 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003850 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003851 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003852 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3854 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003855 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003856 break;
3857 case STAT_STATUS_NONE:
3858 chunk_appendf(&trash,
3859 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003860 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003862 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003863 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3864 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003865 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003866 break;
3867 case STAT_STATUS_PART:
3868 chunk_appendf(&trash,
3869 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003870 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003871 "Action partially processed.<br>"
3872 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003873 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003874 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3875 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003876 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003877 break;
3878 case STAT_STATUS_ERRP:
3879 chunk_appendf(&trash,
3880 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003881 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003882 "Action not processed because of invalid parameters."
3883 "<ul>"
3884 "<li>The action is maybe unknown.</li>"
3885 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3886 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3887 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003888 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003889 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3890 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003891 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003892 break;
3893 case STAT_STATUS_EXCD:
3894 chunk_appendf(&trash,
3895 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003896 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003897 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3898 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003899 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003900 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3901 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003902 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003903 break;
3904 case STAT_STATUS_DENY:
3905 chunk_appendf(&trash,
3906 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003907 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003908 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003909 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003910 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3911 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003912 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003913 break;
3914 default:
3915 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003916 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003917 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003918 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003919 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003920 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3921 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003922 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923 }
3924 chunk_appendf(&trash, "<p>\n");
3925 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003926}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003927
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003928/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3929 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003931static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003932{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003933 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003934}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003935
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003936/* This function dumps statistics onto the stream interface's read buffer in
3937 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003938 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3939 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3940 * and the session must be closed, or -1 in case of any error. This function is
3941 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003942 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003943static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003944{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003945 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003946 struct channel *rep = si->ib;
3947 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003948
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003949 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003950
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003951 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003952 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003953 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003954 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003955
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003956 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003957 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003958 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003959 else
3960 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003961
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962 if (bi_putchk(rep, &trash) == -1)
3963 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003964
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003965 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003967
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003968 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003969 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003970 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003971 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003972 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003973 }
Willy Tarreau91861262007-10-17 17:06:05 +02003974
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003975 appctx->ctx.stats.px = proxy;
3976 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3977 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003978 /* fall through */
3979
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003980 case STAT_ST_LIST:
3981 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003982 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003983 if (buffer_almost_full(rep->buf)) {
3984 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003986 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003987
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003988 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003989 /* skip the disabled proxies, global frontend and non-networked ones */
3990 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003991 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003994 appctx->ctx.stats.px = px->next;
3995 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003996 }
3997 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003998
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003999 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004001
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004002 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004003 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004004 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 if (bi_putchk(rep, &trash) == -1)
4006 return 0;
4007 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004009 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004010 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004011
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004012 case STAT_ST_FIN:
4013 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004015 default:
4016 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004017 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004018 return -1;
4019 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004020}
Willy Tarreauae526782010-03-04 20:34:23 +01004021
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004022/* We reached the stats page through a POST request. The appctx is
4023 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004024 * Parse the posted data and enable/disable servers if necessary.
4025 * Returns 1 if request was parsed or zero if it needs more data.
4026 */
4027static int stats_process_http_post(struct stream_interface *si)
4028{
4029 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004030 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004031
4032 struct proxy *px = NULL;
4033 struct server *sv = NULL;
4034
4035 char key[LINESIZE];
4036 int action = ST_ADM_ACTION_NONE;
4037 int reprocess = 0;
4038
4039 int total_servers = 0;
4040 int altered_servers = 0;
4041
4042 char *first_param, *cur_param, *next_param, *end_params;
4043 char *st_cur_param = NULL;
4044 char *st_next_param = NULL;
4045
4046 struct chunk *temp;
4047 int reql;
4048
4049 temp = get_trash_chunk();
4050 if (temp->size < s->txn.req.body_len) {
4051 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004052 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004053 goto out;
4054 }
4055
4056 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4057 if (reql <= 0) {
4058 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004059 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004060 return 0;
4061 }
4062
4063 first_param = temp->str;
4064 end_params = temp->str + reql;
4065 cur_param = next_param = end_params;
4066 *end_params = '\0';
4067
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004068 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004069
4070 /*
4071 * Parse the parameters in reverse order to only store the last value.
4072 * From the html form, the backend and the action are at the end.
4073 */
4074 while (cur_param > first_param) {
4075 char *value;
4076 int poffset, plen;
4077
4078 cur_param--;
4079
4080 if ((*cur_param == '&') || (cur_param == first_param)) {
4081 reprocess_servers:
4082 /* Parse the key */
4083 poffset = (cur_param != first_param ? 1 : 0);
4084 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4085 if ((plen > 0) && (plen <= sizeof(key))) {
4086 strncpy(key, cur_param + poffset, plen);
4087 key[plen - 1] = '\0';
4088 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004089 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004090 goto out;
4091 }
4092
4093 /* Parse the value */
4094 value = key;
4095 while (*value != '\0' && *value != '=') {
4096 value++;
4097 }
4098 if (*value == '=') {
4099 /* Ok, a value is found, we can mark the end of the key */
4100 *value++ = '\0';
4101 }
4102 if (url_decode(key) < 0 || url_decode(value) < 0)
4103 break;
4104
4105 /* Now we can check the key to see what to do */
4106 if (!px && (strcmp(key, "b") == 0)) {
4107 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4108 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004109 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004110 goto out;
4111 }
4112 }
4113 else if (!action && (strcmp(key, "action") == 0)) {
4114 if (strcmp(value, "disable") == 0) {
4115 action = ST_ADM_ACTION_DISABLE;
4116 }
4117 else if (strcmp(value, "enable") == 0) {
4118 action = ST_ADM_ACTION_ENABLE;
4119 }
4120 else if (strcmp(value, "stop") == 0) {
4121 action = ST_ADM_ACTION_STOP;
4122 }
4123 else if (strcmp(value, "start") == 0) {
4124 action = ST_ADM_ACTION_START;
4125 }
4126 else if (strcmp(value, "shutdown") == 0) {
4127 action = ST_ADM_ACTION_SHUTDOWN;
4128 }
4129 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004130 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004131 goto out;
4132 }
4133 }
4134 else if (strcmp(key, "s") == 0) {
4135 if (!(px && action)) {
4136 /*
4137 * Indicates that we'll need to reprocess the parameters
4138 * as soon as backend and action are known
4139 */
4140 if (!reprocess) {
4141 st_cur_param = cur_param;
4142 st_next_param = next_param;
4143 }
4144 reprocess = 1;
4145 }
4146 else if ((sv = findserver(px, value)) != NULL) {
4147 switch (action) {
4148 case ST_ADM_ACTION_DISABLE:
4149 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4150 /* Not already in maintenance, we can change the server state */
4151 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004152 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004153 set_server_down(&sv->check);
4154 altered_servers++;
4155 total_servers++;
4156 }
4157 break;
4158 case ST_ADM_ACTION_ENABLE:
4159 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004160 /* Already in maintenance, we can change the server state.
4161 * If this server tracks the status of another one,
4162 * we must restore the good status.
4163 */
4164 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4165 set_server_up(&sv->check);
4166 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4167 }
4168 else {
4169 sv->state &= ~SRV_MAINTAIN;
4170 sv->check.state &= ~CHK_ST_PAUSED;
4171 set_server_down(&sv->check);
4172 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004173 altered_servers++;
4174 total_servers++;
4175 }
4176 break;
4177 case ST_ADM_ACTION_STOP:
4178 case ST_ADM_ACTION_START:
4179 if (action == ST_ADM_ACTION_START)
4180 sv->uweight = sv->iweight;
4181 else
4182 sv->uweight = 0;
4183
4184 server_recalc_eweight(sv);
4185 set_server_drain_state(sv);
4186
4187 altered_servers++;
4188 total_servers++;
4189 break;
4190 case ST_ADM_ACTION_SHUTDOWN:
4191 if (px->state != PR_STSTOPPED) {
4192 struct session *sess, *sess_bck;
4193
4194 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4195 if (sess->srv_conn == sv)
4196 session_shutdown(sess, SN_ERR_KILLED);
4197
4198 altered_servers++;
4199 total_servers++;
4200 }
4201 break;
4202 }
4203 } else {
4204 /* the server name is unknown or ambiguous (duplicate names) */
4205 total_servers++;
4206 }
4207 }
4208 if (reprocess && px && action) {
4209 /* Now, we know the backend and the action chosen by the user.
4210 * We can safely restart from the first server parameter
4211 * to reprocess them
4212 */
4213 cur_param = st_cur_param;
4214 next_param = st_next_param;
4215 reprocess = 0;
4216 goto reprocess_servers;
4217 }
4218
4219 next_param = cur_param;
4220 }
4221 }
4222
4223 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004224 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004225 }
4226 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004227 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004228 }
4229 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004231 }
4232 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004233 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004234 }
4235 out:
4236 return 1;
4237}
4238
4239
4240static int stats_send_http_headers(struct stream_interface *si)
4241{
4242 struct session *s = session_from_task(si->owner);
4243 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004244 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004245
4246 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004247 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004248 "Cache-Control: no-cache\r\n"
4249 "Connection: close\r\n"
4250 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004251 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004252
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004253 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004254 chunk_appendf(&trash, "Refresh: %d\r\n",
4255 uri->refresh);
4256
Willy Tarreau51437d22013-12-29 00:43:40 +01004257 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004258
4259 s->txn.status = 200;
4260 s->logs.tv_request = now;
4261
4262 if (bi_putchk(si->ib, &trash) == -1)
4263 return 0;
4264
4265 return 1;
4266}
4267
4268static int stats_send_http_redirect(struct stream_interface *si)
4269{
4270 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4271 struct session *s = session_from_task(si->owner);
4272 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004273 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004274
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004275 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004276 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004277 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004278 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004279 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4280 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004281 }
4282
4283 /* We don't want to land on the posted stats page because a refresh will
4284 * repost the data. We don't want this to happen on accident so we redirect
4285 * the browse to the stats page with a GET.
4286 */
4287 chunk_printf(&trash,
4288 "HTTP/1.1 303 See Other\r\n"
4289 "Cache-Control: no-cache\r\n"
4290 "Content-Type: text/plain\r\n"
4291 "Connection: close\r\n"
4292 "Location: %s;st=%s%s%s%s\r\n"
4293 "\r\n",
4294 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004295 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4296 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4297 stat_status_codes[appctx->ctx.stats.st_code]) ?
4298 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004299 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004300 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4301 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004302 scope_txt);
4303
4304 s->txn.status = 303;
4305 s->logs.tv_request = now;
4306
4307 if (bi_putchk(si->ib, &trash) == -1)
4308 return 0;
4309
4310 return 1;
4311}
4312
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004313/* This I/O handler runs as an applet embedded in a stream interface. It is
4314 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004315 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 * automatically unregisters itself once transfer is complete.
4317 */
4318static void http_stats_io_handler(struct stream_interface *si)
4319{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004320 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004321 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 struct channel *req = si->ob;
4323 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004324
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4326 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004327
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004328 /* check that the output is not closed */
4329 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004330 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004331
Willy Tarreau347a35d2013-11-22 17:51:09 +01004332 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004333 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004334 if (stats_send_http_headers(si)) {
4335 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004336 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004337 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004338 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004340 }
4341
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004342 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004343 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004344 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004345 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004347 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004348 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004349 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004350 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004351 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004352 }
4353
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004354 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004355 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004356 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004357 }
4358
Willy Tarreau51437d22013-12-29 00:43:40 +01004359 if (appctx->st0 == STAT_HTTP_DONE)
4360 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004361
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4363 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004364
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004365 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004366 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4367 si_shutr(si);
4368 res->flags |= CF_READ_NULL;
4369 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370 }
Willy Tarreau91861262007-10-17 17:06:05 +02004371
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004372 /* update all other flags and resync with the other side */
4373 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004374
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 /* we don't want to expire timeouts while we're processing requests */
4376 si->ib->rex = TICK_ETERNITY;
4377 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004378
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004379 out:
4380 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4381 /* check that we have released everything then unregister */
4382 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004383 }
4384}
4385
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004386
Willy Tarreau909d5172012-11-26 03:04:41 +01004387static inline const char *get_conn_ctrl_name(const struct connection *conn)
4388{
Willy Tarreau3c728722014-01-23 13:50:42 +01004389 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004390 return "NONE";
4391 return conn->ctrl->name;
4392}
4393
4394static inline const char *get_conn_xprt_name(const struct connection *conn)
4395{
4396 static char ptr[17];
4397
Willy Tarreauaad69382014-01-23 14:21:42 +01004398 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004399 return "NONE";
4400
4401 if (conn->xprt == &raw_sock)
4402 return "RAW";
4403
4404#ifdef USE_OPENSSL
4405 if (conn->xprt == &ssl_sock)
4406 return "SSL";
4407#endif
4408 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4409 return ptr;
4410}
4411
4412static inline const char *get_conn_data_name(const struct connection *conn)
4413{
4414 static char ptr[17];
4415
4416 if (!conn->data)
4417 return "NONE";
4418
4419 if (conn->data == &sess_conn_cb)
4420 return "SESS";
4421
4422 if (conn->data == &si_conn_cb)
4423 return "STRM";
4424
4425 if (conn->data == &check_conn_cb)
4426 return "CHCK";
4427
4428 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4429 return ptr;
4430}
4431
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004432/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004433 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004434 * 0 if the output buffer is full and it needs to be called again, otherwise
4435 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004436 */
Willy Tarreau76153662012-11-26 01:16:39 +01004437static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004438{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004440 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004441 extern const char *monthname[12];
4442 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004443 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004444 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004445
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004446 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004447
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004448 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004449 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004450 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4451 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004452 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 appctx->ctx.sess.uid = 0;
4454 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004455 return 1;
4456 }
4457
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004458 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004459 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004460 appctx->ctx.sess.uid = sess->uniq_id;
4461 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004462 /* fall through */
4463
4464 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004465 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004466 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004467 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004468 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004469 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4470 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004471 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004472 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004473
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004474 conn = objt_conn(sess->si[0].end);
4475 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004476 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004477 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004478 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004479 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004480 break;
4481 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004482 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004483 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004484 default:
4485 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004486 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004487 break;
4488 }
4489
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004490 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004491 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004492 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004493
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004494 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004495 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004496 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4497 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4498 sess->listener ? sess->listener->luid : 0);
4499
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004500 if (conn)
4501 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004502
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004503 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004504 case AF_INET:
4505 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004506 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004507 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004508 break;
4509 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004511 break;
4512 default:
4513 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004514 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004515 break;
4516 }
4517
Willy Tarreau50943332011-09-02 17:33:05 +02004518 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004519 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004520 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004521 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004522 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4523 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004524 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004525
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 conn = objt_conn(sess->si[1].end);
4527 if (conn)
4528 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004529
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004530 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004531 case AF_INET:
4532 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004533 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004534 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004535 break;
4536 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004537 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004538 break;
4539 default:
4540 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004541 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004542 break;
4543 }
4544
4545 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004546 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004547 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004548 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4549 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004550 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004551 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004552
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004553 if (conn)
4554 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004555
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004556 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004557 case AF_INET:
4558 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004559 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004560 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004561 break;
4562 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004563 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004564 break;
4565 default:
4566 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004567 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004568 break;
4569 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004571 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004572 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004573 sess->task,
4574 sess->task->state,
4575 sess->task->nice, sess->task->calls,
4576 sess->task->expire ?
4577 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4578 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4579 TICKS_TO_MS(1000)) : "<NEVER>",
4580 task_in_rq(sess->task) ? ", running" : "");
4581
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004582 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004583 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004584 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004586 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004587 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4588 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4589 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4590
4591 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004592 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004593 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004594 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004595 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004596 obj_type_name(sess->si[0].end),
4597 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004598 sess->si[0].exp ?
4599 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4600 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4601 TICKS_TO_MS(1000)) : "<NEVER>",
4602 sess->si[0].err_type);
4603
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004604 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004605 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004606 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004607 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004608 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004609 obj_type_name(sess->si[1].end),
4610 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004611 sess->si[1].exp ?
4612 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4613 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4614 TICKS_TO_MS(1000)) : "<NEVER>",
4615 sess->si[1].err_type);
4616
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004617 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004618 chunk_appendf(&trash,
4619 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004620 conn,
4621 get_conn_ctrl_name(conn),
4622 get_conn_xprt_name(conn),
4623 get_conn_data_name(conn),
4624 obj_type_name(conn->target),
4625 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004626
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004627 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004628 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004629 conn->flags,
4630 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004631 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004632 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004633 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004634 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004635 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4636 chunk_appendf(&trash,
4637 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4638 tmpctx,
4639 tmpctx->st0,
4640 tmpctx->st1,
4641 tmpctx->st2,
4642 tmpctx->applet->name);
4643 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004644
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004645 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004646 chunk_appendf(&trash,
4647 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004648 conn,
4649 get_conn_ctrl_name(conn),
4650 get_conn_xprt_name(conn),
4651 get_conn_data_name(conn),
4652 obj_type_name(conn->target),
4653 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004654
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004655 chunk_appendf(&trash,
4656 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004657 conn->flags,
4658 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004659 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004660 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004661 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004662 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004663 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4664 chunk_appendf(&trash,
4665 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4666 tmpctx,
4667 tmpctx->st0,
4668 tmpctx->st1,
4669 tmpctx->st2,
4670 tmpctx->applet->name);
4671 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004672
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004673 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004674 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004675 " an_exp=%s",
4676 sess->req,
4677 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004678 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004679 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004680 sess->req->analyse_exp ?
4681 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4682 TICKS_TO_MS(1000)) : "<NEVER>");
4683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004685 " rex=%s",
4686 sess->req->rex ?
4687 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4688 TICKS_TO_MS(1000)) : "<NEVER>");
4689
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004690 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004691 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004692 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004693 sess->req->wex ?
4694 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4695 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004696 sess->req->buf,
4697 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004698 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004699 sess->txn.req.next, sess->req->buf->i,
4700 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004701
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004702 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004703 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004704 " an_exp=%s",
4705 sess->rep,
4706 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004707 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004708 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004709 sess->rep->analyse_exp ?
4710 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4711 TICKS_TO_MS(1000)) : "<NEVER>");
4712
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004713 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004714 " rex=%s",
4715 sess->rep->rex ?
4716 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4717 TICKS_TO_MS(1000)) : "<NEVER>");
4718
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004719 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004720 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004721 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004722 sess->rep->wex ?
4723 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4724 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004725 sess->rep->buf,
4726 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004727 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004728 sess->txn.rsp.next, sess->rep->buf->i,
4729 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004730
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004731 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004732 return 0;
4733
4734 /* use other states to dump the contents */
4735 }
4736 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004737 appctx->ctx.sess.uid = 0;
4738 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004739 return 1;
4740}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004741
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004742static int stats_maps_list(struct stream_interface *si)
4743{
4744 struct appctx *appctx = __objt_appctx(si->end);
4745
4746 switch (appctx->st2) {
4747 case STAT_ST_INIT:
4748 /* Init to the first entry. The list cannot be change */
4749 appctx->ctx.map.ref = LIST_NEXT(&maps, struct map_reference *, list);
4750 appctx->st2 = STAT_ST_LIST;
4751 /* fall through */
4752
4753 case STAT_ST_LIST:
4754 while (appctx->ctx.map.ref) {
4755
4756 chunk_reset(&trash);
4757
4758 /* build messages */
4759 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
4760
4761 if (bi_putchk(si->ib, &trash) == -1) {
4762 /* let's try again later from this session. We add ourselves into
4763 * this session's users so that it can remove us upon termination.
4764 */
4765 return 0;
4766 }
4767
4768 /* get next list entry and check the end of the list */
4769 appctx->ctx.map.ref = LIST_NEXT(&appctx->ctx.map.ref->list,
4770 struct map_reference *, list);
4771 if (&appctx->ctx.map.ref->list == &maps)
4772 break;
4773 }
4774
4775 appctx->st2 = STAT_ST_FIN;
4776 /* fall through */
4777
4778 default:
4779 appctx->st2 = STAT_ST_FIN;
4780 return 1;
4781 }
4782}
4783
4784static const char *smp_to_type[SMP_TYPES] = {
4785 [SMP_T_BOOL] = "bool",
4786 [SMP_T_UINT] = "uint",
4787 [SMP_T_SINT] = "sint",
4788 [SMP_T_ADDR] = "addr",
4789 [SMP_T_IPV4] = "ipv4",
4790 [SMP_T_IPV6] = "ipv6",
4791 [SMP_T_STR] = "str",
4792 [SMP_T_BIN] = "bin",
4793 [SMP_T_CSTR] = "cstr",
4794 [SMP_T_CBIN] = "cbin",
4795};
4796
4797static int stats_map_lookup(struct stream_interface *si)
4798{
4799 struct appctx *appctx = __objt_appctx(si->end);
4800 struct sample_storage *smp;
4801 struct sample sample;
4802 struct pattern *pat;
4803 struct pat_idx_elt *elt;
4804 enum pat_match_res res;
4805 struct sockaddr_in addr;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004806 char addr_str[INET_ADDRSTRLEN];
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004807
4808 switch (appctx->st2) {
4809 case STAT_ST_INIT:
4810 appctx->ctx.map.desc = NULL;
4811 stats_map_lookup_next(si);
4812 appctx->st2 = STAT_ST_LIST;
4813 /* fall through */
4814
4815 case STAT_ST_LIST:
4816 /* for each lookup type */
4817 while (appctx->ctx.map.desc) {
4818 /* initialise chunk to build new message */
4819 chunk_reset(&trash);
4820
4821 /* execute pattern matching */
4822 sample.type = SMP_T_CSTR;
4823 sample.data.str.len = appctx->ctx.map.chunk.len;
4824 sample.data.str.str = appctx->ctx.map.chunk.str;
4825 pat = NULL;
4826 elt = NULL;
4827 res = pattern_exec_match(appctx->ctx.map.desc->pat, &sample, &smp, &pat, &elt);
4828
4829 /* build return message: set type of match */
4830 /**/ if (appctx->ctx.map.desc->pat->match == NULL)
4831 chunk_appendf(&trash, "found, ");
4832 else if (appctx->ctx.map.desc->pat->match == pat_match_nothing)
4833 chunk_appendf(&trash, "bool, ");
4834 else if (appctx->ctx.map.desc->pat->match == pat_match_int)
4835 chunk_appendf(&trash, "int, ");
4836 else if (appctx->ctx.map.desc->pat->match == pat_match_ip)
4837 chunk_appendf(&trash, "ip, ");
4838 else if (appctx->ctx.map.desc->pat->match == pat_match_bin)
4839 chunk_appendf(&trash, "bin, ");
4840 else if (appctx->ctx.map.desc->pat->match == pat_match_len)
4841 chunk_appendf(&trash, "len, ");
4842 else if (appctx->ctx.map.desc->pat->match == pat_match_str)
4843 chunk_appendf(&trash, "str, ");
4844 else if (appctx->ctx.map.desc->pat->match == pat_match_beg)
4845 chunk_appendf(&trash, "beg, ");
4846 else if (appctx->ctx.map.desc->pat->match == pat_match_sub)
4847 chunk_appendf(&trash, "sub, ");
4848 else if (appctx->ctx.map.desc->pat->match == pat_match_dir)
4849 chunk_appendf(&trash, "dir, ");
4850 else if (appctx->ctx.map.desc->pat->match == pat_match_dom)
4851 chunk_appendf(&trash, "dom, ");
4852 else if (appctx->ctx.map.desc->pat->match == pat_match_end)
4853 chunk_appendf(&trash, "end, ");
4854 else if (appctx->ctx.map.desc->pat->match == pat_match_reg)
4855 chunk_appendf(&trash, "reg, ");
4856 else /* The never appens case */
4857 chunk_appendf(&trash, "unknown(%p), ", appctx->ctx.map.desc->pat->match);
4858
4859 /* Display no match, and set default value */
4860 if (res == PAT_NOMATCH) {
4861 chunk_appendf(&trash, "no-match, ");
4862 smp = appctx->ctx.map.desc->def;
4863 }
4864
4865 /* Display match and match info */
4866 else {
4867 /* display match */
4868 chunk_appendf(&trash, "match, ");
4869
4870 /* display search mode */
4871 if (elt)
4872 chunk_appendf(&trash, "tree, ");
4873 else
4874 chunk_appendf(&trash, "list, ");
4875
4876 /* display search options */
4877 if (pat) {
4878 /* case sensitive */
4879 if (pat->flags & PAT_F_IGNORE_CASE)
4880 chunk_appendf(&trash, "case-insensitive, ");
4881 else
4882 chunk_appendf(&trash, "case-sensitive, ");
4883
4884 /* display source */
4885 if (pat->flags & PAT_F_FROM_FILE)
4886 chunk_appendf(&trash, "from-file, ");
4887 }
4888
4889 /* display match expresion */
4890 if (elt) {
4891 if (appctx->ctx.map.desc->pat->match == pat_match_str) {
4892 chunk_appendf(&trash, "match=\"%s\", ", elt->node.key);
4893 }
4894 /* only IPv4 */
4895 else if (appctx->ctx.map.desc->pat->match == pat_match_ip) {
4896 /* convert ip */
4897 memcpy(&addr.sin_addr, elt->node.key, 4);
4898 addr.sin_family = AF_INET;
Willy Tarreaub908bef2013-12-16 01:42:03 +01004899 if (addr_to_str((struct sockaddr_storage *)&addr, addr_str, INET_ADDRSTRLEN))
4900 chunk_appendf(&trash, "match=\"%s/%d\", ", addr_str, elt->node.node.pfx);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004901 }
4902 }
4903 }
4904
4905 /* display return value */
4906 if (!smp) {
4907 chunk_appendf(&trash, "return=nothing\n");
4908 }
4909 else {
4910 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4911 sample.type = smp->type;
4912 if (sample_casts[sample.type][SMP_T_CSTR] &&
4913 sample_casts[sample.type][SMP_T_CSTR](&sample))
4914 chunk_appendf(&trash, "return=\"%s\", type=\"%s\"\n",
4915 sample.data.str.str, smp_to_type[smp->type]);
4916 else
4917 chunk_appendf(&trash, "return=cannot-display, type=\"%s\"\n",
4918 smp_to_type[smp->type]);
4919 }
4920
4921 /* display response */
4922 if (bi_putchk(si->ib, &trash) == -1) {
4923 /* let's try again later from this session. We add ourselves into
4924 * this session's users so that it can remove us upon termination.
4925 */
4926 return 0;
4927 }
4928
4929 /* get next entry */
4930 stats_map_lookup_next(si);
4931 }
4932
4933 appctx->st2 = STAT_ST_FIN;
4934 /* fall through */
4935
4936 default:
4937 appctx->st2 = STAT_ST_FIN;
4938 free(appctx->ctx.map.chunk.str);
4939 return 1;
4940 }
4941}
4942
4943static int stats_map_list(struct stream_interface *si)
4944{
4945 struct appctx *appctx = __objt_appctx(si->end);
4946
4947 switch (appctx->st2) {
4948
4949 case STAT_ST_INIT:
4950 /* Init to the first entry. The list cannot be change */
4951 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ref->entries,
4952 struct map_entry *, list);
4953 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4954 appctx->ctx.map.ent = NULL;
4955 appctx->st2 = STAT_ST_LIST;
4956 /* fall through */
4957
4958 case STAT_ST_LIST:
4959 while (appctx->ctx.map.ent) {
4960 chunk_reset(&trash);
4961
4962 /* build messages */
4963 chunk_appendf(&trash, "%s %s\n", appctx->ctx.map.ent->key, appctx->ctx.map.ent->value);
4964
4965 if (bi_putchk(si->ib, &trash) == -1) {
4966 /* let's try again later from this session. We add ourselves into
4967 * this session's users so that it can remove us upon termination.
4968 */
4969 return 0;
4970 }
4971
4972 /* get next list entry and check the end of the list */
4973 appctx->ctx.map.ent = LIST_NEXT(&appctx->ctx.map.ent->list,
4974 struct map_entry *, list);
4975 if (&appctx->ctx.map.ent->list == &appctx->ctx.map.ref->entries)
4976 break;
4977 }
4978
4979 appctx->st2 = STAT_ST_FIN;
4980 /* fall through */
4981
4982 default:
4983 appctx->st2 = STAT_ST_FIN;
4984 return 1;
4985 }
4986}
4987
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004988/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004989 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004990 * to be called again, otherwise non-zero. It is designed to be called
4991 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004992 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004993static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004994{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004995 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004996 struct connection *conn;
4997
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004998 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004999 /* If we're forced to shut down, we might have to remove our
5000 * reference to the last session being dumped.
5001 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005002 if (appctx->st2 == STAT_ST_LIST) {
5003 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5004 LIST_DEL(&appctx->ctx.sess.bref.users);
5005 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005006 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005007 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005008 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005009 }
5010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005011 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005012
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005013 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005014 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005015 /* the function had not been called yet, let's prepare the
5016 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005017 * pointer to the first in the global list. When a target
5018 * session is being destroyed, it is responsible for updating
5019 * this pointer. We know we have reached the end when this
5020 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005021 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005022 LIST_INIT(&appctx->ctx.sess.bref.users);
5023 appctx->ctx.sess.bref.ref = sessions.n;
5024 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005025 /* fall through */
5026
Willy Tarreau295a8372011-03-10 11:25:07 +01005027 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005028 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005029 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5030 LIST_DEL(&appctx->ctx.sess.bref.users);
5031 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005032 }
5033
5034 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005035 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005036 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005037 struct session *curr_sess;
5038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005039 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005040
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005041 if (appctx->ctx.sess.target) {
5042 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005043 goto next_sess;
5044
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005045 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005046 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005047 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005048 return 0;
5049
5050 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005051 LIST_DEL(&appctx->ctx.sess.bref.users);
5052 LIST_INIT(&appctx->ctx.sess.bref.users);
5053 if (appctx->ctx.sess.target != (void *)-1) {
5054 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005055 break;
5056 }
5057 else
5058 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005059 }
5060
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005061 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005062 "%p: proto=%s",
5063 curr_sess,
5064 curr_sess->listener->proto->name);
5065
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005066
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005067 conn = objt_conn(curr_sess->si[0].end);
5068 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005069 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005070 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005071 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005072 " src=%s:%d fe=%s be=%s srv=%s",
5073 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005074 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005075 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005076 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005077 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005078 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005079 break;
5080 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005081 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005082 " src=unix:%d fe=%s be=%s srv=%s",
5083 curr_sess->listener->luid,
5084 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005085 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005086 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005087 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005088 break;
5089 }
5090
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005091 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005092 " ts=%02x age=%s calls=%d",
5093 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005094 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5095 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005096
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005097 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005098 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005099 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005100 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005101 curr_sess->req->analysers,
5102 curr_sess->req->rex ?
5103 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5104 TICKS_TO_MS(1000)) : "");
5105
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005106 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005107 ",wx=%s",
5108 curr_sess->req->wex ?
5109 human_time(TICKS_TO_MS(curr_sess->req->wex - 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 ",ax=%s]",
5114 curr_sess->req->analyse_exp ?
5115 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5116 TICKS_TO_MS(1000)) : "");
5117
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005118 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005119 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005120 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005121 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005122 curr_sess->rep->analysers,
5123 curr_sess->rep->rex ?
5124 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5125 TICKS_TO_MS(1000)) : "");
5126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005127 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005128 ",wx=%s",
5129 curr_sess->rep->wex ?
5130 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5131 TICKS_TO_MS(1000)) : "");
5132
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005133 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005134 ",ax=%s]",
5135 curr_sess->rep->analyse_exp ?
5136 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5137 TICKS_TO_MS(1000)) : "");
5138
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005139 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005140 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005141 " s0=[%d,%1xh,fd=%d,ex=%s]",
5142 curr_sess->si[0].state,
5143 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005144 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005145 curr_sess->si[0].exp ?
5146 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5147 TICKS_TO_MS(1000)) : "");
5148
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005149 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005150 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005151 " s1=[%d,%1xh,fd=%d,ex=%s]",
5152 curr_sess->si[1].state,
5153 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005154 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005155 curr_sess->si[1].exp ?
5156 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5157 TICKS_TO_MS(1000)) : "");
5158
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005159 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005160 " exp=%s",
5161 curr_sess->task->expire ?
5162 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5163 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005164 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005165 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005167 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005168
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005169 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005170 /* let's try again later from this session. We add ourselves into
5171 * this session's users so that it can remove us upon termination.
5172 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005173 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005174 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005175 }
5176
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005177 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005178 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005179 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005180
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005181 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005182 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005183 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005184 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005185 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005186 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005187
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005188 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005189 return 0;
5190
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005191 appctx->ctx.sess.target = NULL;
5192 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005193 return 1;
5194 }
5195
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005196 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005197 /* fall through */
5198
5199 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005200 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005201 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005202 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005203}
5204
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005205/* This is called when the stream interface is closed. For instance, upon an
5206 * external abort, we won't call the i/o handler anymore so we may need to
5207 * remove back references to the session currently being dumped.
5208 */
Simon Horman74d88312013-02-12 10:45:50 +09005209static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005210{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005211 struct appctx *appctx = __objt_appctx(si->end);
5212
5213 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5214 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5215 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005216 }
5217}
5218
Willy Tarreau20e99322013-04-13 09:22:25 +02005219/* This function is used to either dump tables states (when action is set
5220 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005221 * It returns 0 if the output buffer is full and it needs to be called
5222 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005223 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005224static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005225{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005226 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005227 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005228 struct ebmb_node *eb;
5229 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005230 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005231 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005232
5233 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005234 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005235 * - STAT_ST_INIT : the first call
5236 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005237 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005238 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005239 * and the entry pointer points to the next entry to be dumped,
5240 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005241 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005242 * data though.
5243 */
5244
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005245 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005246 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005247 if (appctx->st2 == STAT_ST_LIST) {
5248 appctx->ctx.table.entry->ref_cnt--;
5249 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005250 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005251 return 1;
5252 }
5253
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005254 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005255
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005256 while (appctx->st2 != STAT_ST_FIN) {
5257 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005258 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005259 appctx->ctx.table.proxy = appctx->ctx.table.target;
5260 if (!appctx->ctx.table.proxy)
5261 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005262
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005263 appctx->ctx.table.entry = NULL;
5264 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005265 break;
5266
Willy Tarreau295a8372011-03-10 11:25:07 +01005267 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005268 if (!appctx->ctx.table.proxy ||
5269 (appctx->ctx.table.target &&
5270 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5271 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005272 break;
5273 }
5274
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005275 if (appctx->ctx.table.proxy->table.size) {
5276 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5277 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005278 return 0;
5279
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005280 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005281 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005282 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005284 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005285 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5286 appctx->ctx.table.entry->ref_cnt++;
5287 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005288 break;
5289 }
5290 }
5291 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005292 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005293 break;
5294
Willy Tarreau295a8372011-03-10 11:25:07 +01005295 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005296 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005297
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005298 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005299 /* we're filtering on some data contents */
5300 void *ptr;
5301 long long data;
5302
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005303 dt = appctx->ctx.table.data_type;
5304 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5305 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005306 dt);
5307
5308 data = 0;
5309 switch (stktable_data_types[dt].std_type) {
5310 case STD_T_SINT:
5311 data = stktable_data_cast(ptr, std_t_sint);
5312 break;
5313 case STD_T_UINT:
5314 data = stktable_data_cast(ptr, std_t_uint);
5315 break;
5316 case STD_T_ULL:
5317 data = stktable_data_cast(ptr, std_t_ull);
5318 break;
5319 case STD_T_FRQP:
5320 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005321 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005322 break;
5323 }
5324
5325 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005326 if ((data < appctx->ctx.table.value &&
5327 (appctx->ctx.table.data_op == STD_OP_EQ ||
5328 appctx->ctx.table.data_op == STD_OP_GT ||
5329 appctx->ctx.table.data_op == STD_OP_GE)) ||
5330 (data == appctx->ctx.table.value &&
5331 (appctx->ctx.table.data_op == STD_OP_NE ||
5332 appctx->ctx.table.data_op == STD_OP_GT ||
5333 appctx->ctx.table.data_op == STD_OP_LT)) ||
5334 (data > appctx->ctx.table.value &&
5335 (appctx->ctx.table.data_op == STD_OP_EQ ||
5336 appctx->ctx.table.data_op == STD_OP_LT ||
5337 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005338 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005339 }
5340
Simon Hormanc88b8872011-06-15 15:18:49 +09005341 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005342 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5343 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005344 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005345
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005346 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005348 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005349 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005350 struct stksess *old = appctx->ctx.table.entry;
5351 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005352 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005353 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5354 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5355 stksess_kill(&appctx->ctx.table.proxy->table, old);
5356 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005357 break;
5358 }
5359
Simon Hormanc88b8872011-06-15 15:18:49 +09005360
5361 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005362 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5363 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5364 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005365
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005366 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5367 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005368 break;
5369
Willy Tarreau295a8372011-03-10 11:25:07 +01005370 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005371 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005372 break;
5373 }
5374 }
5375 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005376}
5377
Willy Tarreaud426a182010-03-05 14:58:26 +01005378/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005379 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5380 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5381 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5382 * lines are respected within the limit of 70 output chars. Lines that are
5383 * continuation of a previous truncated line begin with "+" instead of " "
5384 * after the offset. The new pointer is returned.
5385 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005386static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5387 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005388{
5389 int end;
5390 unsigned char c;
5391
5392 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005393 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005394 return ptr;
5395
Willy Tarreau77804732012-10-29 16:14:26 +01005396 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005397
Willy Tarreaud426a182010-03-05 14:58:26 +01005398 while (ptr < len && ptr < bsize) {
5399 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005400 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005401 if (out->len > end - 2)
5402 break;
5403 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005404 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005405 if (out->len > end - 3)
5406 break;
5407 out->str[out->len++] = '\\';
5408 switch (c) {
5409 case '\t': c = 't'; break;
5410 case '\n': c = 'n'; break;
5411 case '\r': c = 'r'; break;
5412 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005413 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005414 }
5415 out->str[out->len++] = c;
5416 } else {
5417 if (out->len > end - 5)
5418 break;
5419 out->str[out->len++] = '\\';
5420 out->str[out->len++] = 'x';
5421 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5422 out->str[out->len++] = hextab[c & 0xF];
5423 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005424 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005425 /* we had a line break, let's return now */
5426 out->str[out->len++] = '\n';
5427 *line = ptr;
5428 return ptr;
5429 }
5430 }
5431 /* we have an incomplete line, we return it as-is */
5432 out->str[out->len++] = '\n';
5433 return ptr;
5434}
5435
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005436/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005437 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005438 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005439 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005440static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005441{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005442 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005443 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005444
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005445 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005446 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005447
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005448 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005449
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005450 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005451 /* the function had not been called yet, let's prepare the
5452 * buffer for a response.
5453 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005454 struct tm tm;
5455
5456 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005457 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005458 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5459 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5460 error_snapshot_id);
5461
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005462 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005463 /* Socket buffer full. Let's try again later from the same point */
5464 return 0;
5465 }
5466
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005467 appctx->ctx.errors.px = proxy;
5468 appctx->ctx.errors.buf = 0;
5469 appctx->ctx.errors.bol = 0;
5470 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005471 }
5472
5473 /* we have two inner loops here, one for the proxy, the other one for
5474 * the buffer.
5475 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005476 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005477 struct error_snapshot *es;
5478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005479 if (appctx->ctx.errors.buf == 0)
5480 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005481 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005482 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005483
5484 if (!es->when.tv_sec)
5485 goto next;
5486
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005487 if (appctx->ctx.errors.iid >= 0 &&
5488 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5489 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005490 goto next;
5491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005492 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005493 /* just print headers now */
5494
5495 char pn[INET6_ADDRSTRLEN];
5496 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005497 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005498
5499 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005500 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005501 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005502 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005503
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005504 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5505 case AF_INET:
5506 case AF_INET6:
5507 port = get_host_port(&es->src);
5508 break;
5509 default:
5510 port = 0;
5511 }
5512
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005513 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005514 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005515 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005516 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005517 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005518 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005519 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5520 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005521 break;
5522 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005523 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005524 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005525 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005526 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005527 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005528 break;
5529 }
5530
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005531 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005532 ", server %s (#%d), event #%u\n"
5533 " src %s:%d, session #%d, session flags 0x%08x\n"
5534 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5535 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5536 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5537 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5538 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5539 es->ev_id,
5540 pn, port, es->sid, es->s_flags,
5541 es->state, es->m_flags, es->t_flags,
5542 es->m_clen, es->m_blen,
5543 es->b_flags, es->b_out, es->b_tot,
5544 es->len, es->b_wrap, es->pos);
5545
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005546 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005547 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005548 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005549 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005550 appctx->ctx.errors.ptr = 0;
5551 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005552 }
5553
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005554 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005555 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005556 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005557 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005558 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005559 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005560 goto next;
5561 }
5562
5563 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005564 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005565 int newptr;
5566 int newline;
5567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005568 newline = appctx->ctx.errors.bol;
5569 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5570 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005571 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005572
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005573 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005574 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005575 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005576 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005577 appctx->ctx.errors.ptr = newptr;
5578 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005579 };
5580 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005581 appctx->ctx.errors.bol = 0;
5582 appctx->ctx.errors.ptr = -1;
5583 appctx->ctx.errors.buf++;
5584 if (appctx->ctx.errors.buf > 1) {
5585 appctx->ctx.errors.buf = 0;
5586 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005587 }
5588 }
5589
5590 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005591 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005592}
5593
Willy Tarreaud5781202012-09-22 19:32:35 +02005594/* parse the "level" argument on the bind lines */
5595static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5596{
5597 if (!*args[cur_arg + 1]) {
5598 memprintf(err, "'%s' : missing level", args[cur_arg]);
5599 return ERR_ALERT | ERR_FATAL;
5600 }
5601
5602 if (!strcmp(args[cur_arg+1], "user"))
5603 conf->level = ACCESS_LVL_USER;
5604 else if (!strcmp(args[cur_arg+1], "operator"))
5605 conf->level = ACCESS_LVL_OPER;
5606 else if (!strcmp(args[cur_arg+1], "admin"))
5607 conf->level = ACCESS_LVL_ADMIN;
5608 else {
5609 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5610 args[cur_arg], args[cur_arg+1]);
5611 return ERR_ALERT | ERR_FATAL;
5612 }
5613
5614 return 0;
5615}
5616
Willy Tarreaub24281b2011-02-13 13:16:36 +01005617struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005618 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005619 .name = "<STATS>", /* used for logging */
5620 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005621 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005622};
5623
Simon Horman9bd2c732011-06-15 15:18:44 +09005624static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005625 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005626 .name = "<CLI>", /* used for logging */
5627 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005628 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005629};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005630
Willy Tarreaudc13c112013-06-21 23:16:39 +02005631static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005632 { CFG_GLOBAL, "stats", stats_parse_global },
5633 { 0, NULL, NULL },
5634}};
5635
Willy Tarreaud5781202012-09-22 19:32:35 +02005636static struct bind_kw_list bind_kws = { "STAT", { }, {
5637 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5638 { NULL, NULL, 0 },
5639}};
5640
Willy Tarreau10522fd2008-07-09 20:12:41 +02005641__attribute__((constructor))
5642static void __dumpstats_module_init(void)
5643{
5644 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005645 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005646}
5647
Willy Tarreau91861262007-10-17 17:06:05 +02005648/*
5649 * Local variables:
5650 * c-indent-level: 8
5651 * c-basic-offset: 8
5652 * End:
5653 */