blob: d11d38084f152c99f27f1a3cbcda8eb4c023efef [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 Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 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>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020055#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +020083 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020085 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020086 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020087 " disable : put a server or frontend in maintenance mode\n"
88 " enable : re-enable a server or frontend which is in maintenance mode\n"
89 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020090 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020091
Simon Horman9bd2c732011-06-15 15:18:44 +090092static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020093 "Permission denied\n"
94 "";
95
Willy Tarreau295a8372011-03-10 11:25:07 +010096/* data transmission states for the stats responses */
97enum {
98 STAT_ST_INIT = 0,
99 STAT_ST_HEAD,
100 STAT_ST_INFO,
101 STAT_ST_LIST,
102 STAT_ST_END,
103 STAT_ST_FIN,
104};
105
106/* data transmission states for the stats responses inside a proxy */
107enum {
108 STAT_PX_ST_INIT = 0,
109 STAT_PX_ST_TH,
110 STAT_PX_ST_FE,
111 STAT_PX_ST_LI,
112 STAT_PX_ST_SV,
113 STAT_PX_ST_BE,
114 STAT_PX_ST_END,
115 STAT_PX_ST_FIN,
116};
117
Cyril Bonté19979e12012-04-04 12:57:21 +0200118extern const char *stat_status_codes[];
119
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200120/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200121 * a new stats socket. It returns a positive value upon success, 0 if the connection
122 * needs to be closed and ignored, or a negative value upon critical failure.
123 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900124static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200125{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200126 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100127 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3cefd522012-08-30 15:49:18 +0200128 copy_target(&s->target, &s->si[1].conn.target); // for logging only
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200129 s->si[1].conn.xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100130 s->si[1].applet.st1 = 0;
131 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200132
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200133 tv_zero(&s->logs.tv_request);
134 s->logs.t_queue = 0;
135 s->logs.t_connect = 0;
136 s->logs.t_data = 0;
137 s->logs.t_close = 0;
138 s->logs.bytes_in = s->logs.bytes_out = 0;
139 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
140 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200141
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200142 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200143
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200144 if (s->listener->timeout) {
145 s->req->rto = *s->listener->timeout;
146 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200148 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200149}
150
Willy Tarreau07e9e642010-08-17 21:48:17 +0200151/* allocate a new stats frontend named <name>, and return it
152 * (or NULL in case of lack of memory).
153 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200154static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200155{
156 struct proxy *fe;
157
158 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
159 if (!fe)
160 return NULL;
161
Willy Tarreau237250c2011-07-29 01:49:03 +0200162 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200163 fe->next = proxy;
164 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200165 fe->last_change = now.tv_sec;
166 fe->id = strdup("GLOBAL");
167 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200168 fe->maxconn = 10; /* default to 10 concurrent connections */
169 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200170 fe->conf.file = strdup(file);
171 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200172 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200173
174 /* the stats frontend is the only one able to assign ID #0 */
175 fe->conf.id.key = fe->uuid = 0;
176 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200177 return fe;
178}
179
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200181 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
182 * error message into the <err> buffer which will be preallocated. The trailing
183 * '\n' must not be written. The function must be called with <args> pointing to
184 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200185 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200186static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200187 struct proxy *defpx, const char *file, int line,
188 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200189{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200190 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200191 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200192
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200193 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200194 int cur_arg;
195
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200196 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200197 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 +0200198 return -1;
199 }
200
Willy Tarreau89a63132009-08-16 17:41:45 +0200201 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200202 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200203 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200204 return -1;
205 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200206 }
207
Willy Tarreau4348fad2012-09-20 16:48:07 +0200208 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200209 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200210
Willy Tarreauc53d4222012-09-20 20:19:28 +0200211 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
212 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
213 file, line, args[0], args[1], err && *err ? *err : "error");
214 return -1;
215 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200216
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200217 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200218 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200219 static int bind_dumped;
220 struct bind_kw *kw;
221
222 kw = bind_find_kw(args[cur_arg]);
223 if (kw) {
224 if (!kw->parse) {
225 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
226 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200227 return -1;
228 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200229
230 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
231 if (err && *err)
232 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
233 else
234 memprintf(err, "'%s %s' : error encountered while processing '%s'",
235 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200236 return -1;
237 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200238
239 cur_arg += 1 + kw->skip;
240 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200241 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200242
243 if (!bind_dumped) {
244 bind_dump_kws(err);
245 indent_msg(err, 4);
246 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200247 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200248
249 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
250 args[0], args[1], args[cur_arg],
251 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
252 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200253 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100254
Willy Tarreauc53d4222012-09-20 20:19:28 +0200255 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
256 l->maxconn = global.stats_fe->maxconn;
257 l->backlog = global.stats_fe->backlog;
258 l->timeout = &global.stats_fe->timeout.client;
259 l->accept = session_accept;
260 l->handler = process_session;
261 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
262 l->nice = -64; /* we want to boost priority for local stats */
263 global.maxsock += l->maxconn;
264 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200265 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100267 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200268 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100269
270 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200271 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100272 return -1;
273 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100275 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 return -1;
278 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200279 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200280 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200282 return -1;
283 }
284 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200285 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 else if (!strcmp(args[1], "maxconn")) {
288 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200289
290 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200291 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292 return -1;
293 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200294
295 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200296 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200298 return -1;
299 }
300 }
301 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 }
303 else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200304 memprintf(err, "'%s' only supports 'socket', 'maxconn' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305 return -1;
306 }
307 return 0;
308}
309
Simon Horman9bd2c732011-06-15 15:18:44 +0900310static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100311{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200312 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100313 "# pxname,svname,"
314 "qcur,qmax,"
315 "scur,smax,slim,stot,"
316 "bin,bout,"
317 "dreq,dresp,"
318 "ereq,econ,eresp,"
319 "wretr,wredis,"
320 "status,weight,act,bck,"
321 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200322 "pid,iid,sid,throttle,lbtot,tracked,type,"
323 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200324 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100325 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100326 "req_rate,req_rate_max,req_tot,"
327 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100328 "\n");
329}
330
Simon Hormand9366582011-06-15 15:18:45 +0900331/* print a string of text buffer to <out>. The format is :
332 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
333 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
334 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
335 */
336static int dump_text(struct chunk *out, const char *buf, int bsize)
337{
338 unsigned char c;
339 int ptr = 0;
340
341 while (buf[ptr] && ptr < bsize) {
342 c = buf[ptr];
343 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
344 if (out->len > out->size - 1)
345 break;
346 out->str[out->len++] = c;
347 }
348 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
349 if (out->len > out->size - 2)
350 break;
351 out->str[out->len++] = '\\';
352 switch (c) {
353 case ' ': c = ' '; break;
354 case '\t': c = 't'; break;
355 case '\n': c = 'n'; break;
356 case '\r': c = 'r'; break;
357 case '\e': c = 'e'; break;
358 case '\\': c = '\\'; break;
359 }
360 out->str[out->len++] = c;
361 }
362 else {
363 if (out->len > out->size - 4)
364 break;
365 out->str[out->len++] = '\\';
366 out->str[out->len++] = 'x';
367 out->str[out->len++] = hextab[(c >> 4) & 0xF];
368 out->str[out->len++] = hextab[c & 0xF];
369 }
370 ptr++;
371 }
372
373 return ptr;
374}
375
376/* print a buffer in hexa.
377 * Print stopped if <bsize> is reached, or if no more place in the chunk.
378 */
379static int dump_binary(struct chunk *out, const char *buf, int bsize)
380{
381 unsigned char c;
382 int ptr = 0;
383
384 while (ptr < bsize) {
385 c = buf[ptr];
386
387 if (out->len > out->size - 2)
388 break;
389 out->str[out->len++] = hextab[(c >> 4) & 0xF];
390 out->str[out->len++] = hextab[c & 0xF];
391
392 ptr++;
393 }
394 return ptr;
395}
396
397/* Dump the status of a table to a stream interface's
398 * read buffer. It returns 0 if the output buffer is full
399 * and needs to be called again, otherwise non-zero.
400 */
401static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
402 struct proxy *proxy, struct proxy *target)
403{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200404 struct session *s = si->conn.xprt_ctx;
Simon Hormand9366582011-06-15 15:18:45 +0900405
406 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
407 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
408
409 /* any other information should be dumped here */
410
Willy Tarreau290e63a2012-09-20 18:07:14 +0200411 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Simon Hormand9366582011-06-15 15:18:45 +0900412 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
413
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200414 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900415 return 0;
416
417 return 1;
418}
419
420/* Dump the a table entry to a stream interface's
421 * read buffer. It returns 0 if the output buffer is full
422 * and needs to be called again, otherwise non-zero.
423 */
424static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
425 struct proxy *proxy, struct stksess *entry)
426{
427 int dt;
428
429 chunk_printf(msg, "%p:", entry);
430
431 if (proxy->table.type == STKTABLE_TYPE_IP) {
432 char addr[INET_ADDRSTRLEN];
433 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
434 chunk_printf(msg, " key=%s", addr);
435 }
436 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
437 char addr[INET6_ADDRSTRLEN];
438 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
439 chunk_printf(msg, " key=%s", addr);
440 }
441 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
442 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
443 }
444 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
445 chunk_printf(msg, " key=");
446 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
447 }
448 else {
449 chunk_printf(msg, " key=");
450 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
451 }
452
453 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
454
455 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
456 void *ptr;
457
458 if (proxy->table.data_ofs[dt] == 0)
459 continue;
460 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
461 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
462 else
463 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
464
465 ptr = stktable_data_ptr(&proxy->table, entry, dt);
466 switch (stktable_data_types[dt].std_type) {
467 case STD_T_SINT:
468 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
469 break;
470 case STD_T_UINT:
471 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
472 break;
473 case STD_T_ULL:
474 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
475 break;
476 case STD_T_FRQP:
477 chunk_printf(msg, "%d",
478 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
479 proxy->table.data_arg[dt].u));
480 break;
481 }
482 }
483 chunk_printf(msg, "\n");
484
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200485 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900486 return 0;
487
488 return 1;
489}
490
Willy Tarreaudec98142012-06-06 23:37:08 +0200491static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900492{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200493 struct session *s = si->conn.xprt_ctx;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900494 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900495 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900496 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900497 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreaudec98142012-06-06 23:37:08 +0200498 struct chunk msg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200499 long long value;
500 int data_type;
501 void *ptr;
502 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900503
Simon Hormand5b9fd92011-06-15 15:18:48 +0900504 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900505
506 if (!*args[4]) {
507 si->applet.ctx.cli.msg = "Key value expected\n";
508 si->applet.st0 = STAT_CLI_PRINT;
509 return;
510 }
511
Simon Hormanc5b89f62011-06-15 15:18:50 +0900512 switch (px->table.type) {
513 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900514 uint32_key = htonl(inetaddr_host(args[4]));
515 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900516 break;
517 case STKTABLE_TYPE_IPV6:
518 inet_pton(AF_INET6, args[4], ip6_key);
519 static_table_key.key = &ip6_key;
520 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900521 case STKTABLE_TYPE_INTEGER:
522 {
523 char *endptr;
524 unsigned long val;
525 errno = 0;
526 val = strtoul(args[4], &endptr, 10);
527 if ((errno == ERANGE && val == ULONG_MAX) ||
528 (errno != 0 && val == 0) || endptr == args[4] ||
529 val > 0xffffffff) {
530 si->applet.ctx.cli.msg = "Invalid key\n";
531 si->applet.st0 = STAT_CLI_PRINT;
532 return;
533 }
534 uint32_key = (uint32_t) val;
535 static_table_key.key = &uint32_key;
536 break;
537 }
538 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900539 case STKTABLE_TYPE_STRING:
540 static_table_key.key = args[4];
541 static_table_key.key_len = strlen(args[4]);
542 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900543 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200544 switch (action) {
545 case STAT_CLI_O_TAB:
546 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
547 break;
548 case STAT_CLI_O_CLR:
549 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
550 break;
551 default:
552 si->applet.ctx.cli.msg = "Unknown action\n";
553 break;
554 }
Simon Horman121f3052011-06-15 15:18:46 +0900555 si->applet.st0 = STAT_CLI_PRINT;
556 return;
557 }
558
559 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200560 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900561 si->applet.ctx.cli.msg = stats_permission_denied_msg;
562 si->applet.st0 = STAT_CLI_PRINT;
563 return;
564 }
565
566 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900567
Willy Tarreaudec98142012-06-06 23:37:08 +0200568 switch (action) {
569 case STAT_CLI_O_TAB:
570 if (!ts)
571 return;
David du Colombier7af46052012-05-16 14:16:48 +0200572 chunk_init(&msg, trash, trashlen);
Simon Horman17bce342011-06-15 15:18:47 +0900573 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
574 return;
575 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900576 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200577
578 case STAT_CLI_O_CLR:
579 if (!ts)
580 return;
581 if (ts->ref_cnt) {
582 /* don't delete an entry which is currently referenced */
583 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
584 si->applet.st0 = STAT_CLI_PRINT;
585 return;
586 }
587 stksess_kill(&px->table, ts);
588 break;
Simon Horman17bce342011-06-15 15:18:47 +0900589
Willy Tarreau654694e2012-06-07 01:03:16 +0200590 case STAT_CLI_O_SET:
591 if (strncmp(args[5], "data.", 5) != 0) {
592 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
593 si->applet.st0 = STAT_CLI_PRINT;
594 return;
595 }
596
597 data_type = stktable_get_data_type(args[5] + 5);
598 if (data_type < 0) {
599 si->applet.ctx.cli.msg = "Unknown data type\n";
600 si->applet.st0 = STAT_CLI_PRINT;
601 return;
602 }
603
604 if (!px->table.data_ofs[data_type]) {
605 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
606 si->applet.st0 = STAT_CLI_PRINT;
607 return;
608 }
609
610 if (!*args[6] || strl2llrc(args[6], strlen(args[6]), &value) != 0) {
611 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
612 si->applet.st0 = STAT_CLI_PRINT;
613 return;
614 }
615
616 if (ts)
617 stktable_touch(&px->table, ts, 1);
618 else {
619 ts = stksess_new(&px->table, &static_table_key);
620 if (!ts) {
621 /* don't delete an entry which is currently referenced */
622 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
623 si->applet.st0 = STAT_CLI_PRINT;
624 return;
625 }
626 stktable_store(&px->table, ts, 1);
627 }
628
629 ptr = stktable_data_ptr(&px->table, ts, data_type);
630 switch (stktable_data_types[data_type].std_type) {
631 case STD_T_SINT:
632 stktable_data_cast(ptr, std_t_sint) = value;
633 break;
634 case STD_T_UINT:
635 stktable_data_cast(ptr, std_t_uint) = value;
636 break;
637 case STD_T_ULL:
638 stktable_data_cast(ptr, std_t_ull) = value;
639 break;
640 case STD_T_FRQP:
641 /* We only reset the previous value so that it slowly fades out */
642 frqp = &stktable_data_cast(ptr, std_t_frqp);
643 frqp->curr_tick = now_ms;
644 frqp->prev_ctr = value;
645 frqp->curr_ctr = 0;
646 break;
647 }
648 break;
649
Willy Tarreaudec98142012-06-06 23:37:08 +0200650 default:
651 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900652 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200653 break;
Simon Horman121f3052011-06-15 15:18:46 +0900654 }
Simon Horman121f3052011-06-15 15:18:46 +0900655}
656
Willy Tarreau654694e2012-06-07 01:03:16 +0200657static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900658{
Willy Tarreau654694e2012-06-07 01:03:16 +0200659 if (action != STAT_CLI_O_TAB) {
660 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" action";
661 si->applet.st0 = STAT_CLI_PRINT;
662 return;
663 }
664
Simon Hormand5b9fd92011-06-15 15:18:48 +0900665 /* condition on stored data value */
666 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
667 if (si->applet.ctx.table.data_type < 0) {
668 si->applet.ctx.cli.msg = "Unknown data type\n";
669 si->applet.st0 = STAT_CLI_PRINT;
670 return;
671 }
672
673 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
674 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
675 si->applet.st0 = STAT_CLI_PRINT;
676 return;
677 }
678
679 si->applet.ctx.table.data_op = get_std_op(args[4]);
680 if (si->applet.ctx.table.data_op < 0) {
681 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
682 si->applet.st0 = STAT_CLI_PRINT;
683 return;
684 }
685
686 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
687 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
688 si->applet.st0 = STAT_CLI_PRINT;
689 return;
690 }
691}
692
Willy Tarreaudec98142012-06-06 23:37:08 +0200693static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900694{
695 si->applet.ctx.table.data_type = -1;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200696 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200697 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900698 si->applet.ctx.table.proxy = NULL;
699 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200700 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900701
702 if (*args[2]) {
703 si->applet.ctx.table.target = find_stktable(args[2]);
704 if (!si->applet.ctx.table.target) {
705 si->applet.ctx.cli.msg = "No such table\n";
706 si->applet.st0 = STAT_CLI_PRINT;
707 return;
708 }
709 }
710 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200711 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900712 goto err_args;
713 return;
714 }
715
716 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200717 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900718 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200719 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900720 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900721 goto err_args;
722
723 return;
724
725err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200726 switch (action) {
727 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900728 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200729 break;
730 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900731 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200732 break;
733 default:
734 si->applet.ctx.cli.msg = "Unknown action\n";
735 break;
736 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900737 si->applet.st0 = STAT_CLI_PRINT;
738}
739
Willy Tarreau532a4502011-09-07 22:37:44 +0200740/* Expects to find a frontend named <arg> and returns it, otherwise displays various
741 * adequate error messages and returns NULL. This function also expects the session
742 * level to be admin.
743 */
744static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
745{
746 struct proxy *px;
747
Willy Tarreau290e63a2012-09-20 18:07:14 +0200748 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200749 si->applet.ctx.cli.msg = stats_permission_denied_msg;
750 si->applet.st0 = STAT_CLI_PRINT;
751 return NULL;
752 }
753
754 if (!*arg) {
755 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
756 si->applet.st0 = STAT_CLI_PRINT;
757 return NULL;
758 }
759
760 px = findproxy(arg, PR_CAP_FE);
761 if (!px) {
762 si->applet.ctx.cli.msg = "No such frontend.\n";
763 si->applet.st0 = STAT_CLI_PRINT;
764 return NULL;
765 }
766 return px;
767}
768
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200769/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
770 * and returns the pointer to the server. Otherwise, display adequate error messages
771 * and returns NULL. This function also expects the session level to be admin. Note:
772 * the <arg> is modified to remove the '/'.
773 */
774static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
775{
776 struct proxy *px;
777 struct server *sv;
778 char *line;
779
Willy Tarreau290e63a2012-09-20 18:07:14 +0200780 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200781 si->applet.ctx.cli.msg = stats_permission_denied_msg;
782 si->applet.st0 = STAT_CLI_PRINT;
783 return NULL;
784 }
785
786 /* split "backend/server" and make <line> point to server */
787 for (line = arg; *line; line++)
788 if (*line == '/') {
789 *line++ = '\0';
790 break;
791 }
792
793 if (!*line || !*arg) {
794 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
795 si->applet.st0 = STAT_CLI_PRINT;
796 return NULL;
797 }
798
799 if (!get_backend_server(arg, line, &px, &sv)) {
800 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
801 si->applet.st0 = STAT_CLI_PRINT;
802 return NULL;
803 }
804
805 if (px->state == PR_STSTOPPED) {
806 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
807 si->applet.st0 = STAT_CLI_PRINT;
808 return NULL;
809 }
810
811 return sv;
812}
813
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200814/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200815 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100816 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200817 * designating the function which will have to process the request, which can
818 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200819 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900820static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200821{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200822 struct session *s = si->conn.xprt_ctx;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200823 char *args[MAX_STATS_ARGS + 1];
824 int arg;
825
826 while (isspace((unsigned char)*line))
827 line++;
828
829 arg = 0;
830 args[arg] = line;
831
832 while (*line && arg < MAX_STATS_ARGS) {
833 if (isspace((unsigned char)*line)) {
834 *line++ = '\0';
835
836 while (isspace((unsigned char)*line))
837 line++;
838
839 args[++arg] = line;
840 continue;
841 }
842
843 line++;
844 }
845
846 while (++arg <= MAX_STATS_ARGS)
847 args[arg] = line;
848
Willy Tarreau295a8372011-03-10 11:25:07 +0100849 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200850 if (strcmp(args[0], "show") == 0) {
851 if (strcmp(args[1], "stat") == 0) {
852 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100853 si->applet.ctx.stats.flags |= STAT_BOUND;
854 si->applet.ctx.stats.iid = atoi(args[2]);
855 si->applet.ctx.stats.type = atoi(args[3]);
856 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200857 }
858
Willy Tarreau295a8372011-03-10 11:25:07 +0100859 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
860 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200861 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100862 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200863 }
864 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100865 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
866 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200867 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100868 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200869 }
870 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200871 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200872 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100873 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100874 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200875 return 1;
876 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100877 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100878 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100879 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100880 si->applet.ctx.sess.target = NULL;
881 si->applet.ctx.sess.section = 0; /* start with session status */
882 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100883 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200884 }
885 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200886 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100887 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100888 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200889 return 1;
890 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200891 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100892 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200893 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100894 si->applet.ctx.errors.iid = -1;
895 si->applet.ctx.errors.px = NULL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200896 si->conn.xprt_st = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100897 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200898 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200899 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200900 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200901 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700902 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200903 return 0;
904 }
905 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200906 else if (strcmp(args[0], "clear") == 0) {
907 if (strcmp(args[1], "counters") == 0) {
908 struct proxy *px;
909 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200910 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200911 int clrall = 0;
912
913 if (strcmp(args[2], "all") == 0)
914 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200915
Willy Tarreau6162db22009-10-10 17:13:00 +0200916 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200917 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
918 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100919 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100920 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200921 return 1;
922 }
923
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200924 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100925 if (clrall) {
926 memset(&px->be_counters, 0, sizeof(px->be_counters));
927 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
928 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200929 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100930 px->be_counters.conn_max = 0;
931 px->be_counters.p.http.rps_max = 0;
932 px->be_counters.sps_max = 0;
933 px->be_counters.cps_max = 0;
934 px->be_counters.nbpend_max = 0;
935
936 px->fe_counters.conn_max = 0;
937 px->fe_counters.p.http.rps_max = 0;
938 px->fe_counters.sps_max = 0;
939 px->fe_counters.cps_max = 0;
940 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200941 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200942
943 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200944 if (clrall)
945 memset(&sv->counters, 0, sizeof(sv->counters));
946 else {
947 sv->counters.cur_sess_max = 0;
948 sv->counters.nbpend_max = 0;
949 sv->counters.sps_max = 0;
950 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200951
Willy Tarreaua7944ad2012-09-26 21:03:11 +0200952 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200953 if (li->counters) {
954 if (clrall)
955 memset(li->counters, 0, sizeof(*li->counters));
956 else
957 li->counters->conn_max = 0;
958 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200959 }
960
Willy Tarreau81c25d02011-09-07 15:17:21 +0200961 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200962 return 1;
963 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200964 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200965 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200966 /* end of processing */
967 return 1;
968 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200969 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200970 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200971 return 0;
972 }
973 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200974 else if (strcmp(args[0], "get") == 0) {
975 if (strcmp(args[1], "weight") == 0) {
976 struct proxy *px;
977 struct server *sv;
978
979 /* split "backend/server" and make <line> point to server */
980 for (line = args[2]; *line; line++)
981 if (*line == '/') {
982 *line++ = '\0';
983 break;
984 }
985
986 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100987 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100988 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200989 return 1;
990 }
991
992 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100993 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100994 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200995 return 1;
996 }
997
998 /* return server's effective weight at the moment */
David du Colombier7af46052012-05-16 14:16:48 +0200999 snprintf(trash, trashlen, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001000 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001001 return 1;
1002 }
1003 else { /* not "get weight" */
1004 return 0;
1005 }
1006 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001007 else if (strcmp(args[0], "set") == 0) {
1008 if (strcmp(args[1], "weight") == 0) {
1009 struct proxy *px;
1010 struct server *sv;
1011 int w;
1012
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001013 sv = expect_server_admin(s, si, args[2]);
1014 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001015 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001016 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +02001017
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001018 /* if the weight is terminated with '%', it is set relative to
1019 * the initial weight, otherwise it is absolute.
1020 */
1021 if (!*args[3]) {
1022 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001023 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001024 return 1;
1025 }
1026
Willy Tarreau4483d432009-10-10 19:30:08 +02001027 w = atoi(args[3]);
1028 if (strchr(args[3], '%') != NULL) {
1029 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001030 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001031 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001032 return 1;
1033 }
1034 w = sv->iweight * w / 100;
1035 }
1036 else {
1037 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001038 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001039 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001040 return 1;
1041 }
1042 }
1043
1044 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001045 si->applet.ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001046 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001047 return 1;
1048 }
1049
1050 sv->uweight = w;
1051
1052 if (px->lbprm.algo & BE_LB_PROP_DYN) {
1053 /* we must take care of not pushing the server to full throttle during slow starts */
1054 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
1055 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
1056 else
1057 sv->eweight = BE_WEIGHT_SCALE;
1058 sv->eweight *= sv->uweight;
1059 } else {
1060 sv->eweight = sv->uweight;
1061 }
1062
1063 /* static LB algorithms are a bit harder to update */
1064 if (px->lbprm.update_server_eweight)
1065 px->lbprm.update_server_eweight(sv);
Willy Tarreau9580d162012-05-19 19:07:40 +02001066 else if (sv->eweight) {
1067 if (px->lbprm.set_server_status_up)
1068 px->lbprm.set_server_status_up(sv);
1069 }
1070 else {
1071 if (px->lbprm.set_server_status_down)
1072 px->lbprm.set_server_status_down(sv);
1073 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001074
1075 return 1;
1076 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001077 else if (strcmp(args[1], "timeout") == 0) {
1078 if (strcmp(args[2], "cli") == 0) {
1079 unsigned timeout;
1080 const char *res;
1081
1082 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001083 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001084 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001085 return 1;
1086 }
1087
1088 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1089 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001090 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001091 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001092 return 1;
1093 }
1094
1095 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1096 return 1;
1097 }
1098 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001099 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001100 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001101 return 1;
1102 }
1103 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001104 else if (strcmp(args[1], "maxconn") == 0) {
1105 if (strcmp(args[2], "frontend") == 0) {
1106 struct proxy *px;
1107 struct listener *l;
1108 int v;
1109
Willy Tarreau532a4502011-09-07 22:37:44 +02001110 px = expect_frontend_admin(s, si, args[3]);
1111 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001112 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001113
1114 if (!*args[4]) {
1115 si->applet.ctx.cli.msg = "Integer value expected.\n";
1116 si->applet.st0 = STAT_CLI_PRINT;
1117 return 1;
1118 }
1119
1120 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001121 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001122 si->applet.ctx.cli.msg = "Value out of range.\n";
1123 si->applet.st0 = STAT_CLI_PRINT;
1124 return 1;
1125 }
1126
1127 /* OK, the value is fine, so we assign it to the proxy and to all of
1128 * its listeners. The blocked ones will be dequeued.
1129 */
1130 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001131 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001132 l->maxconn = v;
1133 if (l->state == LI_FULL)
1134 resume_listener(l);
1135 }
1136
1137 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1138 dequeue_all_listeners(&s->fe->listener_queue);
1139
1140 return 1;
1141 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001142 else if (strcmp(args[2], "global") == 0) {
1143 int v;
1144
Willy Tarreau290e63a2012-09-20 18:07:14 +02001145 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001146 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1147 si->applet.st0 = STAT_CLI_PRINT;
1148 return 1;
1149 }
1150
1151 if (!*args[3]) {
1152 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1153 si->applet.st0 = STAT_CLI_PRINT;
1154 return 1;
1155 }
1156
1157 v = atoi(args[3]);
1158 if (v > global.hardmaxconn) {
1159 si->applet.ctx.cli.msg = "Value out of range.\n";
1160 si->applet.st0 = STAT_CLI_PRINT;
1161 return 1;
1162 }
1163
1164 /* check for unlimited values */
1165 if (v <= 0)
1166 v = global.hardmaxconn;
1167
1168 global.maxconn = v;
1169
1170 /* Dequeues all of the listeners waiting for a resource */
1171 if (!LIST_ISEMPTY(&global_listener_queue))
1172 dequeue_all_listeners(&global_listener_queue);
1173
1174 return 1;
1175 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001176 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001177 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001178 si->applet.st0 = STAT_CLI_PRINT;
1179 return 1;
1180 }
1181 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001182 else if (strcmp(args[1], "rate-limit") == 0) {
1183 if (strcmp(args[2], "connections") == 0) {
1184 if (strcmp(args[3], "global") == 0) {
1185 int v;
1186
Willy Tarreau290e63a2012-09-20 18:07:14 +02001187 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001188 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1189 si->applet.st0 = STAT_CLI_PRINT;
1190 return 1;
1191 }
1192
1193 if (!*args[4]) {
1194 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1195 si->applet.st0 = STAT_CLI_PRINT;
1196 return 1;
1197 }
1198
1199 v = atoi(args[4]);
1200 if (v < 0) {
1201 si->applet.ctx.cli.msg = "Value out of range.\n";
1202 si->applet.st0 = STAT_CLI_PRINT;
1203 return 1;
1204 }
1205
1206 global.cps_lim = v;
1207
1208 /* Dequeues all of the listeners waiting for a resource */
1209 if (!LIST_ISEMPTY(&global_listener_queue))
1210 dequeue_all_listeners(&global_listener_queue);
1211
1212 return 1;
1213 }
1214 else {
1215 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1216 si->applet.st0 = STAT_CLI_PRINT;
1217 return 1;
1218 }
1219 }
1220 else {
1221 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1222 si->applet.st0 = STAT_CLI_PRINT;
1223 return 1;
1224 }
1225 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001226 else if (strcmp(args[1], "table") == 0) {
1227 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1228 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001229 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001230 return 0;
1231 }
1232 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001233 else if (strcmp(args[0], "enable") == 0) {
1234 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001235 struct server *sv;
1236
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001237 sv = expect_server_admin(s, si, args[2]);
1238 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001239 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001240
Cyril Bontécd19e512010-01-31 22:34:03 +01001241 if (sv->state & SRV_MAINTAIN) {
1242 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001243 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001244 /* If this server tracks the status of another one,
1245 * we must restore the good status.
1246 */
Willy Tarreau44267702011-10-28 15:35:33 +02001247 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001248 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001249 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001250 } else {
1251 sv->state &= ~SRV_MAINTAIN;
1252 set_server_down(sv);
1253 }
1254 } else {
1255 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001256 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001257 }
1258 }
1259
Willy Tarreau532a4502011-09-07 22:37:44 +02001260 return 1;
1261 }
1262 else if (strcmp(args[1], "frontend") == 0) {
1263 struct proxy *px;
1264
1265 px = expect_frontend_admin(s, si, args[2]);
1266 if (!px)
1267 return 1;
1268
1269 if (px->state == PR_STSTOPPED) {
1270 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1271 si->applet.st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274
1275 if (px->state != PR_STPAUSED) {
1276 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1277 si->applet.st0 = STAT_CLI_PRINT;
1278 return 1;
1279 }
1280
1281 if (!resume_proxy(px)) {
1282 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1283 si->applet.st0 = STAT_CLI_PRINT;
1284 return 1;
1285 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001286 return 1;
1287 }
1288 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001289 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1290 si->applet.st0 = STAT_CLI_PRINT;
1291 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001292 }
1293 }
1294 else if (strcmp(args[0], "disable") == 0) {
1295 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001296 struct server *sv;
1297
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001298 sv = expect_server_admin(s, si, args[2]);
1299 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001300 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001301
Cyril Bontécd19e512010-01-31 22:34:03 +01001302 if (! (sv->state & SRV_MAINTAIN)) {
1303 /* Not already in maintenance, we can change the server state */
1304 sv->state |= SRV_MAINTAIN;
1305 set_server_down(sv);
1306 }
1307
Willy Tarreau532a4502011-09-07 22:37:44 +02001308 return 1;
1309 }
1310 else if (strcmp(args[1], "frontend") == 0) {
1311 struct proxy *px;
1312
1313 px = expect_frontend_admin(s, si, args[2]);
1314 if (!px)
1315 return 1;
1316
1317 if (px->state == PR_STSTOPPED) {
1318 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1319 si->applet.st0 = STAT_CLI_PRINT;
1320 return 1;
1321 }
1322
1323 if (px->state == PR_STPAUSED) {
1324 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1325 si->applet.st0 = STAT_CLI_PRINT;
1326 return 1;
1327 }
1328
1329 if (!pause_proxy(px)) {
1330 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1331 si->applet.st0 = STAT_CLI_PRINT;
1332 return 1;
1333 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001334 return 1;
1335 }
1336 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001337 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1338 si->applet.st0 = STAT_CLI_PRINT;
1339 return 1;
1340 }
1341 }
1342 else if (strcmp(args[0], "shutdown") == 0) {
1343 if (strcmp(args[1], "frontend") == 0) {
1344 struct proxy *px;
1345
1346 px = expect_frontend_admin(s, si, args[2]);
1347 if (!px)
1348 return 1;
1349
1350 if (px->state == PR_STSTOPPED) {
1351 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1352 si->applet.st0 = STAT_CLI_PRINT;
1353 return 1;
1354 }
1355
1356 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1357 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1358 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1359 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1360 stop_proxy(px);
1361 return 1;
1362 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001363 else if (strcmp(args[1], "session") == 0) {
1364 struct session *sess, *ptr;
1365
Willy Tarreau290e63a2012-09-20 18:07:14 +02001366 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001367 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1368 si->applet.st0 = STAT_CLI_PRINT;
1369 return 1;
1370 }
1371
1372 if (!*args[2]) {
1373 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1374 si->applet.st0 = STAT_CLI_PRINT;
1375 return 1;
1376 }
1377
1378 ptr = (void *)strtoul(args[2], NULL, 0);
1379
1380 /* first, look for the requested session in the session table */
1381 list_for_each_entry(sess, &sessions, list) {
1382 if (sess == ptr)
1383 break;
1384 }
1385
1386 /* do we have the session ? */
1387 if (sess != ptr) {
1388 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1389 si->applet.st0 = STAT_CLI_PRINT;
1390 return 1;
1391 }
1392
1393 session_shutdown(sess, SN_ERR_KILLED);
1394 return 1;
1395 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001396 else if (strcmp(args[1], "sessions") == 0) {
1397 if (strcmp(args[2], "server") == 0) {
1398 struct server *sv;
1399 struct session *sess, *sess_bck;
1400
1401 sv = expect_server_admin(s, si, args[3]);
1402 if (!sv)
1403 return 1;
1404
1405 /* kill all the session that are on this server */
1406 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1407 if (sess->srv_conn == sv)
1408 session_shutdown(sess, SN_ERR_KILLED);
1409
1410 return 1;
1411 }
1412 else {
1413 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1414 si->applet.st0 = STAT_CLI_PRINT;
1415 return 1;
1416 }
1417 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001418 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001419 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001420 si->applet.st0 = STAT_CLI_PRINT;
1421 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001422 }
1423 }
1424 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001425 return 0;
1426 }
1427 return 1;
1428}
1429
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001430/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001431 * used to processes I/O from/to the stats unix socket. The system relies on a
1432 * state machine handling requests and various responses. We read a request,
1433 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001434 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1435 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001436 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001437 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001438static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001439{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001440 struct channel *req = si->ob;
1441 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001442 int reql;
1443 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001444
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001445 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1446 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001447
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001448 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001449 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001450 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001451 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001452 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001453 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001454 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001455 /* Let's close for real now. We just close the request
1456 * side, the conditions below will complete if needed.
1457 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001458 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001459 break;
1460 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001461 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001462 /* ensure we have some output room left in the event we
1463 * would want to return some info right after parsing.
1464 */
Willy Tarreau572bf902012-07-02 17:01:20 +02001465 if (buffer_almost_full(&si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001466 break;
1467
David du Colombier7af46052012-05-16 14:16:48 +02001468 reql = bo_getline(si->ob, trash, trashlen);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001469 if (reql <= 0) { /* closed or EOL not found */
1470 if (reql == 0)
1471 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001472 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001473 continue;
1474 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001475
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001476 /* seek for a possible semi-colon. If we find one, we
1477 * replace it with an LF and skip only this part.
1478 */
1479 for (len = 0; len < reql; len++)
1480 if (trash[len] == ';') {
1481 trash[len] = '\n';
1482 reql = len + 1;
1483 break;
1484 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001485
Willy Tarreau816fc222009-10-04 07:36:58 +02001486 /* now it is time to check that we have a full line,
1487 * remove the trailing \n and possibly \r, then cut the
1488 * line.
1489 */
1490 len = reql - 1;
1491 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001492 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001493 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001494 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495
Willy Tarreau816fc222009-10-04 07:36:58 +02001496 if (len && trash[len-1] == '\r')
1497 len--;
1498
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001499 trash[len] = '\0';
1500
Willy Tarreaubc4af052011-02-13 13:25:14 +01001501 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001502 if (len) {
1503 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001504 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001505 continue;
1506 }
1507 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001508 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001509 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001510 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001511 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001513 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001514 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001515 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001516 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001517 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001518 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001519 /* if prompt is disabled, print help on empty lines,
1520 * so that the user at least knows how to enable
1521 * prompt and find help.
1522 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001523 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001524 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525 }
1526
1527 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001528 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001529 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001530 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001531 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001532 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001533 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001534 continue;
1535 }
1536
Willy Tarreaubc4af052011-02-13 13:25:14 +01001537 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001538 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001539 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001540 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001542 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001543 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001545 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001546 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001547 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001548 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001549 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001550 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001551 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001552 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001553 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001554 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001555 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001556 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001557 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001558 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001559 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001560 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001561 break;
1562 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001563
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001564 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001565 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001566 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001567 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001568 }
1569
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001570 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001571 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001573
1574 /* Now we close the output if one of the writers did so,
1575 * or if we're not in interactive mode and the request
1576 * buffer is empty. This still allows pipelined requests
1577 * to be sent in non-interactive mode.
1578 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001579 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf.o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001580 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001581 continue;
1582 }
1583
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001584 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001585 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001586 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001587 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001588
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001589 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001590 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1591 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001592 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001593 * and nothing more to consume. This is comparable to a broken pipe, so
1594 * we forward the close to the request side so that it flows upstream to
1595 * the client.
1596 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001597 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001598 }
1599
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001600 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001601 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1602 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1603 /* We have no more processing to do, and nothing more to send, and
1604 * the client side has closed. So we'll forward this state downstream
1605 * on the response buffer.
1606 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001607 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001608 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609 }
1610
1611 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001612 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001613
1614 /* we don't want to expire timeouts while we're processing requests */
1615 si->ib->rex = TICK_ETERNITY;
1616 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001617
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001618 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001619 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 +02001620 __FUNCTION__, __LINE__,
Cyril Bonté3aaba442012-09-23 14:19:12 +02001621 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 +02001622
1623 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1624 /* check that we have released everything then unregister */
1625 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001626 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001627}
1628
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001629/* This function dumps statistics onto the stream interface's read buffer.
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001630 * The xprt_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001631 * It returns 0 as long as it does not complete, non-zero upon completion.
1632 * Some states are not used but it makes the code more similar to other
1633 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001634 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001635static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001636{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001637 struct proxy *px;
1638 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001639 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001640
David du Colombier7af46052012-05-16 14:16:48 +02001641 chunk_init(&msg, trash, trashlen);
Willy Tarreau3e76e722007-10-17 18:57:38 +02001642
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001643 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001644 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001645 /* the function had not been called yet */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001646 si->conn.xprt_st = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001647 /* fall through */
1648
Willy Tarreau295a8372011-03-10 11:25:07 +01001649 case STAT_ST_HEAD:
1650 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001651 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001652 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001653 return 0;
1654 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001655
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001656 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001657 /* fall through */
1658
Willy Tarreau295a8372011-03-10 11:25:07 +01001659 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001660 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001661 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001662 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001663 "Name: " PRODUCT_NAME "\n"
1664 "Version: " HAPROXY_VERSION "\n"
1665 "Release_date: " HAPROXY_DATE "\n"
1666 "Nbproc: %d\n"
1667 "Process_num: %d\n"
1668 "Pid: %d\n"
1669 "Uptime: %dd %dh%02dm%02ds\n"
1670 "Uptime_sec: %d\n"
1671 "Memmax_MB: %d\n"
1672 "Ulimit-n: %d\n"
1673 "Maxsock: %d\n"
1674 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001675 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001676 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001677 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001678 "PipesUsed: %d\n"
1679 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001680 "ConnRate: %d\n"
1681 "ConnRateLimit: %d\n"
1682 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001683 "Tasks: %d\n"
1684 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001685 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001686 "node: %s\n"
1687 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001688 "",
1689 global.nbproc,
1690 relative_pid,
1691 pid,
1692 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1693 up,
1694 global.rlimit_memmax,
1695 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001696 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001697 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001698 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001699 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001700 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001701 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001702 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001703 return 0;
1704 }
1705
Willy Tarreau295a8372011-03-10 11:25:07 +01001706 si->applet.ctx.stats.px = proxy;
1707 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1708 si->applet.ctx.stats.sv = NULL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001709 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001710 /* fall through */
1711
Willy Tarreau295a8372011-03-10 11:25:07 +01001712 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001713 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001714 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1715 while (si->applet.ctx.stats.px) {
1716 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02001717 /* skip the disabled proxies, global frontend and non-networked ones */
1718 if (px->state != PR_STSTOPPED && px->uuid > 0 &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001719 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001720 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001721 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001722 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001723
Willy Tarreau295a8372011-03-10 11:25:07 +01001724 si->applet.ctx.stats.px = px->next;
1725 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001726 }
1727 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001728 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001729
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001730 si->conn.xprt_st = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001731 /* fall through */
1732
Willy Tarreau295a8372011-03-10 11:25:07 +01001733 case STAT_ST_END:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001734 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001735 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001736
Willy Tarreau295a8372011-03-10 11:25:07 +01001737 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001738 return 1;
1739
1740 default:
1741 /* unknown state ! */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001742 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001743 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001744 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001745}
1746
1747
Cyril Bonté70be45d2010-10-12 00:14:35 +02001748/* We don't want to land on the posted stats page because a refresh will
1749 * repost the data. We don't want this to happen on accident so we redirect
1750 * the browse to the stats page with a GET.
1751 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001752static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001753{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001754 struct session *s = si->conn.xprt_ctx;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001755 struct chunk msg;
1756
David du Colombier7af46052012-05-16 14:16:48 +02001757 chunk_init(&msg, trash, trashlen);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001758
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001759 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001760 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001761 chunk_printf(&msg,
1762 "HTTP/1.0 303 See Other\r\n"
1763 "Cache-Control: no-cache\r\n"
1764 "Content-Type: text/plain\r\n"
1765 "Connection: close\r\n"
1766 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001767 uri->uri_prefix,
1768 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1769 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1770 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1771 stat_status_codes[si->applet.ctx.stats.st_code] :
1772 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001773 chunk_printf(&msg, "\r\n\r\n");
1774
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001775 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001776 return 0;
1777
1778 s->txn.status = 303;
1779
1780 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1781 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1782 if (!(s->flags & SN_FINST_MASK))
1783 s->flags |= SN_FINST_R;
1784
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001785 si->conn.xprt_st = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001786 return 1;
1787 }
1788 return 1;
1789}
1790
1791
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001792/* This I/O handler runs as an applet embedded in a stream interface. It is
1793 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001794 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001795 * automatically unregisters itself once transfer is complete.
1796 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001797static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001798{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001799 struct session *s = si->conn.xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001800 struct channel *req = si->ob;
1801 struct channel *res = si->ib;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001802
1803 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1804 goto out;
1805
1806 /* check that the output is not closed */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001807 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001808 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001809
Willy Tarreaubc4af052011-02-13 13:25:14 +01001810 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001811 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001812 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001813 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001814 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001815 }
1816 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001817 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001818 si->applet.st0 = 1;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001819 si_shutw(si);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001820 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001821 }
1822 }
1823
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001824 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
Willy Tarreau73b013b2012-05-21 16:31:45 +02001825 si_shutw(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001826
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001827 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001828 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001829 res->flags |= CF_READ_NULL;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001830 }
1831
1832 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001833 si_update(si);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001834
1835 /* we don't want to expire timeouts while we're processing requests */
1836 si->ib->rex = TICK_ETERNITY;
1837 si->ob->wex = TICK_ETERNITY;
1838
1839 out:
1840 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1841 /* check that we have released everything then unregister */
1842 stream_int_unregister_handler(si);
1843 }
1844}
1845
1846
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001847/* This function dumps statistics in HTTP format onto the stream interface's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001848 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001849 * properly set. It returns 0 if it had to stop writing data and an I/O is
1850 * needed, 1 if the dump is finished and the session must be closed, or -1
1851 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001852 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001853static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001854{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001855 struct session *s = si->conn.xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02001856 struct channel *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001857 struct proxy *px;
1858 struct chunk msg;
1859 unsigned int up;
1860
David du Colombier7af46052012-05-16 14:16:48 +02001861 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02001862
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001863 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001864 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001865 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001866 "HTTP/1.0 200 OK\r\n"
1867 "Cache-Control: no-cache\r\n"
1868 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001869 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001870 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001871
Willy Tarreau295a8372011-03-10 11:25:07 +01001872 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001873 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001874 uri->refresh);
1875
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001876 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001877
1878 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001879 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001880 return 0;
1881
Willy Tarreau91861262007-10-17 17:06:05 +02001882 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1883 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1884 if (!(s->flags & SN_FINST_MASK))
1885 s->flags |= SN_FINST_R;
1886
1887 if (s->txn.meth == HTTP_METH_HEAD) {
1888 /* that's all we return in case of HEAD request */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001889 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001890 return 1;
1891 }
1892
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001893 si->conn.xprt_st = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001894 /* fall through */
1895
Willy Tarreau295a8372011-03-10 11:25:07 +01001896 case STAT_ST_HEAD:
1897 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001898 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001899 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001900 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1901 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001902 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001903 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1904 "<style type=\"text/css\"><!--\n"
1905 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001906 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001907 " font-size: 12px;"
1908 " font-weight: normal;"
1909 " color: black;"
1910 " background: white;"
1911 "}\n"
1912 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001913 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001914 "}\n"
1915 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001916 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001917 " margin-bottom: 0.5em;"
1918 "}\n"
1919 "h2 {"
1920 " font-family: helvetica, arial;"
1921 " font-size: x-large;"
1922 " font-weight: bold;"
1923 " font-style: italic;"
1924 " color: #6020a0;"
1925 " margin-top: 0em;"
1926 " margin-bottom: 0em;"
1927 "}\n"
1928 "h3 {"
1929 " font-family: helvetica, arial;"
1930 " font-size: 16px;"
1931 " font-weight: bold;"
1932 " color: #b00040;"
1933 " background: #e8e8d0;"
1934 " margin-top: 0em;"
1935 " margin-bottom: 0em;"
1936 "}\n"
1937 "li {"
1938 " margin-top: 0.25em;"
1939 " margin-right: 2em;"
1940 "}\n"
1941 ".hr {margin-top: 0.25em;"
1942 " border-color: black;"
1943 " border-bottom-style: solid;"
1944 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001945 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001946 ".total {background: #20D0D0;color: #ffff80;}\n"
1947 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001948 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001949 ".backend {background: #e8e8d0;}\n"
1950 ".active0 {background: #ff9090;}\n"
1951 ".active1 {background: #ffd020;}\n"
1952 ".active2 {background: #ffffa0;}\n"
1953 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001954 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1955 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1956 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001957 ".backup0 {background: #ff9090;}\n"
1958 ".backup1 {background: #ff80ff;}\n"
1959 ".backup2 {background: #c060ff;}\n"
1960 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001961 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1962 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1963 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001964 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001965 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001966 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001967 "a.px:link {color: #ffff40; text-decoration: none;}"
1968 "a.px:visited {color: #ffff40; text-decoration: none;}"
1969 "a.px:hover {color: #ffffff; text-decoration: none;}"
1970 "a.lfsb:link {color: #000000; text-decoration: none;}"
1971 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1972 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1973 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001974 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001975 "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"
1976 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001977 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001978 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001979 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001980 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001981 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001982 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1983 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1984 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001985 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001986 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001987 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001988 (uri->flags&ST_SHNODE) ? " on " : "",
1989 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001990 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001991 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001992 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001993 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001994 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001995 return 0;
1996
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001997 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001998 /* fall through */
1999
Willy Tarreau295a8372011-03-10 11:25:07 +01002000 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02002001 up = (now.tv_sec - start_date.tv_sec);
2002
2003 /* WARNING! this has to fit the first packet too.
2004 * We are around 3.5 kB, add adding entries will
2005 * become tricky if we want to support 4kB buffers !
2006 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002007 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002008 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002009 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2010 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002011 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002012 "<hr width=\"100%%\" class=\"hr\">\n"
2013 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002014 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01002015 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002016 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002017 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2018 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002019 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02002020 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002021 "</td><td align=\"center\" nowrap>\n"
2022 "<table class=\"lgd\"><tr>\n"
2023 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2024 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2025 "</tr><tr>\n"
2026 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
2027 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
2028 "</tr><tr>\n"
2029 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
2030 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2031 "</tr><tr>\n"
2032 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002033 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01002034 "</tr><tr>\n"
2035 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002036 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01002037 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02002038 "</td>"
2039 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2040 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2041 "",
2042 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002043 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2044 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
2045 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02002046 up / 86400, (up % 86400) / 3600,
2047 (up % 3600) / 60, (up % 60),
2048 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2049 global.rlimit_memmax ? " MB" : "",
2050 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01002051 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02002052 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02002053 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02002054 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002055
Willy Tarreau295a8372011-03-10 11:25:07 +01002056 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002057 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002058 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
2059 uri->uri_prefix,
2060 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01002061 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002062 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002063 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002064 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2065 uri->uri_prefix,
2066 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01002067 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002068
Willy Tarreau55bb8452007-10-17 18:44:57 +02002069 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002070 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002071 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002072 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
2073 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002074 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002075 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002076 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002077 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002078 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2079 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002080 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002081 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002082 }
Willy Tarreau91861262007-10-17 17:06:05 +02002083
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002084 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002085 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2086 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002087 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2088 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002089
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002090 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002091 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2092 uri->uri_prefix,
2093 (uri->refresh > 0) ? ";norefresh" : "");
2094
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002095 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002096 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002097 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002098 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002099 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2100 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2101 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2102 "</ul>"
2103 "</td>"
2104 "</tr></table>\n"
2105 ""
2106 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002107
Willy Tarreau295a8372011-03-10 11:25:07 +01002108 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002109 switch (si->applet.ctx.stats.st_code) {
2110 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002111 chunk_printf(&msg,
2112 "<p><div class=active3>"
2113 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2114 "Action processed successfully."
2115 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002116 break;
2117 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002118 chunk_printf(&msg,
2119 "<p><div class=active2>"
2120 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2121 "Nothing has changed."
2122 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002123 break;
2124 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002125 chunk_printf(&msg,
2126 "<p><div class=active2>"
2127 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2128 "Action partially processed.<br>"
2129 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2130 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002131 break;
2132 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002133 chunk_printf(&msg,
2134 "<p><div class=active0>"
2135 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2136 "Action not processed because of invalid parameters."
2137 "<ul>"
2138 "<li>The action is maybe unknown.</li>"
2139 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2140 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2141 "</ul>"
2142 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002143 break;
2144 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002145 chunk_printf(&msg,
2146 "<p><div class=active0>"
2147 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2148 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2149 "You should retry with less servers at a time.</b>"
2150 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002151 break;
2152 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002153 chunk_printf(&msg,
2154 "<p><div class=active0>"
2155 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2156 "<b>Action denied.</b>"
2157 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002158 break;
2159 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002160 chunk_printf(&msg,
2161 "<p><div class=active6>"
2162 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2163 "Unexpected result."
2164 "</div>\n", uri->uri_prefix);
2165 }
2166 chunk_printf(&msg,"<p>\n");
2167 }
2168
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002169 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002170 return 0;
2171 }
Willy Tarreau91861262007-10-17 17:06:05 +02002172
Willy Tarreau295a8372011-03-10 11:25:07 +01002173 si->applet.ctx.stats.px = proxy;
2174 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002175 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002176 /* fall through */
2177
Willy Tarreau295a8372011-03-10 11:25:07 +01002178 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002179 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002180 while (si->applet.ctx.stats.px) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002181 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002182 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002183 px = si->applet.ctx.stats.px;
Willy Tarreau050536d2012-10-04 08:47:34 +02002184 /* skip the disabled proxies, global frontend and non-networked ones */
2185 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002186 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002187 return 0;
2188
Willy Tarreau295a8372011-03-10 11:25:07 +01002189 si->applet.ctx.stats.px = px->next;
2190 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002191 }
2192 /* here, we just have reached the last proxy */
2193
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002194 si->conn.xprt_st = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002195 /* fall through */
2196
Willy Tarreau295a8372011-03-10 11:25:07 +01002197 case STAT_ST_END:
2198 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002199 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002200 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002201 return 0;
2202 }
Willy Tarreau91861262007-10-17 17:06:05 +02002203
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002204 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002205 /* fall through */
2206
Willy Tarreau295a8372011-03-10 11:25:07 +01002207 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002208 return 1;
2209
2210 default:
2211 /* unknown state ! */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002212 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002213 return -1;
2214 }
2215}
2216
2217
2218/*
2219 * Dumps statistics for a proxy.
2220 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2221 * ot non-zero if everything completed.
2222 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002223static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002224{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02002225 struct session *s = si->conn.xprt_ctx;
Willy Tarreau7421efb2012-07-02 15:11:27 +02002226 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002227 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002228 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002229 struct chunk msg;
2230
David du Colombier7af46052012-05-16 14:16:48 +02002231 chunk_init(&msg, trash, trashlen);
Willy Tarreau91861262007-10-17 17:06:05 +02002232
Willy Tarreau295a8372011-03-10 11:25:07 +01002233 switch (si->applet.ctx.stats.px_st) {
2234 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002235 /* we are on a new proxy */
2236
2237 if (uri && uri->scope) {
2238 /* we have a limited scope, we have to check the proxy name */
2239 struct stat_scope *scope;
2240 int len;
2241
2242 len = strlen(px->id);
2243 scope = uri->scope;
2244
2245 while (scope) {
2246 /* match exact proxy name */
2247 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2248 break;
2249
2250 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002251 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002252 break;
2253 scope = scope->next;
2254 }
2255
2256 /* proxy name not found : don't dump anything */
2257 if (scope == NULL)
2258 return 1;
2259 }
2260
Willy Tarreau295a8372011-03-10 11:25:07 +01002261 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2262 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002263 return 1;
2264
Willy Tarreau295a8372011-03-10 11:25:07 +01002265 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002266 /* fall through */
2267
Willy Tarreau295a8372011-03-10 11:25:07 +01002268 case STAT_PX_ST_TH:
2269 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2270 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002271 /* A form to enable/disable this proxy servers */
2272 chunk_printf(&msg,
2273 "<form action=\"%s\" method=\"post\">",
2274 uri->uri_prefix);
2275 }
2276
Willy Tarreau55bb8452007-10-17 18:44:57 +02002277 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002278 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002279 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002280 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002281 "<th class=\"pxname\" width=\"10%%\"");
2282
2283 if (uri->flags&ST_SHLGNDS) {
2284 /* cap, mode, id */
2285 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2286 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2287 px->uuid);
2288
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002289 chunk_printf(&msg, "\"");
2290 }
2291
2292 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002293 ">%s<a name=\"%s\"></a>"
2294 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002295 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002296 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002297 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002298 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002299 "<tr class=\"titre\">",
2300 (uri->flags & ST_SHLGNDS)?"<u>":"",
2301 px->id, px->id, px->id,
2302 (uri->flags & ST_SHLGNDS)?"</u>":"",
2303 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2304
Willy Tarreau295a8372011-03-10 11:25:07 +01002305 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002306 /* Column heading for Enable or Disable server */
2307 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2308 }
2309
2310 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002311 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002312 "<th colspan=3>Queue</th>"
2313 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002314 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002315 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002316 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002317 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002318 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002319 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002320 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002321 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002322 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002323 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002324 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002325 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2326 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002327 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002328
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002329 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002330 return 0;
2331 }
Willy Tarreau91861262007-10-17 17:06:05 +02002332
Willy Tarreau295a8372011-03-10 11:25:07 +01002333 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002334 /* fall through */
2335
Willy Tarreau295a8372011-03-10 11:25:07 +01002336 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002337 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002338 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002339 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2340 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002341 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002342 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002343 "<tr class=\"frontend\">");
2344
Willy Tarreau295a8372011-03-10 11:25:07 +01002345 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002346 /* Column sub-heading for Enable or Disable server */
2347 chunk_printf(&msg, "<td></td>");
2348 }
2349
2350 chunk_printf(&msg,
2351 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002352 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002353 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2354 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002355 "",
2356 px->id, px->id);
2357
2358 if (px->mode == PR_MODE_HTTP) {
2359 chunk_printf(&msg,
2360 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002361 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002362 "",
2363 read_freq_ctr(&px->fe_req_per_sec),
2364 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002365 px->fe_counters.p.http.rps_max,
2366 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002367 LIM2A2(px->fe_sps_lim, "-"));
2368 } else {
2369 chunk_printf(&msg,
2370 /* sessions rate : current, max, limit */
2371 "<td>%s</td><td>%s</td><td>%s</td>"
2372 "",
2373 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002374 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002375 }
2376
2377 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002378 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002379 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002380 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002381 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002382 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002383
2384 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2385 if (px->mode == PR_MODE_HTTP) {
2386 int i;
2387
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002388 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002389
2390 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002391 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002392
Willy Tarreaueabea072011-09-10 23:29:44 +02002393 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2394 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002395 }
2396
2397 chunk_printf(&msg,
2398 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002399 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002400 /* bytes : in, out */
2401 "<td>%s</td><td>%s</td>"
2402 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002403 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002404 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002405 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002406 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002407
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002408 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002409 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002410 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002411 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002412 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002413 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002414 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002415 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002416 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002417 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002418 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002419 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002420 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2421 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002422 px->state == PR_STREADY ? "OPEN" :
2423 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002424 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002425 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002426 /* pxid, name, queue cur, queue max, */
2427 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002428 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002429 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002430 /* bytes : in, out */
2431 "%lld,%lld,"
2432 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002433 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002434 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002435 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002436 /* warnings: retries, redispatches */
2437 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002438 /* server status : reflect frontend status */
2439 "%s,"
2440 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002441 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002442 /* pid, iid, sid, throttle, lbtot, tracked, type */
2443 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002444 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002445 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002446 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002447 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002448 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002449 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2450 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2451 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2452 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002453 px->state == PR_STREADY ? "OPEN" :
2454 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002455 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002456 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002457 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002458
2459 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2460 if (px->mode == PR_MODE_HTTP) {
2461 int i;
2462
2463 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002464 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002465
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002466 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002467 } else {
2468 chunk_printf(&msg, ",,,,,,");
2469 }
2470
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002471 /* failed health analyses */
2472 chunk_printf(&msg, ",");
2473
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002474 /* requests : req_rate, req_rate_max, req_tot, */
2475 chunk_printf(&msg, "%u,%u,%lld,",
2476 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002477 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002478
Willy Tarreauae526782010-03-04 20:34:23 +01002479 /* errors: cli_aborts, srv_aborts */
2480 chunk_printf(&msg, ",,");
2481
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002482 /* finish with EOL */
2483 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002484 }
Willy Tarreau91861262007-10-17 17:06:05 +02002485
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002486 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002487 return 0;
2488 }
2489
Willy Tarreau4348fad2012-09-20 16:48:07 +02002490 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002491 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002492 /* fall through */
2493
Willy Tarreau295a8372011-03-10 11:25:07 +01002494 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002495 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002496 for (; si->applet.ctx.stats.l != &px->conf.listeners; si->applet.ctx.stats.l = l->by_fe.n) {
Willy Tarreau572bf902012-07-02 17:01:20 +02002497 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002498 return 0;
2499
Willy Tarreau4348fad2012-09-20 16:48:07 +02002500 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002501 if (!l->counters)
2502 continue;
2503
Willy Tarreau295a8372011-03-10 11:25:07 +01002504 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2505 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506 break;
2507
Willy Tarreau295a8372011-03-10 11:25:07 +01002508 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002509 continue;
2510 }
2511
Willy Tarreau295a8372011-03-10 11:25:07 +01002512 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002513 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002514 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002515 /* Column sub-heading for Enable or Disable server */
2516 chunk_printf(&msg, "<td></td>");
2517 }
2518 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002519
2520 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002521 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002522 int port;
2523
Willy Tarreau631f01c2011-09-05 00:36:48 +02002524 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002525
Willy Tarreau631f01c2011-09-05 00:36:48 +02002526 port = get_host_port(&l->addr);
2527 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2528 case AF_INET:
2529 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2530 break;
2531 case AF_INET6:
2532 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2533 break;
2534 case AF_UNIX:
2535 chunk_printf(&msg, "unix, ");
2536 break;
2537 case -1:
2538 chunk_printf(&msg, "(%s), ", strerror(errno));
2539 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002540 }
2541
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002542 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002543 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002544 }
2545
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002546 chunk_printf(&msg,
2547 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002548 ">%s<a name=\"%s/+%s\"></a>"
2549 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002550 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002551 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002552 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002553 "<td>%s</td><td>%s</td><td>%s</td>"
2554 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002555 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002556 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002557 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002558 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002559 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002560 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002561 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2562 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2563
2564 chunk_printf(&msg,
2565 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002566 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002567 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002568 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002569 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002570 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002571 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002572 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002573 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002574 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002575 "",
2576 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2577 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002578 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002579 } else {
2580 chunk_printf(&msg,
2581 /* pxid, name, queue cur, queue max, */
2582 "%s,%s,,,"
2583 /* sessions: current, max, limit, total */
2584 "%d,%d,%d,%lld,"
2585 /* bytes: in, out */
2586 "%lld,%lld,"
2587 /* denied: req, resp */
2588 "%lld,%lld,"
2589 /* errors: request, connect, response */
2590 "%lld,,,"
2591 /* warnings: retries, redispatches */
2592 ",,"
2593 /* server status: reflect listener status */
2594 "%s,"
2595 /* rest of server: nothing */
2596 ",,,,,,,,"
2597 /* pid, iid, sid, throttle, lbtot, tracked, type */
2598 "%d,%d,%d,,,,%d,"
2599 /* rate, rate_lim, rate_max */
2600 ",,,"
2601 /* check_status, check_code, check_duration */
2602 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002603 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2604 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002605 /* failed health analyses */
2606 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002607 /* requests : req_rate, req_rate_max, req_tot, */
2608 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002609 /* errors: cli_aborts, srv_aborts */
2610 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002611 "\n",
2612 px->id, l->name,
2613 l->nbconn, l->counters->conn_max,
2614 l->maxconn, l->counters->cum_conn,
2615 l->counters->bytes_in, l->counters->bytes_out,
2616 l->counters->denied_req, l->counters->denied_resp,
2617 l->counters->failed_req,
2618 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2619 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2620 }
2621
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002622 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002623 return 0;
2624 }
2625
Willy Tarreau295a8372011-03-10 11:25:07 +01002626 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2627 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002628 /* fall through */
2629
Willy Tarreau295a8372011-03-10 11:25:07 +01002630 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002631 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002632 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002633 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 +02002634
Willy Tarreau572bf902012-07-02 17:01:20 +02002635 if (buffer_almost_full(&rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002636 return 0;
2637
Willy Tarreau295a8372011-03-10 11:25:07 +01002638 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002639
Willy Tarreau295a8372011-03-10 11:25:07 +01002640 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2641 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002642 break;
2643
Willy Tarreau295a8372011-03-10 11:25:07 +01002644 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002645 continue;
2646 }
2647
Willy Tarreau44267702011-10-28 15:35:33 +02002648 if (sv->track)
2649 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002650 else
2651 svs = sv;
2652
Willy Tarreau91861262007-10-17 17:06:05 +02002653 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002654 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002655 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002656 else if (svs->state & SRV_RUNNING) {
2657 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002658 sv_state = 3; /* UP */
2659 else
2660 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002661
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002662 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002663 sv_state += 2;
2664 }
Willy Tarreau91861262007-10-17 17:06:05 +02002665 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002666 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002667 sv_state = 1; /* going up */
2668 else
2669 sv_state = 0; /* DOWN */
2670
Willy Tarreau295a8372011-03-10 11:25:07 +01002671 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002672 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002673 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002674 continue;
2675 }
2676
Willy Tarreau295a8372011-03-10 11:25:07 +01002677 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002678 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2679 "UP %d/%d &darr;", "UP",
2680 "NOLB %d/%d &darr;", "NOLB",
2681 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002682 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002683 chunk_printf(&msg,
2684 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002685 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002686 );
2687 }
2688 else {
2689 chunk_printf(&msg,
2690 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002691 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002692 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2693 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002694
Willy Tarreau295a8372011-03-10 11:25:07 +01002695 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002696 chunk_printf(&msg,
2697 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2698 sv->id);
2699 }
2700
2701 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002702
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002703 if (uri->flags&ST_SHLGNDS) {
2704 char str[INET6_ADDRSTRLEN];
2705
Willy Tarreau631f01c2011-09-05 00:36:48 +02002706 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002707
Willy Tarreau631f01c2011-09-05 00:36:48 +02002708 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002709 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002710 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002711 break;
2712 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002713 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2714 break;
2715 case AF_UNIX:
2716 chunk_printf(&msg, "unix, ");
2717 break;
2718 case -1:
2719 chunk_printf(&msg, "(%s), ", strerror(errno));
2720 break;
2721 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002722 break;
2723 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002724
2725 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002726 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002727
2728 /* cookie */
2729 if (sv->cookie) {
2730 struct chunk src;
2731
2732 chunk_printf(&msg, ", cookie: '");
2733
2734 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2735 chunk_htmlencode(&msg, &src);
2736
2737 chunk_printf(&msg, "'");
2738 }
2739
2740 chunk_printf(&msg, "\"");
2741 }
2742
2743 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002744 ">%s<a name=\"%s/%s\"></a>"
2745 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002746 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002747 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002748 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002749 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002750 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002751 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002752 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002753 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002754 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002755 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002756 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002757 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2758 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002759 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2760
2761 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2762 if (px->mode == PR_MODE_HTTP) {
2763 int i;
2764
2765 chunk_printf(&msg, " title=\"rsp codes:");
2766
2767 for (i = 1; i < 6; i++)
2768 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2769
2770 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2771 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002772
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002773 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002774 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002775 ">%s%s%s</td><td>%s</td>",
2776 (px->mode == PR_MODE_HTTP)?"<u>":"",
2777 U2H0(sv->counters.cum_sess),
2778 (px->mode == PR_MODE_HTTP)?"</u>":"",
2779 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002780
2781 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002782 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002783 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002784 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002785 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002786 /* errors : request, connect */
2787 "<td></td><td>%s</td>"
2788 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002789 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002790 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002791 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002792 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002793 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2794 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002795 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002796 sv->counters.cli_aborts,
2797 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002798 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002799 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002800
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002801 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002802 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002803
Cyril Bontécd19e512010-01-31 22:34:03 +01002804 if (sv->state & SRV_MAINTAIN) {
2805 chunk_printf(&msg, "%s ",
2806 human_time(now.tv_sec - sv->last_change, 1));
2807 chunk_printf(&msg, "MAINT");
2808 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002809 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2810 chunk_printf(&msg, "%s ",
2811 human_time(now.tv_sec - svs->last_change, 1));
2812 chunk_printf(&msg, "MAINT(via)");
2813 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002814 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002815 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002816 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002817
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002818 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002819 srv_hlt_st[sv_state],
2820 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2821 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002822 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002823
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002824 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002825 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002826 get_check_status_description(sv->check.status));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002827
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002828 if (*sv->check.desc) {
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002829 struct chunk src;
2830
2831 chunk_printf(&msg, ": ");
2832
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002833 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002834 chunk_htmlencode(&msg, &src);
2835 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002836
Willy Tarreaue0454092010-02-26 12:29:07 +01002837 chunk_printf(&msg, "\"><u> %s%s",
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002838 tv_iszero(&sv->check.start)?"":"* ",
2839 get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002840
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002841 if (sv->check.status >= HCHK_STATUS_L57DATA)
2842 chunk_printf(&msg, "/%d", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002843
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002844 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
2845 chunk_printf(&msg, " in %lums</u>", sv->check.duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002846 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002847 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002848
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002849 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002850 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002851 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002852 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002853 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002854 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002855 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002856 (sv->state & SRV_BACKUP) ? "-" : "Y",
2857 (sv->state & SRV_BACKUP) ? "Y" : "-");
2858
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002859 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002860 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002861 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002862 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2863
2864 if (svs->observe)
2865 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2866
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002867 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002868 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002869 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002870 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002871 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002872 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002873 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002874 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2875 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002876 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002877 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002878 "<td colspan=3></td>");
2879
2880 /* throttle */
2881 if ((sv->state & SRV_WARMINGUP) &&
2882 now.tv_sec < sv->last_change + sv->slowstart &&
2883 now.tv_sec >= sv->last_change) {
2884 unsigned int ratio;
2885 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002886 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002887 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002888 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002889 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002890 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002891 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002892 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002893 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2894 "UP %d/%d,", "UP,",
2895 "NOLB %d/%d,", "NOLB,",
2896 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002897 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002898 /* pxid, name */
2899 "%s,%s,"
2900 /* queue : current, max */
2901 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002902 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002903 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002904 /* bytes : in, out */
2905 "%lld,%lld,"
2906 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002907 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002908 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002909 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002910 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002911 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002912 "",
2913 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002914 sv->nbpend, sv->counters.nbpend_max,
2915 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002916 sv->counters.bytes_in, sv->counters.bytes_out,
2917 sv->counters.failed_secu,
2918 sv->counters.failed_conns, sv->counters.failed_resp,
2919 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002920
Willy Tarreau55bb8452007-10-17 18:44:57 +02002921 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002922 if (sv->state & SRV_MAINTAIN) {
2923 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002924 }
2925 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2926 chunk_printf(&msg, "MAINT(via),");
2927 }
2928 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002929 chunk_printf(&msg,
2930 srv_hlt_st[sv_state],
2931 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2932 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2933 }
Willy Tarreau91861262007-10-17 17:06:05 +02002934
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002935 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002936 /* weight, active, backup */
2937 "%d,%d,%d,"
2938 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002939 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002940 (sv->state & SRV_BACKUP) ? 0 : 1,
2941 (sv->state & SRV_BACKUP) ? 1 : 0);
2942
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002943 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002944 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002945 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002946 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002947 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002948 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002949 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002950 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002951 ",,,,");
2952
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002953 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002954 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002955 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002956 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002957 LIM2A0(sv->maxqueue, ""),
2958 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002959
2960 /* throttle */
2961 if ((sv->state & SRV_WARMINGUP) &&
2962 now.tv_sec < sv->last_change + sv->slowstart &&
2963 now.tv_sec >= sv->last_change) {
2964 unsigned int ratio;
2965 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002966 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002967 }
2968
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002969 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002970 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002971
2972 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002973 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002974 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002975 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002976 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002977 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002978
Willy Tarreau7f062c42009-03-05 18:43:00 +01002979 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002980 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002981
2982 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002983 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002984 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002985 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002986
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002987 if (sv->state & SRV_CHECKED) {
2988 /* check_status */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002989 chunk_printf(&msg, "%s,", get_check_status_info(sv->check.status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002990
2991 /* check_code */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002992 if (sv->check.status >= HCHK_STATUS_L57DATA)
2993 chunk_printf(&msg, "%u,", sv->check.code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002994 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002995 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002996
2997 /* check_duration */
Willy Tarreau5b3a2022012-09-28 15:01:02 +02002998 if (sv->check.status >= HCHK_STATUS_CHECKED)
2999 chunk_printf(&msg, "%lu,", sv->check.duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003000 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003001 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003002
3003 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003004 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003005 }
3006
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003007 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3008 if (px->mode == PR_MODE_HTTP) {
3009 int i;
3010
3011 for (i=1; i<6; i++)
3012 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
3013
3014 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
3015 } else {
3016 chunk_printf(&msg, ",,,,,,");
3017 }
3018
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003019 /* failed health analyses */
3020 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
3021
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003022 /* requests : req_rate, req_rate_max, req_tot, */
3023 chunk_printf(&msg, ",,,");
3024
Willy Tarreauae526782010-03-04 20:34:23 +01003025 /* errors: cli_aborts, srv_aborts */
3026 chunk_printf(&msg, "%lld,%lld,",
3027 sv->counters.cli_aborts, sv->counters.srv_aborts);
3028
Willy Tarreau7f062c42009-03-05 18:43:00 +01003029 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003030 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003031 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003032 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003033 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003034 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02003035
Willy Tarreau295a8372011-03-10 11:25:07 +01003036 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02003037 /* fall through */
3038
Willy Tarreau295a8372011-03-10 11:25:07 +01003039 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02003040 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01003041 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01003042 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
3043 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003044 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01003045 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003046 /* Column sub-heading for Enable or Disable server */
3047 chunk_printf(&msg, "<td></td>");
3048 }
3049 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003050
3051 if (uri->flags&ST_SHLGNDS) {
3052 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003053 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003054 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01003055
3056 /* cookie */
3057 if (px->cookie_name) {
3058 struct chunk src;
3059
3060 chunk_printf(&msg, ", cookie: '");
3061
3062 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3063 chunk_htmlencode(&msg, &src);
3064
3065 chunk_printf(&msg, "'");
3066 }
3067
3068 chunk_printf(&msg, "\"");
3069
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003070 }
3071
3072 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02003073 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01003074 ">%s<a name=\"%s/Backend\"></a>"
3075 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003076 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003077 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003078 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003079 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003080 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003081 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003082 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003083 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003084 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3085 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003086
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003087 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003088 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003089 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003090 "<td"
3091 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003092 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003093
3094 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3095 if (px->mode == PR_MODE_HTTP) {
3096 int i;
3097
3098 chunk_printf(&msg, " title=\"rsp codes:");
3099
3100 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003101 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003102
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003103 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003104 }
3105
3106 chunk_printf(&msg,
3107 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003108 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003109 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003110 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003111 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003112 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003113 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003114 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003115 U2H7(px->be_counters.cum_lbconn),
3116 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003117
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003118 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003119 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003120 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003121 /* errors : request, connect */
3122 "<td></td><td>%s</td>"
3123 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003124 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003125 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003126 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003127 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003128 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003129 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003130 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003131 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3132 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003133 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003134 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3135 U2H2(px->be_counters.failed_conns),
3136 px->be_counters.cli_aborts,
3137 px->be_counters.srv_aborts,
3138 U2H5(px->be_counters.failed_resp),
3139 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003140 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003141 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3142 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003143 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003144 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003145
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003146 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003147 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003148 "<td class=ac>&nbsp;</td><td>%d</td>"
3149 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003150 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003151 "</tr>",
3152 px->down_trans,
3153 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003154 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003155 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003156 /* pxid, name */
3157 "%s,BACKEND,"
3158 /* queue : current, max */
3159 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003160 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003161 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003162 /* bytes : in, out */
3163 "%lld,%lld,"
3164 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003165 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003166 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003167 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003168 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003169 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003170 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003171 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003172 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003173 * active and backups. */
3174 "%s,"
3175 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003176 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003177 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003178 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003179 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003180 /* rate, rate_lim, rate_max, */
3181 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003182 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003183 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003184 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003185 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3186 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3187 px->be_counters.bytes_in, px->be_counters.bytes_out,
3188 px->be_counters.denied_req, px->be_counters.denied_resp,
3189 px->be_counters.failed_conns, px->be_counters.failed_resp,
3190 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003191 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003192 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003193 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003194 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003195 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003196 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003197 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003198 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003199 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003200
3201 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3202 if (px->mode == PR_MODE_HTTP) {
3203 int i;
3204
3205 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003206 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003207
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003208 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003209 } else {
3210 chunk_printf(&msg, ",,,,,,");
3211 }
3212
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003213 /* failed health analyses */
3214 chunk_printf(&msg, ",");
3215
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003216 /* requests : req_rate, req_rate_max, req_tot, */
3217 chunk_printf(&msg, ",,,");
3218
Willy Tarreauae526782010-03-04 20:34:23 +01003219 /* errors: cli_aborts, srv_aborts */
3220 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003221 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003222
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003223 /* finish with EOL */
3224 chunk_printf(&msg, "\n");
3225
Willy Tarreau55bb8452007-10-17 18:44:57 +02003226 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003227 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003228 return 0;
3229 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003230
Willy Tarreau295a8372011-03-10 11:25:07 +01003231 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003232 /* fall through */
3233
Willy Tarreau295a8372011-03-10 11:25:07 +01003234 case STAT_PX_ST_END:
3235 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003236 chunk_printf(&msg, "</table>");
3237
Willy Tarreau295a8372011-03-10 11:25:07 +01003238 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003239 /* close the form used to enable/disable this proxy servers */
3240 chunk_printf(&msg,
3241 "Choose the action to perform on the checked servers : "
3242 "<select name=action>"
3243 "<option value=\"\"></option>"
3244 "<option value=\"disable\">Disable</option>"
3245 "<option value=\"enable\">Enable</option>"
Willy Tarreaud7282242012-06-04 00:22:44 +02003246 "<option value=\"stop\">Soft Stop</option>"
3247 "<option value=\"start\">Soft Start</option>"
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003248 "<option value=\"shutdown\">Kill Sessions</option>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003249 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003250 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003251 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3252 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003253 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003254 }
3255
3256 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003257
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003258 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003259 return 0;
3260 }
Willy Tarreau91861262007-10-17 17:06:05 +02003261
Willy Tarreau295a8372011-03-10 11:25:07 +01003262 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003263 /* fall through */
3264
Willy Tarreau295a8372011-03-10 11:25:07 +01003265 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003266 return 1;
3267
3268 default:
3269 /* unknown state, we should put an abort() here ! */
3270 return 1;
3271 }
3272}
3273
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003274/* This function dumps a complete session state onto the stream interface's
3275 * read buffer. The xprt_ctx must have been zeroed first, and the flags
3276 * properly set. The session has to be set in xprt_ctx.sess.target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003277 * 0 if the output buffer is full and it needs to be called again, otherwise
3278 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003279 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003280static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003281{
3282 struct tm tm;
3283 struct chunk msg;
3284 struct session *sess;
3285 extern const char *monthname[12];
3286 char pn[INET6_ADDRSTRLEN];
3287
David du Colombier7af46052012-05-16 14:16:48 +02003288 chunk_init(&msg, trash, trashlen);
Willy Tarreau295a8372011-03-10 11:25:07 +01003289 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003290
Willy Tarreau295a8372011-03-10 11:25:07 +01003291 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003292 /* session changed, no need to go any further */
3293 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003294 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003295 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003296 si->applet.ctx.sess.target = NULL;
3297 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003298 return 1;
3299 }
3300
Willy Tarreau295a8372011-03-10 11:25:07 +01003301 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003302 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003303 si->applet.ctx.sess.uid = sess->uniq_id;
3304 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003305 /* fall through */
3306
3307 case 1:
3308 chunk_printf(&msg,
3309 "%p: id=%u, proto=%s",
3310 sess,
3311 sess->uniq_id,
3312 sess->listener->proto->name);
3313
Willy Tarreau986a9d22012-08-30 21:11:38 +02003314 switch (addr_to_str(&sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003315 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003316 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003317 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003318 pn, get_host_port(&sess->si[0].conn.addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003319 break;
3320 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003321 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003322 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003323 default:
3324 /* no more information to print right now */
3325 chunk_printf(&msg, "\n");
3326 break;
3327 }
3328
3329 chunk_printf(&msg,
3330 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003331 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003332
3333 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003334 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003335 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3336 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3337 sess->listener ? sess->listener->luid : 0);
3338
Willy Tarreau986a9d22012-08-30 21:11:38 +02003339 conn_get_to_addr(&sess->si[0].conn);
3340 switch (addr_to_str(&sess->si[0].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003341 case AF_INET:
3342 case AF_INET6:
3343 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003344 pn, get_host_port(&sess->si[0].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003345 break;
3346 case AF_UNIX:
3347 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3348 break;
3349 default:
3350 /* no more information to print right now */
3351 chunk_printf(&msg, "\n");
3352 break;
3353 }
3354
Willy Tarreau50943332011-09-02 17:33:05 +02003355 if (sess->be->cap & PR_CAP_BE)
3356 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003357 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003358 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003359 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3360 else
3361 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3362
Willy Tarreau986a9d22012-08-30 21:11:38 +02003363 conn_get_from_addr(&sess->si[1].conn);
3364 switch (addr_to_str(&sess->si[1].conn.addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003365 case AF_INET:
3366 case AF_INET6:
3367 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003368 pn, get_host_port(&sess->si[1].conn.addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003369 break;
3370 case AF_UNIX:
3371 chunk_printf(&msg, " addr=unix\n");
3372 break;
3373 default:
3374 /* no more information to print right now */
3375 chunk_printf(&msg, "\n");
3376 break;
3377 }
3378
3379 if (sess->be->cap & PR_CAP_BE)
3380 chunk_printf(&msg,
3381 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003382 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3383 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3384 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003385 chunk_printf(&msg, " server=<NONE> (id=-1)");
3386
Willy Tarreau986a9d22012-08-30 21:11:38 +02003387 conn_get_to_addr(&sess->si[1].conn);
3388 switch (addr_to_str(&sess->si[1].conn.addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003389 case AF_INET:
3390 case AF_INET6:
3391 chunk_printf(&msg, " addr=%s:%d\n",
Willy Tarreau986a9d22012-08-30 21:11:38 +02003392 pn, get_host_port(&sess->si[1].conn.addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003393 break;
3394 case AF_UNIX:
3395 chunk_printf(&msg, " addr=unix\n");
3396 break;
3397 default:
3398 /* no more information to print right now */
3399 chunk_printf(&msg, "\n");
3400 break;
3401 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003402
3403 chunk_printf(&msg,
3404 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3405 sess->task,
3406 sess->task->state,
3407 sess->task->nice, sess->task->calls,
3408 sess->task->expire ?
3409 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3410 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3411 TICKS_TO_MS(1000)) : "<NEVER>",
3412 task_in_rq(sess->task) ? ", running" : "");
3413
3414 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3415 chunk_printf(&msg,
3416 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3417 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3418 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3419 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3420
3421 chunk_printf(&msg,
3422 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3423 &sess->si[0],
3424 sess->si[0].state,
3425 sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003426 si_fd(&sess->si[0]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003427 sess->si[0].exp ?
3428 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3429 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3430 TICKS_TO_MS(1000)) : "<NEVER>",
3431 sess->si[0].err_type);
3432
3433 chunk_printf(&msg,
3434 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3435 &sess->si[1],
3436 sess->si[1].state,
3437 sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003438 si_fd(&sess->si[1]),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003439 sess->si[1].exp ?
3440 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3441 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3442 TICKS_TO_MS(1000)) : "<NEVER>",
3443 sess->si[1].err_type);
3444
3445 chunk_printf(&msg,
3446 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3447 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3448 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3449
3450
3451 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003452 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003453 " an_exp=%s",
3454 sess->req,
3455 sess->req->flags, sess->req->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003456 sess->req->buf.i, sess->req->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003457 sess->req->pipe ? sess->req->pipe->data : 0,
3458 sess->req->to_forward,
3459 sess->req->analyse_exp ?
3460 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3461 TICKS_TO_MS(1000)) : "<NEVER>");
3462
3463 chunk_printf(&msg,
3464 " rex=%s",
3465 sess->req->rex ?
3466 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3467 TICKS_TO_MS(1000)) : "<NEVER>");
3468
3469 chunk_printf(&msg,
3470 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003471 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003472 sess->req->wex ?
3473 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3474 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003475 sess->req->buf.data,
3476 (int)(sess->req->buf.p - sess->req->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003477 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003478 sess->req->total);
3479
3480 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003481 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003482 " an_exp=%s",
3483 sess->rep,
3484 sess->rep->flags, sess->rep->analysers,
Willy Tarreau572bf902012-07-02 17:01:20 +02003485 sess->rep->buf.i, sess->rep->buf.o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003486 sess->rep->pipe ? sess->rep->pipe->data : 0,
3487 sess->rep->to_forward,
3488 sess->rep->analyse_exp ?
3489 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3490 TICKS_TO_MS(1000)) : "<NEVER>");
3491
3492 chunk_printf(&msg,
3493 " rex=%s",
3494 sess->rep->rex ?
3495 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3496 TICKS_TO_MS(1000)) : "<NEVER>");
3497
3498 chunk_printf(&msg,
3499 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003500 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003501 sess->rep->wex ?
3502 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3503 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau572bf902012-07-02 17:01:20 +02003504 sess->rep->buf.data,
3505 (int)(sess->rep->buf.p - sess->rep->buf.data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003506 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003507 sess->rep->total);
3508
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003509 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003510 return 0;
3511
3512 /* use other states to dump the contents */
3513 }
3514 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003515 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003516 return 1;
3517}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003518
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003519/* This function dumps all sessions' states onto the stream interface's
3520 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003521 * properly set. It returns 0 if the output buffer is full and it needs
3522 * to be called again, otherwise non-zero. It is designed to be called
3523 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003524 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003525static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003526{
3527 struct chunk msg;
3528
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003529 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003530 /* If we're forced to shut down, we might have to remove our
3531 * reference to the last session being dumped.
3532 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003533 if (si->conn.xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003534 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3535 LIST_DEL(&si->applet.ctx.sess.bref.users);
3536 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003537 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003538 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003539 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003540 }
3541
David du Colombier7af46052012-05-16 14:16:48 +02003542 chunk_init(&msg, trash, trashlen);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003543
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003544 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003545 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003546 /* the function had not been called yet, let's prepare the
3547 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003548 * pointer to the first in the global list. When a target
3549 * session is being destroyed, it is responsible for updating
3550 * this pointer. We know we have reached the end when this
3551 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003552 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003553 LIST_INIT(&si->applet.ctx.sess.bref.users);
3554 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003555 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003556 /* fall through */
3557
Willy Tarreau295a8372011-03-10 11:25:07 +01003558 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003559 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003560 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3561 LIST_DEL(&si->applet.ctx.sess.bref.users);
3562 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003563 }
3564
3565 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003566 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003567 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003568 struct session *curr_sess;
3569
Willy Tarreau295a8372011-03-10 11:25:07 +01003570 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003571
Willy Tarreau295a8372011-03-10 11:25:07 +01003572 if (si->applet.ctx.sess.target) {
3573 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003574 goto next_sess;
3575
Willy Tarreau295a8372011-03-10 11:25:07 +01003576 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003577 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003578 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003579 return 0;
3580
3581 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003582 LIST_DEL(&si->applet.ctx.sess.bref.users);
3583 LIST_INIT(&si->applet.ctx.sess.bref.users);
3584 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003585 break;
3586 }
3587
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003588 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003589 "%p: proto=%s",
3590 curr_sess,
3591 curr_sess->listener->proto->name);
3592
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003593
Willy Tarreau986a9d22012-08-30 21:11:38 +02003594 switch (addr_to_str(&curr_sess->si[0].conn.addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003595 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003596 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003597 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003598 " src=%s:%d fe=%s be=%s srv=%s",
3599 pn,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003600 get_host_port(&curr_sess->si[0].conn.addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003601 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003602 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003603 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003604 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003605 break;
3606 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003607 chunk_printf(&msg,
3608 " src=unix:%d fe=%s be=%s srv=%s",
3609 curr_sess->listener->luid,
3610 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003611 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003612 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003613 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003614 break;
3615 }
3616
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003617 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003618 " ts=%02x age=%s calls=%d",
3619 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003620 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3621 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003622
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003623 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003624 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003625 curr_sess->req->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003626 curr_sess->req->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003627 curr_sess->req->analysers,
3628 curr_sess->req->rex ?
3629 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3630 TICKS_TO_MS(1000)) : "");
3631
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003632 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003633 ",wx=%s",
3634 curr_sess->req->wex ?
3635 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3636 TICKS_TO_MS(1000)) : "");
3637
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003638 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003639 ",ax=%s]",
3640 curr_sess->req->analyse_exp ?
3641 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3642 TICKS_TO_MS(1000)) : "");
3643
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003644 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003645 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003646 curr_sess->rep->flags,
Willy Tarreau572bf902012-07-02 17:01:20 +02003647 curr_sess->rep->buf.i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003648 curr_sess->rep->analysers,
3649 curr_sess->rep->rex ?
3650 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3651 TICKS_TO_MS(1000)) : "");
3652
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003653 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003654 ",wx=%s",
3655 curr_sess->rep->wex ?
3656 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3657 TICKS_TO_MS(1000)) : "");
3658
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003659 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003660 ",ax=%s]",
3661 curr_sess->rep->analyse_exp ?
3662 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3663 TICKS_TO_MS(1000)) : "");
3664
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003665 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003666 " s0=[%d,%1xh,fd=%d,ex=%s]",
3667 curr_sess->si[0].state,
3668 curr_sess->si[0].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003669 si_fd(&curr_sess->si[0]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003670 curr_sess->si[0].exp ?
3671 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3672 TICKS_TO_MS(1000)) : "");
3673
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003674 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003675 " s1=[%d,%1xh,fd=%d,ex=%s]",
3676 curr_sess->si[1].state,
3677 curr_sess->si[1].flags,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003678 si_fd(&curr_sess->si[1]),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003679 curr_sess->si[1].exp ?
3680 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3681 TICKS_TO_MS(1000)) : "");
3682
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003683 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003684 " exp=%s",
3685 curr_sess->task->expire ?
3686 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3687 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003688 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003689 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003690
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003691 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003692
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003693 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003694 /* let's try again later from this session. We add ourselves into
3695 * this session's users so that it can remove us upon termination.
3696 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003697 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003698 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003699 }
3700
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003701 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003702 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003703 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003704
Willy Tarreau295a8372011-03-10 11:25:07 +01003705 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003706 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003707 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003708 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3709 else
3710 chunk_printf(&msg, "Session not found.\n");
3711
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003712 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003713 return 0;
3714
Willy Tarreau295a8372011-03-10 11:25:07 +01003715 si->applet.ctx.sess.target = NULL;
3716 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003717 return 1;
3718 }
3719
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003720 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003721 /* fall through */
3722
3723 default:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003724 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003725 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003726 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003727}
3728
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003729/* This function dumps all tables' states onto the stream interface's
3730 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003731 * properly set. It returns 0 if the output buffer is full and it needs
3732 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003733 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003734static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003735{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003736 struct session *s = si->conn.xprt_ctx;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003737 struct chunk msg;
3738 struct ebmb_node *eb;
3739 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003740 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003741
3742 /*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003743 * We have 3 possible states in si->conn.xprt_st :
Willy Tarreau295a8372011-03-10 11:25:07 +01003744 * - STAT_ST_INIT : the first call
3745 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003746 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003747 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003748 * and the entry pointer points to the next entry to be dumped,
3749 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003750 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003751 * data though.
3752 */
3753
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003754 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003755 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003756 if (si->conn.xprt_st == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003757 si->applet.ctx.table.entry->ref_cnt--;
3758 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003759 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003760 return 1;
3761 }
3762
David du Colombier7af46052012-05-16 14:16:48 +02003763 chunk_init(&msg, trash, trashlen);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003764
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003765 while (si->conn.xprt_st != STAT_ST_FIN) {
3766 switch (si->conn.xprt_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003767 case STAT_ST_INIT:
3768 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3769 if (!si->applet.ctx.table.proxy)
3770 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003771
Willy Tarreau295a8372011-03-10 11:25:07 +01003772 si->applet.ctx.table.entry = NULL;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003773 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003774 break;
3775
Willy Tarreau295a8372011-03-10 11:25:07 +01003776 case STAT_ST_INFO:
3777 if (!si->applet.ctx.table.proxy ||
3778 (si->applet.ctx.table.target &&
3779 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003780 si->conn.xprt_st = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003781 break;
3782 }
3783
Willy Tarreau295a8372011-03-10 11:25:07 +01003784 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003785 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3786 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003787 return 0;
3788
Willy Tarreau295a8372011-03-10 11:25:07 +01003789 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02003790 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003791 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003792 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003793 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003794 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3795 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003796 si->conn.xprt_st = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003797 break;
3798 }
3799 }
3800 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003801 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003802 break;
3803
Willy Tarreau295a8372011-03-10 11:25:07 +01003804 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003805 skip_entry = false;
3806
Willy Tarreau295a8372011-03-10 11:25:07 +01003807 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003808 /* we're filtering on some data contents */
3809 void *ptr;
3810 long long data;
3811
Willy Tarreau295a8372011-03-10 11:25:07 +01003812 dt = si->applet.ctx.table.data_type;
3813 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3814 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003815 dt);
3816
3817 data = 0;
3818 switch (stktable_data_types[dt].std_type) {
3819 case STD_T_SINT:
3820 data = stktable_data_cast(ptr, std_t_sint);
3821 break;
3822 case STD_T_UINT:
3823 data = stktable_data_cast(ptr, std_t_uint);
3824 break;
3825 case STD_T_ULL:
3826 data = stktable_data_cast(ptr, std_t_ull);
3827 break;
3828 case STD_T_FRQP:
3829 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003830 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003831 break;
3832 }
3833
3834 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003835 if ((data < si->applet.ctx.table.value &&
3836 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3837 si->applet.ctx.table.data_op == STD_OP_GT ||
3838 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3839 (data == si->applet.ctx.table.value &&
3840 (si->applet.ctx.table.data_op == STD_OP_NE ||
3841 si->applet.ctx.table.data_op == STD_OP_GT ||
3842 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3843 (data > si->applet.ctx.table.value &&
3844 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3845 si->applet.ctx.table.data_op == STD_OP_LT ||
3846 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003847 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003848 }
3849
Simon Hormanc88b8872011-06-15 15:18:49 +09003850 if (show && !skip_entry &&
3851 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003852 si->applet.ctx.table.entry))
3853 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003854
Willy Tarreau295a8372011-03-10 11:25:07 +01003855 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003856
Willy Tarreau295a8372011-03-10 11:25:07 +01003857 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003858 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003859 struct stksess *old = si->applet.ctx.table.entry;
3860 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003861 if (show)
3862 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3863 else
3864 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003865 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003866 break;
3867 }
3868
Simon Hormanc88b8872011-06-15 15:18:49 +09003869
3870 if (show)
3871 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3872 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3873 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3874
Willy Tarreau295a8372011-03-10 11:25:07 +01003875 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003876 si->conn.xprt_st = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003877 break;
3878
Willy Tarreau295a8372011-03-10 11:25:07 +01003879 case STAT_ST_END:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003880 si->conn.xprt_st = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003881 break;
3882 }
3883 }
3884 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003885}
3886
Willy Tarreaud426a182010-03-05 14:58:26 +01003887/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003888 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3889 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3890 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3891 * lines are respected within the limit of 70 output chars. Lines that are
3892 * continuation of a previous truncated line begin with "+" instead of " "
3893 * after the offset. The new pointer is returned.
3894 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003895static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3896 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003897{
3898 int end;
3899 unsigned char c;
3900
3901 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003902 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003903 return ptr;
3904
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003905 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003906
Willy Tarreaud426a182010-03-05 14:58:26 +01003907 while (ptr < len && ptr < bsize) {
3908 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003909 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003910 if (out->len > end - 2)
3911 break;
3912 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003913 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003914 if (out->len > end - 3)
3915 break;
3916 out->str[out->len++] = '\\';
3917 switch (c) {
3918 case '\t': c = 't'; break;
3919 case '\n': c = 'n'; break;
3920 case '\r': c = 'r'; break;
3921 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003922 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003923 }
3924 out->str[out->len++] = c;
3925 } else {
3926 if (out->len > end - 5)
3927 break;
3928 out->str[out->len++] = '\\';
3929 out->str[out->len++] = 'x';
3930 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3931 out->str[out->len++] = hextab[c & 0xF];
3932 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003933 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003934 /* we had a line break, let's return now */
3935 out->str[out->len++] = '\n';
3936 *line = ptr;
3937 return ptr;
3938 }
3939 }
3940 /* we have an incomplete line, we return it as-is */
3941 out->str[out->len++] = '\n';
3942 return ptr;
3943}
3944
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003945/* This function dumps all captured errors onto the stream interface's
3946 * read buffer. The xprt_ctx must have been zeroed first, and the flags
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003947 * properly set. It returns 0 if the output buffer is full and it needs
3948 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003949 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003950static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003951{
3952 extern const char *monthname[12];
3953 struct chunk msg;
3954
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003955 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003956 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003957
David du Colombier7af46052012-05-16 14:16:48 +02003958 chunk_init(&msg, trash, trashlen);
Willy Tarreau74808cb2009-03-04 15:53:18 +01003959
Willy Tarreau295a8372011-03-10 11:25:07 +01003960 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003961 /* the function had not been called yet, let's prepare the
3962 * buffer for a response.
3963 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003964 struct tm tm;
3965
3966 get_localtime(date.tv_sec, &tm);
3967 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3968 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3969 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3970 error_snapshot_id);
3971
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003972 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003973 /* Socket buffer full. Let's try again later from the same point */
3974 return 0;
3975 }
3976
Willy Tarreau295a8372011-03-10 11:25:07 +01003977 si->applet.ctx.errors.px = proxy;
3978 si->applet.ctx.errors.buf = 0;
3979 si->applet.ctx.errors.bol = 0;
3980 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003981 }
3982
3983 /* we have two inner loops here, one for the proxy, the other one for
3984 * the buffer.
3985 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003986 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003987 struct error_snapshot *es;
3988
Willy Tarreau295a8372011-03-10 11:25:07 +01003989 if (si->applet.ctx.errors.buf == 0)
3990 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003991 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003992 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003993
3994 if (!es->when.tv_sec)
3995 goto next;
3996
Willy Tarreau295a8372011-03-10 11:25:07 +01003997 if (si->applet.ctx.errors.iid >= 0 &&
3998 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3999 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004000 goto next;
4001
Willy Tarreau295a8372011-03-10 11:25:07 +01004002 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004003 /* just print headers now */
4004
4005 char pn[INET6_ADDRSTRLEN];
4006 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004007 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004008
4009 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01004010 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004011 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004012 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004013
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004014 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4015 case AF_INET:
4016 case AF_INET6:
4017 port = get_host_port(&es->src);
4018 break;
4019 default:
4020 port = 0;
4021 }
4022
Willy Tarreau295a8372011-03-10 11:25:07 +01004023 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004024 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004025 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004026 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004027 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004028 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004029 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4030 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004031 break;
4032 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004033 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004034 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004035 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004036 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004037 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004038 break;
4039 }
4040
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004041 chunk_printf(&msg,
4042 ", server %s (#%d), event #%u\n"
4043 " src %s:%d, session #%d, session flags 0x%08x\n"
4044 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4045 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4046 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4047 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4048 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4049 es->ev_id,
4050 pn, port, es->sid, es->s_flags,
4051 es->state, es->m_flags, es->t_flags,
4052 es->m_clen, es->m_blen,
4053 es->b_flags, es->b_out, es->b_tot,
4054 es->len, es->b_wrap, es->pos);
4055
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004056 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004057 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004058 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004059 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004060 si->applet.ctx.errors.ptr = 0;
4061 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004062 }
4063
Willy Tarreau295a8372011-03-10 11:25:07 +01004064 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004065 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004066 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004067 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004068 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004069 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004070 goto next;
4071 }
4072
4073 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004074 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004075 int newptr;
4076 int newline;
4077
Willy Tarreau295a8372011-03-10 11:25:07 +01004078 newline = si->applet.ctx.errors.bol;
4079 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
4080 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004081 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004082
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004083 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004084 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004085 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004086 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004087 si->applet.ctx.errors.ptr = newptr;
4088 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004089 };
4090 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004091 si->applet.ctx.errors.bol = 0;
4092 si->applet.ctx.errors.ptr = -1;
4093 si->applet.ctx.errors.buf++;
4094 if (si->applet.ctx.errors.buf > 1) {
4095 si->applet.ctx.errors.buf = 0;
4096 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004097 }
4098 }
4099
4100 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004101 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004102}
4103
Willy Tarreaud5781202012-09-22 19:32:35 +02004104/* parse the "level" argument on the bind lines */
4105static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4106{
4107 if (!*args[cur_arg + 1]) {
4108 memprintf(err, "'%s' : missing level", args[cur_arg]);
4109 return ERR_ALERT | ERR_FATAL;
4110 }
4111
4112 if (!strcmp(args[cur_arg+1], "user"))
4113 conf->level = ACCESS_LVL_USER;
4114 else if (!strcmp(args[cur_arg+1], "operator"))
4115 conf->level = ACCESS_LVL_OPER;
4116 else if (!strcmp(args[cur_arg+1], "admin"))
4117 conf->level = ACCESS_LVL_ADMIN;
4118 else {
4119 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4120 args[cur_arg], args[cur_arg+1]);
4121 return ERR_ALERT | ERR_FATAL;
4122 }
4123
4124 return 0;
4125}
4126
Willy Tarreaub24281b2011-02-13 13:16:36 +01004127struct si_applet http_stats_applet = {
4128 .name = "<STATS>", /* used for logging */
4129 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004130 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004131};
4132
Simon Horman9bd2c732011-06-15 15:18:44 +09004133static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004134 .name = "<CLI>", /* used for logging */
4135 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004136 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004137};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004138
Willy Tarreau10522fd2008-07-09 20:12:41 +02004139static struct cfg_kw_list cfg_kws = {{ },{
4140 { CFG_GLOBAL, "stats", stats_parse_global },
4141 { 0, NULL, NULL },
4142}};
4143
Willy Tarreaud5781202012-09-22 19:32:35 +02004144static struct bind_kw_list bind_kws = { "STAT", { }, {
4145 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4146 { NULL, NULL, 0 },
4147}};
4148
Willy Tarreau10522fd2008-07-09 20:12:41 +02004149__attribute__((constructor))
4150static void __dumpstats_module_init(void)
4151{
4152 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004153 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004154}
4155
Willy Tarreau91861262007-10-17 17:06:05 +02004156/*
4157 * Local variables:
4158 * c-indent-level: 8
4159 * c-basic-offset: 8
4160 * End:
4161 */