blob: b78ce4464155ad74a86cc42752e68680519701db [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020050#include <proto/listener.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010051#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020056#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010057#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Willy Tarreau7a0169a2012-11-19 17:13:16 +010060#ifdef USE_OPENSSL
61#include <proto/ssl_sock.h>
62#endif
63
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010064static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010065static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090066static int stats_dump_sess_to_buffer(struct stream_interface *si);
67static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010068static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010069static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
70static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Simon Horman9bd2c732011-06-15 15:18:44 +090071
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010072/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010073 * cli_io_handler()
74 * -> stats_dump_sess_to_buffer() // "show sess"
75 * -> stats_dump_errors_to_buffer() // "show errors"
76 * -> stats_dump_info_to_buffer() // "show info"
77 * -> stats_dump_stat_to_buffer() // "show stat"
78 * -> stats_dump_csv_header()
79 * -> stats_dump_proxy_to_buffer()
80 * -> stats_dump_fe_stats()
81 * -> stats_dump_li_stats()
82 * -> stats_dump_sv_stats()
83 * -> stats_dump_be_stats()
84 *
85 * http_stats_io_handler()
86 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
87 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
88 * -> stats_dump_html_head() // emits the HTML headers
89 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
90 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
91 * -> stats_dump_html_px_hdr()
92 * -> stats_dump_fe_stats()
93 * -> stats_dump_li_stats()
94 * -> stats_dump_sv_stats()
95 * -> stats_dump_be_stats()
96 * -> stats_dump_html_px_end()
97 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +010098 */
99
Simon Horman9bd2c732011-06-15 15:18:44 +0900100static struct si_applet cli_applet;
101
102static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200103 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200104 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200105 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200106 " help : this message\n"
107 " prompt : toggle interactive mode with prompt\n"
108 " quit : disconnect\n"
109 " show info : report information about the running process\n"
110 " show stat : report counters for each proxy and server\n"
111 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100112 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200113 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200114 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200115 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200116 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100117 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200118 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200119 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200120 " disable : put a server or frontend in maintenance mode\n"
121 " enable : re-enable a server or frontend which is in maintenance mode\n"
122 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200123 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200124
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200126 "Permission denied\n"
127 "";
128
Willy Tarreau295a8372011-03-10 11:25:07 +0100129/* data transmission states for the stats responses */
130enum {
131 STAT_ST_INIT = 0,
132 STAT_ST_HEAD,
133 STAT_ST_INFO,
134 STAT_ST_LIST,
135 STAT_ST_END,
136 STAT_ST_FIN,
137};
138
139/* data transmission states for the stats responses inside a proxy */
140enum {
141 STAT_PX_ST_INIT = 0,
142 STAT_PX_ST_TH,
143 STAT_PX_ST_FE,
144 STAT_PX_ST_LI,
145 STAT_PX_ST_SV,
146 STAT_PX_ST_BE,
147 STAT_PX_ST_END,
148 STAT_PX_ST_FIN,
149};
150
Cyril Bonté19979e12012-04-04 12:57:21 +0200151extern const char *stat_status_codes[];
152
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200153/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200154 * a new stats socket. It returns a positive value upon success, 0 if the connection
155 * needs to be closed and ignored, or a negative value upon critical failure.
156 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900157static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200158{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200159 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100160 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100161 s->target = s->si[1].conn->target; // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100162 s->si[1].applet.st1 = 0;
163 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200164
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200165 tv_zero(&s->logs.tv_request);
166 s->logs.t_queue = 0;
167 s->logs.t_connect = 0;
168 s->logs.t_data = 0;
169 s->logs.t_close = 0;
170 s->logs.bytes_in = s->logs.bytes_out = 0;
171 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
172 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200173
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200174 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200175
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200176 if (s->listener->timeout) {
177 s->req->rto = *s->listener->timeout;
178 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200181}
182
Willy Tarreau07e9e642010-08-17 21:48:17 +0200183/* allocate a new stats frontend named <name>, and return it
184 * (or NULL in case of lack of memory).
185 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200186static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200187{
188 struct proxy *fe;
189
190 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
191 if (!fe)
192 return NULL;
193
Willy Tarreau237250c2011-07-29 01:49:03 +0200194 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200195 fe->next = proxy;
196 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200197 fe->last_change = now.tv_sec;
198 fe->id = strdup("GLOBAL");
199 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200200 fe->maxconn = 10; /* default to 10 concurrent connections */
201 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200202 fe->conf.file = strdup(file);
203 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200204 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200205
206 /* the stats frontend is the only one able to assign ID #0 */
207 fe->conf.id.key = fe->uuid = 0;
208 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200209 return fe;
210}
211
Willy Tarreaufbee7132007-10-18 13:53:22 +0200212/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200213 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
214 * error message into the <err> buffer which will be preallocated. The trailing
215 * '\n' must not be written. The function must be called with <args> pointing to
216 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200217 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200218static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200219 struct proxy *defpx, const char *file, int line,
220 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200221{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200222 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200223 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200224
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200225 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200226 int cur_arg;
227
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200228 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200229 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 +0200230 return -1;
231 }
232
Willy Tarreau89a63132009-08-16 17:41:45 +0200233 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200234 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200235 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200236 return -1;
237 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200238 }
239
Willy Tarreau4348fad2012-09-20 16:48:07 +0200240 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200241 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200242
Willy Tarreauc53d4222012-09-20 20:19:28 +0200243 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
244 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
245 file, line, args[0], args[1], err && *err ? *err : "error");
246 return -1;
247 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200248
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200249 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200250 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200251 static int bind_dumped;
252 struct bind_kw *kw;
253
254 kw = bind_find_kw(args[cur_arg]);
255 if (kw) {
256 if (!kw->parse) {
257 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
258 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200259 return -1;
260 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200261
262 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
263 if (err && *err)
264 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
265 else
266 memprintf(err, "'%s %s' : error encountered while processing '%s'",
267 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200268 return -1;
269 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200270
271 cur_arg += 1 + kw->skip;
272 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200273 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200274
275 if (!bind_dumped) {
276 bind_dump_kws(err);
277 indent_msg(err, 4);
278 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200280
281 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
282 args[0], args[1], args[cur_arg],
283 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
284 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200285 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100286
Willy Tarreauc53d4222012-09-20 20:19:28 +0200287 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
288 l->maxconn = global.stats_fe->maxconn;
289 l->backlog = global.stats_fe->backlog;
290 l->timeout = &global.stats_fe->timeout.client;
291 l->accept = session_accept;
292 l->handler = process_session;
293 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
294 l->nice = -64; /* we want to boost priority for local stats */
295 global.maxsock += l->maxconn;
296 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200298 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100299 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100301
302 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100304 return -1;
305 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200306
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100307 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200308 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 return -1;
310 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200311 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200312 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200313 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200314 return -1;
315 }
316 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200317 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200319 else if (!strcmp(args[1], "maxconn")) {
320 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321
322 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200323 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 return -1;
325 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200326
327 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200328 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200329 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200330 return -1;
331 }
332 }
333 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200334 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200335 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
336 int cur_arg = 2;
337 unsigned int set = 0;
338
Willy Tarreau91319572013-04-20 09:48:50 +0200339 if (!global.stats_fe) {
340 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
341 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
342 return -1;
343 }
344 }
345
Willy Tarreau35b7b162012-10-22 23:17:18 +0200346 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100347 unsigned int low, high;
348
Willy Tarreau35b7b162012-10-22 23:17:18 +0200349 if (strcmp(args[cur_arg], "all") == 0) {
350 set = 0;
351 break;
352 }
353 else if (strcmp(args[cur_arg], "odd") == 0) {
354 set |= 0x55555555;
355 }
356 else if (strcmp(args[cur_arg], "even") == 0) {
357 set |= 0xAAAAAAAA;
358 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100359 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100360 char *dash = strchr(args[cur_arg], '-');
361
362 low = high = str2uic(args[cur_arg]);
363 if (dash)
364 high = str2uic(dash + 1);
365
366 if (high < low) {
367 unsigned int swap = low;
368 low = high;
369 high = swap;
370 }
371
372 if (low < 1 || high > 32) {
373 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
374 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200375 return -1;
376 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100377
378 while (low <= high)
379 set |= 1 << (low++ - 1);
380 }
381 else {
382 memprintf(err,
383 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
384 args[0], args[1]);
385 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200386 }
387 cur_arg++;
388 }
389 global.stats_fe->bind_proc = set;
390 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200391 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200392 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200393 return -1;
394 }
395 return 0;
396}
397
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100398/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
399 * for clearing it if needed.
400 */
401static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100402{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100403 chunk_appendf(&trash,
404 "# pxname,svname,"
405 "qcur,qmax,"
406 "scur,smax,slim,stot,"
407 "bin,bout,"
408 "dreq,dresp,"
409 "ereq,econ,eresp,"
410 "wretr,wredis,"
411 "status,weight,act,bck,"
412 "chkfail,chkdown,lastchg,downtime,qlimit,"
413 "pid,iid,sid,throttle,lbtot,tracked,type,"
414 "rate,rate_lim,rate_max,"
415 "check_status,check_code,check_duration,"
416 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
417 "req_rate,req_rate_max,req_tot,"
418 "cli_abrt,srv_abrt,"
419 "comp_in,comp_out,comp_byp,comp_rsp,"
420 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100421}
422
Simon Hormand9366582011-06-15 15:18:45 +0900423/* print a string of text buffer to <out>. The format is :
424 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
425 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
426 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
427 */
428static int dump_text(struct chunk *out, const char *buf, int bsize)
429{
430 unsigned char c;
431 int ptr = 0;
432
433 while (buf[ptr] && ptr < bsize) {
434 c = buf[ptr];
435 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
436 if (out->len > out->size - 1)
437 break;
438 out->str[out->len++] = c;
439 }
440 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
441 if (out->len > out->size - 2)
442 break;
443 out->str[out->len++] = '\\';
444 switch (c) {
445 case ' ': c = ' '; break;
446 case '\t': c = 't'; break;
447 case '\n': c = 'n'; break;
448 case '\r': c = 'r'; break;
449 case '\e': c = 'e'; break;
450 case '\\': c = '\\'; break;
451 }
452 out->str[out->len++] = c;
453 }
454 else {
455 if (out->len > out->size - 4)
456 break;
457 out->str[out->len++] = '\\';
458 out->str[out->len++] = 'x';
459 out->str[out->len++] = hextab[(c >> 4) & 0xF];
460 out->str[out->len++] = hextab[c & 0xF];
461 }
462 ptr++;
463 }
464
465 return ptr;
466}
467
468/* print a buffer in hexa.
469 * Print stopped if <bsize> is reached, or if no more place in the chunk.
470 */
471static int dump_binary(struct chunk *out, const char *buf, int bsize)
472{
473 unsigned char c;
474 int ptr = 0;
475
476 while (ptr < bsize) {
477 c = buf[ptr];
478
479 if (out->len > out->size - 2)
480 break;
481 out->str[out->len++] = hextab[(c >> 4) & 0xF];
482 out->str[out->len++] = hextab[c & 0xF];
483
484 ptr++;
485 }
486 return ptr;
487}
488
489/* Dump the status of a table to a stream interface's
490 * read buffer. It returns 0 if the output buffer is full
491 * and needs to be called again, otherwise non-zero.
492 */
493static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
494 struct proxy *proxy, struct proxy *target)
495{
Willy Tarreau306f8302013-07-08 15:53:06 +0200496 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900497
Willy Tarreau77804732012-10-29 16:14:26 +0100498 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900499 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
500
501 /* any other information should be dumped here */
502
Willy Tarreau290e63a2012-09-20 18:07:14 +0200503 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100504 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900505
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200506 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900507 return 0;
508
509 return 1;
510}
511
512/* Dump the a table entry to a stream interface's
513 * read buffer. It returns 0 if the output buffer is full
514 * and needs to be called again, otherwise non-zero.
515 */
516static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
517 struct proxy *proxy, struct stksess *entry)
518{
519 int dt;
520
Willy Tarreau77804732012-10-29 16:14:26 +0100521 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900522
523 if (proxy->table.type == STKTABLE_TYPE_IP) {
524 char addr[INET_ADDRSTRLEN];
525 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100526 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900527 }
528 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
529 char addr[INET6_ADDRSTRLEN];
530 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100531 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900532 }
533 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100534 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900535 }
536 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900538 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
539 }
540 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100541 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900542 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
543 }
544
Willy Tarreau77804732012-10-29 16:14:26 +0100545 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900546
547 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
548 void *ptr;
549
550 if (proxy->table.data_ofs[dt] == 0)
551 continue;
552 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100553 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900554 else
Willy Tarreau77804732012-10-29 16:14:26 +0100555 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900556
557 ptr = stktable_data_ptr(&proxy->table, entry, dt);
558 switch (stktable_data_types[dt].std_type) {
559 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900561 break;
562 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100563 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900564 break;
565 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900567 break;
568 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100569 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900570 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
571 proxy->table.data_arg[dt].u));
572 break;
573 }
574 }
Willy Tarreau77804732012-10-29 16:14:26 +0100575 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900576
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200577 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900578 return 0;
579
580 return 1;
581}
582
Willy Tarreaudec98142012-06-06 23:37:08 +0200583static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900584{
Willy Tarreau306f8302013-07-08 15:53:06 +0200585 struct session *s = session_from_task(si->owner);
Simon Hormand5b9fd92011-06-15 15:18:48 +0900586 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900587 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900588 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900589 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200590 long long value;
591 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200592 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200593 void *ptr;
594 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900595
Simon Hormand5b9fd92011-06-15 15:18:48 +0900596 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900597
598 if (!*args[4]) {
599 si->applet.ctx.cli.msg = "Key value expected\n";
600 si->applet.st0 = STAT_CLI_PRINT;
601 return;
602 }
603
Simon Hormanc5b89f62011-06-15 15:18:50 +0900604 switch (px->table.type) {
605 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900606 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100607 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900608 break;
609 case STKTABLE_TYPE_IPV6:
610 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100611 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900612 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900613 case STKTABLE_TYPE_INTEGER:
614 {
615 char *endptr;
616 unsigned long val;
617 errno = 0;
618 val = strtoul(args[4], &endptr, 10);
619 if ((errno == ERANGE && val == ULONG_MAX) ||
620 (errno != 0 && val == 0) || endptr == args[4] ||
621 val > 0xffffffff) {
622 si->applet.ctx.cli.msg = "Invalid key\n";
623 si->applet.st0 = STAT_CLI_PRINT;
624 return;
625 }
626 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100627 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900628 break;
629 }
630 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900631 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100632 static_table_key->key = args[4];
633 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900634 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900635 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200636 switch (action) {
637 case STAT_CLI_O_TAB:
638 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
639 break;
640 case STAT_CLI_O_CLR:
641 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
642 break;
643 default:
644 si->applet.ctx.cli.msg = "Unknown action\n";
645 break;
646 }
Simon Horman121f3052011-06-15 15:18:46 +0900647 si->applet.st0 = STAT_CLI_PRINT;
648 return;
649 }
650
651 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200652 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Simon Horman121f3052011-06-15 15:18:46 +0900653 si->applet.ctx.cli.msg = stats_permission_denied_msg;
654 si->applet.st0 = STAT_CLI_PRINT;
655 return;
656 }
657
Willy Tarreau07115412012-10-29 21:56:59 +0100658 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900659
Willy Tarreaudec98142012-06-06 23:37:08 +0200660 switch (action) {
661 case STAT_CLI_O_TAB:
662 if (!ts)
663 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100664 chunk_reset(&trash);
665 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900666 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100667 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900668 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200669
670 case STAT_CLI_O_CLR:
671 if (!ts)
672 return;
673 if (ts->ref_cnt) {
674 /* don't delete an entry which is currently referenced */
675 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
676 si->applet.st0 = STAT_CLI_PRINT;
677 return;
678 }
679 stksess_kill(&px->table, ts);
680 break;
Simon Horman17bce342011-06-15 15:18:47 +0900681
Willy Tarreau654694e2012-06-07 01:03:16 +0200682 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200683 if (ts)
684 stktable_touch(&px->table, ts, 1);
685 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100686 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200687 if (!ts) {
688 /* don't delete an entry which is currently referenced */
689 si->applet.ctx.cli.msg = "Unable to allocate a new entry\n";
690 si->applet.st0 = STAT_CLI_PRINT;
691 return;
692 }
693 stktable_store(&px->table, ts, 1);
694 }
695
Willy Tarreau47060b62013-08-01 21:11:42 +0200696 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
697 if (strncmp(args[cur_arg], "data.", 5) != 0) {
698 si->applet.ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
699 si->applet.st0 = STAT_CLI_PRINT;
700 return;
701 }
702
703 data_type = stktable_get_data_type(args[cur_arg] + 5);
704 if (data_type < 0) {
705 si->applet.ctx.cli.msg = "Unknown data type\n";
706 si->applet.st0 = STAT_CLI_PRINT;
707 return;
708 }
709
710 if (!px->table.data_ofs[data_type]) {
711 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
712 si->applet.st0 = STAT_CLI_PRINT;
713 return;
714 }
715
716 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
717 si->applet.ctx.cli.msg = "Require a valid integer value to store\n";
718 si->applet.st0 = STAT_CLI_PRINT;
719 return;
720 }
721
722 ptr = stktable_data_ptr(&px->table, ts, data_type);
723
724 switch (stktable_data_types[data_type].std_type) {
725 case STD_T_SINT:
726 stktable_data_cast(ptr, std_t_sint) = value;
727 break;
728 case STD_T_UINT:
729 stktable_data_cast(ptr, std_t_uint) = value;
730 break;
731 case STD_T_ULL:
732 stktable_data_cast(ptr, std_t_ull) = value;
733 break;
734 case STD_T_FRQP:
735 /* We set both the current and previous values. That way
736 * the reported frequency is stable during all the period
737 * then slowly fades out. This allows external tools to
738 * push measures without having to update them too often.
739 */
740 frqp = &stktable_data_cast(ptr, std_t_frqp);
741 frqp->curr_tick = now_ms;
742 frqp->prev_ctr = 0;
743 frqp->curr_ctr = value;
744 break;
745 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200746 }
747 break;
748
Willy Tarreaudec98142012-06-06 23:37:08 +0200749 default:
750 si->applet.ctx.cli.msg = "Unknown action\n";
Simon Horman121f3052011-06-15 15:18:46 +0900751 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200752 break;
Simon Horman121f3052011-06-15 15:18:46 +0900753 }
Simon Horman121f3052011-06-15 15:18:46 +0900754}
755
Willy Tarreau654694e2012-06-07 01:03:16 +0200756static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900757{
Willy Tarreau04b3a192013-04-13 09:41:37 +0200758 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
759 si->applet.ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau654694e2012-06-07 01:03:16 +0200760 si->applet.st0 = STAT_CLI_PRINT;
761 return;
762 }
763
Simon Hormand5b9fd92011-06-15 15:18:48 +0900764 /* condition on stored data value */
765 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
766 if (si->applet.ctx.table.data_type < 0) {
767 si->applet.ctx.cli.msg = "Unknown data type\n";
768 si->applet.st0 = STAT_CLI_PRINT;
769 return;
770 }
771
772 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
773 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
774 si->applet.st0 = STAT_CLI_PRINT;
775 return;
776 }
777
778 si->applet.ctx.table.data_op = get_std_op(args[4]);
779 if (si->applet.ctx.table.data_op < 0) {
780 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
781 si->applet.st0 = STAT_CLI_PRINT;
782 return;
783 }
784
785 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
786 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
787 si->applet.st0 = STAT_CLI_PRINT;
788 return;
789 }
790}
791
Willy Tarreaudec98142012-06-06 23:37:08 +0200792static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900793{
794 si->applet.ctx.table.data_type = -1;
Willy Tarreaua94d2d72013-07-08 15:38:15 +0200795 si->applet.st2 = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200796 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900797 si->applet.ctx.table.proxy = NULL;
798 si->applet.ctx.table.entry = NULL;
Willy Tarreaudec98142012-06-06 23:37:08 +0200799 si->applet.st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900800
801 if (*args[2]) {
802 si->applet.ctx.table.target = find_stktable(args[2]);
803 if (!si->applet.ctx.table.target) {
804 si->applet.ctx.cli.msg = "No such table\n";
805 si->applet.st0 = STAT_CLI_PRINT;
806 return;
807 }
808 }
809 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200810 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811 goto err_args;
812 return;
813 }
814
815 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200816 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900817 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200818 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900819 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820 goto err_args;
821
822 return;
823
824err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200825 switch (action) {
826 case STAT_CLI_O_TAB:
Simon Hormand5b9fd92011-06-15 15:18:48 +0900827 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 +0200828 break;
829 case STAT_CLI_O_CLR:
Simon Hormanc88b8872011-06-15 15:18:49 +0900830 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 +0200831 break;
832 default:
833 si->applet.ctx.cli.msg = "Unknown action\n";
834 break;
835 }
Simon Hormand5b9fd92011-06-15 15:18:48 +0900836 si->applet.st0 = STAT_CLI_PRINT;
837}
838
Willy Tarreau532a4502011-09-07 22:37:44 +0200839/* Expects to find a frontend named <arg> and returns it, otherwise displays various
840 * adequate error messages and returns NULL. This function also expects the session
841 * level to be admin.
842 */
843static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
844{
845 struct proxy *px;
846
Willy Tarreau290e63a2012-09-20 18:07:14 +0200847 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau532a4502011-09-07 22:37:44 +0200848 si->applet.ctx.cli.msg = stats_permission_denied_msg;
849 si->applet.st0 = STAT_CLI_PRINT;
850 return NULL;
851 }
852
853 if (!*arg) {
854 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
855 si->applet.st0 = STAT_CLI_PRINT;
856 return NULL;
857 }
858
859 px = findproxy(arg, PR_CAP_FE);
860 if (!px) {
861 si->applet.ctx.cli.msg = "No such frontend.\n";
862 si->applet.st0 = STAT_CLI_PRINT;
863 return NULL;
864 }
865 return px;
866}
867
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200868/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
869 * and returns the pointer to the server. Otherwise, display adequate error messages
870 * and returns NULL. This function also expects the session level to be admin. Note:
871 * the <arg> is modified to remove the '/'.
872 */
873static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
874{
875 struct proxy *px;
876 struct server *sv;
877 char *line;
878
Willy Tarreau290e63a2012-09-20 18:07:14 +0200879 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200880 si->applet.ctx.cli.msg = stats_permission_denied_msg;
881 si->applet.st0 = STAT_CLI_PRINT;
882 return NULL;
883 }
884
885 /* split "backend/server" and make <line> point to server */
886 for (line = arg; *line; line++)
887 if (*line == '/') {
888 *line++ = '\0';
889 break;
890 }
891
892 if (!*line || !*arg) {
893 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
894 si->applet.st0 = STAT_CLI_PRINT;
895 return NULL;
896 }
897
898 if (!get_backend_server(arg, line, &px, &sv)) {
899 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
900 si->applet.st0 = STAT_CLI_PRINT;
901 return NULL;
902 }
903
904 if (px->state == PR_STSTOPPED) {
905 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
906 si->applet.st0 = STAT_CLI_PRINT;
907 return NULL;
908 }
909
910 return sv;
911}
912
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200913/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200914 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100915 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200916 * designating the function which will have to process the request, which can
917 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200918 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900919static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200920{
Willy Tarreau306f8302013-07-08 15:53:06 +0200921 struct session *s = session_from_task(si->owner);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200922 char *args[MAX_STATS_ARGS + 1];
923 int arg;
924
925 while (isspace((unsigned char)*line))
926 line++;
927
928 arg = 0;
929 args[arg] = line;
930
931 while (*line && arg < MAX_STATS_ARGS) {
932 if (isspace((unsigned char)*line)) {
933 *line++ = '\0';
934
935 while (isspace((unsigned char)*line))
936 line++;
937
938 args[++arg] = line;
939 continue;
940 }
941
942 line++;
943 }
944
945 while (++arg <= MAX_STATS_ARGS)
946 args[arg] = line;
947
Willy Tarreau295a8372011-03-10 11:25:07 +0100948 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200949 if (strcmp(args[0], "show") == 0) {
950 if (strcmp(args[1], "stat") == 0) {
951 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100952 si->applet.ctx.stats.flags |= STAT_BOUND;
953 si->applet.ctx.stats.iid = atoi(args[2]);
954 si->applet.ctx.stats.type = atoi(args[3]);
955 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200956 }
957
Willy Tarreaua94d2d72013-07-08 15:38:15 +0200958 si->applet.st2 = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100959 si->applet.st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200960 }
961 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaua94d2d72013-07-08 15:38:15 +0200962 si->applet.st2 = STAT_ST_INIT;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100963 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200964 }
965 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreaua94d2d72013-07-08 15:38:15 +0200966 si->applet.st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +0200967 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100968 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100969 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200970 return 1;
971 }
Willy Tarreau76153662012-11-26 01:16:39 +0100972 if (*args[2] && strcmp(args[2], "all") == 0)
973 si->applet.ctx.sess.target = (void *)-1;
974 else if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100975 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100976 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100977 si->applet.ctx.sess.target = NULL;
978 si->applet.ctx.sess.section = 0; /* start with session status */
979 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100980 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200981 }
982 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +0200983 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100984 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100985 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200986 return 1;
987 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200988 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100989 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200990 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100991 si->applet.ctx.errors.iid = -1;
992 si->applet.ctx.errors.px = NULL;
Willy Tarreaua94d2d72013-07-08 15:38:15 +0200993 si->applet.st2 = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100994 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200995 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200996 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +0200997 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200998 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700999 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001000 return 0;
1001 }
1002 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001003 else if (strcmp(args[0], "clear") == 0) {
1004 if (strcmp(args[1], "counters") == 0) {
1005 struct proxy *px;
1006 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001007 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001008 int clrall = 0;
1009
1010 if (strcmp(args[2], "all") == 0)
1011 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001012
Willy Tarreau6162db22009-10-10 17:13:00 +02001013 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001014 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1015 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001016 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001017 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001018 return 1;
1019 }
1020
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001021 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001022 if (clrall) {
1023 memset(&px->be_counters, 0, sizeof(px->be_counters));
1024 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1025 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001026 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001027 px->be_counters.conn_max = 0;
1028 px->be_counters.p.http.rps_max = 0;
1029 px->be_counters.sps_max = 0;
1030 px->be_counters.cps_max = 0;
1031 px->be_counters.nbpend_max = 0;
1032
1033 px->fe_counters.conn_max = 0;
1034 px->fe_counters.p.http.rps_max = 0;
1035 px->fe_counters.sps_max = 0;
1036 px->fe_counters.cps_max = 0;
1037 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001038 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001039
1040 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001041 if (clrall)
1042 memset(&sv->counters, 0, sizeof(sv->counters));
1043 else {
1044 sv->counters.cur_sess_max = 0;
1045 sv->counters.nbpend_max = 0;
1046 sv->counters.sps_max = 0;
1047 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001048
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001049 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001050 if (li->counters) {
1051 if (clrall)
1052 memset(li->counters, 0, sizeof(*li->counters));
1053 else
1054 li->counters->conn_max = 0;
1055 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001056 }
1057
Willy Tarreau81c25d02011-09-07 15:17:21 +02001058 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001059 return 1;
1060 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001061 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001062 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001063 /* end of processing */
1064 return 1;
1065 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001066 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001067 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001068 return 0;
1069 }
1070 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001071 else if (strcmp(args[0], "get") == 0) {
1072 if (strcmp(args[1], "weight") == 0) {
1073 struct proxy *px;
1074 struct server *sv;
1075
1076 /* split "backend/server" and make <line> point to server */
1077 for (line = args[2]; *line; line++)
1078 if (*line == '/') {
1079 *line++ = '\0';
1080 break;
1081 }
1082
1083 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001084 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001085 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001086 return 1;
1087 }
1088
1089 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001090 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001091 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001092 return 1;
1093 }
1094
1095 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001096 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1097 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001098 return 1;
1099 }
1100 else { /* not "get weight" */
1101 return 0;
1102 }
1103 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001104 else if (strcmp(args[0], "set") == 0) {
1105 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001106 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001107 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001108
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001109 sv = expect_server_admin(s, si, args[2]);
1110 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001111 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001112
Simon Horman7d09b9a2013-02-12 10:45:51 +09001113 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001114 /*
1115 * The user-weight may now be zero and thus
1116 * the server considered to be draining.
1117 * Update the server's drain state as necessary.
1118 */
1119 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001120 if (warning) {
1121 si->applet.ctx.cli.msg = warning;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001122 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001123 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001124 return 1;
1125 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001126 else if (strcmp(args[1], "timeout") == 0) {
1127 if (strcmp(args[2], "cli") == 0) {
1128 unsigned timeout;
1129 const char *res;
1130
1131 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001132 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001133 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001134 return 1;
1135 }
1136
1137 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1138 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001139 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001140 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001141 return 1;
1142 }
1143
1144 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1145 return 1;
1146 }
1147 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001148 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001149 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001150 return 1;
1151 }
1152 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001153 else if (strcmp(args[1], "maxconn") == 0) {
1154 if (strcmp(args[2], "frontend") == 0) {
1155 struct proxy *px;
1156 struct listener *l;
1157 int v;
1158
Willy Tarreau532a4502011-09-07 22:37:44 +02001159 px = expect_frontend_admin(s, si, args[3]);
1160 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001161 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001162
1163 if (!*args[4]) {
1164 si->applet.ctx.cli.msg = "Integer value expected.\n";
1165 si->applet.st0 = STAT_CLI_PRINT;
1166 return 1;
1167 }
1168
1169 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001170 if (v < 0) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001171 si->applet.ctx.cli.msg = "Value out of range.\n";
1172 si->applet.st0 = STAT_CLI_PRINT;
1173 return 1;
1174 }
1175
1176 /* OK, the value is fine, so we assign it to the proxy and to all of
1177 * its listeners. The blocked ones will be dequeued.
1178 */
1179 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001180 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001181 l->maxconn = v;
1182 if (l->state == LI_FULL)
1183 resume_listener(l);
1184 }
1185
1186 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1187 dequeue_all_listeners(&s->fe->listener_queue);
1188
1189 return 1;
1190 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001191 else if (strcmp(args[2], "global") == 0) {
1192 int v;
1193
Willy Tarreau290e63a2012-09-20 18:07:14 +02001194 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau91886b62011-09-07 14:38:31 +02001195 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1196 si->applet.st0 = STAT_CLI_PRINT;
1197 return 1;
1198 }
1199
1200 if (!*args[3]) {
1201 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1202 si->applet.st0 = STAT_CLI_PRINT;
1203 return 1;
1204 }
1205
1206 v = atoi(args[3]);
1207 if (v > global.hardmaxconn) {
1208 si->applet.ctx.cli.msg = "Value out of range.\n";
1209 si->applet.st0 = STAT_CLI_PRINT;
1210 return 1;
1211 }
1212
1213 /* check for unlimited values */
1214 if (v <= 0)
1215 v = global.hardmaxconn;
1216
1217 global.maxconn = v;
1218
1219 /* Dequeues all of the listeners waiting for a resource */
1220 if (!LIST_ISEMPTY(&global_listener_queue))
1221 dequeue_all_listeners(&global_listener_queue);
1222
1223 return 1;
1224 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001225 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001226 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001227 si->applet.st0 = STAT_CLI_PRINT;
1228 return 1;
1229 }
1230 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001231 else if (strcmp(args[1], "rate-limit") == 0) {
1232 if (strcmp(args[2], "connections") == 0) {
1233 if (strcmp(args[3], "global") == 0) {
1234 int v;
1235
Willy Tarreau290e63a2012-09-20 18:07:14 +02001236 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreauf5b22872011-09-07 16:13:44 +02001237 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1238 si->applet.st0 = STAT_CLI_PRINT;
1239 return 1;
1240 }
1241
1242 if (!*args[4]) {
1243 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1244 si->applet.st0 = STAT_CLI_PRINT;
1245 return 1;
1246 }
1247
1248 v = atoi(args[4]);
1249 if (v < 0) {
1250 si->applet.ctx.cli.msg = "Value out of range.\n";
1251 si->applet.st0 = STAT_CLI_PRINT;
1252 return 1;
1253 }
1254
1255 global.cps_lim = v;
1256
1257 /* Dequeues all of the listeners waiting for a resource */
1258 if (!LIST_ISEMPTY(&global_listener_queue))
1259 dequeue_all_listeners(&global_listener_queue);
1260
1261 return 1;
1262 }
1263 else {
1264 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1265 si->applet.st0 = STAT_CLI_PRINT;
1266 return 1;
1267 }
1268 }
William Lallemandd85f9172012-11-09 17:05:39 +01001269 else if (strcmp(args[2], "http-compression") == 0) {
1270 if (strcmp(args[3], "global") == 0) {
1271 int v;
1272
Willy Tarreau85d47f92012-11-21 00:29:50 +01001273 if (!*args[4]) {
1274 si->applet.ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1275 si->applet.st0 = STAT_CLI_PRINT;
1276 return 1;
1277 }
1278
William Lallemandd85f9172012-11-09 17:05:39 +01001279 v = atoi(args[4]);
1280 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1281 }
1282 else {
1283 si->applet.ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1284 si->applet.st0 = STAT_CLI_PRINT;
1285 return 1;
1286 }
1287 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001288 else {
William Lallemandd85f9172012-11-09 17:05:39 +01001289 si->applet.ctx.cli.msg = "'set rate-limit' supports 'connections' and 'http-compression'.\n";
Willy Tarreauf5b22872011-09-07 16:13:44 +02001290 si->applet.st0 = STAT_CLI_PRINT;
1291 return 1;
1292 }
1293 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001294 else if (strcmp(args[1], "table") == 0) {
1295 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1296 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001297 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001298 return 0;
1299 }
1300 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001301 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001302 if (strcmp(args[1], "agent") == 0) {
1303 struct server *sv;
1304
1305 sv = expect_server_admin(s, si, args[2]);
1306 if (!sv)
1307 return 1;
1308
1309 sv->agent.state &= ~CHK_STATE_DISABLED;
1310
1311 return 1;
1312 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001313 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001314 struct server *sv;
1315
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001316 sv = expect_server_admin(s, si, args[2]);
1317 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001318 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001319
Cyril Bontécd19e512010-01-31 22:34:03 +01001320 if (sv->state & SRV_MAINTAIN) {
1321 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001322 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001323 /* If this server tracks the status of another one,
1324 * we must restore the good status.
1325 */
Willy Tarreau44267702011-10-28 15:35:33 +02001326 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001327 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001328 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001329 } else {
1330 sv->state &= ~SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001331 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001332 }
1333 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001334 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001335 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001336 }
1337 }
1338
Willy Tarreau532a4502011-09-07 22:37:44 +02001339 return 1;
1340 }
1341 else if (strcmp(args[1], "frontend") == 0) {
1342 struct proxy *px;
1343
1344 px = expect_frontend_admin(s, si, args[2]);
1345 if (!px)
1346 return 1;
1347
1348 if (px->state == PR_STSTOPPED) {
1349 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1350 si->applet.st0 = STAT_CLI_PRINT;
1351 return 1;
1352 }
1353
1354 if (px->state != PR_STPAUSED) {
1355 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1356 si->applet.st0 = STAT_CLI_PRINT;
1357 return 1;
1358 }
1359
1360 if (!resume_proxy(px)) {
1361 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1362 si->applet.st0 = STAT_CLI_PRINT;
1363 return 1;
1364 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001365 return 1;
1366 }
1367 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001368 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1369 si->applet.st0 = STAT_CLI_PRINT;
1370 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001371 }
1372 }
1373 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001374 if (strcmp(args[1], "agent") == 0) {
1375 struct server *sv;
1376
1377 sv = expect_server_admin(s, si, args[2]);
1378 if (!sv)
1379 return 1;
1380
1381 sv->agent.state |= CHK_STATE_DISABLED;
1382
1383 return 1;
1384 }
1385 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001386 struct server *sv;
1387
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001388 sv = expect_server_admin(s, si, args[2]);
1389 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001390 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001391
Cyril Bontécd19e512010-01-31 22:34:03 +01001392 if (! (sv->state & SRV_MAINTAIN)) {
1393 /* Not already in maintenance, we can change the server state */
1394 sv->state |= SRV_MAINTAIN;
Simon Horman4a741432013-02-23 15:35:38 +09001395 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001396 }
1397
Willy Tarreau532a4502011-09-07 22:37:44 +02001398 return 1;
1399 }
1400 else if (strcmp(args[1], "frontend") == 0) {
1401 struct proxy *px;
1402
1403 px = expect_frontend_admin(s, si, args[2]);
1404 if (!px)
1405 return 1;
1406
1407 if (px->state == PR_STSTOPPED) {
1408 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1409 si->applet.st0 = STAT_CLI_PRINT;
1410 return 1;
1411 }
1412
1413 if (px->state == PR_STPAUSED) {
1414 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1415 si->applet.st0 = STAT_CLI_PRINT;
1416 return 1;
1417 }
1418
1419 if (!pause_proxy(px)) {
1420 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1421 si->applet.st0 = STAT_CLI_PRINT;
1422 return 1;
1423 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001424 return 1;
1425 }
1426 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001427 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1428 si->applet.st0 = STAT_CLI_PRINT;
1429 return 1;
1430 }
1431 }
1432 else if (strcmp(args[0], "shutdown") == 0) {
1433 if (strcmp(args[1], "frontend") == 0) {
1434 struct proxy *px;
1435
1436 px = expect_frontend_admin(s, si, args[2]);
1437 if (!px)
1438 return 1;
1439
1440 if (px->state == PR_STSTOPPED) {
1441 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1442 si->applet.st0 = STAT_CLI_PRINT;
1443 return 1;
1444 }
1445
1446 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1447 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1448 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1449 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1450 stop_proxy(px);
1451 return 1;
1452 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001453 else if (strcmp(args[1], "session") == 0) {
1454 struct session *sess, *ptr;
1455
Willy Tarreau290e63a2012-09-20 18:07:14 +02001456 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02001457 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1458 si->applet.st0 = STAT_CLI_PRINT;
1459 return 1;
1460 }
1461
1462 if (!*args[2]) {
1463 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1464 si->applet.st0 = STAT_CLI_PRINT;
1465 return 1;
1466 }
1467
1468 ptr = (void *)strtoul(args[2], NULL, 0);
1469
1470 /* first, look for the requested session in the session table */
1471 list_for_each_entry(sess, &sessions, list) {
1472 if (sess == ptr)
1473 break;
1474 }
1475
1476 /* do we have the session ? */
1477 if (sess != ptr) {
1478 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1479 si->applet.st0 = STAT_CLI_PRINT;
1480 return 1;
1481 }
1482
1483 session_shutdown(sess, SN_ERR_KILLED);
1484 return 1;
1485 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001486 else if (strcmp(args[1], "sessions") == 0) {
1487 if (strcmp(args[2], "server") == 0) {
1488 struct server *sv;
1489 struct session *sess, *sess_bck;
1490
1491 sv = expect_server_admin(s, si, args[3]);
1492 if (!sv)
1493 return 1;
1494
1495 /* kill all the session that are on this server */
1496 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1497 if (sess->srv_conn == sv)
1498 session_shutdown(sess, SN_ERR_KILLED);
1499
1500 return 1;
1501 }
1502 else {
1503 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1504 si->applet.st0 = STAT_CLI_PRINT;
1505 return 1;
1506 }
1507 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001508 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001509 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001510 si->applet.st0 = STAT_CLI_PRINT;
1511 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001512 }
1513 }
1514 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001515 return 0;
1516 }
1517 return 1;
1518}
1519
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001520/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001521 * used to processes I/O from/to the stats unix socket. The system relies on a
1522 * state machine handling requests and various responses. We read a request,
1523 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001524 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1525 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001526 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001527 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001528static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001529{
Willy Tarreau7421efb2012-07-02 15:11:27 +02001530 struct channel *req = si->ob;
1531 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001532 int reql;
1533 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001534
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001535 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1536 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001537
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001538 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001539 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001540 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001541 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001542 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001543 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001544 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001545 /* Let's close for real now. We just close the request
1546 * side, the conditions below will complete if needed.
1547 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001548 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001549 break;
1550 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001551 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001552 /* ensure we have some output room left in the event we
1553 * would want to return some info right after parsing.
1554 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001555 if (buffer_almost_full(si->ib->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02001556 break;
1557
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001558 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001559 if (reql <= 0) { /* closed or EOL not found */
1560 if (reql == 0)
1561 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001562 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001563 continue;
1564 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001565
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001566 /* seek for a possible semi-colon. If we find one, we
1567 * replace it with an LF and skip only this part.
1568 */
1569 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001570 if (trash.str[len] == ';') {
1571 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001572 reql = len + 1;
1573 break;
1574 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001575
Willy Tarreau816fc222009-10-04 07:36:58 +02001576 /* now it is time to check that we have a full line,
1577 * remove the trailing \n and possibly \r, then cut the
1578 * line.
1579 */
1580 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001581 if (trash.str[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001582 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001583 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001584 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001586 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02001587 len--;
1588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001589 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001590
Willy Tarreaubc4af052011-02-13 13:25:14 +01001591 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001592 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001593 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001594 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001595 continue;
1596 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001597 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001598 si->applet.st1 = !si->applet.st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001599 else if (strcmp(trash.str, "help") == 0 ||
1600 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001601 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001602 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001603 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001604 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001605 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001606 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001607 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001608 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001609 /* if prompt is disabled, print help on empty lines,
1610 * so that the user at least knows how to enable
1611 * prompt and find help.
1612 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001613 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001614 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001615 }
1616
1617 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001618 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001619 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001620 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001621 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001622 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001623 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001624 continue;
1625 }
1626
Willy Tarreaubc4af052011-02-13 13:25:14 +01001627 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001628 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001629 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001630 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001631 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001632 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001633 if (stats_dump_info_to_buffer(si))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001634 si->applet.st0 = STAT_CLI_PROMPT;
1635 break;
1636 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001637 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001638 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001639 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001640 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001641 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001642 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001643 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001644 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001645 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001646 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001647 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001648 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001649 case STAT_CLI_O_CLR:
Willy Tarreaudec98142012-06-06 23:37:08 +02001650 if (stats_table_request(si, si->applet.st0))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001651 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001652 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001653 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001654 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001655 break;
1656 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001657
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001658 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001659 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001660 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001661 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001662 }
1663
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001664 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001665 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001666 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001667
1668 /* Now we close the output if one of the writers did so,
1669 * or if we're not in interactive mode and the request
1670 * buffer is empty. This still allows pipelined requests
1671 * to be sent in non-interactive mode.
1672 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001673 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!si->applet.st1 && !req->buf->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001674 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001675 continue;
1676 }
1677
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001678 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001679 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001680 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001681 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001682
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001683 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001684 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1685 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001686 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001687 * and nothing more to consume. This is comparable to a broken pipe, so
1688 * we forward the close to the request side so that it flows upstream to
1689 * the client.
1690 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001691 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001692 }
1693
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001694 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001695 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1696 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1697 /* We have no more processing to do, and nothing more to send, and
1698 * the client side has closed. So we'll forward this state downstream
1699 * on the response buffer.
1700 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001701 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001702 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001703 }
1704
1705 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001706 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001707
1708 /* we don't want to expire timeouts while we're processing requests */
1709 si->ib->rex = TICK_ETERNITY;
1710 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001711
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001712 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001713 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 +02001714 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001715 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 +02001716
1717 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1718 /* check that we have released everything then unregister */
1719 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001720 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001721}
1722
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001723/* This function dumps information onto the stream interface's read buffer.
1724 * It returns 0 as long as it does not complete, non-zero upon completion.
1725 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001726 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001727static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001728{
1729 unsigned int up = (now.tv_sec - start_date.tv_sec);
1730
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001731 chunk_printf(&trash,
1732 "Name: " PRODUCT_NAME "\n"
1733 "Version: " HAPROXY_VERSION "\n"
1734 "Release_date: " HAPROXY_DATE "\n"
1735 "Nbproc: %d\n"
1736 "Process_num: %d\n"
1737 "Pid: %d\n"
1738 "Uptime: %dd %dh%02dm%02ds\n"
1739 "Uptime_sec: %d\n"
1740 "Memmax_MB: %d\n"
1741 "Ulimit-n: %d\n"
1742 "Maxsock: %d\n"
1743 "Maxconn: %d\n"
1744 "Hard_maxconn: %d\n"
1745 "Maxpipes: %d\n"
1746 "CurrConns: %d\n"
1747 "PipesUsed: %d\n"
1748 "PipesFree: %d\n"
1749 "ConnRate: %d\n"
1750 "ConnRateLimit: %d\n"
1751 "MaxConnRate: %d\n"
1752 "CompressBpsIn: %u\n"
1753 "CompressBpsOut: %u\n"
1754 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001755#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001756 "ZlibMemUsage: %ld\n"
1757 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001758#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001759 "Tasks: %d\n"
1760 "Run_queue: %d\n"
1761 "Idle_pct: %d\n"
1762 "node: %s\n"
1763 "description: %s\n"
1764 "",
1765 global.nbproc,
1766 relative_pid,
1767 pid,
1768 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1769 up,
1770 global.rlimit_memmax,
1771 global.rlimit_nofile,
1772 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
1773 actconn, pipes_used, pipes_free,
1774 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
1775 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
1776 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001777#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001778 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001779#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01001780 nb_tasks_cur, run_queue_cur, idle_pct,
1781 global.node, global.desc ? global.desc : ""
1782 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001783
1784 if (bi_putchk(si->ib, &trash) == -1)
1785 return 0;
1786
1787 return 1;
1788}
1789
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001790/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1791 * the state from stream interface <si>. The caller is responsible for clearing
1792 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02001793 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001794static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001795{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001796 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001797
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001798 if (!(px->cap & PR_CAP_FE))
1799 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001800
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001801 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))
1802 return 0;
1803
Willy Tarreau354898b2012-12-23 18:15:23 +01001804 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001805 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001806 /* name, queue */
1807 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02001808
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001809 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
1810 /* Column sub-heading for Enable or Disable server */
1811 chunk_appendf(&trash, "<td></td>");
1812 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02001813
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001814 chunk_appendf(&trash,
1815 "<td class=ac>"
1816 "<a name=\"%s/Frontend\"></a>"
1817 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
1818 "<td colspan=3></td>"
1819 "",
1820 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001821
Willy Tarreau466c9b52012-12-23 02:25:03 +01001822 chunk_appendf(&trash,
1823 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001824 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001825 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
1826 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
1827 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001828 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
1829 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
1830 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001831
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001832 if (px->mode == PR_MODE_HTTP)
1833 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001834 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001835 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001836
1837 chunk_appendf(&trash,
1838 "</table></div></u></td>"
1839 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001840 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001841 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
1842 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
1843 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001844 U2H(px->fe_counters.sps_max),
1845 U2H(px->fe_counters.cps_max),
1846 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001847
1848 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001849 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001850 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001851 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01001852
1853 chunk_appendf(&trash,
1854 "</table></div></u></td>"
1855 /* sessions rate : limit */
1856 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001857 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001858
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001859 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001860 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001861 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001862 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01001863 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
1864 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001865 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001866 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
1867 U2H(px->fe_counters.cum_sess),
1868 U2H(px->fe_counters.cum_conn),
1869 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02001870
Willy Tarreau466c9b52012-12-23 02:25:03 +01001871 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001872 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01001873 chunk_appendf(&trash,
1874 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1875 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1876 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1877 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1878 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1879 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1880 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1881 "<tr><th>- other responses:</th><td>%s</td></tr>"
1882 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
1883 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001884 U2H(px->fe_counters.p.http.cum_req),
1885 U2H(px->fe_counters.p.http.rsp[1]),
1886 U2H(px->fe_counters.p.http.rsp[2]),
1887 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001888 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01001889 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01001890 U2H(px->fe_counters.p.http.rsp[3]),
1891 U2H(px->fe_counters.p.http.rsp[4]),
1892 U2H(px->fe_counters.p.http.rsp[5]),
1893 U2H(px->fe_counters.p.http.rsp[0]),
1894 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001895 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001896
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001897 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01001898 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001899 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01001900 "<td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001901 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001902 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001903 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001904 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001905
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001906 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001907 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02001908 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001909 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001910 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01001911 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
1912 px->fe_counters.comp_in ?
1913 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001914 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001915
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001916 chunk_appendf(&trash,
1917 /* denied: req, resp */
1918 "<td>%s</td><td>%s</td>"
1919 /* errors : request, connect, response */
1920 "<td>%s</td><td></td><td></td>"
1921 /* warnings: retries, redispatches */
1922 "<td></td><td></td>"
1923 /* server status : reflect frontend status */
1924 "<td class=ac>%s</td>"
1925 /* rest of server: nothing */
1926 "<td class=ac colspan=8></td></tr>"
1927 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01001928 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
1929 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001930 px->state == PR_STREADY ? "OPEN" :
1931 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001932 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001933 else { /* CSV mode */
1934 chunk_appendf(&trash,
1935 /* pxid, name, queue cur, queue max, */
1936 "%s,FRONTEND,,,"
1937 /* sessions : current, max, limit, total */
1938 "%d,%d,%d,%lld,"
1939 /* bytes : in, out */
1940 "%lld,%lld,"
1941 /* denied: req, resp */
1942 "%lld,%lld,"
1943 /* errors : request, connect, response */
1944 "%lld,,,"
1945 /* warnings: retries, redispatches */
1946 ",,"
1947 /* server status : reflect frontend status */
1948 "%s,"
1949 /* rest of server: nothing */
1950 ",,,,,,,,"
1951 /* pid, iid, sid, throttle, lbtot, tracked, type */
1952 "%d,%d,0,,,,%d,"
1953 /* rate, rate_lim, rate_max */
1954 "%u,%u,%u,"
1955 /* check_status, check_code, check_duration */
1956 ",,,",
1957 px->id,
1958 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
1959 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
1960 px->fe_counters.denied_req, px->fe_counters.denied_resp,
1961 px->fe_counters.failed_req,
1962 px->state == PR_STREADY ? "OPEN" :
1963 px->state == PR_STFULL ? "FULL" : "STOP",
1964 relative_pid, px->uuid, STATS_TYPE_FE,
1965 read_freq_ctr(&px->fe_sess_per_sec),
1966 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001967
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001968 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1969 if (px->mode == PR_MODE_HTTP) {
1970 for (i=1; i<6; i++)
1971 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
1972 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
1973 }
1974 else
1975 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001976
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001977 /* failed health analyses */
1978 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001979
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001980 /* requests : req_rate, req_rate_max, req_tot, */
1981 chunk_appendf(&trash, "%u,%u,%lld,",
1982 read_freq_ctr(&px->fe_req_per_sec),
1983 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
1984
1985 /* errors: cli_aborts, srv_aborts */
1986 chunk_appendf(&trash, ",,");
1987
1988 /* compression: in, out, bypassed */
1989 chunk_appendf(&trash, "%lld,%lld,%lld,",
1990 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
1991
1992 /* compression: comp_rsp */
1993 chunk_appendf(&trash, "%lld,",
1994 px->fe_counters.p.http.comp_rsp);
1995
1996 /* finish with EOL */
1997 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001998 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01001999 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002000}
2001
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002002/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2003 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2004 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2005 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002006 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002007static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002008{
Willy Tarreau354898b2012-12-23 18:15:23 +01002009 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002010 chunk_appendf(&trash, "<tr class=socket>");
2011 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2012 /* Column sub-heading for Enable or Disable server */
2013 chunk_appendf(&trash, "<td></td>");
2014 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002015 chunk_appendf(&trash,
2016 /* frontend name, listener name */
2017 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2018 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2019 "",
2020 px->id, l->name,
2021 (flags & ST_SHLGNDS)?"<u>":"",
2022 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002023
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002024 if (flags & ST_SHLGNDS) {
2025 char str[INET6_ADDRSTRLEN];
2026 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002027
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002028 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002029
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002030 port = get_host_port(&l->addr);
2031 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2032 case AF_INET:
2033 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2034 break;
2035 case AF_INET6:
2036 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2037 break;
2038 case AF_UNIX:
2039 chunk_appendf(&trash, "unix, ");
2040 break;
2041 case -1:
2042 chunk_appendf(&trash, "(%s), ", strerror(errno));
2043 break;
2044 }
Willy Tarreau91861262007-10-17 17:06:05 +02002045
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002046 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002047 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002048 }
Willy Tarreau91861262007-10-17 17:06:05 +02002049
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002050 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002051 /* queue */
2052 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002053 /* sessions rate: current, max, limit */
2054 "<td colspan=3>&nbsp;</td>"
2055 /* sessions: current, max, limit, total, lbtot */
2056 "<td>%s</td><td>%s</td><td>%s</td>"
2057 "<td>%s</td><td>&nbsp;</td>"
2058 /* bytes: in, out */
2059 "<td>%s</td><td>%s</td>"
2060 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002061 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002062 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2063 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002064
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002065 chunk_appendf(&trash,
2066 /* denied: req, resp */
2067 "<td>%s</td><td>%s</td>"
2068 /* errors: request, connect, response */
2069 "<td>%s</td><td></td><td></td>"
2070 /* warnings: retries, redispatches */
2071 "<td></td><td></td>"
2072 /* server status: reflect listener status */
2073 "<td class=ac>%s</td>"
2074 /* rest of server: nothing */
2075 "<td class=ac colspan=8></td></tr>"
2076 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002077 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2078 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002079 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2080 }
2081 else { /* CSV mode */
2082 chunk_appendf(&trash,
2083 /* pxid, name, queue cur, queue max, */
2084 "%s,%s,,,"
2085 /* sessions: current, max, limit, total */
2086 "%d,%d,%d,%lld,"
2087 /* bytes: in, out */
2088 "%lld,%lld,"
2089 /* denied: req, resp */
2090 "%lld,%lld,"
2091 /* errors: request, connect, response */
2092 "%lld,,,"
2093 /* warnings: retries, redispatches */
2094 ",,"
2095 /* server status: reflect listener status */
2096 "%s,"
2097 /* rest of server: nothing */
2098 ",,,,,,,,"
2099 /* pid, iid, sid, throttle, lbtot, tracked, type */
2100 "%d,%d,%d,,,,%d,"
2101 /* rate, rate_lim, rate_max */
2102 ",,,"
2103 /* check_status, check_code, check_duration */
2104 ",,,"
2105 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2106 ",,,,,,"
2107 /* failed health analyses */
2108 ","
2109 /* requests : req_rate, req_rate_max, req_tot, */
2110 ",,,"
2111 /* errors: cli_aborts, srv_aborts */
2112 ",,"
2113 /* compression: in, out, bypassed, comp_rsp */
2114 ",,,,"
2115 "\n",
2116 px->id, l->name,
2117 l->nbconn, l->counters->conn_max,
2118 l->maxconn, l->counters->cum_conn,
2119 l->counters->bytes_in, l->counters->bytes_out,
2120 l->counters->denied_req, l->counters->denied_resp,
2121 l->counters->failed_req,
2122 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2123 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2124 }
2125 return 1;
2126}
Willy Tarreau91861262007-10-17 17:06:05 +02002127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002128/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2129 * from stream interface <si>, stats flags <flags>, and server state <state>.
2130 * The caller is responsible for clearing the trash if needed. Returns non-zero
2131 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002132 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2133 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002134 */
2135static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2136{
2137 struct server *ref = sv->track ? sv->track : sv;
2138 char str[INET6_ADDRSTRLEN];
2139 struct chunk src;
2140 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002141
Willy Tarreau354898b2012-12-23 18:15:23 +01002142 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002143 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002144 "DOWN",
2145 "DN %d/%d &uarr;",
2146 "UP %d/%d &darr;",
2147 "UP",
2148 "NOLB %d/%d &darr;",
2149 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002150 "DRAIN %d/%d &darr;",
2151 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002152 "<i>no check</i>"
2153 };
Willy Tarreau91861262007-10-17 17:06:05 +02002154
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002155 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2156 chunk_appendf(&trash, "<tr class=\"maintain\">");
2157 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002158 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002159 "<tr class=\"%s%d\">",
2160 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002161
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002162 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002163 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002164 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2165 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002166
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002167 chunk_appendf(&trash,
2168 "<td class=ac><a name=\"%s/%s\"></a>%s"
2169 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2170 "",
2171 px->id, sv->id,
2172 (flags & ST_SHLGNDS) ? "<u>" : "",
2173 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002175 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002176 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002177
2178 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2179 case AF_INET:
2180 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2181 break;
2182 case AF_INET6:
2183 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2184 break;
2185 case AF_UNIX:
2186 chunk_appendf(&trash, "unix, ");
2187 break;
2188 case -1:
2189 chunk_appendf(&trash, "(%s), ", strerror(errno));
2190 break;
2191 default: /* address family not supported */
2192 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002193 }
Willy Tarreau91861262007-10-17 17:06:05 +02002194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002195 /* id */
2196 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002197
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002198 /* cookie */
2199 if (sv->cookie) {
2200 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002201
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002202 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2203 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002205 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002206 }
2207
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002208 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002209 }
Willy Tarreau91861262007-10-17 17:06:05 +02002210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002211 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002212 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002213 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002214 /* sessions rate : current, max, limit */
2215 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002216 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002217 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002218 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2219 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002220
Willy Tarreau466c9b52012-12-23 02:25:03 +01002221
2222 chunk_appendf(&trash,
2223 /* sessions: current, max, limit, total */
2224 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002225 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002226 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2227 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002228 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2229 U2H(sv->counters.cum_sess),
2230 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002231
Willy Tarreau466c9b52012-12-23 02:25:03 +01002232 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2233 if (px->mode == PR_MODE_HTTP) {
2234 unsigned long long tot;
2235 for (tot = i = 0; i < 6; i++)
2236 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002237
Willy Tarreau466c9b52012-12-23 02:25:03 +01002238 chunk_appendf(&trash,
2239 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2240 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2241 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2242 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2243 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2244 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2245 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2246 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002247 U2H(tot),
2248 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2249 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2250 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2251 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2252 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2253 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02002254 }
Willy Tarreau91861262007-10-17 17:06:05 +02002255
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002256 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002257 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002258 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002259 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002260 U2H(sv->counters.cum_lbconn));
Willy Tarreau91861262007-10-17 17:06:05 +02002261
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002262 chunk_appendf(&trash,
2263 /* bytes : in, out */
2264 "<td>%s</td><td>%s</td>"
2265 /* denied: req, resp */
2266 "<td></td><td>%s</td>"
2267 /* errors : request, connect */
2268 "<td></td><td>%s</td>"
2269 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002270 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002271 /* warnings: retries, redispatches */
2272 "<td>%lld</td><td>%lld</td>"
2273 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002274 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2275 U2H(sv->counters.failed_secu),
2276 U2H(sv->counters.failed_conns),
2277 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002278 sv->counters.cli_aborts,
2279 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002280 sv->counters.retries, sv->counters.redispatches);
2281
2282 /* status, lest check */
2283 chunk_appendf(&trash, "<td class=ac>");
2284
2285 if (sv->state & SRV_MAINTAIN) {
2286 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2287 chunk_appendf(&trash, "MAINT");
2288 }
2289 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2290 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2291 chunk_appendf(&trash, "MAINT(via)");
2292 }
2293 else if (ref->state & SRV_CHECKED) {
2294 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2295 chunk_appendf(&trash,
2296 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002297 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2298 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002299 }
Willy Tarreau91861262007-10-17 17:06:05 +02002300
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002301 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002302 chunk_appendf(&trash,
2303 "</td><td class=ac><u> %s%s",
Simon Hormancd5d7b62013-02-24 17:23:38 +09002304 (sv->check.state & CHK_STATE_RUNNING) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002305 get_check_status_info(sv->check.status));
2306
2307 if (sv->check.status >= HCHK_STATUS_L57DATA)
2308 chunk_appendf(&trash, "/%d", sv->check.code);
2309
2310 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002311 chunk_appendf(&trash, " in %lums", sv->check.duration);
2312
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002313 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002314 get_check_status_description(sv->check.status));
2315 if (*sv->check.desc) {
2316 chunk_appendf(&trash, ": ");
2317 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2318 chunk_htmlencode(&trash, &src);
2319 }
2320 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002321 }
2322 else
2323 chunk_appendf(&trash, "</td><td>");
2324
2325 chunk_appendf(&trash,
2326 /* weight */
2327 "</td><td class=ac>%d</td>"
2328 /* act, bck */
2329 "<td class=ac>%s</td><td class=ac>%s</td>"
2330 "",
2331 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2332 (sv->state & SRV_BACKUP) ? "-" : "Y",
2333 (sv->state & SRV_BACKUP) ? "Y" : "-");
2334
2335 /* check failures: unique, fatal, down time */
2336 if (sv->state & SRV_CHECKED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002337 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002338
2339 if (ref->observe)
2340 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2341
2342 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002343 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002344 "<td>%lld</td><td>%s</td>"
2345 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002346 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002347 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2348 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002349 else if (sv != ref) {
2350 if (sv->state & SRV_MAINTAIN)
2351 chunk_appendf(&trash,
2352 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2353 ref->proxy->id, ref->id);
2354 else
2355 chunk_appendf(&trash,
2356 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2357 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2358 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359 else
2360 chunk_appendf(&trash, "<td colspan=3></td>");
2361
2362 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002363 if (sv->state & SRV_WARMINGUP)
2364 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002365 else
2366 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2367 }
2368 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002369 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002370 "DOWN,",
2371 "DOWN %d/%d,",
2372 "UP %d/%d,",
2373 "UP,",
2374 "NOLB %d/%d,",
2375 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002376 "DRAIN %d/%d,",
2377 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002378 "no check,"
2379 };
2380
2381 chunk_appendf(&trash,
2382 /* pxid, name */
2383 "%s,%s,"
2384 /* queue : current, max */
2385 "%d,%d,"
2386 /* sessions : current, max, limit, total */
2387 "%d,%d,%s,%lld,"
2388 /* bytes : in, out */
2389 "%lld,%lld,"
2390 /* denied: req, resp */
2391 ",%lld,"
2392 /* errors : request, connect, response */
2393 ",%lld,%lld,"
2394 /* warnings: retries, redispatches */
2395 "%lld,%lld,"
2396 "",
2397 px->id, sv->id,
2398 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002399 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002400 sv->counters.bytes_in, sv->counters.bytes_out,
2401 sv->counters.failed_secu,
2402 sv->counters.failed_conns, sv->counters.failed_resp,
2403 sv->counters.retries, sv->counters.redispatches);
2404
2405 /* status */
2406 if (sv->state & SRV_MAINTAIN)
2407 chunk_appendf(&trash, "MAINT,");
2408 else if (ref != sv && ref->state & SRV_MAINTAIN)
2409 chunk_appendf(&trash, "MAINT(via),");
2410 else
2411 chunk_appendf(&trash,
2412 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002413 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2414 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002415
2416 chunk_appendf(&trash,
2417 /* weight, active, backup */
2418 "%d,%d,%d,"
2419 "",
2420 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2421 (sv->state & SRV_BACKUP) ? 0 : 1,
2422 (sv->state & SRV_BACKUP) ? 1 : 0);
2423
2424 /* check failures: unique, fatal; last change, total downtime */
2425 if (sv->state & SRV_CHECKED)
2426 chunk_appendf(&trash,
2427 "%lld,%lld,%d,%d,",
2428 sv->counters.failed_checks, sv->counters.down_trans,
2429 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2430 else
2431 chunk_appendf(&trash, ",,,,");
2432
2433 /* queue limit, pid, iid, sid, */
2434 chunk_appendf(&trash,
2435 "%s,"
2436 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002437 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002438 relative_pid, px->uuid, sv->puid);
2439
2440 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002441 if (sv->state & SRV_WARMINGUP)
2442 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002443
2444 /* sessions: lbtot */
2445 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2446
2447 /* tracked */
2448 if (sv->track)
2449 chunk_appendf(&trash, "%s/%s,",
2450 sv->track->proxy->id, sv->track->id);
2451 else
2452 chunk_appendf(&trash, ",");
2453
2454 /* type */
2455 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2456
2457 /* rate */
2458 chunk_appendf(&trash, "%u,,%u,",
2459 read_freq_ctr(&sv->sess_per_sec),
2460 sv->counters.sps_max);
2461
2462 if (sv->state & SRV_CHECKED) {
2463 /* check_status */
2464 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2465
2466 /* check_code */
2467 if (sv->check.status >= HCHK_STATUS_L57DATA)
2468 chunk_appendf(&trash, "%u,", sv->check.code);
2469 else
2470 chunk_appendf(&trash, ",");
2471
2472 /* check_duration */
2473 if (sv->check.status >= HCHK_STATUS_CHECKED)
2474 chunk_appendf(&trash, "%lu,", sv->check.duration);
2475 else
2476 chunk_appendf(&trash, ",");
2477
2478 }
2479 else
2480 chunk_appendf(&trash, ",,,");
2481
2482 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2483 if (px->mode == PR_MODE_HTTP) {
2484 for (i=1; i<6; i++)
2485 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
2486
2487 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
2488 }
2489 else
2490 chunk_appendf(&trash, ",,,,,,");
2491
2492 /* failed health analyses */
2493 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
2494
2495 /* requests : req_rate, req_rate_max, req_tot, */
2496 chunk_appendf(&trash, ",,,");
2497
2498 /* errors: cli_aborts, srv_aborts */
2499 chunk_appendf(&trash, "%lld,%lld,",
2500 sv->counters.cli_aborts, sv->counters.srv_aborts);
2501
2502 /* compression: in, out, bypassed, comp_rsp */
2503 chunk_appendf(&trash, ",,,,");
2504
2505 /* finish with EOL */
2506 chunk_appendf(&trash, "\n");
2507 }
2508 return 1;
2509}
2510
2511/* Dumps a line for backend <px> to the trash for and uses the state from stream
2512 * interface <si> and stats flags <flags>. The caller is responsible for clearing
2513 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
2514 */
2515static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
2516{
2517 struct chunk src;
2518 int i;
2519
2520 if (!(px->cap & PR_CAP_BE))
2521 return 0;
2522
2523 if ((si->applet.ctx.stats.flags & STAT_BOUND) && !(si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))
2524 return 0;
2525
Willy Tarreau354898b2012-12-23 18:15:23 +01002526 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002527 chunk_appendf(&trash, "<tr class=\"backend\">");
2528 if (px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2529 /* Column sub-heading for Enable or Disable server */
2530 chunk_appendf(&trash, "<td></td>");
2531 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002532 chunk_appendf(&trash,
2533 "<td class=ac>"
2534 /* name */
2535 "%s<a name=\"%s/Backend\"></a>"
2536 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
2537 "",
2538 (flags & ST_SHLGNDS)?"<u>":"",
2539 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002540
2541 if (flags & ST_SHLGNDS) {
2542 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002543 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002544 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
2545
2546 /* cookie */
2547 if (px->cookie_name) {
2548 chunk_appendf(&trash, ", cookie: '");
2549 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2550 chunk_htmlencode(&trash, &src);
2551 chunk_appendf(&trash, "'");
2552 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002553 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002554 }
2555
2556 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002557 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002558 /* queue : current, max */
2559 "<td>%s</td><td>%s</td><td></td>"
2560 /* sessions rate : current, max, limit */
2561 "<td>%s</td><td>%s</td><td></td>"
2562 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002564 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
2565 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002566
2567 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002568 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002570 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002571 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002573 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
2574 U2H(px->be_counters.cum_conn),
2575 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002576
Willy Tarreau466c9b52012-12-23 02:25:03 +01002577 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002579 chunk_appendf(&trash,
2580 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2581 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2582 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2583 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2584 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2585 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2586 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2587 "<tr><th>- other responses:</th><td>%s</td></tr>"
2588 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2589 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002590 U2H(px->be_counters.p.http.cum_req),
2591 U2H(px->be_counters.p.http.rsp[1]),
2592 U2H(px->be_counters.p.http.rsp[2]),
2593 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002595 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002596 U2H(px->be_counters.p.http.rsp[3]),
2597 U2H(px->be_counters.p.http.rsp[4]),
2598 U2H(px->be_counters.p.http.rsp[5]),
2599 U2H(px->be_counters.p.http.rsp[0]),
2600 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002601 }
2602
2603 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002604 "</table></div></u></td>"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002605 /* sessions: lbtot */
Willy Tarreau466c9b52012-12-23 02:25:03 +01002606 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002608 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002609 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002610 U2H(px->be_counters.cum_lbconn),
2611 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002613 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002614 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002615 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002617 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002618 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
2619 px->be_counters.comp_in ?
2620 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002621 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
2622
2623 chunk_appendf(&trash,
2624 /* denied: req, resp */
2625 "<td>%s</td><td>%s</td>"
2626 /* errors : request, connect */
2627 "<td></td><td>%s</td>"
2628 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002629 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002630 /* warnings: retries, redispatches */
2631 "<td>%lld</td><td>%lld</td>"
2632 /* backend status: reflect backend status (up/down): we display UP
2633 * if the backend has known working servers or if it has no server at
2634 * all (eg: for stats). Then we display the total weight, number of
2635 * active and backups. */
2636 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
2637 "<td class=ac>%d</td><td class=ac>%d</td>"
2638 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002639 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
2640 U2H(px->be_counters.failed_conns),
2641 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002642 px->be_counters.cli_aborts,
2643 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002644 px->be_counters.retries, px->be_counters.redispatches,
2645 human_time(now.tv_sec - px->last_change, 1),
2646 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
2647 "<font color=\"red\"><b>DOWN</b></font>",
2648 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2649 px->srv_act, px->srv_bck);
2650
2651 chunk_appendf(&trash,
2652 /* rest of backend: nothing, down transitions, total downtime, throttle */
2653 "<td class=ac>&nbsp;</td><td>%d</td>"
2654 "<td>%s</td>"
2655 "<td></td>"
2656 "</tr>",
2657 px->down_trans,
2658 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
2659 }
2660 else { /* CSV mode */
2661 chunk_appendf(&trash,
2662 /* pxid, name */
2663 "%s,BACKEND,"
2664 /* queue : current, max */
2665 "%d,%d,"
2666 /* sessions : current, max, limit, total */
2667 "%d,%d,%d,%lld,"
2668 /* bytes : in, out */
2669 "%lld,%lld,"
2670 /* denied: req, resp */
2671 "%lld,%lld,"
2672 /* errors : request, connect, response */
2673 ",%lld,%lld,"
2674 /* warnings: retries, redispatches */
2675 "%lld,%lld,"
2676 /* backend status: reflect backend status (up/down): we display UP
2677 * if the backend has known working servers or if it has no server at
2678 * all (eg: for stats). Then we display the total weight, number of
2679 * active and backups. */
2680 "%s,"
2681 "%d,%d,%d,"
2682 /* rest of backend: nothing, down transitions, last change, total downtime */
2683 ",%d,%d,%d,,"
2684 /* pid, iid, sid, throttle, lbtot, tracked, type */
2685 "%d,%d,0,,%lld,,%d,"
2686 /* rate, rate_lim, rate_max, */
2687 "%u,,%u,"
2688 /* check_status, check_code, check_duration */
2689 ",,,",
2690 px->id,
2691 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
2692 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
2693 px->be_counters.bytes_in, px->be_counters.bytes_out,
2694 px->be_counters.denied_req, px->be_counters.denied_resp,
2695 px->be_counters.failed_conns, px->be_counters.failed_resp,
2696 px->be_counters.retries, px->be_counters.redispatches,
2697 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
2698 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2699 px->srv_act, px->srv_bck,
2700 px->down_trans, (int)(now.tv_sec - px->last_change),
2701 px->srv?be_downtime(px):0,
2702 relative_pid, px->uuid,
2703 px->be_counters.cum_lbconn, STATS_TYPE_BE,
2704 read_freq_ctr(&px->be_sess_per_sec),
2705 px->be_counters.sps_max);
2706
2707 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2708 if (px->mode == PR_MODE_HTTP) {
2709 for (i=1; i<6; i++)
2710 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
2711 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
2712 }
2713 else
2714 chunk_appendf(&trash, ",,,,,,");
2715
2716 /* failed health analyses */
2717 chunk_appendf(&trash, ",");
2718
2719 /* requests : req_rate, req_rate_max, req_tot, */
2720 chunk_appendf(&trash, ",,,");
2721
2722 /* errors: cli_aborts, srv_aborts */
2723 chunk_appendf(&trash, "%lld,%lld,",
2724 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
2725
2726 /* compression: in, out, bypassed */
2727 chunk_appendf(&trash, "%lld,%lld,%lld,",
2728 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
2729
2730 /* compression: comp_rsp */
2731 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
2732
2733 /* finish with EOL */
2734 chunk_appendf(&trash, "\n");
2735 }
2736 return 1;
2737}
2738
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002739/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002740 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002741 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002742 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002743static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744{
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002745 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2746
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2748 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002749
2750 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2751 scope_txt[0] = 0;
2752 if (si->applet.ctx.stats.scope_len) {
2753 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2754 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
2755 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
2756 }
2757
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002758 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002759 "<form action=\"%s%s%s%s\" method=\"post\">",
2760 uri->uri_prefix,
2761 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2762 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2763 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 }
2765
2766 /* print a new table */
2767 chunk_appendf(&trash,
2768 "<table class=\"tbl\" width=\"100%%\">\n"
2769 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002770 "<th class=\"pxname\" width=\"10%%\">");
2771
2772 chunk_appendf(&trash,
2773 "<a name=\"%s\"></a>%s"
2774 "<a class=px href=\"#%s\">%s</a>",
2775 px->id,
2776 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2777 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778
2779 if (uri->flags & ST_SHLGNDS) {
2780 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002781 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2783 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002784 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 }
2786
2787 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002788 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 "<th class=\"%s\" width=\"90%%\">%s</th>"
2790 "</tr>\n"
2791 "</table>\n"
2792 "<table class=\"tbl\" width=\"100%%\">\n"
2793 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2795 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2796
2797 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2798 /* Column heading for Enable or Disable server */
2799 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02002800 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801
2802 chunk_appendf(&trash,
2803 "<th rowspan=2></th>"
2804 "<th colspan=3>Queue</th>"
2805 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
2806 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2807 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2808 "<th colspan=9>Server</th>"
2809 "</tr>\n"
2810 "<tr class=\"titre\">"
2811 "<th>Cur</th><th>Max</th><th>Limit</th>"
2812 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2813 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
2814 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2815 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2816 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2817 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2818 "<th>Thrtle</th>\n"
2819 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02002820}
2821
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002822/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002823 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002824 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002825static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002826{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 chunk_appendf(&trash, "</table>");
2828
2829 if ((px->cap & PR_CAP_BE) && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
2830 /* close the form used to enable/disable this proxy servers */
2831 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002832 "Choose the action to perform on the checked servers : "
2833 "<select name=action>"
2834 "<option value=\"\"></option>"
2835 "<option value=\"disable\">Disable</option>"
2836 "<option value=\"enable\">Enable</option>"
2837 "<option value=\"stop\">Soft Stop</option>"
2838 "<option value=\"start\">Soft Start</option>"
2839 "<option value=\"shutdown\">Kill Sessions</option>"
2840 "</select>"
2841 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2842 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2843 "</form>",
2844 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002845 }
2846
2847 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848}
Willy Tarreau91861262007-10-17 17:06:05 +02002849
2850/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002851 * Dumps statistics for a proxy. The output is sent to the stream interface's
2852 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2853 * buffer space, or non-zero if everything completed. This function is used
2854 * both by the CLI and the HTTP entry points, and is able to dump the output
2855 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02002856 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002857static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002858{
Willy Tarreau306f8302013-07-08 15:53:06 +02002859 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002860 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002861 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002862 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002864 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02002865
Willy Tarreau295a8372011-03-10 11:25:07 +01002866 switch (si->applet.ctx.stats.px_st) {
2867 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002868 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02002869 if (uri && uri->scope) {
2870 /* we have a limited scope, we have to check the proxy name */
2871 struct stat_scope *scope;
2872 int len;
2873
2874 len = strlen(px->id);
2875 scope = uri->scope;
2876
2877 while (scope) {
2878 /* match exact proxy name */
2879 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2880 break;
2881
2882 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002883 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002884 break;
2885 scope = scope->next;
2886 }
2887
2888 /* proxy name not found : don't dump anything */
2889 if (scope == NULL)
2890 return 1;
2891 }
2892
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002893 /* if the user has requested a limited output and the proxy
2894 * name does not match, skip it.
2895 */
2896 if (si->applet.ctx.stats.scope_len &&
2897 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len) == NULL)
2898 return 1;
2899
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002900 if ((si->applet.ctx.stats.flags & STAT_BOUND) &&
2901 (si->applet.ctx.stats.iid != -1) &&
2902 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002903 return 1;
2904
Willy Tarreau295a8372011-03-10 11:25:07 +01002905 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002906 /* fall through */
2907
Willy Tarreau295a8372011-03-10 11:25:07 +01002908 case STAT_PX_ST_TH:
Willy Tarreau354898b2012-12-23 18:15:23 +01002909 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002910 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002911 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002912 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002913 }
Willy Tarreau91861262007-10-17 17:06:05 +02002914
Willy Tarreau295a8372011-03-10 11:25:07 +01002915 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002916 /* fall through */
2917
Willy Tarreau295a8372011-03-10 11:25:07 +01002918 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002919 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002920 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002921 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002922 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02002923
Willy Tarreau4348fad2012-09-20 16:48:07 +02002924 si->applet.ctx.stats.l = px->conf.listeners.n;
Willy Tarreau295a8372011-03-10 11:25:07 +01002925 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002926 /* fall through */
2927
Willy Tarreau295a8372011-03-10 11:25:07 +01002928 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002929 /* stats.l has been initialized above */
Willy Tarreau4348fad2012-09-20 16:48:07 +02002930 for (; si->applet.ctx.stats.l != &px->conf.listeners; si->applet.ctx.stats.l = l->by_fe.n) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002931 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002932 return 0;
2933
Willy Tarreau4348fad2012-09-20 16:48:07 +02002934 l = LIST_ELEM(si->applet.ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002935 if (!l->counters)
2936 continue;
2937
Willy Tarreau295a8372011-03-10 11:25:07 +01002938 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2939 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002940 break;
2941
Willy Tarreau295a8372011-03-10 11:25:07 +01002942 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002943 continue;
2944 }
2945
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946 /* print the frontend */
2947 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
2948 if (bi_putchk(rep, &trash) == -1)
2949 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002950 }
2951
Willy Tarreau295a8372011-03-10 11:25:07 +01002952 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2953 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002954 /* fall through */
2955
Willy Tarreau295a8372011-03-10 11:25:07 +01002956 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002957 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002958 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002959 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 +02002960
Willy Tarreau9b28e032012-10-12 23:49:43 +02002961 if (buffer_almost_full(rep->buf))
Willy Tarreau4e33d862009-10-11 23:35:10 +02002962 return 0;
2963
Willy Tarreau295a8372011-03-10 11:25:07 +01002964 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002965
Willy Tarreau295a8372011-03-10 11:25:07 +01002966 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2967 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002968 break;
2969
Willy Tarreau295a8372011-03-10 11:25:07 +01002970 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002971 continue;
2972 }
2973
Willy Tarreau44267702011-10-28 15:35:33 +02002974 if (sv->track)
2975 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002976 else
2977 svs = sv;
2978
Willy Tarreau91861262007-10-17 17:06:05 +02002979 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002980 if (!(svs->state & SRV_CHECKED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09002981 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002982 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09002983 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002984 sv_state = 3; /* UP */
2985 else
2986 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002987
Willy Tarreau6b7764a2013-12-04 00:43:21 +01002988 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09002989 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01002990 else if (svs->state & SRV_GOINGDOWN)
2991 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01002992 }
Willy Tarreau91861262007-10-17 17:06:05 +02002993 else
Simon Horman125d0992013-02-24 17:23:38 +09002994 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02002995 sv_state = 1; /* going up */
2996 else
2997 sv_state = 0; /* DOWN */
2998
Willy Tarreau295a8372011-03-10 11:25:07 +01002999 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003000 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01003001 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003002 continue;
3003 }
3004
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3006 if (bi_putchk(rep, &trash) == -1)
3007 return 0;
3008 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003009
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003010 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
3011 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 case STAT_PX_ST_BE:
3014 /* print the backend */
3015 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3016 if (bi_putchk(rep, &trash) == -1)
3017 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003018
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003019 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
3020 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003021
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003022 case STAT_PX_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003023 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003024 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 if (bi_putchk(rep, &trash) == -1)
3026 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003027 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003028
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
3030 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003031
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003032 case STAT_PX_ST_FIN:
3033 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035 default:
3036 /* unknown state, we should put an abort() here ! */
3037 return 1;
3038 }
3039}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003040
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003041/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3042 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003043 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003044static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045{
3046 /* WARNING! This must fit in the first buffer !!! */
3047 chunk_appendf(&trash,
3048 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3049 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3050 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3051 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3052 "<style type=\"text/css\"><!--\n"
3053 "body {"
3054 " font-family: arial, helvetica, sans-serif;"
3055 " font-size: 12px;"
3056 " font-weight: normal;"
3057 " color: black;"
3058 " background: white;"
3059 "}\n"
3060 "th,td {"
3061 " font-size: 10px;"
3062 "}\n"
3063 "h1 {"
3064 " font-size: x-large;"
3065 " margin-bottom: 0.5em;"
3066 "}\n"
3067 "h2 {"
3068 " font-family: helvetica, arial;"
3069 " font-size: x-large;"
3070 " font-weight: bold;"
3071 " font-style: italic;"
3072 " color: #6020a0;"
3073 " margin-top: 0em;"
3074 " margin-bottom: 0em;"
3075 "}\n"
3076 "h3 {"
3077 " font-family: helvetica, arial;"
3078 " font-size: 16px;"
3079 " font-weight: bold;"
3080 " color: #b00040;"
3081 " background: #e8e8d0;"
3082 " margin-top: 0em;"
3083 " margin-bottom: 0em;"
3084 "}\n"
3085 "li {"
3086 " margin-top: 0.25em;"
3087 " margin-right: 2em;"
3088 "}\n"
3089 ".hr {margin-top: 0.25em;"
3090 " border-color: black;"
3091 " border-bottom-style: solid;"
3092 "}\n"
3093 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3094 ".total {background: #20D0D0;color: #ffff80;}\n"
3095 ".frontend {background: #e8e8d0;}\n"
3096 ".socket {background: #d0d0d0;}\n"
3097 ".backend {background: #e8e8d0;}\n"
3098 ".active0 {background: #ff9090;}\n"
3099 ".active1 {background: #ffd020;}\n"
3100 ".active2 {background: #ffffa0;}\n"
3101 ".active3 {background: #c0ffc0;}\n"
3102 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003103 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3104 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3105 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003106 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107 ".backup0 {background: #ff9090;}\n"
3108 ".backup1 {background: #ff80ff;}\n"
3109 ".backup2 {background: #c060ff;}\n"
3110 ".backup3 {background: #b0d0ff;}\n"
3111 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3112 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003113 ".backup6 {background: #c060ff;}\n"
3114 ".backup7 {background: #cc9900;}\n"
3115 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003116 ".maintain {background: #c07820;}\n"
3117 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3118 "\n"
3119 "a.px:link {color: #ffff40; text-decoration: none;}"
3120 "a.px:visited {color: #ffff40; text-decoration: none;}"
3121 "a.px:hover {color: #ffffff; text-decoration: none;}"
3122 "a.lfsb:link {color: #000000; text-decoration: none;}"
3123 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3124 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3125 "\n"
3126 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3127 "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"
3128 "table.tbl td.ac { text-align: center;}\n"
3129 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3130 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3131 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3132 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3133 "\n"
3134 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3135 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3136 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003137 "table.det { border-collapse: collapse; border-style: none; }\n"
3138 "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n"
3139 "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;font-family: monospace;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003141 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003142 " display:block;\n"
3143 " visibility:hidden;\n"
3144 " z-index:2147483647;\n"
3145 " position:absolute;\n"
3146 " padding:2px 4px 3px;\n"
3147 " background:#f0f060; color:#000000;\n"
3148 " border:1px solid #7040c0;\n"
3149 " white-space:nowrap;\n"
3150 " font-style:normal;font-size:11px;font-weight:normal;\n"
3151 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3152 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3153 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003154 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003155 "-->\n"
3156 "</style></head>\n",
3157 (uri->flags & ST_SHNODE) ? " on " : "",
3158 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3159 );
3160}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003161
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003162/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003163 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003164 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003165 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003166static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167{
3168 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003169 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171 /* WARNING! this has to fit the first packet too.
3172 * We are around 3.5 kB, add adding entries will
3173 * become tricky if we want to support 4kB buffers !
3174 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 chunk_appendf(&trash,
3176 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3177 PRODUCT_NAME "%s</a></h1>\n"
3178 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3179 "<hr width=\"100%%\" class=\"hr\">\n"
3180 "<h3>&gt; General process information</h3>\n"
3181 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3182 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3183 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3184 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3185 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3186 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3187 "Running tasks: %d/%d; idle = %d %%<br>\n"
3188 "</td><td align=\"center\" nowrap>\n"
3189 "<table class=\"lgd\"><tr>\n"
3190 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3191 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3192 "</tr><tr>\n"
3193 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3194 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3195 "</tr><tr>\n"
3196 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3197 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3198 "</tr><tr>\n"
3199 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003200 "</tr><tr>\n"
3201 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 "</tr><tr>\n"
3203 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07003204 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003205 "<td class=\"active7\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003207 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003208 "</td>"
3209 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3210 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3211 "",
3212 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3213 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3214 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3215 (uri->flags & ST_SHDESC) ? ": " : "",
3216 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3217 pid, relative_pid, global.nbproc,
3218 up / 86400, (up % 86400) / 3600,
3219 (up % 3600) / 60, (up % 60),
3220 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3221 global.rlimit_memmax ? " MB" : "",
3222 global.rlimit_nofile,
3223 global.maxsock, global.maxconn, global.maxpipes,
3224 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3225 run_queue_cur, nb_tasks_cur, idle_pct
3226 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003227
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003228 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3229 memcpy(scope_txt, bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3230 scope_txt[si->applet.ctx.stats.scope_len] = '\0';
3231
3232 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003233 "<li><form method=\"GET\" action=\"%s%s%s\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003234 uri->uri_prefix,
3235 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3236 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3237 (si->applet.ctx.stats.scope_len > 0) ? scope_txt : "",
3238 STAT_SCOPE_TXT_MAXLEN);
3239
3240 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3241 scope_txt[0] = 0;
3242 if (si->applet.ctx.stats.scope_len) {
3243 strcpy(scope_txt, STAT_SCOPE_PATTERN);
3244 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3245 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3246 }
3247
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003248 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
3249 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003250 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 uri->uri_prefix,
3252 "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003253 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3254 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003255 else
3256 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003257 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003258 uri->uri_prefix,
3259 ";up",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003260 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3261 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003262
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263 if (uri->refresh > 0) {
3264 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
3265 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003266 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267 uri->uri_prefix,
3268 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003269 "",
3270 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003271 else
3272 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003273 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274 uri->uri_prefix,
3275 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003276 ";norefresh",
3277 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003279
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003280 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003281 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 uri->uri_prefix,
3283 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003284 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3285 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003286
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003288 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003289 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003290 (uri->refresh > 0) ? ";norefresh" : "",
3291 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003292
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003293 chunk_appendf(&trash,
3294 "</ul></td>"
3295 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3296 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3297 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3298 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3299 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3300 "</ul>"
3301 "</td>"
3302 "</tr></table>\n"
3303 ""
3304 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003305
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003306 if (si->applet.ctx.stats.st_code) {
3307 switch (si->applet.ctx.stats.st_code) {
3308 case STAT_STATUS_DONE:
3309 chunk_appendf(&trash,
3310 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003311 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003313 "</div>\n", uri->uri_prefix,
3314 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3315 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3316 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 break;
3318 case STAT_STATUS_NONE:
3319 chunk_appendf(&trash,
3320 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003321 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003323 "</div>\n", uri->uri_prefix,
3324 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3325 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3326 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 break;
3328 case STAT_STATUS_PART:
3329 chunk_appendf(&trash,
3330 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003331 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 "Action partially processed.<br>"
3333 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003334 "</div>\n", uri->uri_prefix,
3335 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3336 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3337 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003338 break;
3339 case STAT_STATUS_ERRP:
3340 chunk_appendf(&trash,
3341 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003342 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 "Action not processed because of invalid parameters."
3344 "<ul>"
3345 "<li>The action is maybe unknown.</li>"
3346 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3347 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3348 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003349 "</div>\n", uri->uri_prefix,
3350 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3351 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3352 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 break;
3354 case STAT_STATUS_EXCD:
3355 chunk_appendf(&trash,
3356 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003357 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3359 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003360 "</div>\n", uri->uri_prefix,
3361 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3362 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3363 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 break;
3365 case STAT_STATUS_DENY:
3366 chunk_appendf(&trash,
3367 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003368 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003370 "</div>\n", uri->uri_prefix,
3371 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3372 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3373 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 break;
3375 default:
3376 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003377 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003378 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003380 "</div>\n", uri->uri_prefix,
3381 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3382 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3383 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 }
3385 chunk_appendf(&trash, "<p>\n");
3386 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003387}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003388
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003389/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3390 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003392static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003394 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003396
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003397/* This function dumps statistics onto the stream interface's read buffer in
3398 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003399 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3400 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3401 * and the session must be closed, or -1 in case of any error. This function is
3402 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003404static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003406 struct channel *rep = si->ib;
3407 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003408
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003410
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003411 switch (si->applet.st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 case STAT_ST_INIT:
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003413 si->applet.st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003415
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 case STAT_ST_HEAD:
Willy Tarreau354898b2012-12-23 18:15:23 +01003417 if (si->applet.ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003418 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003419 else
3420 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003421
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 if (bi_putchk(rep, &trash) == -1)
3423 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003424
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003425 si->applet.st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003427
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 case STAT_ST_INFO:
Willy Tarreau354898b2012-12-23 18:15:23 +01003429 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003430 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003431 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003432 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 }
Willy Tarreau91861262007-10-17 17:06:05 +02003434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435 si->applet.ctx.stats.px = proxy;
3436 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003437 si->applet.st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003438 /* fall through */
3439
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440 case STAT_ST_LIST:
3441 /* dump proxies */
3442 while (si->applet.ctx.stats.px) {
3443 if (buffer_almost_full(rep->buf))
3444 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003445
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 px = si->applet.ctx.stats.px;
3447 /* skip the disabled proxies, global frontend and non-networked ones */
3448 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003449 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003451
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 si->applet.ctx.stats.px = px->next;
3453 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
3454 }
3455 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003456
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003457 si->applet.st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003458 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003459
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003460 case STAT_ST_END:
Willy Tarreau354898b2012-12-23 18:15:23 +01003461 if (si->applet.ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003462 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 if (bi_putchk(rep, &trash) == -1)
3464 return 0;
3465 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003466
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003467 si->applet.st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003468 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003469
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 case STAT_ST_FIN:
3471 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01003472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 default:
3474 /* unknown state ! */
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003475 si->applet.st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003476 return -1;
3477 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478}
Willy Tarreauae526782010-03-04 20:34:23 +01003479
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003480/* This I/O handler runs as an applet embedded in a stream interface. It is
3481 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3482 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
3483 * automatically unregisters itself once transfer is complete.
3484 */
3485static void http_stats_io_handler(struct stream_interface *si)
3486{
Willy Tarreau306f8302013-07-08 15:53:06 +02003487 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003488 struct channel *req = si->ob;
3489 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01003490
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003491 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3492 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003493
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 /* check that the output is not closed */
3495 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3496 si->applet.st0 = 1;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003497
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003498 if (!si->applet.st0) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003499 if (stats_dump_stat_to_buffer(si, s->be->uri_auth)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003500 si->applet.st0 = 1;
3501 si_shutw(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003502 }
3503 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003504
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003505 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3506 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003507
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003508 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
3509 si_shutr(si);
3510 res->flags |= CF_READ_NULL;
3511 }
Willy Tarreau91861262007-10-17 17:06:05 +02003512
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003513 /* update all other flags and resync with the other side */
3514 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003515
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003516 /* we don't want to expire timeouts while we're processing requests */
3517 si->ib->rex = TICK_ETERNITY;
3518 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02003519
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003520 out:
3521 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
3522 /* check that we have released everything then unregister */
3523 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003524 }
3525}
3526
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003527
Willy Tarreau909d5172012-11-26 03:04:41 +01003528static inline const char *get_conn_ctrl_name(const struct connection *conn)
3529{
3530 if (!conn->ctrl)
3531 return "NONE";
3532 return conn->ctrl->name;
3533}
3534
3535static inline const char *get_conn_xprt_name(const struct connection *conn)
3536{
3537 static char ptr[17];
3538
3539 if (!conn->xprt)
3540 return "NONE";
3541
3542 if (conn->xprt == &raw_sock)
3543 return "RAW";
3544
3545#ifdef USE_OPENSSL
3546 if (conn->xprt == &ssl_sock)
3547 return "SSL";
3548#endif
3549 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
3550 return ptr;
3551}
3552
3553static inline const char *get_conn_data_name(const struct connection *conn)
3554{
3555 static char ptr[17];
3556
3557 if (!conn->data)
3558 return "NONE";
3559
3560 if (conn->data == &sess_conn_cb)
3561 return "SESS";
3562
3563 if (conn->data == &si_conn_cb)
3564 return "STRM";
3565
3566 if (conn->data == &check_conn_cb)
3567 return "CHCK";
3568
3569 snprintf(ptr, sizeof(ptr), "%p", conn->data);
3570 return ptr;
3571}
3572
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003573/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003574 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003575 * 0 if the output buffer is full and it needs to be called again, otherwise
3576 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003577 */
Willy Tarreau76153662012-11-26 01:16:39 +01003578static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003579{
3580 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003581 extern const char *monthname[12];
3582 char pn[INET6_ADDRSTRLEN];
3583
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003584 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003585
Willy Tarreau295a8372011-03-10 11:25:07 +01003586 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003587 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003588 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3589 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003590 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003591 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003592 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003593 return 1;
3594 }
3595
Willy Tarreau295a8372011-03-10 11:25:07 +01003596 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003597 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003598 si->applet.ctx.sess.uid = sess->uniq_id;
3599 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003600 /* fall through */
3601
3602 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003603 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003604 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003605 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003606 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003607 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3608 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003609 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01003610 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003611
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003612 switch (addr_to_str(&sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003613 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003614 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003615 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003616 pn, get_host_port(&sess->si[0].conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003617 break;
3618 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003619 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003620 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003621 default:
3622 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003623 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003624 break;
3625 }
3626
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003627 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003628 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003629 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003630
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003631 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003632 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003633 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3634 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3635 sess->listener ? sess->listener->luid : 0);
3636
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003637 conn_get_to_addr(sess->si[0].conn);
3638 switch (addr_to_str(&sess->si[0].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003639 case AF_INET:
3640 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003641 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003642 pn, get_host_port(&sess->si[0].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003643 break;
3644 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003645 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07003646 break;
3647 default:
3648 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003649 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003650 break;
3651 }
3652
Willy Tarreau50943332011-09-02 17:33:05 +02003653 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003654 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003655 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003656 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003657 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3658 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003659 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003660
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003661 conn_get_from_addr(sess->si[1].conn);
3662 switch (addr_to_str(&sess->si[1].conn->addr.from, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003663 case AF_INET:
3664 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003665 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003666 pn, get_host_port(&sess->si[1].conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07003667 break;
3668 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003669 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003670 break;
3671 default:
3672 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003673 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003674 break;
3675 }
3676
3677 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003678 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07003679 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003680 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
3681 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02003682 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003683 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07003684
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003685 conn_get_to_addr(sess->si[1].conn);
3686 switch (addr_to_str(&sess->si[1].conn->addr.to, pn, sizeof(pn))) {
Daniel Schultze90690c72012-03-23 10:53:36 -07003687 case AF_INET:
3688 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003689 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003690 pn, get_host_port(&sess->si[1].conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07003691 break;
3692 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003693 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003694 break;
3695 default:
3696 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003697 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07003698 break;
3699 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003700
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003701 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003702 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003703 sess->task,
3704 sess->task->state,
3705 sess->task->nice, sess->task->calls,
3706 sess->task->expire ?
3707 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3708 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3709 TICKS_TO_MS(1000)) : "<NEVER>",
3710 task_in_rq(sess->task) ? ", running" : "");
3711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003712 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003713 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003714 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3715
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003716 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003717 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
3718 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3719 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
3720
3721 chunk_appendf(&trash,
3722 " si[0]=%p (state=%s flags=0x%02x conn0=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003723 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003724 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003725 sess->si[0].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003726 sess->si[0].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003727 sess->si[0].exp ?
3728 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3729 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3730 TICKS_TO_MS(1000)) : "<NEVER>",
3731 sess->si[0].err_type);
3732
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003733 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003734 " si[1]=%p (state=%s flags=0x%02x conn1=%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003735 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003736 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003737 sess->si[1].flags,
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003738 sess->si[1].conn,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003739 sess->si[1].exp ?
3740 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3741 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3742 TICKS_TO_MS(1000)) : "<NEVER>",
3743 sess->si[1].err_type);
3744
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003745 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003746 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003747 sess->si[0].conn,
3748 get_conn_ctrl_name(sess->si[0].conn),
3749 get_conn_xprt_name(sess->si[0].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003750 get_conn_data_name(sess->si[0].conn),
3751 obj_type_name(sess->si[0].conn->target),
3752 obj_base_ptr(sess->si[0].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003753
3754 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003755 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003756 sess->si[0].conn->flags,
3757 sess->si[0].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003758 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_e : 0,
3759 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].spec_p : 0,
3760 sess->si[0].conn->t.sock.fd >= 0 ? fdtab[sess->si[0].conn->t.sock.fd].updated : 0);
Willy Tarreaubc174aa2012-11-19 16:10:32 +01003761
3762 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003763 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreau909d5172012-11-26 03:04:41 +01003764 sess->si[1].conn,
3765 get_conn_ctrl_name(sess->si[1].conn),
3766 get_conn_xprt_name(sess->si[1].conn),
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003767 get_conn_data_name(sess->si[1].conn),
3768 obj_type_name(sess->si[1].conn->target),
3769 obj_base_ptr(sess->si[1].conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01003770
3771 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003772 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreau1feca012012-11-19 18:15:19 +01003773 sess->si[1].conn->flags,
3774 sess->si[1].conn->t.sock.fd,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01003775 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_e : 0,
3776 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].spec_p : 0,
3777 sess->si[1].conn->t.sock.fd >= 0 ? fdtab[sess->si[1].conn->t.sock.fd].updated : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003778
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003779 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003780 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003781 " an_exp=%s",
3782 sess->req,
3783 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003784 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003785 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003786 sess->req->analyse_exp ?
3787 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3788 TICKS_TO_MS(1000)) : "<NEVER>");
3789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003790 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003791 " rex=%s",
3792 sess->req->rex ?
3793 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3794 TICKS_TO_MS(1000)) : "<NEVER>");
3795
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003796 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003797 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003798 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003799 sess->req->wex ?
3800 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3801 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003802 sess->req->buf,
3803 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003804 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003805 sess->txn.req.next, sess->req->buf->i,
3806 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003807
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003808 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01003809 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003810 " an_exp=%s",
3811 sess->rep,
3812 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003813 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01003814 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003815 sess->rep->analyse_exp ?
3816 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3817 TICKS_TO_MS(1000)) : "<NEVER>");
3818
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003819 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003820 " rex=%s",
3821 sess->rep->rex ?
3822 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3823 TICKS_TO_MS(1000)) : "<NEVER>");
3824
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003825 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003826 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01003827 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003828 sess->rep->wex ?
3829 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3830 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01003831 sess->rep->buf,
3832 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003833 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01003834 sess->txn.rsp.next, sess->rep->buf->i,
3835 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003836
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003837 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003838 return 0;
3839
3840 /* use other states to dump the contents */
3841 }
3842 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003843 si->applet.ctx.sess.uid = 0;
Willy Tarreau76153662012-11-26 01:16:39 +01003844 si->applet.ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003845 return 1;
3846}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003847
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003848/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02003849 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003850 * to be called again, otherwise non-zero. It is designed to be called
3851 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003852 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003853static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003854{
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003855 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003856 /* If we're forced to shut down, we might have to remove our
3857 * reference to the last session being dumped.
3858 */
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003859 if (si->applet.st2 == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003860 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3861 LIST_DEL(&si->applet.ctx.sess.bref.users);
3862 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003863 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003864 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003865 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003866 }
3867
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003868 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003869
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003870 switch (si->applet.st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003871 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003872 /* the function had not been called yet, let's prepare the
3873 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003874 * pointer to the first in the global list. When a target
3875 * session is being destroyed, it is responsible for updating
3876 * this pointer. We know we have reached the end when this
3877 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003878 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003879 LIST_INIT(&si->applet.ctx.sess.bref.users);
3880 si->applet.ctx.sess.bref.ref = sessions.n;
Willy Tarreaua94d2d72013-07-08 15:38:15 +02003881 si->applet.st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003882 /* fall through */
3883
Willy Tarreau295a8372011-03-10 11:25:07 +01003884 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003885 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003886 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3887 LIST_DEL(&si->applet.ctx.sess.bref.users);
3888 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003889 }
3890
3891 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003892 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003893 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003894 struct session *curr_sess;
3895
Willy Tarreau295a8372011-03-10 11:25:07 +01003896 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003897
Willy Tarreau295a8372011-03-10 11:25:07 +01003898 if (si->applet.ctx.sess.target) {
Willy Tarreau76153662012-11-26 01:16:39 +01003899 if (si->applet.ctx.sess.target != (void *)-1 && si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003900 goto next_sess;
3901
Willy Tarreau295a8372011-03-10 11:25:07 +01003902 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003903 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01003904 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003905 return 0;
3906
3907 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003908 LIST_DEL(&si->applet.ctx.sess.bref.users);
3909 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreau76153662012-11-26 01:16:39 +01003910 if (si->applet.ctx.sess.target != (void *)-1) {
3911 si->applet.ctx.sess.target = NULL;
3912 break;
3913 }
3914 else
3915 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003916 }
3917
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003918 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003919 "%p: proto=%s",
3920 curr_sess,
3921 curr_sess->listener->proto->name);
3922
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003923
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003924 switch (addr_to_str(&curr_sess->si[0].conn->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003925 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003926 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003927 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003928 " src=%s:%d fe=%s be=%s srv=%s",
3929 pn,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003930 get_host_port(&curr_sess->si[0].conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003931 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003932 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003933 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003934 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003935 break;
3936 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003937 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02003938 " src=unix:%d fe=%s be=%s srv=%s",
3939 curr_sess->listener->luid,
3940 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003941 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003942 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003943 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003944 break;
3945 }
3946
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003947 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003948 " ts=%02x age=%s calls=%d",
3949 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003950 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3951 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003952
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003953 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003954 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003955 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003956 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003957 curr_sess->req->analysers,
3958 curr_sess->req->rex ?
3959 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3960 TICKS_TO_MS(1000)) : "");
3961
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003963 ",wx=%s",
3964 curr_sess->req->wex ?
3965 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3966 TICKS_TO_MS(1000)) : "");
3967
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003968 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003969 ",ax=%s]",
3970 curr_sess->req->analyse_exp ?
3971 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3972 TICKS_TO_MS(1000)) : "");
3973
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003974 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003975 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003976 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003977 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003978 curr_sess->rep->analysers,
3979 curr_sess->rep->rex ?
3980 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3981 TICKS_TO_MS(1000)) : "");
3982
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003983 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003984 ",wx=%s",
3985 curr_sess->rep->wex ?
3986 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3987 TICKS_TO_MS(1000)) : "");
3988
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003989 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003990 ",ax=%s]",
3991 curr_sess->rep->analyse_exp ?
3992 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3993 TICKS_TO_MS(1000)) : "");
3994
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003995 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003996 " s0=[%d,%1xh,fd=%d,ex=%s]",
3997 curr_sess->si[0].state,
3998 curr_sess->si[0].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003999 curr_sess->si[0].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004000 curr_sess->si[0].exp ?
4001 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
4002 TICKS_TO_MS(1000)) : "");
4003
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004004 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004005 " s1=[%d,%1xh,fd=%d,ex=%s]",
4006 curr_sess->si[1].state,
4007 curr_sess->si[1].flags,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01004008 curr_sess->si[1].conn->t.sock.fd,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004009 curr_sess->si[1].exp ?
4010 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
4011 TICKS_TO_MS(1000)) : "");
4012
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004013 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004014 " exp=%s",
4015 curr_sess->task->expire ?
4016 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
4017 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01004018 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004019 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01004020
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004021 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004022
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004023 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01004024 /* let's try again later from this session. We add ourselves into
4025 * this session's users so that it can remove us upon termination.
4026 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004027 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004028 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004029 }
4030
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004031 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01004032 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004033 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004034
Willy Tarreau76153662012-11-26 01:16:39 +01004035 if (si->applet.ctx.sess.target && si->applet.ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004036 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01004037 if (si->applet.ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004038 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004039 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004040 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004041
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004042 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004043 return 0;
4044
Willy Tarreau295a8372011-03-10 11:25:07 +01004045 si->applet.ctx.sess.target = NULL;
4046 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004047 return 1;
4048 }
4049
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004050 si->applet.st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004051 /* fall through */
4052
4053 default:
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004054 si->applet.st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02004055 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004056 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02004057}
4058
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004059/* This is called when the stream interface is closed. For instance, upon an
4060 * external abort, we won't call the i/o handler anymore so we may need to
4061 * remove back references to the session currently being dumped.
4062 */
Simon Horman74d88312013-02-12 10:45:50 +09004063static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004064{
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004065 if (si->applet.st0 == STAT_CLI_O_SESS && si->applet.st2 == STAT_ST_LIST) {
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004066 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users))
4067 LIST_DEL(&si->applet.ctx.sess.bref.users);
4068 }
4069}
4070
Willy Tarreau20e99322013-04-13 09:22:25 +02004071/* This function is used to either dump tables states (when action is set
4072 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02004073 * It returns 0 if the output buffer is full and it needs to be called
4074 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02004075 */
Willy Tarreau20e99322013-04-13 09:22:25 +02004076static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02004077{
Willy Tarreau306f8302013-07-08 15:53:06 +02004078 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004079 struct ebmb_node *eb;
4080 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01004081 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02004082 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004083
4084 /*
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004085 * We have 3 possible states in si->applet.st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01004086 * - STAT_ST_INIT : the first call
4087 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02004088 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004089 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02004090 * and the entry pointer points to the next entry to be dumped,
4091 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01004092 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02004093 * data though.
4094 */
4095
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004096 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004097 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004098 if (si->applet.st2 == STAT_ST_LIST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004099 si->applet.ctx.table.entry->ref_cnt--;
4100 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02004101 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02004102 return 1;
4103 }
4104
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004105 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004106
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004107 while (si->applet.st2 != STAT_ST_FIN) {
4108 switch (si->applet.st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004109 case STAT_ST_INIT:
4110 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
4111 if (!si->applet.ctx.table.proxy)
4112 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004113
Willy Tarreau295a8372011-03-10 11:25:07 +01004114 si->applet.ctx.table.entry = NULL;
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004115 si->applet.st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004116 break;
4117
Willy Tarreau295a8372011-03-10 11:25:07 +01004118 case STAT_ST_INFO:
4119 if (!si->applet.ctx.table.proxy ||
4120 (si->applet.ctx.table.target &&
4121 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004122 si->applet.st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004123 break;
4124 }
4125
Willy Tarreau295a8372011-03-10 11:25:07 +01004126 if (si->applet.ctx.table.proxy->table.size) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004127 if (show && !stats_dump_table_head_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormanc88b8872011-06-15 15:18:49 +09004128 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02004129 return 0;
4130
Willy Tarreau295a8372011-03-10 11:25:07 +01004131 if (si->applet.ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02004132 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02004133 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01004134 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004135 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004136 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
4137 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004138 si->applet.st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004139 break;
4140 }
4141 }
4142 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004143 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004144 break;
4145
Willy Tarreau295a8372011-03-10 11:25:07 +01004146 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01004147 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09004148
Willy Tarreau295a8372011-03-10 11:25:07 +01004149 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004150 /* we're filtering on some data contents */
4151 void *ptr;
4152 long long data;
4153
Willy Tarreau295a8372011-03-10 11:25:07 +01004154 dt = si->applet.ctx.table.data_type;
4155 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
4156 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004157 dt);
4158
4159 data = 0;
4160 switch (stktable_data_types[dt].std_type) {
4161 case STD_T_SINT:
4162 data = stktable_data_cast(ptr, std_t_sint);
4163 break;
4164 case STD_T_UINT:
4165 data = stktable_data_cast(ptr, std_t_uint);
4166 break;
4167 case STD_T_ULL:
4168 data = stktable_data_cast(ptr, std_t_ull);
4169 break;
4170 case STD_T_FRQP:
4171 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01004172 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004173 break;
4174 }
4175
4176 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01004177 if ((data < si->applet.ctx.table.value &&
4178 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4179 si->applet.ctx.table.data_op == STD_OP_GT ||
4180 si->applet.ctx.table.data_op == STD_OP_GE)) ||
4181 (data == si->applet.ctx.table.value &&
4182 (si->applet.ctx.table.data_op == STD_OP_NE ||
4183 si->applet.ctx.table.data_op == STD_OP_GT ||
4184 si->applet.ctx.table.data_op == STD_OP_LT)) ||
4185 (data > si->applet.ctx.table.value &&
4186 (si->applet.ctx.table.data_op == STD_OP_EQ ||
4187 si->applet.ctx.table.data_op == STD_OP_LT ||
4188 si->applet.ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01004189 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02004190 }
4191
Simon Hormanc88b8872011-06-15 15:18:49 +09004192 if (show && !skip_entry &&
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004193 !stats_dump_table_entry_to_buffer(&trash, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09004194 si->applet.ctx.table.entry))
4195 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004196
Willy Tarreau295a8372011-03-10 11:25:07 +01004197 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004198
Willy Tarreau295a8372011-03-10 11:25:07 +01004199 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02004200 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004201 struct stksess *old = si->applet.ctx.table.entry;
4202 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004203 if (show)
4204 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau33fba6f2013-08-13 16:44:40 +02004205 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
Willy Tarreau8fa52f42012-01-09 11:50:03 +01004206 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01004207 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004208 break;
4209 }
4210
Simon Hormanc88b8872011-06-15 15:18:49 +09004211
4212 if (show)
4213 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4214 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
4215 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
4216
Willy Tarreau295a8372011-03-10 11:25:07 +01004217 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004218 si->applet.st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004219 break;
4220
Willy Tarreau295a8372011-03-10 11:25:07 +01004221 case STAT_ST_END:
Willy Tarreaua94d2d72013-07-08 15:38:15 +02004222 si->applet.st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02004223 break;
4224 }
4225 }
4226 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004227}
4228
Willy Tarreaud426a182010-03-05 14:58:26 +01004229/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01004230 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
4231 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
4232 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
4233 * lines are respected within the limit of 70 output chars. Lines that are
4234 * continuation of a previous truncated line begin with "+" instead of " "
4235 * after the offset. The new pointer is returned.
4236 */
Willy Tarreaud426a182010-03-05 14:58:26 +01004237static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
4238 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004239{
4240 int end;
4241 unsigned char c;
4242
4243 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02004244 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004245 return ptr;
4246
Willy Tarreau77804732012-10-29 16:14:26 +01004247 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01004248
Willy Tarreaud426a182010-03-05 14:58:26 +01004249 while (ptr < len && ptr < bsize) {
4250 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01004251 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004252 if (out->len > end - 2)
4253 break;
4254 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004255 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004256 if (out->len > end - 3)
4257 break;
4258 out->str[out->len++] = '\\';
4259 switch (c) {
4260 case '\t': c = 't'; break;
4261 case '\n': c = 'n'; break;
4262 case '\r': c = 'r'; break;
4263 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01004264 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004265 }
4266 out->str[out->len++] = c;
4267 } else {
4268 if (out->len > end - 5)
4269 break;
4270 out->str[out->len++] = '\\';
4271 out->str[out->len++] = 'x';
4272 out->str[out->len++] = hextab[(c >> 4) & 0xF];
4273 out->str[out->len++] = hextab[c & 0xF];
4274 }
Willy Tarreaud426a182010-03-05 14:58:26 +01004275 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004276 /* we had a line break, let's return now */
4277 out->str[out->len++] = '\n';
4278 *line = ptr;
4279 return ptr;
4280 }
4281 }
4282 /* we have an incomplete line, we return it as-is */
4283 out->str[out->len++] = '\n';
4284 return ptr;
4285}
4286
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004287/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004288 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004289 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01004290 */
Simon Horman9bd2c732011-06-15 15:18:44 +09004291static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004292{
4293 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01004294
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004295 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02004296 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004297
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004298 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004299
Willy Tarreau295a8372011-03-10 11:25:07 +01004300 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004301 /* the function had not been called yet, let's prepare the
4302 * buffer for a response.
4303 */
Willy Tarreau10479e42010-12-12 14:00:34 +01004304 struct tm tm;
4305
4306 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004307 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01004308 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4309 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
4310 error_snapshot_id);
4311
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004312 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01004313 /* Socket buffer full. Let's try again later from the same point */
4314 return 0;
4315 }
4316
Willy Tarreau295a8372011-03-10 11:25:07 +01004317 si->applet.ctx.errors.px = proxy;
4318 si->applet.ctx.errors.buf = 0;
4319 si->applet.ctx.errors.bol = 0;
4320 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004321 }
4322
4323 /* we have two inner loops here, one for the proxy, the other one for
4324 * the buffer.
4325 */
Willy Tarreau295a8372011-03-10 11:25:07 +01004326 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004327 struct error_snapshot *es;
4328
Willy Tarreau295a8372011-03-10 11:25:07 +01004329 if (si->applet.ctx.errors.buf == 0)
4330 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004331 else
Willy Tarreau295a8372011-03-10 11:25:07 +01004332 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004333
4334 if (!es->when.tv_sec)
4335 goto next;
4336
Willy Tarreau295a8372011-03-10 11:25:07 +01004337 if (si->applet.ctx.errors.iid >= 0 &&
4338 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
4339 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01004340 goto next;
4341
Willy Tarreau295a8372011-03-10 11:25:07 +01004342 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004343 /* just print headers now */
4344
4345 char pn[INET6_ADDRSTRLEN];
4346 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004347 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004348
4349 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004350 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01004351 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02004352 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01004353
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004354 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
4355 case AF_INET:
4356 case AF_INET6:
4357 port = get_host_port(&es->src);
4358 break;
4359 default:
4360 port = 0;
4361 }
4362
Willy Tarreau295a8372011-03-10 11:25:07 +01004363 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004364 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004365 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004366 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004367 " backend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004368 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004369 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
4370 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004371 break;
4372 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004374 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004375 " frontend %s (#%d)",
Willy Tarreau295a8372011-03-10 11:25:07 +01004376 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004377 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01004378 break;
4379 }
4380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004381 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02004382 ", server %s (#%d), event #%u\n"
4383 " src %s:%d, session #%d, session flags 0x%08x\n"
4384 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
4385 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
4386 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
4387 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
4388 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
4389 es->ev_id,
4390 pn, port, es->sid, es->s_flags,
4391 es->state, es->m_flags, es->t_flags,
4392 es->m_clen, es->m_blen,
4393 es->b_flags, es->b_out, es->b_tot,
4394 es->len, es->b_wrap, es->pos);
4395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004396 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004397 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004398 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004399 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004400 si->applet.ctx.errors.ptr = 0;
4401 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004402 }
4403
Willy Tarreau295a8372011-03-10 11:25:07 +01004404 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004405 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004406 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01004407 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004408 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02004409 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004410 goto next;
4411 }
4412
4413 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01004414 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004415 int newptr;
4416 int newline;
4417
Willy Tarreau295a8372011-03-10 11:25:07 +01004418 newline = si->applet.ctx.errors.bol;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004419 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
Willy Tarreau295a8372011-03-10 11:25:07 +01004420 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02004421 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004422
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004423 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01004424 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02004425 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004426 }
Willy Tarreau295a8372011-03-10 11:25:07 +01004427 si->applet.ctx.errors.ptr = newptr;
4428 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004429 };
4430 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004431 si->applet.ctx.errors.bol = 0;
4432 si->applet.ctx.errors.ptr = -1;
4433 si->applet.ctx.errors.buf++;
4434 if (si->applet.ctx.errors.buf > 1) {
4435 si->applet.ctx.errors.buf = 0;
4436 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004437 }
4438 }
4439
4440 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004441 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004442}
4443
Willy Tarreaud5781202012-09-22 19:32:35 +02004444/* parse the "level" argument on the bind lines */
4445static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
4446{
4447 if (!*args[cur_arg + 1]) {
4448 memprintf(err, "'%s' : missing level", args[cur_arg]);
4449 return ERR_ALERT | ERR_FATAL;
4450 }
4451
4452 if (!strcmp(args[cur_arg+1], "user"))
4453 conf->level = ACCESS_LVL_USER;
4454 else if (!strcmp(args[cur_arg+1], "operator"))
4455 conf->level = ACCESS_LVL_OPER;
4456 else if (!strcmp(args[cur_arg+1], "admin"))
4457 conf->level = ACCESS_LVL_ADMIN;
4458 else {
4459 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
4460 args[cur_arg], args[cur_arg+1]);
4461 return ERR_ALERT | ERR_FATAL;
4462 }
4463
4464 return 0;
4465}
4466
Willy Tarreaub24281b2011-02-13 13:16:36 +01004467struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004468 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004469 .name = "<STATS>", /* used for logging */
4470 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004471 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004472};
4473
Simon Horman9bd2c732011-06-15 15:18:44 +09004474static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004475 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004476 .name = "<CLI>", /* used for logging */
4477 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01004478 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004479};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004480
Willy Tarreaudc13c112013-06-21 23:16:39 +02004481static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02004482 { CFG_GLOBAL, "stats", stats_parse_global },
4483 { 0, NULL, NULL },
4484}};
4485
Willy Tarreaud5781202012-09-22 19:32:35 +02004486static struct bind_kw_list bind_kws = { "STAT", { }, {
4487 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
4488 { NULL, NULL, 0 },
4489}};
4490
Willy Tarreau10522fd2008-07-09 20:12:41 +02004491__attribute__((constructor))
4492static void __dumpstats_module_init(void)
4493{
4494 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02004495 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02004496}
4497
Willy Tarreau91861262007-10-17 17:06:05 +02004498/*
4499 * Local variables:
4500 * c-indent-level: 8
4501 * c-basic-offset: 8
4502 * End:
4503 */