blob: cddbc3ed7b8a17374c4de016dad87059467c287b [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
Simon Horman17bce342011-06-15 15:18:47 +090017#include <stdbool.h>
Willy Tarreau91861262007-10-17 17:06:05 +020018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020021#include <pwd.h>
22#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020023
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
Willy Tarreau10522fd2008-07-09 20:12:41 +020028#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020029#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
43#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010049#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020050#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020051#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020052#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020053#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020054#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010055#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020056#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010057#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058
Simon Horman9bd2c732011-06-15 15:18:44 +090059static int stats_dump_raw_to_buffer(struct stream_interface *si);
60static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
61static int stats_dump_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090063static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090064static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
65static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
66
67static struct si_applet cli_applet;
68
69static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020070 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020071 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020072 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020073 " help : this message\n"
74 " prompt : toggle interactive mode with prompt\n"
75 " quit : disconnect\n"
76 " show info : report information about the running process\n"
77 " show stat : report counters for each proxy and server\n"
78 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010079 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020080 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020081 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020082 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010083 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020084 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020085 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020086 " disable : put a server or frontend in maintenance mode\n"
87 " enable : re-enable a server or frontend which is in maintenance mode\n"
88 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020089 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020090
Simon Horman9bd2c732011-06-15 15:18:44 +090091static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020092 "Permission denied\n"
93 "";
94
Willy Tarreau295a8372011-03-10 11:25:07 +010095/* data transmission states for the stats responses */
96enum {
97 STAT_ST_INIT = 0,
98 STAT_ST_HEAD,
99 STAT_ST_INFO,
100 STAT_ST_LIST,
101 STAT_ST_END,
102 STAT_ST_FIN,
103};
104
105/* data transmission states for the stats responses inside a proxy */
106enum {
107 STAT_PX_ST_INIT = 0,
108 STAT_PX_ST_TH,
109 STAT_PX_ST_FE,
110 STAT_PX_ST_LI,
111 STAT_PX_ST_SV,
112 STAT_PX_ST_BE,
113 STAT_PX_ST_END,
114 STAT_PX_ST_FIN,
115};
116
Cyril Bonté19979e12012-04-04 12:57:21 +0200117extern const char *stat_status_codes[];
118
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200119/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200120 * a new stats socket. It returns a positive value upon success, 0 if the connection
121 * needs to be closed and ignored, or a negative value upon critical failure.
122 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900123static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200124{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200125 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100126 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200127 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100128 s->si[1].applet.private = s;
129 s->si[1].applet.st1 = 0;
130 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200131
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200132 tv_zero(&s->logs.tv_request);
133 s->logs.t_queue = 0;
134 s->logs.t_connect = 0;
135 s->logs.t_data = 0;
136 s->logs.t_close = 0;
137 s->logs.bytes_in = s->logs.bytes_out = 0;
138 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
139 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200140
Willy Tarreaueb472682010-05-28 18:46:57 +0200141 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
142
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200143 if (s->listener->timeout) {
144 s->req->rto = *s->listener->timeout;
145 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200146 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200148}
149
Willy Tarreau07e9e642010-08-17 21:48:17 +0200150/* allocate a new stats frontend named <name>, and return it
151 * (or NULL in case of lack of memory).
152 */
153static struct proxy *alloc_stats_fe(const char *name)
154{
155 struct proxy *fe;
156
157 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
158 if (!fe)
159 return NULL;
160
Willy Tarreau237250c2011-07-29 01:49:03 +0200161 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200162
163 fe->last_change = now.tv_sec;
164 fe->id = strdup("GLOBAL");
165 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200166 fe->maxconn = 10; /* default to 10 concurrent connections */
167 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
168
Willy Tarreau07e9e642010-08-17 21:48:17 +0200169 return fe;
170}
171
Willy Tarreaufbee7132007-10-18 13:53:22 +0200172/* This function parses a "stats" statement in the "global" section. It returns
173 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
174 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
175 * zero included. The trailing '\n' must not be written. The function must be
176 * called with <args> pointing to the first word after "stats".
177 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200178static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
179 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200180{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200181 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200182 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100183 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200184 int cur_arg;
185
186 if (*args[1] == 0) {
187 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
188 return -1;
189 }
190
191 if (global.stats_sock.state != LI_NEW) {
192 snprintf(err, errlen, "'stats socket' already specified in global section");
193 return -1;
194 }
195
Andreas Kohn16171e22011-01-19 20:29:32 +0100196 su = str2sun(args[1]);
197 if (!su) {
198 snprintf(err, errlen, "'stats socket' path would require truncation");
199 return -1;
200 }
201 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200202
Willy Tarreau89a63132009-08-16 17:41:45 +0200203 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200204 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200205 snprintf(err, errlen, "out of memory");
206 return -1;
207 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200208 }
209
Willy Tarreaufbee7132007-10-18 13:53:22 +0200210 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200211 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200212 global.stats_sock.accept = session_accept;
213 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200214 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200215 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200216 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200217 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200218 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200219 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200220 global.stats_sock.timeout = &global.stats_fe->timeout.client;
221
222 global.stats_sock.next = global.stats_fe->listen;
223 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200224
225 cur_arg = 2;
226 while (*args[cur_arg]) {
227 if (!strcmp(args[cur_arg], "uid")) {
228 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
229 cur_arg += 2;
230 }
231 else if (!strcmp(args[cur_arg], "gid")) {
232 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
233 cur_arg += 2;
234 }
235 else if (!strcmp(args[cur_arg], "mode")) {
236 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
237 cur_arg += 2;
238 }
239 else if (!strcmp(args[cur_arg], "user")) {
240 struct passwd *user;
241 user = getpwnam(args[cur_arg + 1]);
242 if (!user) {
243 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
244 args[cur_arg + 1]);
245 return -1;
246 }
247 global.stats_sock.perm.ux.uid = user->pw_uid;
248 cur_arg += 2;
249 }
250 else if (!strcmp(args[cur_arg], "group")) {
251 struct group *group;
252 group = getgrnam(args[cur_arg + 1]);
253 if (!group) {
254 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
255 args[cur_arg + 1]);
256 return -1;
257 }
258 global.stats_sock.perm.ux.gid = group->gr_gid;
259 cur_arg += 2;
260 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200261 else if (!strcmp(args[cur_arg], "level")) {
262 if (!strcmp(args[cur_arg+1], "user"))
263 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
264 else if (!strcmp(args[cur_arg+1], "operator"))
265 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
266 else if (!strcmp(args[cur_arg+1], "admin"))
267 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
268 else {
269 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
270 return -1;
271 }
272 cur_arg += 2;
273 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200275 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200276 return -1;
277 }
278 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100279
Willy Tarreaufbee7132007-10-18 13:53:22 +0200280 uxst_add_listener(&global.stats_sock);
281 global.maxsock++;
282 }
283 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100284 unsigned timeout;
285 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
286
287 if (res) {
288 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
289 return -1;
290 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100292 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200293 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
294 return -1;
295 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200296 if (!global.stats_fe) {
297 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
298 snprintf(err, errlen, "out of memory");
299 return -1;
300 }
301 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200302 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200303 }
304 else if (!strcmp(args[0], "maxconn")) {
305 int maxconn = atol(args[1]);
306
307 if (maxconn <= 0) {
308 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
309 return -1;
310 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200311
312 if (!global.stats_fe) {
313 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
314 snprintf(err, errlen, "out of memory");
315 return -1;
316 }
317 }
318 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200319 }
320 else {
321 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
322 return -1;
323 }
324 return 0;
325}
326
Simon Horman9bd2c732011-06-15 15:18:44 +0900327static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100328{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200329 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100330 "# pxname,svname,"
331 "qcur,qmax,"
332 "scur,smax,slim,stot,"
333 "bin,bout,"
334 "dreq,dresp,"
335 "ereq,econ,eresp,"
336 "wretr,wredis,"
337 "status,weight,act,bck,"
338 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200339 "pid,iid,sid,throttle,lbtot,tracked,type,"
340 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200341 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100342 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100343 "req_rate,req_rate_max,req_tot,"
344 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100345 "\n");
346}
347
Simon Hormand9366582011-06-15 15:18:45 +0900348/* print a string of text buffer to <out>. The format is :
349 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
350 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
351 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
352 */
353static int dump_text(struct chunk *out, const char *buf, int bsize)
354{
355 unsigned char c;
356 int ptr = 0;
357
358 while (buf[ptr] && ptr < bsize) {
359 c = buf[ptr];
360 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
361 if (out->len > out->size - 1)
362 break;
363 out->str[out->len++] = c;
364 }
365 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
366 if (out->len > out->size - 2)
367 break;
368 out->str[out->len++] = '\\';
369 switch (c) {
370 case ' ': c = ' '; break;
371 case '\t': c = 't'; break;
372 case '\n': c = 'n'; break;
373 case '\r': c = 'r'; break;
374 case '\e': c = 'e'; break;
375 case '\\': c = '\\'; break;
376 }
377 out->str[out->len++] = c;
378 }
379 else {
380 if (out->len > out->size - 4)
381 break;
382 out->str[out->len++] = '\\';
383 out->str[out->len++] = 'x';
384 out->str[out->len++] = hextab[(c >> 4) & 0xF];
385 out->str[out->len++] = hextab[c & 0xF];
386 }
387 ptr++;
388 }
389
390 return ptr;
391}
392
393/* print a buffer in hexa.
394 * Print stopped if <bsize> is reached, or if no more place in the chunk.
395 */
396static int dump_binary(struct chunk *out, const char *buf, int bsize)
397{
398 unsigned char c;
399 int ptr = 0;
400
401 while (ptr < bsize) {
402 c = buf[ptr];
403
404 if (out->len > out->size - 2)
405 break;
406 out->str[out->len++] = hextab[(c >> 4) & 0xF];
407 out->str[out->len++] = hextab[c & 0xF];
408
409 ptr++;
410 }
411 return ptr;
412}
413
414/* Dump the status of a table to a stream interface's
415 * read buffer. It returns 0 if the output buffer is full
416 * and needs to be called again, otherwise non-zero.
417 */
418static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
419 struct proxy *proxy, struct proxy *target)
420{
421 struct session *s = si->applet.private;
422
423 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
424 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
425
426 /* any other information should be dumped here */
427
428 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
429 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
430
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200431 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900432 return 0;
433
434 return 1;
435}
436
437/* Dump the a table entry to a stream interface's
438 * read buffer. It returns 0 if the output buffer is full
439 * and needs to be called again, otherwise non-zero.
440 */
441static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
442 struct proxy *proxy, struct stksess *entry)
443{
444 int dt;
445
446 chunk_printf(msg, "%p:", entry);
447
448 if (proxy->table.type == STKTABLE_TYPE_IP) {
449 char addr[INET_ADDRSTRLEN];
450 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
451 chunk_printf(msg, " key=%s", addr);
452 }
453 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
454 char addr[INET6_ADDRSTRLEN];
455 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
456 chunk_printf(msg, " key=%s", addr);
457 }
458 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
459 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
460 }
461 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
462 chunk_printf(msg, " key=");
463 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
464 }
465 else {
466 chunk_printf(msg, " key=");
467 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
468 }
469
470 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
471
472 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
473 void *ptr;
474
475 if (proxy->table.data_ofs[dt] == 0)
476 continue;
477 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
478 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
479 else
480 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
481
482 ptr = stktable_data_ptr(&proxy->table, entry, dt);
483 switch (stktable_data_types[dt].std_type) {
484 case STD_T_SINT:
485 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
486 break;
487 case STD_T_UINT:
488 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
489 break;
490 case STD_T_ULL:
491 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
492 break;
493 case STD_T_FRQP:
494 chunk_printf(msg, "%d",
495 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
496 proxy->table.data_arg[dt].u));
497 break;
498 }
499 }
500 chunk_printf(msg, "\n");
501
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200502 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900503 return 0;
504
505 return 1;
506}
507
Simon Horman17bce342011-06-15 15:18:47 +0900508static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900509{
510 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900511 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900512 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900513 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900514 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900515
Simon Hormand5b9fd92011-06-15 15:18:48 +0900516 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900517
518 if (!*args[4]) {
519 si->applet.ctx.cli.msg = "Key value expected\n";
520 si->applet.st0 = STAT_CLI_PRINT;
521 return;
522 }
523
Simon Hormanc5b89f62011-06-15 15:18:50 +0900524 switch (px->table.type) {
525 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900526 uint32_key = htonl(inetaddr_host(args[4]));
527 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900528 break;
529 case STKTABLE_TYPE_IPV6:
530 inet_pton(AF_INET6, args[4], ip6_key);
531 static_table_key.key = &ip6_key;
532 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900533 case STKTABLE_TYPE_INTEGER:
534 {
535 char *endptr;
536 unsigned long val;
537 errno = 0;
538 val = strtoul(args[4], &endptr, 10);
539 if ((errno == ERANGE && val == ULONG_MAX) ||
540 (errno != 0 && val == 0) || endptr == args[4] ||
541 val > 0xffffffff) {
542 si->applet.ctx.cli.msg = "Invalid key\n";
543 si->applet.st0 = STAT_CLI_PRINT;
544 return;
545 }
546 uint32_key = (uint32_t) val;
547 static_table_key.key = &uint32_key;
548 break;
549 }
550 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900551 case STKTABLE_TYPE_STRING:
552 static_table_key.key = args[4];
553 static_table_key.key_len = strlen(args[4]);
554 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900555 default:
Simon Horman17bce342011-06-15 15:18:47 +0900556 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900557 si->applet.ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman17bce342011-06-15 15:18:47 +0900558 else
Simon Hormancec9a222011-06-15 15:18:51 +0900559 si->applet.ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6 and integer is not supported\n";
Simon Horman121f3052011-06-15 15:18:46 +0900560 si->applet.st0 = STAT_CLI_PRINT;
561 return;
562 }
563
564 /* check permissions */
565 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
566 si->applet.ctx.cli.msg = stats_permission_denied_msg;
567 si->applet.st0 = STAT_CLI_PRINT;
568 return;
569 }
570
571 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900572 if (!ts)
573 return;
574
575 if (show) {
576 struct chunk msg;
577 chunk_init(&msg, trash, sizeof(trash));
578 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
579 return;
580 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900581 return;
582 }
Simon Horman17bce342011-06-15 15:18:47 +0900583
584 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900585 /* don't delete an entry which is currently referenced */
586 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
587 si->applet.st0 = STAT_CLI_PRINT;
588 return;
589 }
590
591 stksess_kill(&px->table, ts);
592}
593
Simon Hormand5b9fd92011-06-15 15:18:48 +0900594static void stats_sock_table_data_request(struct stream_interface *si, char **args)
595{
596 /* condition on stored data value */
597 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
598 if (si->applet.ctx.table.data_type < 0) {
599 si->applet.ctx.cli.msg = "Unknown data type\n";
600 si->applet.st0 = STAT_CLI_PRINT;
601 return;
602 }
603
604 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
605 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
606 si->applet.st0 = STAT_CLI_PRINT;
607 return;
608 }
609
610 si->applet.ctx.table.data_op = get_std_op(args[4]);
611 if (si->applet.ctx.table.data_op < 0) {
612 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
613 si->applet.st0 = STAT_CLI_PRINT;
614 return;
615 }
616
617 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
618 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
619 si->applet.st0 = STAT_CLI_PRINT;
620 return;
621 }
622}
623
624static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
625{
626 si->applet.ctx.table.data_type = -1;
627 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200628 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900629 si->applet.ctx.table.proxy = NULL;
630 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900631 if (show)
632 si->applet.st0 = STAT_CLI_O_TAB;
633 else
634 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900635
636 if (*args[2]) {
637 si->applet.ctx.table.target = find_stktable(args[2]);
638 if (!si->applet.ctx.table.target) {
639 si->applet.ctx.cli.msg = "No such table\n";
640 si->applet.st0 = STAT_CLI_PRINT;
641 return;
642 }
643 }
644 else {
645 if (!show)
646 goto err_args;
647 return;
648 }
649
650 if (strcmp(args[3], "key") == 0)
651 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900652 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900653 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900654 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900655 goto err_args;
656
657 return;
658
659err_args:
660 if (show)
661 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
662 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900663 si->applet.ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Simon Hormand5b9fd92011-06-15 15:18:48 +0900664 si->applet.st0 = STAT_CLI_PRINT;
665}
666
Willy Tarreau532a4502011-09-07 22:37:44 +0200667/* Expects to find a frontend named <arg> and returns it, otherwise displays various
668 * adequate error messages and returns NULL. This function also expects the session
669 * level to be admin.
670 */
671static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
672{
673 struct proxy *px;
674
675 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
676 si->applet.ctx.cli.msg = stats_permission_denied_msg;
677 si->applet.st0 = STAT_CLI_PRINT;
678 return NULL;
679 }
680
681 if (!*arg) {
682 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
683 si->applet.st0 = STAT_CLI_PRINT;
684 return NULL;
685 }
686
687 px = findproxy(arg, PR_CAP_FE);
688 if (!px) {
689 si->applet.ctx.cli.msg = "No such frontend.\n";
690 si->applet.st0 = STAT_CLI_PRINT;
691 return NULL;
692 }
693 return px;
694}
695
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200696/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
697 * and returns the pointer to the server. Otherwise, display adequate error messages
698 * and returns NULL. This function also expects the session level to be admin. Note:
699 * the <arg> is modified to remove the '/'.
700 */
701static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
702{
703 struct proxy *px;
704 struct server *sv;
705 char *line;
706
707 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
708 si->applet.ctx.cli.msg = stats_permission_denied_msg;
709 si->applet.st0 = STAT_CLI_PRINT;
710 return NULL;
711 }
712
713 /* split "backend/server" and make <line> point to server */
714 for (line = arg; *line; line++)
715 if (*line == '/') {
716 *line++ = '\0';
717 break;
718 }
719
720 if (!*line || !*arg) {
721 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
722 si->applet.st0 = STAT_CLI_PRINT;
723 return NULL;
724 }
725
726 if (!get_backend_server(arg, line, &px, &sv)) {
727 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
728 si->applet.st0 = STAT_CLI_PRINT;
729 return NULL;
730 }
731
732 if (px->state == PR_STSTOPPED) {
733 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
734 si->applet.st0 = STAT_CLI_PRINT;
735 return NULL;
736 }
737
738 return sv;
739}
740
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200741/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200742 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100743 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200744 * designating the function which will have to process the request, which can
745 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200746 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900747static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200748{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100749 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200750 char *args[MAX_STATS_ARGS + 1];
751 int arg;
752
753 while (isspace((unsigned char)*line))
754 line++;
755
756 arg = 0;
757 args[arg] = line;
758
759 while (*line && arg < MAX_STATS_ARGS) {
760 if (isspace((unsigned char)*line)) {
761 *line++ = '\0';
762
763 while (isspace((unsigned char)*line))
764 line++;
765
766 args[++arg] = line;
767 continue;
768 }
769
770 line++;
771 }
772
773 while (++arg <= MAX_STATS_ARGS)
774 args[arg] = line;
775
Willy Tarreau295a8372011-03-10 11:25:07 +0100776 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200777 if (strcmp(args[0], "show") == 0) {
778 if (strcmp(args[1], "stat") == 0) {
779 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100780 si->applet.ctx.stats.flags |= STAT_BOUND;
781 si->applet.ctx.stats.iid = atoi(args[2]);
782 si->applet.ctx.stats.type = atoi(args[3]);
783 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200784 }
785
Willy Tarreau295a8372011-03-10 11:25:07 +0100786 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
787 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
788 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100789 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200790 }
791 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100792 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
793 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
794 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100795 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200796 }
797 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100798 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200799 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100800 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100801 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200802 return 1;
803 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100804 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100805 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100806 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100807 si->applet.ctx.sess.target = NULL;
808 si->applet.ctx.sess.section = 0; /* start with session status */
809 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100810 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200811 }
812 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200813 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100814 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100815 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200816 return 1;
817 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200818 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100819 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200820 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100821 si->applet.ctx.errors.iid = -1;
822 si->applet.ctx.errors.px = NULL;
823 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100824 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200825 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200826 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900827 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200828 }
Aman Guptaceafb4a2012-04-02 18:57:54 -0700829 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200830 return 0;
831 }
832 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200833 else if (strcmp(args[0], "clear") == 0) {
834 if (strcmp(args[1], "counters") == 0) {
835 struct proxy *px;
836 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200837 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200838 int clrall = 0;
839
840 if (strcmp(args[2], "all") == 0)
841 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200842
Willy Tarreau6162db22009-10-10 17:13:00 +0200843 /* check permissions */
844 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
845 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100846 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100847 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200848 return 1;
849 }
850
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200851 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100852 if (clrall) {
853 memset(&px->be_counters, 0, sizeof(px->be_counters));
854 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
855 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200856 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100857 px->be_counters.conn_max = 0;
858 px->be_counters.p.http.rps_max = 0;
859 px->be_counters.sps_max = 0;
860 px->be_counters.cps_max = 0;
861 px->be_counters.nbpend_max = 0;
862
863 px->fe_counters.conn_max = 0;
864 px->fe_counters.p.http.rps_max = 0;
865 px->fe_counters.sps_max = 0;
866 px->fe_counters.cps_max = 0;
867 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200868 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200869
870 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200871 if (clrall)
872 memset(&sv->counters, 0, sizeof(sv->counters));
873 else {
874 sv->counters.cur_sess_max = 0;
875 sv->counters.nbpend_max = 0;
876 sv->counters.sps_max = 0;
877 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200878
879 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200880 if (li->counters) {
881 if (clrall)
882 memset(li->counters, 0, sizeof(*li->counters));
883 else
884 li->counters->conn_max = 0;
885 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200886 }
887
Willy Tarreau81c25d02011-09-07 15:17:21 +0200888 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200889 return 1;
890 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200891 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900892 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200893 /* end of processing */
894 return 1;
895 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200896 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200897 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200898 return 0;
899 }
900 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200901 else if (strcmp(args[0], "get") == 0) {
902 if (strcmp(args[1], "weight") == 0) {
903 struct proxy *px;
904 struct server *sv;
905
906 /* split "backend/server" and make <line> point to server */
907 for (line = args[2]; *line; line++)
908 if (*line == '/') {
909 *line++ = '\0';
910 break;
911 }
912
913 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100914 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100915 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200916 return 1;
917 }
918
919 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100920 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100921 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200922 return 1;
923 }
924
925 /* return server's effective weight at the moment */
926 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200927 bi_putstr(si->ib, trash);
Willy Tarreau38338fa2009-10-10 18:37:29 +0200928 return 1;
929 }
930 else { /* not "get weight" */
931 return 0;
932 }
933 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200934 else if (strcmp(args[0], "set") == 0) {
935 if (strcmp(args[1], "weight") == 0) {
936 struct proxy *px;
937 struct server *sv;
938 int w;
939
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200940 sv = expect_server_admin(s, si, args[2]);
941 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +0200942 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200943 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +0200944
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200945 /* if the weight is terminated with '%', it is set relative to
946 * the initial weight, otherwise it is absolute.
947 */
948 if (!*args[3]) {
949 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100950 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100951 return 1;
952 }
953
Willy Tarreau4483d432009-10-10 19:30:08 +0200954 w = atoi(args[3]);
955 if (strchr(args[3], '%') != NULL) {
956 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100957 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100958 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200959 return 1;
960 }
961 w = sv->iweight * w / 100;
962 }
963 else {
964 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100965 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100966 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200967 return 1;
968 }
969 }
970
971 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100972 si->applet.ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100973 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200974 return 1;
975 }
976
977 sv->uweight = w;
978
979 if (px->lbprm.algo & BE_LB_PROP_DYN) {
980 /* we must take care of not pushing the server to full throttle during slow starts */
981 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
982 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
983 else
984 sv->eweight = BE_WEIGHT_SCALE;
985 sv->eweight *= sv->uweight;
986 } else {
987 sv->eweight = sv->uweight;
988 }
989
990 /* static LB algorithms are a bit harder to update */
991 if (px->lbprm.update_server_eweight)
992 px->lbprm.update_server_eweight(sv);
993 else if (sv->eweight)
994 px->lbprm.set_server_status_up(sv);
995 else
996 px->lbprm.set_server_status_down(sv);
997
998 return 1;
999 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001000 else if (strcmp(args[1], "timeout") == 0) {
1001 if (strcmp(args[2], "cli") == 0) {
1002 unsigned timeout;
1003 const char *res;
1004
1005 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001006 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001007 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001008 return 1;
1009 }
1010
1011 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1012 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001013 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001014 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001015 return 1;
1016 }
1017
1018 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1019 return 1;
1020 }
1021 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001022 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001023 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001024 return 1;
1025 }
1026 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001027 else if (strcmp(args[1], "maxconn") == 0) {
1028 if (strcmp(args[2], "frontend") == 0) {
1029 struct proxy *px;
1030 struct listener *l;
1031 int v;
1032
Willy Tarreau532a4502011-09-07 22:37:44 +02001033 px = expect_frontend_admin(s, si, args[3]);
1034 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001035 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001036
1037 if (!*args[4]) {
1038 si->applet.ctx.cli.msg = "Integer value expected.\n";
1039 si->applet.st0 = STAT_CLI_PRINT;
1040 return 1;
1041 }
1042
1043 v = atoi(args[4]);
1044 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1045 if (v < 1) {
1046 si->applet.ctx.cli.msg = "Value out of range.\n";
1047 si->applet.st0 = STAT_CLI_PRINT;
1048 return 1;
1049 }
1050
1051 /* OK, the value is fine, so we assign it to the proxy and to all of
1052 * its listeners. The blocked ones will be dequeued.
1053 */
1054 px->maxconn = v;
1055 for (l = px->listen; l != NULL; l = l->next) {
1056 l->maxconn = v;
1057 if (l->state == LI_FULL)
1058 resume_listener(l);
1059 }
1060
1061 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1062 dequeue_all_listeners(&s->fe->listener_queue);
1063
1064 return 1;
1065 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001066 else if (strcmp(args[2], "global") == 0) {
1067 int v;
1068
1069 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1070 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1071 si->applet.st0 = STAT_CLI_PRINT;
1072 return 1;
1073 }
1074
1075 if (!*args[3]) {
1076 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1077 si->applet.st0 = STAT_CLI_PRINT;
1078 return 1;
1079 }
1080
1081 v = atoi(args[3]);
1082 if (v > global.hardmaxconn) {
1083 si->applet.ctx.cli.msg = "Value out of range.\n";
1084 si->applet.st0 = STAT_CLI_PRINT;
1085 return 1;
1086 }
1087
1088 /* check for unlimited values */
1089 if (v <= 0)
1090 v = global.hardmaxconn;
1091
1092 global.maxconn = v;
1093
1094 /* Dequeues all of the listeners waiting for a resource */
1095 if (!LIST_ISEMPTY(&global_listener_queue))
1096 dequeue_all_listeners(&global_listener_queue);
1097
1098 return 1;
1099 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001100 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001101 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001102 si->applet.st0 = STAT_CLI_PRINT;
1103 return 1;
1104 }
1105 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001106 else if (strcmp(args[1], "rate-limit") == 0) {
1107 if (strcmp(args[2], "connections") == 0) {
1108 if (strcmp(args[3], "global") == 0) {
1109 int v;
1110
1111 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1112 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1113 si->applet.st0 = STAT_CLI_PRINT;
1114 return 1;
1115 }
1116
1117 if (!*args[4]) {
1118 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1119 si->applet.st0 = STAT_CLI_PRINT;
1120 return 1;
1121 }
1122
1123 v = atoi(args[4]);
1124 if (v < 0) {
1125 si->applet.ctx.cli.msg = "Value out of range.\n";
1126 si->applet.st0 = STAT_CLI_PRINT;
1127 return 1;
1128 }
1129
1130 global.cps_lim = v;
1131
1132 /* Dequeues all of the listeners waiting for a resource */
1133 if (!LIST_ISEMPTY(&global_listener_queue))
1134 dequeue_all_listeners(&global_listener_queue);
1135
1136 return 1;
1137 }
1138 else {
1139 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1140 si->applet.st0 = STAT_CLI_PRINT;
1141 return 1;
1142 }
1143 }
1144 else {
1145 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1146 si->applet.st0 = STAT_CLI_PRINT;
1147 return 1;
1148 }
1149 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001150 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001151 return 0;
1152 }
1153 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001154 else if (strcmp(args[0], "enable") == 0) {
1155 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001156 struct server *sv;
1157
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001158 sv = expect_server_admin(s, si, args[2]);
1159 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001160 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001161
Cyril Bontécd19e512010-01-31 22:34:03 +01001162 if (sv->state & SRV_MAINTAIN) {
1163 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001164 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001165 /* If this server tracks the status of another one,
1166 * we must restore the good status.
1167 */
Willy Tarreau44267702011-10-28 15:35:33 +02001168 if (sv->track->state & SRV_RUNNING) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001169 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001170 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001171 } else {
1172 sv->state &= ~SRV_MAINTAIN;
1173 set_server_down(sv);
1174 }
1175 } else {
1176 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001177 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001178 }
1179 }
1180
Willy Tarreau532a4502011-09-07 22:37:44 +02001181 return 1;
1182 }
1183 else if (strcmp(args[1], "frontend") == 0) {
1184 struct proxy *px;
1185
1186 px = expect_frontend_admin(s, si, args[2]);
1187 if (!px)
1188 return 1;
1189
1190 if (px->state == PR_STSTOPPED) {
1191 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1192 si->applet.st0 = STAT_CLI_PRINT;
1193 return 1;
1194 }
1195
1196 if (px->state != PR_STPAUSED) {
1197 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1198 si->applet.st0 = STAT_CLI_PRINT;
1199 return 1;
1200 }
1201
1202 if (!resume_proxy(px)) {
1203 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1204 si->applet.st0 = STAT_CLI_PRINT;
1205 return 1;
1206 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001207 return 1;
1208 }
1209 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001210 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1211 si->applet.st0 = STAT_CLI_PRINT;
1212 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001213 }
1214 }
1215 else if (strcmp(args[0], "disable") == 0) {
1216 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001217 struct server *sv;
1218
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001219 sv = expect_server_admin(s, si, args[2]);
1220 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001221 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001222
Cyril Bontécd19e512010-01-31 22:34:03 +01001223 if (! (sv->state & SRV_MAINTAIN)) {
1224 /* Not already in maintenance, we can change the server state */
1225 sv->state |= SRV_MAINTAIN;
1226 set_server_down(sv);
1227 }
1228
Willy Tarreau532a4502011-09-07 22:37:44 +02001229 return 1;
1230 }
1231 else if (strcmp(args[1], "frontend") == 0) {
1232 struct proxy *px;
1233
1234 px = expect_frontend_admin(s, si, args[2]);
1235 if (!px)
1236 return 1;
1237
1238 if (px->state == PR_STSTOPPED) {
1239 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1240 si->applet.st0 = STAT_CLI_PRINT;
1241 return 1;
1242 }
1243
1244 if (px->state == PR_STPAUSED) {
1245 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1246 si->applet.st0 = STAT_CLI_PRINT;
1247 return 1;
1248 }
1249
1250 if (!pause_proxy(px)) {
1251 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1252 si->applet.st0 = STAT_CLI_PRINT;
1253 return 1;
1254 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001255 return 1;
1256 }
1257 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001258 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1259 si->applet.st0 = STAT_CLI_PRINT;
1260 return 1;
1261 }
1262 }
1263 else if (strcmp(args[0], "shutdown") == 0) {
1264 if (strcmp(args[1], "frontend") == 0) {
1265 struct proxy *px;
1266
1267 px = expect_frontend_admin(s, si, args[2]);
1268 if (!px)
1269 return 1;
1270
1271 if (px->state == PR_STSTOPPED) {
1272 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1273 si->applet.st0 = STAT_CLI_PRINT;
1274 return 1;
1275 }
1276
1277 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1278 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1279 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1280 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1281 stop_proxy(px);
1282 return 1;
1283 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001284 else if (strcmp(args[1], "session") == 0) {
1285 struct session *sess, *ptr;
1286
1287 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1288 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1289 si->applet.st0 = STAT_CLI_PRINT;
1290 return 1;
1291 }
1292
1293 if (!*args[2]) {
1294 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1295 si->applet.st0 = STAT_CLI_PRINT;
1296 return 1;
1297 }
1298
1299 ptr = (void *)strtoul(args[2], NULL, 0);
1300
1301 /* first, look for the requested session in the session table */
1302 list_for_each_entry(sess, &sessions, list) {
1303 if (sess == ptr)
1304 break;
1305 }
1306
1307 /* do we have the session ? */
1308 if (sess != ptr) {
1309 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1310 si->applet.st0 = STAT_CLI_PRINT;
1311 return 1;
1312 }
1313
1314 session_shutdown(sess, SN_ERR_KILLED);
1315 return 1;
1316 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001317 else if (strcmp(args[1], "sessions") == 0) {
1318 if (strcmp(args[2], "server") == 0) {
1319 struct server *sv;
1320 struct session *sess, *sess_bck;
1321
1322 sv = expect_server_admin(s, si, args[3]);
1323 if (!sv)
1324 return 1;
1325
1326 /* kill all the session that are on this server */
1327 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1328 if (sess->srv_conn == sv)
1329 session_shutdown(sess, SN_ERR_KILLED);
1330
1331 return 1;
1332 }
1333 else {
1334 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1335 si->applet.st0 = STAT_CLI_PRINT;
1336 return 1;
1337 }
1338 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001339 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001340 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001341 si->applet.st0 = STAT_CLI_PRINT;
1342 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001343 }
1344 }
1345 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001346 return 0;
1347 }
1348 return 1;
1349}
1350
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001351/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001352 * used to processes I/O from/to the stats unix socket. The system relies on a
1353 * state machine handling requests and various responses. We read a request,
1354 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001355 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1356 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001357 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001358 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001359static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001360{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001361 struct buffer *req = si->ob;
1362 struct buffer *res = si->ib;
1363 int reql;
1364 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001365
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001366 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1367 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001368
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001369 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001370 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001371 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001372 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001373 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001374 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001375 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001376 /* Let's close for real now. We just close the request
1377 * side, the conditions below will complete if needed.
1378 */
1379 si->shutw(si);
1380 break;
1381 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001382 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001383 /* ensure we have some output room left in the event we
1384 * would want to return some info right after parsing.
1385 */
1386 if (buffer_almost_full(si->ib))
1387 break;
1388
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001389 reql = bo_getline(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001390 if (reql <= 0) { /* closed or EOL not found */
1391 if (reql == 0)
1392 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001393 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001394 continue;
1395 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001396
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001397 /* seek for a possible semi-colon. If we find one, we
1398 * replace it with an LF and skip only this part.
1399 */
1400 for (len = 0; len < reql; len++)
1401 if (trash[len] == ';') {
1402 trash[len] = '\n';
1403 reql = len + 1;
1404 break;
1405 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001406
Willy Tarreau816fc222009-10-04 07:36:58 +02001407 /* now it is time to check that we have a full line,
1408 * remove the trailing \n and possibly \r, then cut the
1409 * line.
1410 */
1411 len = reql - 1;
1412 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001413 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001414 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001415 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001416
Willy Tarreau816fc222009-10-04 07:36:58 +02001417 if (len && trash[len-1] == '\r')
1418 len--;
1419
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001420 trash[len] = '\0';
1421
Willy Tarreaubc4af052011-02-13 13:25:14 +01001422 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001423 if (len) {
1424 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001425 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001426 continue;
1427 }
1428 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001429 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001430 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001431 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001432 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001433 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001434 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001435 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001436 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001437 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001438 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001439 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001440 /* if prompt is disabled, print help on empty lines,
1441 * so that the user at least knows how to enable
1442 * prompt and find help.
1443 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001444 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001445 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001446 }
1447
1448 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001449 bo_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001450 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001451 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001452 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001453 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001454 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001455 continue;
1456 }
1457
Willy Tarreaubc4af052011-02-13 13:25:14 +01001458 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001459 case STAT_CLI_PRINT:
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001460 if (bi_putstr(si->ib, si->applet.ctx.cli.msg) != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001461 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001462 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001463 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001464 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001465 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001466 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001467 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001468 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001469 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001470 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001471 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001472 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001473 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001474 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001475 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001476 if (stats_table_request(si, true))
1477 si->applet.st0 = STAT_CLI_PROMPT;
1478 break;
1479 case STAT_CLI_O_CLR:
1480 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001481 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001482 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001483 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001484 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001485 break;
1486 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001487
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001488 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001489 if (si->applet.st0 == STAT_CLI_PROMPT) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001490 if (bi_putstr(si->ib, si->applet.st1 ? "\n> " : "\n") != -1)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001491 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001492 }
1493
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001494 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001495 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001496 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001497
1498 /* Now we close the output if one of the writers did so,
1499 * or if we're not in interactive mode and the request
1500 * buffer is empty. This still allows pipelined requests
1501 * to be sent in non-interactive mode.
1502 */
Willy Tarreau2e046c62012-03-01 16:08:30 +01001503 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->o)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001504 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001505 continue;
1506 }
1507
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001508 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001509 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001510 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001511 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001512
Willy Tarreaubc4af052011-02-13 13:25:14 +01001513 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001514 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1515 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07001516 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001517 * and nothing more to consume. This is comparable to a broken pipe, so
1518 * we forward the close to the request side so that it flows upstream to
1519 * the client.
1520 */
1521 si->shutw(si);
1522 }
1523
Willy Tarreaubc4af052011-02-13 13:25:14 +01001524 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001525 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1526 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1527 /* We have no more processing to do, and nothing more to send, and
1528 * the client side has closed. So we'll forward this state downstream
1529 * on the response buffer.
1530 */
1531 si->shutr(si);
1532 res->flags |= BF_READ_NULL;
1533 }
1534
1535 /* update all other flags and resync with the other side */
1536 si->update(si);
1537
1538 /* we don't want to expire timeouts while we're processing requests */
1539 si->ib->rex = TICK_ETERNITY;
1540 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001541
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001542 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001543 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 +02001544 __FUNCTION__, __LINE__,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001545 si->state, req->flags, res->flags, req->i, req->o, res->i, res->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001546
1547 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1548 /* check that we have released everything then unregister */
1549 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001550 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001551}
1552
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001553/* This function dumps statistics onto the stream interface's read buffer.
1554 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001555 * It returns 0 as long as it does not complete, non-zero upon completion.
1556 * Some states are not used but it makes the code more similar to other
1557 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001558 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001559static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001560{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001561 struct proxy *px;
1562 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001563 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001564
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001565 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001566
Willy Tarreau295a8372011-03-10 11:25:07 +01001567 switch (si->applet.state) {
1568 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001569 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001570 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001571 /* fall through */
1572
Willy Tarreau295a8372011-03-10 11:25:07 +01001573 case STAT_ST_HEAD:
1574 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001575 print_csv_header(&msg);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001576 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001577 return 0;
1578 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001579
Willy Tarreau295a8372011-03-10 11:25:07 +01001580 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001581 /* fall through */
1582
Willy Tarreau295a8372011-03-10 11:25:07 +01001583 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001584 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001585 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001586 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001587 "Name: " PRODUCT_NAME "\n"
1588 "Version: " HAPROXY_VERSION "\n"
1589 "Release_date: " HAPROXY_DATE "\n"
1590 "Nbproc: %d\n"
1591 "Process_num: %d\n"
1592 "Pid: %d\n"
1593 "Uptime: %dd %dh%02dm%02ds\n"
1594 "Uptime_sec: %d\n"
1595 "Memmax_MB: %d\n"
1596 "Ulimit-n: %d\n"
1597 "Maxsock: %d\n"
1598 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001599 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001600 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001601 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001602 "PipesUsed: %d\n"
1603 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001604 "ConnRate: %d\n"
1605 "ConnRateLimit: %d\n"
1606 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001607 "Tasks: %d\n"
1608 "Run_queue: %d\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001609 "Idle_pct: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001610 "node: %s\n"
1611 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001612 "",
1613 global.nbproc,
1614 relative_pid,
1615 pid,
1616 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1617 up,
1618 global.rlimit_memmax,
1619 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001620 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001621 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001622 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau45a12512011-09-10 16:56:42 +02001623 nb_tasks_cur, run_queue_cur, idle_pct,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001624 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001625 );
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001626 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001627 return 0;
1628 }
1629
Willy Tarreau295a8372011-03-10 11:25:07 +01001630 si->applet.ctx.stats.px = proxy;
1631 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1632 si->applet.ctx.stats.sv = NULL;
1633 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001634 /* fall through */
1635
Willy Tarreau295a8372011-03-10 11:25:07 +01001636 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001637 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001638 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1639 while (si->applet.ctx.stats.px) {
1640 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001641 /* skip the disabled proxies and non-networked ones */
1642 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001643 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001644 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001645 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001646 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001647
Willy Tarreau295a8372011-03-10 11:25:07 +01001648 si->applet.ctx.stats.px = px->next;
1649 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001650 }
1651 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001652 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001653
Willy Tarreau295a8372011-03-10 11:25:07 +01001654 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001655 /* fall through */
1656
Willy Tarreau295a8372011-03-10 11:25:07 +01001657 case STAT_ST_END:
1658 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001659 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001660
Willy Tarreau295a8372011-03-10 11:25:07 +01001661 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001662 return 1;
1663
1664 default:
1665 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001666 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001667 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001668 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001669}
1670
1671
Cyril Bonté70be45d2010-10-12 00:14:35 +02001672/* We don't want to land on the posted stats page because a refresh will
1673 * repost the data. We don't want this to happen on accident so we redirect
1674 * the browse to the stats page with a GET.
1675 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001676static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001677{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001678 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001679 struct chunk msg;
1680
1681 chunk_init(&msg, trash, sizeof(trash));
1682
Willy Tarreau295a8372011-03-10 11:25:07 +01001683 switch (si->applet.state) {
1684 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001685 chunk_printf(&msg,
1686 "HTTP/1.0 303 See Other\r\n"
1687 "Cache-Control: no-cache\r\n"
1688 "Content-Type: text/plain\r\n"
1689 "Connection: close\r\n"
1690 "Location: %s;st=%s",
Cyril Bonté19979e12012-04-04 12:57:21 +02001691 uri->uri_prefix,
1692 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
1693 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
1694 stat_status_codes[si->applet.ctx.stats.st_code]) ?
1695 stat_status_codes[si->applet.ctx.stats.st_code] :
1696 stat_status_codes[STAT_STATUS_UNKN]);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001697 chunk_printf(&msg, "\r\n\r\n");
1698
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001699 if (bi_putchk(si->ib, &msg) == -1)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001700 return 0;
1701
1702 s->txn.status = 303;
1703
1704 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1705 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1706 if (!(s->flags & SN_FINST_MASK))
1707 s->flags |= SN_FINST_R;
1708
Willy Tarreau295a8372011-03-10 11:25:07 +01001709 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001710 return 1;
1711 }
1712 return 1;
1713}
1714
1715
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001716/* This I/O handler runs as an applet embedded in a stream interface. It is
1717 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001718 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001719 * automatically unregisters itself once transfer is complete.
1720 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001721static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001722{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001723 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001724 struct buffer *req = si->ob;
1725 struct buffer *res = si->ib;
1726
1727 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1728 goto out;
1729
1730 /* check that the output is not closed */
1731 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001732 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001733
Willy Tarreaubc4af052011-02-13 13:25:14 +01001734 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001735 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001736 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001737 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001738 si->shutw(si);
1739 }
1740 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001741 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001742 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001743 si->shutw(si);
1744 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001745 }
1746 }
1747
1748 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1749 si->shutw(si);
1750
Willy Tarreaubc4af052011-02-13 13:25:14 +01001751 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001752 si->shutr(si);
1753 res->flags |= BF_READ_NULL;
1754 }
1755
1756 /* update all other flags and resync with the other side */
1757 si->update(si);
1758
1759 /* we don't want to expire timeouts while we're processing requests */
1760 si->ib->rex = TICK_ETERNITY;
1761 si->ob->wex = TICK_ETERNITY;
1762
1763 out:
1764 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1765 /* check that we have released everything then unregister */
1766 stream_int_unregister_handler(si);
1767 }
1768}
1769
1770
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001771/* This function dumps statistics in HTTP format onto the stream interface's
1772 * read buffer. The data_ctx must have been zeroed first, and the flags
1773 * properly set. It returns 0 if it had to stop writing data and an I/O is
1774 * needed, 1 if the dump is finished and the session must be closed, or -1
1775 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001776 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001777static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001778{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001779 struct session *s = si->applet.private;
1780 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001781 struct proxy *px;
1782 struct chunk msg;
1783 unsigned int up;
1784
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001785 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001786
Willy Tarreau295a8372011-03-10 11:25:07 +01001787 switch (si->applet.state) {
1788 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001789 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001790 "HTTP/1.0 200 OK\r\n"
1791 "Cache-Control: no-cache\r\n"
1792 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001793 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001794 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001795
Willy Tarreau295a8372011-03-10 11:25:07 +01001796 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001797 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001798 uri->refresh);
1799
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001800 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001801
1802 s->txn.status = 200;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001803 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001804 return 0;
1805
Willy Tarreau91861262007-10-17 17:06:05 +02001806 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1807 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1808 if (!(s->flags & SN_FINST_MASK))
1809 s->flags |= SN_FINST_R;
1810
1811 if (s->txn.meth == HTTP_METH_HEAD) {
1812 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001813 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001814 return 1;
1815 }
1816
Willy Tarreau295a8372011-03-10 11:25:07 +01001817 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001818 /* fall through */
1819
Willy Tarreau295a8372011-03-10 11:25:07 +01001820 case STAT_ST_HEAD:
1821 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001822 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001823 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001824 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1825 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001826 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001827 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1828 "<style type=\"text/css\"><!--\n"
1829 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001830 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001831 " font-size: 12px;"
1832 " font-weight: normal;"
1833 " color: black;"
1834 " background: white;"
1835 "}\n"
1836 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001837 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001838 "}\n"
1839 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001840 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001841 " margin-bottom: 0.5em;"
1842 "}\n"
1843 "h2 {"
1844 " font-family: helvetica, arial;"
1845 " font-size: x-large;"
1846 " font-weight: bold;"
1847 " font-style: italic;"
1848 " color: #6020a0;"
1849 " margin-top: 0em;"
1850 " margin-bottom: 0em;"
1851 "}\n"
1852 "h3 {"
1853 " font-family: helvetica, arial;"
1854 " font-size: 16px;"
1855 " font-weight: bold;"
1856 " color: #b00040;"
1857 " background: #e8e8d0;"
1858 " margin-top: 0em;"
1859 " margin-bottom: 0em;"
1860 "}\n"
1861 "li {"
1862 " margin-top: 0.25em;"
1863 " margin-right: 2em;"
1864 "}\n"
1865 ".hr {margin-top: 0.25em;"
1866 " border-color: black;"
1867 " border-bottom-style: solid;"
1868 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001869 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001870 ".total {background: #20D0D0;color: #ffff80;}\n"
1871 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001872 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001873 ".backend {background: #e8e8d0;}\n"
1874 ".active0 {background: #ff9090;}\n"
1875 ".active1 {background: #ffd020;}\n"
1876 ".active2 {background: #ffffa0;}\n"
1877 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001878 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1879 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1880 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001881 ".backup0 {background: #ff9090;}\n"
1882 ".backup1 {background: #ff80ff;}\n"
1883 ".backup2 {background: #c060ff;}\n"
1884 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001885 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1886 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1887 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001888 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001889 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001890 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001891 "a.px:link {color: #ffff40; text-decoration: none;}"
1892 "a.px:visited {color: #ffff40; text-decoration: none;}"
1893 "a.px:hover {color: #ffffff; text-decoration: none;}"
1894 "a.lfsb:link {color: #000000; text-decoration: none;}"
1895 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1896 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1897 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001898 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001899 "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"
1900 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001901 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001902 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001903 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001904 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001905 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001906 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1907 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1908 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001909 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001910 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001911 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001912 (uri->flags&ST_SHNODE) ? " on " : "",
1913 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001914 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001915 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001916 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001917 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001918 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02001919 return 0;
1920
Willy Tarreau295a8372011-03-10 11:25:07 +01001921 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001922 /* fall through */
1923
Willy Tarreau295a8372011-03-10 11:25:07 +01001924 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001925 up = (now.tv_sec - start_date.tv_sec);
1926
1927 /* WARNING! this has to fit the first packet too.
1928 * We are around 3.5 kB, add adding entries will
1929 * become tricky if we want to support 4kB buffers !
1930 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001931 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001932 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001933 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1934 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001935 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001936 "<hr width=\"100%%\" class=\"hr\">\n"
1937 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001938 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001939 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001940 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001941 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1942 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001943 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Willy Tarreau45a12512011-09-10 16:56:42 +02001944 "Running tasks: %d/%d; idle = %d %%<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001945 "</td><td align=\"center\" nowrap>\n"
1946 "<table class=\"lgd\"><tr>\n"
1947 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1948 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1949 "</tr><tr>\n"
1950 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1951 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1952 "</tr><tr>\n"
1953 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1954 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1955 "</tr><tr>\n"
1956 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001957 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001958 "</tr><tr>\n"
1959 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02001960 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001961 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001962 "</td>"
1963 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1964 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1965 "",
1966 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001967 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1968 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1969 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001970 up / 86400, (up % 86400) / 3600,
1971 (up % 3600) / 60, (up % 60),
1972 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1973 global.rlimit_memmax ? " MB" : "",
1974 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001975 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001976 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau45a12512011-09-10 16:56:42 +02001977 run_queue_cur, nb_tasks_cur, idle_pct
Willy Tarreau91861262007-10-17 17:06:05 +02001978 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001979
Willy Tarreau295a8372011-03-10 11:25:07 +01001980 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001981 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001982 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1983 uri->uri_prefix,
1984 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001985 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001986 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001987 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001988 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1989 uri->uri_prefix,
1990 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001991 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001992
Willy Tarreau55bb8452007-10-17 18:44:57 +02001993 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001994 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001995 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001996 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1997 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001998 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001999 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002000 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002001 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002002 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
2003 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002004 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02002005 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002006 }
Willy Tarreau91861262007-10-17 17:06:05 +02002007
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002008 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002009 "<li><a href=\"%s%s%s\">Refresh now</a><br>\n",
2010 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002011 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2012 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002013
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002014 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002015 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2016 uri->uri_prefix,
2017 (uri->refresh > 0) ? ";norefresh" : "");
2018
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002019 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002020 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002021 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
Adrian Bridgettafdb6e52012-03-19 23:36:42 +00002022 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
Willy Tarreau91861262007-10-17 17:06:05 +02002023 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2024 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2025 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2026 "</ul>"
2027 "</td>"
2028 "</tr></table>\n"
2029 ""
2030 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002031
Willy Tarreau295a8372011-03-10 11:25:07 +01002032 if (si->applet.ctx.stats.st_code) {
Cyril Bonté19979e12012-04-04 12:57:21 +02002033 switch (si->applet.ctx.stats.st_code) {
2034 case STAT_STATUS_DONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002035 chunk_printf(&msg,
2036 "<p><div class=active3>"
2037 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2038 "Action processed successfully."
2039 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002040 break;
2041 case STAT_STATUS_NONE:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002042 chunk_printf(&msg,
2043 "<p><div class=active2>"
2044 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2045 "Nothing has changed."
2046 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002047 break;
2048 case STAT_STATUS_PART:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002049 chunk_printf(&msg,
2050 "<p><div class=active2>"
2051 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2052 "Action partially processed.<br>"
2053 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2054 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002055 break;
2056 case STAT_STATUS_ERRP:
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002057 chunk_printf(&msg,
2058 "<p><div class=active0>"
2059 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2060 "Action not processed because of invalid parameters."
2061 "<ul>"
2062 "<li>The action is maybe unknown.</li>"
2063 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2064 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2065 "</ul>"
2066 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002067 break;
2068 case STAT_STATUS_EXCD:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002069 chunk_printf(&msg,
2070 "<p><div class=active0>"
2071 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2072 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2073 "You should retry with less servers at a time.</b>"
2074 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002075 break;
2076 case STAT_STATUS_DENY:
Cyril Bonté474be412010-10-12 00:14:36 +02002077 chunk_printf(&msg,
2078 "<p><div class=active0>"
2079 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2080 "<b>Action denied.</b>"
2081 "</div>\n", uri->uri_prefix);
Cyril Bonté19979e12012-04-04 12:57:21 +02002082 break;
2083 default:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002084 chunk_printf(&msg,
2085 "<p><div class=active6>"
2086 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2087 "Unexpected result."
2088 "</div>\n", uri->uri_prefix);
2089 }
2090 chunk_printf(&msg,"<p>\n");
2091 }
2092
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002093 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002094 return 0;
2095 }
Willy Tarreau91861262007-10-17 17:06:05 +02002096
Willy Tarreau295a8372011-03-10 11:25:07 +01002097 si->applet.ctx.stats.px = proxy;
2098 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
2099 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002100 /* fall through */
2101
Willy Tarreau295a8372011-03-10 11:25:07 +01002102 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002103 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002104 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002105 if (buffer_almost_full(rep))
2106 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002107 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002108 /* skip the disabled proxies and non-networked ones */
2109 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002110 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002111 return 0;
2112
Willy Tarreau295a8372011-03-10 11:25:07 +01002113 si->applet.ctx.stats.px = px->next;
2114 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002115 }
2116 /* here, we just have reached the last proxy */
2117
Willy Tarreau295a8372011-03-10 11:25:07 +01002118 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002119 /* fall through */
2120
Willy Tarreau295a8372011-03-10 11:25:07 +01002121 case STAT_ST_END:
2122 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002123 chunk_printf(&msg, "</body></html>\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002124 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002125 return 0;
2126 }
Willy Tarreau91861262007-10-17 17:06:05 +02002127
Willy Tarreau295a8372011-03-10 11:25:07 +01002128 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002129 /* fall through */
2130
Willy Tarreau295a8372011-03-10 11:25:07 +01002131 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002132 return 1;
2133
2134 default:
2135 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01002136 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002137 return -1;
2138 }
2139}
2140
2141
2142/*
2143 * Dumps statistics for a proxy.
2144 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2145 * ot non-zero if everything completed.
2146 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002147static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002148{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002149 struct session *s = si->applet.private;
2150 struct buffer *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02002151 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002152 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002153 struct chunk msg;
2154
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002155 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02002156
Willy Tarreau295a8372011-03-10 11:25:07 +01002157 switch (si->applet.ctx.stats.px_st) {
2158 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002159 /* we are on a new proxy */
2160
2161 if (uri && uri->scope) {
2162 /* we have a limited scope, we have to check the proxy name */
2163 struct stat_scope *scope;
2164 int len;
2165
2166 len = strlen(px->id);
2167 scope = uri->scope;
2168
2169 while (scope) {
2170 /* match exact proxy name */
2171 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2172 break;
2173
2174 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002175 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002176 break;
2177 scope = scope->next;
2178 }
2179
2180 /* proxy name not found : don't dump anything */
2181 if (scope == NULL)
2182 return 1;
2183 }
2184
Willy Tarreau295a8372011-03-10 11:25:07 +01002185 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2186 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002187 return 1;
2188
Willy Tarreau295a8372011-03-10 11:25:07 +01002189 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002190 /* fall through */
2191
Willy Tarreau295a8372011-03-10 11:25:07 +01002192 case STAT_PX_ST_TH:
2193 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2194 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002195 /* A form to enable/disable this proxy servers */
2196 chunk_printf(&msg,
2197 "<form action=\"%s\" method=\"post\">",
2198 uri->uri_prefix);
2199 }
2200
Willy Tarreau55bb8452007-10-17 18:44:57 +02002201 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002202 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002203 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002204 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002205 "<th class=\"pxname\" width=\"10%%\"");
2206
2207 if (uri->flags&ST_SHLGNDS) {
2208 /* cap, mode, id */
2209 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2210 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2211 px->uuid);
2212
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002213 chunk_printf(&msg, "\"");
2214 }
2215
2216 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002217 ">%s<a name=\"%s\"></a>"
2218 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002219 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002220 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002221 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002222 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002223 "<tr class=\"titre\">",
2224 (uri->flags & ST_SHLGNDS)?"<u>":"",
2225 px->id, px->id, px->id,
2226 (uri->flags & ST_SHLGNDS)?"</u>":"",
2227 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2228
Willy Tarreau295a8372011-03-10 11:25:07 +01002229 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002230 /* Column heading for Enable or Disable server */
2231 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2232 }
2233
2234 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002235 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002236 "<th colspan=3>Queue</th>"
2237 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002238 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002239 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002240 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002241 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002242 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002243 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002244 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002245 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002246 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002247 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002248 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002249 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2250 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002251 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002252
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002253 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002254 return 0;
2255 }
Willy Tarreau91861262007-10-17 17:06:05 +02002256
Willy Tarreau295a8372011-03-10 11:25:07 +01002257 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002258 /* fall through */
2259
Willy Tarreau295a8372011-03-10 11:25:07 +01002260 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002261 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002262 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002263 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2264 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002265 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002266 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002267 "<tr class=\"frontend\">");
2268
Willy Tarreau295a8372011-03-10 11:25:07 +01002269 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002270 /* Column sub-heading for Enable or Disable server */
2271 chunk_printf(&msg, "<td></td>");
2272 }
2273
2274 chunk_printf(&msg,
2275 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002276 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002277 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2278 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002279 "",
2280 px->id, px->id);
2281
2282 if (px->mode == PR_MODE_HTTP) {
2283 chunk_printf(&msg,
2284 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002285 "<td title=\"Cur: %u req/s\"><u>%s</u></td><td title=\"Max: %u req/s\"><u>%s</u></td><td>%s</td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002286 "",
2287 read_freq_ctr(&px->fe_req_per_sec),
2288 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002289 px->fe_counters.p.http.rps_max,
2290 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002291 LIM2A2(px->fe_sps_lim, "-"));
2292 } else {
2293 chunk_printf(&msg,
2294 /* sessions rate : current, max, limit */
2295 "<td>%s</td><td>%s</td><td>%s</td>"
2296 "",
2297 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002298 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002299 }
2300
2301 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002302 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002303 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002304 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002305 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002306 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002307
2308 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2309 if (px->mode == PR_MODE_HTTP) {
2310 int i;
2311
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002312 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002313
2314 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002315 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002316
Willy Tarreaueabea072011-09-10 23:29:44 +02002317 chunk_printf(&msg, " other=%lld,", px->fe_counters.p.http.rsp[0]);
2318 chunk_printf(&msg, " intercepted=%lld\"", px->fe_counters.intercepted_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002319 }
2320
2321 chunk_printf(&msg,
2322 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002323 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002324 /* bytes : in, out */
2325 "<td>%s</td><td>%s</td>"
2326 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002327 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002328 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002329 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002330 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002331
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002332 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002333 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002334 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002335 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002336 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002337 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002338 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002339 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002340 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002341 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002342 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002343 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002344 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2345 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002346 px->state == PR_STREADY ? "OPEN" :
2347 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002348 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002349 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002350 /* pxid, name, queue cur, queue max, */
2351 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002352 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002353 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002354 /* bytes : in, out */
2355 "%lld,%lld,"
2356 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002357 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002358 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002359 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002360 /* warnings: retries, redispatches */
2361 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002362 /* server status : reflect frontend status */
2363 "%s,"
2364 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002365 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002366 /* pid, iid, sid, throttle, lbtot, tracked, type */
2367 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002368 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002369 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002370 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002371 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002372 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002373 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2374 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2375 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2376 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002377 px->state == PR_STREADY ? "OPEN" :
2378 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002379 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002380 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002381 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002382
2383 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2384 if (px->mode == PR_MODE_HTTP) {
2385 int i;
2386
2387 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002388 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002389
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002390 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002391 } else {
2392 chunk_printf(&msg, ",,,,,,");
2393 }
2394
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002395 /* failed health analyses */
2396 chunk_printf(&msg, ",");
2397
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002398 /* requests : req_rate, req_rate_max, req_tot, */
2399 chunk_printf(&msg, "%u,%u,%lld,",
2400 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002401 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002402
Willy Tarreauae526782010-03-04 20:34:23 +01002403 /* errors: cli_aborts, srv_aborts */
2404 chunk_printf(&msg, ",,");
2405
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002406 /* finish with EOL */
2407 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002408 }
Willy Tarreau91861262007-10-17 17:06:05 +02002409
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002410 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002411 return 0;
2412 }
2413
Willy Tarreau295a8372011-03-10 11:25:07 +01002414 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2415 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002416 /* fall through */
2417
Willy Tarreau295a8372011-03-10 11:25:07 +01002418 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002419 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002420 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002421 if (buffer_almost_full(rep))
2422 return 0;
2423
Willy Tarreau295a8372011-03-10 11:25:07 +01002424 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002425 if (!l->counters)
2426 continue;
2427
Willy Tarreau295a8372011-03-10 11:25:07 +01002428 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2429 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002430 break;
2431
Willy Tarreau295a8372011-03-10 11:25:07 +01002432 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002433 continue;
2434 }
2435
Willy Tarreau295a8372011-03-10 11:25:07 +01002436 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002437 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002438 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002439 /* Column sub-heading for Enable or Disable server */
2440 chunk_printf(&msg, "<td></td>");
2441 }
2442 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002443
2444 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002445 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002446 int port;
2447
Willy Tarreau631f01c2011-09-05 00:36:48 +02002448 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002449
Willy Tarreau631f01c2011-09-05 00:36:48 +02002450 port = get_host_port(&l->addr);
2451 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2452 case AF_INET:
2453 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2454 break;
2455 case AF_INET6:
2456 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2457 break;
2458 case AF_UNIX:
2459 chunk_printf(&msg, "unix, ");
2460 break;
2461 case -1:
2462 chunk_printf(&msg, "(%s), ", strerror(errno));
2463 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002464 }
2465
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002466 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002467 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002468 }
2469
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002470 chunk_printf(&msg,
2471 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002472 ">%s<a name=\"%s/+%s\"></a>"
2473 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002474 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002475 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002476 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002477 "<td>%s</td><td>%s</td><td>%s</td>"
2478 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002479 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002480 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002481 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002482 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002483 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002484 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002485 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2486 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2487
2488 chunk_printf(&msg,
2489 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002490 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002491 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002492 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002493 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002494 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002495 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002496 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002497 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002498 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002499 "",
2500 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2501 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002502 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002503 } else {
2504 chunk_printf(&msg,
2505 /* pxid, name, queue cur, queue max, */
2506 "%s,%s,,,"
2507 /* sessions: current, max, limit, total */
2508 "%d,%d,%d,%lld,"
2509 /* bytes: in, out */
2510 "%lld,%lld,"
2511 /* denied: req, resp */
2512 "%lld,%lld,"
2513 /* errors: request, connect, response */
2514 "%lld,,,"
2515 /* warnings: retries, redispatches */
2516 ",,"
2517 /* server status: reflect listener status */
2518 "%s,"
2519 /* rest of server: nothing */
2520 ",,,,,,,,"
2521 /* pid, iid, sid, throttle, lbtot, tracked, type */
2522 "%d,%d,%d,,,,%d,"
2523 /* rate, rate_lim, rate_max */
2524 ",,,"
2525 /* check_status, check_code, check_duration */
2526 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002527 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2528 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002529 /* failed health analyses */
2530 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002531 /* requests : req_rate, req_rate_max, req_tot, */
2532 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002533 /* errors: cli_aborts, srv_aborts */
2534 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002535 "\n",
2536 px->id, l->name,
2537 l->nbconn, l->counters->conn_max,
2538 l->maxconn, l->counters->cum_conn,
2539 l->counters->bytes_in, l->counters->bytes_out,
2540 l->counters->denied_req, l->counters->denied_resp,
2541 l->counters->failed_req,
2542 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2543 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2544 }
2545
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002546 if (bi_putchk(rep, &msg) == -1)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002547 return 0;
2548 }
2549
Willy Tarreau295a8372011-03-10 11:25:07 +01002550 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2551 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002552 /* fall through */
2553
Willy Tarreau295a8372011-03-10 11:25:07 +01002554 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002555 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002556 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002557 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 +02002558
Willy Tarreau4e33d862009-10-11 23:35:10 +02002559 if (buffer_almost_full(rep))
2560 return 0;
2561
Willy Tarreau295a8372011-03-10 11:25:07 +01002562 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002563
Willy Tarreau295a8372011-03-10 11:25:07 +01002564 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2565 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002566 break;
2567
Willy Tarreau295a8372011-03-10 11:25:07 +01002568 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002569 continue;
2570 }
2571
Willy Tarreau44267702011-10-28 15:35:33 +02002572 if (sv->track)
2573 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002574 else
2575 svs = sv;
2576
Willy Tarreau91861262007-10-17 17:06:05 +02002577 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002578 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002579 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002580 else if (svs->state & SRV_RUNNING) {
2581 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002582 sv_state = 3; /* UP */
2583 else
2584 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002585
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002586 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002587 sv_state += 2;
2588 }
Willy Tarreau91861262007-10-17 17:06:05 +02002589 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002590 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002591 sv_state = 1; /* going up */
2592 else
2593 sv_state = 0; /* DOWN */
2594
Willy Tarreau295a8372011-03-10 11:25:07 +01002595 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002596 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002597 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002598 continue;
2599 }
2600
Willy Tarreau295a8372011-03-10 11:25:07 +01002601 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002602 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2603 "UP %d/%d &darr;", "UP",
2604 "NOLB %d/%d &darr;", "NOLB",
2605 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002606 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002607 chunk_printf(&msg,
2608 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002609 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002610 );
2611 }
2612 else {
2613 chunk_printf(&msg,
2614 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002615 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002616 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2617 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002618
Willy Tarreau295a8372011-03-10 11:25:07 +01002619 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002620 chunk_printf(&msg,
2621 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2622 sv->id);
2623 }
2624
2625 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002626
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002627 if (uri->flags&ST_SHLGNDS) {
2628 char str[INET6_ADDRSTRLEN];
2629
Willy Tarreau631f01c2011-09-05 00:36:48 +02002630 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002631
Willy Tarreau631f01c2011-09-05 00:36:48 +02002632 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002633 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002634 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002635 break;
2636 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002637 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2638 break;
2639 case AF_UNIX:
2640 chunk_printf(&msg, "unix, ");
2641 break;
2642 case -1:
2643 chunk_printf(&msg, "(%s), ", strerror(errno));
2644 break;
2645 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002646 break;
2647 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002648
2649 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002650 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002651
2652 /* cookie */
2653 if (sv->cookie) {
2654 struct chunk src;
2655
2656 chunk_printf(&msg, ", cookie: '");
2657
2658 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2659 chunk_htmlencode(&msg, &src);
2660
2661 chunk_printf(&msg, "'");
2662 }
2663
2664 chunk_printf(&msg, "\"");
2665 }
2666
2667 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002668 ">%s<a name=\"%s/%s\"></a>"
2669 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002670 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002671 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002672 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002673 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002674 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002675 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002676 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002677 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002678 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002679 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002680 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002681 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2682 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002683 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2684
2685 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2686 if (px->mode == PR_MODE_HTTP) {
2687 int i;
2688
2689 chunk_printf(&msg, " title=\"rsp codes:");
2690
2691 for (i = 1; i < 6; i++)
2692 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2693
2694 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2695 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002696
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002697 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002698 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002699 ">%s%s%s</td><td>%s</td>",
2700 (px->mode == PR_MODE_HTTP)?"<u>":"",
2701 U2H0(sv->counters.cum_sess),
2702 (px->mode == PR_MODE_HTTP)?"</u>":"",
2703 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002704
2705 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002706 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002707 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002708 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002709 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002710 /* errors : request, connect */
2711 "<td></td><td>%s</td>"
2712 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002713 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002714 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002715 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002716 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002717 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2718 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002719 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002720 sv->counters.cli_aborts,
2721 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002722 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002723 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002724
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002725 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002726 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002727
Cyril Bontécd19e512010-01-31 22:34:03 +01002728 if (sv->state & SRV_MAINTAIN) {
2729 chunk_printf(&msg, "%s ",
2730 human_time(now.tv_sec - sv->last_change, 1));
2731 chunk_printf(&msg, "MAINT");
2732 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002733 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2734 chunk_printf(&msg, "%s ",
2735 human_time(now.tv_sec - svs->last_change, 1));
2736 chunk_printf(&msg, "MAINT(via)");
2737 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002738 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002739 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002740 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002741
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002742 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002743 srv_hlt_st[sv_state],
2744 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2745 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002746 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002747
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002748 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002749 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002750 get_check_status_description(sv->check_status));
2751
2752 if (*sv->check_desc) {
2753 struct chunk src;
2754
2755 chunk_printf(&msg, ": ");
2756
2757 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2758 chunk_htmlencode(&msg, &src);
2759 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002760
Willy Tarreaue0454092010-02-26 12:29:07 +01002761 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002762 tv_iszero(&sv->check_start)?"":"* ",
2763 get_check_status_info(sv->check_status));
2764
2765 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002766 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002767
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002768 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002769 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002770 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002771 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002772
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002773 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002774 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002775 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002776 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002777 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002778 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002779 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002780 (sv->state & SRV_BACKUP) ? "-" : "Y",
2781 (sv->state & SRV_BACKUP) ? "Y" : "-");
2782
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002783 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002784 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002785 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002786 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2787
2788 if (svs->observe)
2789 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2790
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002791 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002792 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002793 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002794 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002795 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002796 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002797 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002798 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2799 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002800 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002801 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002802 "<td colspan=3></td>");
2803
2804 /* throttle */
2805 if ((sv->state & SRV_WARMINGUP) &&
2806 now.tv_sec < sv->last_change + sv->slowstart &&
2807 now.tv_sec >= sv->last_change) {
2808 unsigned int ratio;
2809 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002810 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002811 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002812 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002813 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002814 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002815 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002816 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002817 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2818 "UP %d/%d,", "UP,",
2819 "NOLB %d/%d,", "NOLB,",
2820 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002821 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002822 /* pxid, name */
2823 "%s,%s,"
2824 /* queue : current, max */
2825 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002826 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002827 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002828 /* bytes : in, out */
2829 "%lld,%lld,"
2830 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002831 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002832 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002833 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002834 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002835 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002836 "",
2837 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002838 sv->nbpend, sv->counters.nbpend_max,
2839 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002840 sv->counters.bytes_in, sv->counters.bytes_out,
2841 sv->counters.failed_secu,
2842 sv->counters.failed_conns, sv->counters.failed_resp,
2843 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002844
Willy Tarreau55bb8452007-10-17 18:44:57 +02002845 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002846 if (sv->state & SRV_MAINTAIN) {
2847 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002848 }
2849 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2850 chunk_printf(&msg, "MAINT(via),");
2851 }
2852 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002853 chunk_printf(&msg,
2854 srv_hlt_st[sv_state],
2855 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2856 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2857 }
Willy Tarreau91861262007-10-17 17:06:05 +02002858
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002859 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002860 /* weight, active, backup */
2861 "%d,%d,%d,"
2862 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002863 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002864 (sv->state & SRV_BACKUP) ? 0 : 1,
2865 (sv->state & SRV_BACKUP) ? 1 : 0);
2866
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002867 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002868 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002869 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002870 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002871 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002872 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002873 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002874 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002875 ",,,,");
2876
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002877 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002878 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002879 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002880 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002881 LIM2A0(sv->maxqueue, ""),
2882 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002883
2884 /* throttle */
2885 if ((sv->state & SRV_WARMINGUP) &&
2886 now.tv_sec < sv->last_change + sv->slowstart &&
2887 now.tv_sec >= sv->last_change) {
2888 unsigned int ratio;
2889 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002890 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002891 }
2892
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002893 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002894 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002895
2896 /* tracked */
Willy Tarreau44267702011-10-28 15:35:33 +02002897 if (sv->track)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002898 chunk_printf(&msg, "%s/%s,",
Willy Tarreau44267702011-10-28 15:35:33 +02002899 sv->track->proxy->id, sv->track->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002900 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002901 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002902
Willy Tarreau7f062c42009-03-05 18:43:00 +01002903 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002904 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002905
2906 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002907 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002908 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002909 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002910
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002911 if (sv->state & SRV_CHECKED) {
2912 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002913 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002914
2915 /* check_code */
2916 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002917 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002918 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002919 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002920
2921 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002922 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002923 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002924 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002925 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002926
2927 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002928 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002929 }
2930
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002931 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2932 if (px->mode == PR_MODE_HTTP) {
2933 int i;
2934
2935 for (i=1; i<6; i++)
2936 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2937
2938 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2939 } else {
2940 chunk_printf(&msg, ",,,,,,");
2941 }
2942
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002943 /* failed health analyses */
2944 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2945
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002946 /* requests : req_rate, req_rate_max, req_tot, */
2947 chunk_printf(&msg, ",,,");
2948
Willy Tarreauae526782010-03-04 20:34:23 +01002949 /* errors: cli_aborts, srv_aborts */
2950 chunk_printf(&msg, "%lld,%lld,",
2951 sv->counters.cli_aborts, sv->counters.srv_aborts);
2952
Willy Tarreau7f062c42009-03-05 18:43:00 +01002953 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002954 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002955 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002956 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02002957 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002958 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002959
Willy Tarreau295a8372011-03-10 11:25:07 +01002960 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002961 /* fall through */
2962
Willy Tarreau295a8372011-03-10 11:25:07 +01002963 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002964 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002965 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002966 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2967 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002968 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002969 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002970 /* Column sub-heading for Enable or Disable server */
2971 chunk_printf(&msg, "<td></td>");
2972 }
2973 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002974
2975 if (uri->flags&ST_SHLGNDS) {
2976 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002977 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002978 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002979
2980 /* cookie */
2981 if (px->cookie_name) {
2982 struct chunk src;
2983
2984 chunk_printf(&msg, ", cookie: '");
2985
2986 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2987 chunk_htmlencode(&msg, &src);
2988
2989 chunk_printf(&msg, "'");
2990 }
2991
2992 chunk_printf(&msg, "\"");
2993
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002994 }
2995
2996 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002997 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002998 ">%s<a name=\"%s/Backend\"></a>"
2999 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02003000 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003001 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003002 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003003 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02003004 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003005 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02003006 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01003007 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003008 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
3009 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02003010
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003011 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003012 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003013 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003014 "<td"
3015 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003016 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003017
3018 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3019 if (px->mode == PR_MODE_HTTP) {
3020 int i;
3021
3022 chunk_printf(&msg, " title=\"rsp codes:");
3023
3024 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003025 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003026
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003027 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003028 }
3029
3030 chunk_printf(&msg,
3031 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003032 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003033 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003034 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003035 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003036 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003037 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003038 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003039 U2H7(px->be_counters.cum_lbconn),
3040 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003041
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003042 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003043 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003044 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003045 /* errors : request, connect */
3046 "<td></td><td>%s</td>"
3047 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003048 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003049 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003050 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003051 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003052 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003053 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003054 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003055 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3056 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003057 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003058 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3059 U2H2(px->be_counters.failed_conns),
3060 px->be_counters.cli_aborts,
3061 px->be_counters.srv_aborts,
3062 U2H5(px->be_counters.failed_resp),
3063 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003064 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003065 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3066 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003067 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003068 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003069
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003070 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003071 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003072 "<td class=ac>&nbsp;</td><td>%d</td>"
3073 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003074 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003075 "</tr>",
3076 px->down_trans,
3077 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003078 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003079 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003080 /* pxid, name */
3081 "%s,BACKEND,"
3082 /* queue : current, max */
3083 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003084 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003085 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003086 /* bytes : in, out */
3087 "%lld,%lld,"
3088 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003089 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003090 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003091 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003092 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003093 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003094 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003095 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003096 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003097 * active and backups. */
3098 "%s,"
3099 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003100 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003101 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003102 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003103 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003104 /* rate, rate_lim, rate_max, */
3105 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003106 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003107 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003108 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003109 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3110 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3111 px->be_counters.bytes_in, px->be_counters.bytes_out,
3112 px->be_counters.denied_req, px->be_counters.denied_resp,
3113 px->be_counters.failed_conns, px->be_counters.failed_resp,
3114 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003115 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003116 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003117 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003118 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003119 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003120 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003121 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003122 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003123 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003124
3125 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3126 if (px->mode == PR_MODE_HTTP) {
3127 int i;
3128
3129 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003130 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003131
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003132 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003133 } else {
3134 chunk_printf(&msg, ",,,,,,");
3135 }
3136
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003137 /* failed health analyses */
3138 chunk_printf(&msg, ",");
3139
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003140 /* requests : req_rate, req_rate_max, req_tot, */
3141 chunk_printf(&msg, ",,,");
3142
Willy Tarreauae526782010-03-04 20:34:23 +01003143 /* errors: cli_aborts, srv_aborts */
3144 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003145 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003146
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003147 /* finish with EOL */
3148 chunk_printf(&msg, "\n");
3149
Willy Tarreau55bb8452007-10-17 18:44:57 +02003150 }
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003151 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003152 return 0;
3153 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003154
Willy Tarreau295a8372011-03-10 11:25:07 +01003155 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003156 /* fall through */
3157
Willy Tarreau295a8372011-03-10 11:25:07 +01003158 case STAT_PX_ST_END:
3159 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003160 chunk_printf(&msg, "</table>");
3161
Willy Tarreau295a8372011-03-10 11:25:07 +01003162 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003163 /* close the form used to enable/disable this proxy servers */
3164 chunk_printf(&msg,
3165 "Choose the action to perform on the checked servers : "
3166 "<select name=action>"
3167 "<option value=\"\"></option>"
3168 "<option value=\"disable\">Disable</option>"
3169 "<option value=\"enable\">Enable</option>"
3170 "</select>"
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003171 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
Cyril Bonté70be45d2010-10-12 00:14:35 +02003172 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3173 "</form>",
Cyril Bontéaa0a45d2012-04-04 12:57:20 +02003174 px->uuid);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003175 }
3176
3177 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003178
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003179 if (bi_putchk(rep, &msg) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003180 return 0;
3181 }
Willy Tarreau91861262007-10-17 17:06:05 +02003182
Willy Tarreau295a8372011-03-10 11:25:07 +01003183 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003184 /* fall through */
3185
Willy Tarreau295a8372011-03-10 11:25:07 +01003186 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003187 return 1;
3188
3189 default:
3190 /* unknown state, we should put an abort() here ! */
3191 return 1;
3192 }
3193}
3194
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003195/* This function dumps a complete session state onto the stream intreface's
3196 * read buffer. The data_ctx must have been zeroed first, and the flags
3197 * properly set. The session has to be set in data_ctx.sess.target. It returns
3198 * 0 if the output buffer is full and it needs to be called again, otherwise
3199 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003200 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003201static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003202{
3203 struct tm tm;
3204 struct chunk msg;
3205 struct session *sess;
3206 extern const char *monthname[12];
3207 char pn[INET6_ADDRSTRLEN];
3208
3209 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01003210 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003211
Willy Tarreau295a8372011-03-10 11:25:07 +01003212 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003213 /* session changed, no need to go any further */
3214 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003215 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003216 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003217 si->applet.ctx.sess.target = NULL;
3218 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003219 return 1;
3220 }
3221
Willy Tarreau295a8372011-03-10 11:25:07 +01003222 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003223 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003224 si->applet.ctx.sess.uid = sess->uniq_id;
3225 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003226 /* fall through */
3227
3228 case 1:
3229 chunk_printf(&msg,
3230 "%p: id=%u, proto=%s",
3231 sess,
3232 sess->uniq_id,
3233 sess->listener->proto->name);
3234
Willy Tarreau6471afb2011-09-23 10:54:59 +02003235 switch (addr_to_str(&sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003236 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003237 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003238 chunk_printf(&msg, " source=%s:%d\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02003239 pn, get_host_port(&sess->si[0].addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003240 break;
3241 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003242 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003243 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003244 default:
3245 /* no more information to print right now */
3246 chunk_printf(&msg, "\n");
3247 break;
3248 }
3249
3250 chunk_printf(&msg,
3251 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003252 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003253
3254 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003255 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003256 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3257 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3258 sess->listener ? sess->listener->luid : 0);
3259
Daniel Schultze90690c72012-03-23 10:53:36 -07003260 stream_sock_get_to_addr(&sess->si[0]);
3261 switch (addr_to_str(&sess->si[0].addr.to, pn, sizeof(pn))) {
3262 case AF_INET:
3263 case AF_INET6:
3264 chunk_printf(&msg, " addr=%s:%d\n",
3265 pn, get_host_port(&sess->si[0].addr.to));
3266 break;
3267 case AF_UNIX:
3268 chunk_printf(&msg, " addr=unix:%d\n", sess->listener->luid);
3269 break;
3270 default:
3271 /* no more information to print right now */
3272 chunk_printf(&msg, "\n");
3273 break;
3274 }
3275
Willy Tarreau50943332011-09-02 17:33:05 +02003276 if (sess->be->cap & PR_CAP_BE)
3277 chunk_printf(&msg,
Daniel Schultze90690c72012-03-23 10:53:36 -07003278 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02003279 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07003280 sess->be->uuid, sess->be->mode ? "http" : "tcp");
3281 else
3282 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-)");
3283
3284 stream_sock_get_from_addr(&sess->si[1]);
3285 switch (addr_to_str(&sess->si[1].addr.from, pn, sizeof(pn))) {
3286 case AF_INET:
3287 case AF_INET6:
3288 chunk_printf(&msg, " addr=%s:%d\n",
3289 pn, get_host_port(&sess->si[1].addr.from));
3290 break;
3291 case AF_UNIX:
3292 chunk_printf(&msg, " addr=unix\n");
3293 break;
3294 default:
3295 /* no more information to print right now */
3296 chunk_printf(&msg, "\n");
3297 break;
3298 }
3299
3300 if (sess->be->cap & PR_CAP_BE)
3301 chunk_printf(&msg,
3302 " server=%s (id=%u)",
Willy Tarreau50943332011-09-02 17:33:05 +02003303 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3304 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3305 else
Daniel Schultze90690c72012-03-23 10:53:36 -07003306 chunk_printf(&msg, " server=<NONE> (id=-1)");
3307
3308 stream_sock_get_to_addr(&sess->si[1]);
3309 switch (addr_to_str(&sess->si[1].addr.to, pn, sizeof(pn))) {
3310 case AF_INET:
3311 case AF_INET6:
3312 chunk_printf(&msg, " addr=%s:%d\n",
3313 pn, get_host_port(&sess->si[1].addr.to));
3314 break;
3315 case AF_UNIX:
3316 chunk_printf(&msg, " addr=unix\n");
3317 break;
3318 default:
3319 /* no more information to print right now */
3320 chunk_printf(&msg, "\n");
3321 break;
3322 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003323
3324 chunk_printf(&msg,
3325 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3326 sess->task,
3327 sess->task->state,
3328 sess->task->nice, sess->task->calls,
3329 sess->task->expire ?
3330 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3331 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3332 TICKS_TO_MS(1000)) : "<NEVER>",
3333 task_in_rq(sess->task) ? ", running" : "");
3334
3335 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3336 chunk_printf(&msg,
3337 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3338 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3339 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3340 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3341
3342 chunk_printf(&msg,
3343 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3344 &sess->si[0],
3345 sess->si[0].state,
3346 sess->si[0].flags,
3347 sess->si[0].fd,
3348 sess->si[0].exp ?
3349 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3350 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3351 TICKS_TO_MS(1000)) : "<NEVER>",
3352 sess->si[0].err_type);
3353
3354 chunk_printf(&msg,
3355 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3356 &sess->si[1],
3357 sess->si[1].state,
3358 sess->si[1].flags,
3359 sess->si[1].fd,
3360 sess->si[1].exp ?
3361 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3362 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3363 TICKS_TO_MS(1000)) : "<NEVER>",
3364 sess->si[1].err_type);
3365
3366 chunk_printf(&msg,
3367 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3368 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3369 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3370
3371
3372 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003373 " req=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003374 " an_exp=%s",
3375 sess->req,
3376 sess->req->flags, sess->req->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003377 sess->req->i, sess->req->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003378 sess->req->pipe ? sess->req->pipe->data : 0,
3379 sess->req->to_forward,
3380 sess->req->analyse_exp ?
3381 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3382 TICKS_TO_MS(1000)) : "<NEVER>");
3383
3384 chunk_printf(&msg,
3385 " rex=%s",
3386 sess->req->rex ?
3387 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3388 TICKS_TO_MS(1000)) : "<NEVER>");
3389
3390 chunk_printf(&msg,
3391 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003392 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003393 sess->req->wex ?
3394 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3395 TICKS_TO_MS(1000)) : "<NEVER>",
3396 sess->req->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003397 (int)(sess->req->p - sess->req->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003398 sess->txn.req.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003399 sess->req->total);
3400
3401 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003402 " res=%p (f=0x%06x an=0x%x i=%d o=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003403 " an_exp=%s",
3404 sess->rep,
3405 sess->rep->flags, sess->rep->analysers,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003406 sess->rep->i, sess->rep->o,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003407 sess->rep->pipe ? sess->rep->pipe->data : 0,
3408 sess->rep->to_forward,
3409 sess->rep->analyse_exp ?
3410 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3411 TICKS_TO_MS(1000)) : "<NEVER>");
3412
3413 chunk_printf(&msg,
3414 " rex=%s",
3415 sess->rep->rex ?
3416 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3417 TICKS_TO_MS(1000)) : "<NEVER>");
3418
3419 chunk_printf(&msg,
3420 " wex=%s\n"
Willy Tarreaua458b672012-03-05 11:17:50 +01003421 " data=%p p=%d next=%d total=%lld\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003422 sess->rep->wex ?
3423 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3424 TICKS_TO_MS(1000)) : "<NEVER>",
3425 sess->rep->data,
Willy Tarreau89fa7062012-03-02 16:13:16 +01003426 (int)(sess->rep->p - sess->rep->data),
Willy Tarreaua458b672012-03-05 11:17:50 +01003427 sess->txn.rsp.next,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003428 sess->rep->total);
3429
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003430 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003431 return 0;
3432
3433 /* use other states to dump the contents */
3434 }
3435 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003436 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003437 return 1;
3438}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003439
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003440/* This function dumps all sessions' states onto the stream intreface's
3441 * read buffer. The data_ctx must have been zeroed first, and the flags
3442 * properly set. It returns 0 if the output buffer is full and it needs
3443 * to be called again, otherwise non-zero. It is designed to be called
3444 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003445 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003446static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003447{
3448 struct chunk msg;
3449
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003450 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003451 /* If we're forced to shut down, we might have to remove our
3452 * reference to the last session being dumped.
3453 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003454 if (si->applet.state == STAT_ST_LIST) {
3455 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3456 LIST_DEL(&si->applet.ctx.sess.bref.users);
3457 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003458 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003459 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003460 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003461 }
3462
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003463 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003464
Willy Tarreau295a8372011-03-10 11:25:07 +01003465 switch (si->applet.state) {
3466 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003467 /* the function had not been called yet, let's prepare the
3468 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003469 * pointer to the first in the global list. When a target
3470 * session is being destroyed, it is responsible for updating
3471 * this pointer. We know we have reached the end when this
3472 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003473 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003474 LIST_INIT(&si->applet.ctx.sess.bref.users);
3475 si->applet.ctx.sess.bref.ref = sessions.n;
3476 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003477 /* fall through */
3478
Willy Tarreau295a8372011-03-10 11:25:07 +01003479 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003480 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003481 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3482 LIST_DEL(&si->applet.ctx.sess.bref.users);
3483 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003484 }
3485
3486 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003487 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003488 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003489 struct session *curr_sess;
3490
Willy Tarreau295a8372011-03-10 11:25:07 +01003491 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003492
Willy Tarreau295a8372011-03-10 11:25:07 +01003493 if (si->applet.ctx.sess.target) {
3494 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003495 goto next_sess;
3496
Willy Tarreau295a8372011-03-10 11:25:07 +01003497 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003498 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003499 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003500 return 0;
3501
3502 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003503 LIST_DEL(&si->applet.ctx.sess.bref.users);
3504 LIST_INIT(&si->applet.ctx.sess.bref.users);
3505 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003506 break;
3507 }
3508
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003509 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003510 "%p: proto=%s",
3511 curr_sess,
3512 curr_sess->listener->proto->name);
3513
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003514
Willy Tarreau6471afb2011-09-23 10:54:59 +02003515 switch (addr_to_str(&curr_sess->si[0].addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02003516 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003517 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003518 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003519 " src=%s:%d fe=%s be=%s srv=%s",
3520 pn,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003521 get_host_port(&curr_sess->si[0].addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003522 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003523 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003524 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003525 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003526 break;
3527 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003528 chunk_printf(&msg,
3529 " src=unix:%d fe=%s be=%s srv=%s",
3530 curr_sess->listener->luid,
3531 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003532 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003533 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003534 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003535 break;
3536 }
3537
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003538 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003539 " ts=%02x age=%s calls=%d",
3540 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003541 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3542 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003543
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003544 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003545 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003546 curr_sess->req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003547 curr_sess->req->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003548 curr_sess->req->analysers,
3549 curr_sess->req->rex ?
3550 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3551 TICKS_TO_MS(1000)) : "");
3552
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003553 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003554 ",wx=%s",
3555 curr_sess->req->wex ?
3556 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3557 TICKS_TO_MS(1000)) : "");
3558
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003559 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003560 ",ax=%s]",
3561 curr_sess->req->analyse_exp ?
3562 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3563 TICKS_TO_MS(1000)) : "");
3564
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003565 chunk_printf(&msg,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003566 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003567 curr_sess->rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003568 curr_sess->rep->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003569 curr_sess->rep->analysers,
3570 curr_sess->rep->rex ?
3571 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3572 TICKS_TO_MS(1000)) : "");
3573
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003574 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003575 ",wx=%s",
3576 curr_sess->rep->wex ?
3577 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3578 TICKS_TO_MS(1000)) : "");
3579
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003580 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003581 ",ax=%s]",
3582 curr_sess->rep->analyse_exp ?
3583 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3584 TICKS_TO_MS(1000)) : "");
3585
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003586 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003587 " s0=[%d,%1xh,fd=%d,ex=%s]",
3588 curr_sess->si[0].state,
3589 curr_sess->si[0].flags,
3590 curr_sess->si[0].fd,
3591 curr_sess->si[0].exp ?
3592 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3593 TICKS_TO_MS(1000)) : "");
3594
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003595 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003596 " s1=[%d,%1xh,fd=%d,ex=%s]",
3597 curr_sess->si[1].state,
3598 curr_sess->si[1].flags,
3599 curr_sess->si[1].fd,
3600 curr_sess->si[1].exp ?
3601 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3602 TICKS_TO_MS(1000)) : "");
3603
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003604 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003605 " exp=%s",
3606 curr_sess->task->expire ?
3607 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3608 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003609 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003610 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003611
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003612 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003613
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003614 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003615 /* let's try again later from this session. We add ourselves into
3616 * this session's users so that it can remove us upon termination.
3617 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003618 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003619 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003620 }
3621
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003622 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003623 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003624 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003625
Willy Tarreau295a8372011-03-10 11:25:07 +01003626 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003627 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003628 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003629 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3630 else
3631 chunk_printf(&msg, "Session not found.\n");
3632
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003633 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003634 return 0;
3635
Willy Tarreau295a8372011-03-10 11:25:07 +01003636 si->applet.ctx.sess.target = NULL;
3637 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003638 return 1;
3639 }
3640
Willy Tarreau295a8372011-03-10 11:25:07 +01003641 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003642 /* fall through */
3643
3644 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003645 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003646 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003647 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003648}
3649
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003650/* This function dumps all tables' states onto the stream intreface's
3651 * read buffer. The data_ctx must have been zeroed first, and the flags
3652 * properly set. It returns 0 if the output buffer is full and it needs
3653 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003654 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003655static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003656{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003657 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003658 struct chunk msg;
3659 struct ebmb_node *eb;
3660 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003661 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003662
3663 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003664 * We have 3 possible states in si->applet.state :
3665 * - STAT_ST_INIT : the first call
3666 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003667 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003668 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003669 * and the entry pointer points to the next entry to be dumped,
3670 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003671 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003672 * data though.
3673 */
3674
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003675 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003676 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003677 if (si->applet.state == STAT_ST_LIST) {
3678 si->applet.ctx.table.entry->ref_cnt--;
3679 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003680 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003681 return 1;
3682 }
3683
3684 chunk_init(&msg, trash, sizeof(trash));
3685
Willy Tarreau295a8372011-03-10 11:25:07 +01003686 while (si->applet.state != STAT_ST_FIN) {
3687 switch (si->applet.state) {
3688 case STAT_ST_INIT:
3689 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3690 if (!si->applet.ctx.table.proxy)
3691 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003692
Willy Tarreau295a8372011-03-10 11:25:07 +01003693 si->applet.ctx.table.entry = NULL;
3694 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003695 break;
3696
Willy Tarreau295a8372011-03-10 11:25:07 +01003697 case STAT_ST_INFO:
3698 if (!si->applet.ctx.table.proxy ||
3699 (si->applet.ctx.table.target &&
3700 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3701 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003702 break;
3703 }
3704
Willy Tarreau295a8372011-03-10 11:25:07 +01003705 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003706 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3707 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003708 return 0;
3709
Willy Tarreau295a8372011-03-10 11:25:07 +01003710 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003711 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3712 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003713 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003714 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003715 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3716 si->applet.ctx.table.entry->ref_cnt++;
3717 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003718 break;
3719 }
3720 }
3721 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003722 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003723 break;
3724
Willy Tarreau295a8372011-03-10 11:25:07 +01003725 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003726 skip_entry = false;
3727
Willy Tarreau295a8372011-03-10 11:25:07 +01003728 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003729 /* we're filtering on some data contents */
3730 void *ptr;
3731 long long data;
3732
Willy Tarreau295a8372011-03-10 11:25:07 +01003733 dt = si->applet.ctx.table.data_type;
3734 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3735 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003736 dt);
3737
3738 data = 0;
3739 switch (stktable_data_types[dt].std_type) {
3740 case STD_T_SINT:
3741 data = stktable_data_cast(ptr, std_t_sint);
3742 break;
3743 case STD_T_UINT:
3744 data = stktable_data_cast(ptr, std_t_uint);
3745 break;
3746 case STD_T_ULL:
3747 data = stktable_data_cast(ptr, std_t_ull);
3748 break;
3749 case STD_T_FRQP:
3750 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003751 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003752 break;
3753 }
3754
3755 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003756 if ((data < si->applet.ctx.table.value &&
3757 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3758 si->applet.ctx.table.data_op == STD_OP_GT ||
3759 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3760 (data == si->applet.ctx.table.value &&
3761 (si->applet.ctx.table.data_op == STD_OP_NE ||
3762 si->applet.ctx.table.data_op == STD_OP_GT ||
3763 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3764 (data > si->applet.ctx.table.value &&
3765 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3766 si->applet.ctx.table.data_op == STD_OP_LT ||
3767 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003768 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003769 }
3770
Simon Hormanc88b8872011-06-15 15:18:49 +09003771 if (show && !skip_entry &&
3772 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003773 si->applet.ctx.table.entry))
3774 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003775
Willy Tarreau295a8372011-03-10 11:25:07 +01003776 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003777
Willy Tarreau295a8372011-03-10 11:25:07 +01003778 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003779 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003780 struct stksess *old = si->applet.ctx.table.entry;
3781 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01003782 if (show)
3783 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3784 else
3785 stksess_kill(&si->applet.ctx.table.proxy->table, old);
Willy Tarreau295a8372011-03-10 11:25:07 +01003786 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003787 break;
3788 }
3789
Simon Hormanc88b8872011-06-15 15:18:49 +09003790
3791 if (show)
3792 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3793 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3794 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3795
Willy Tarreau295a8372011-03-10 11:25:07 +01003796 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3797 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003798 break;
3799
Willy Tarreau295a8372011-03-10 11:25:07 +01003800 case STAT_ST_END:
3801 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003802 break;
3803 }
3804 }
3805 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003806}
3807
Willy Tarreaud426a182010-03-05 14:58:26 +01003808/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003809 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3810 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3811 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3812 * lines are respected within the limit of 70 output chars. Lines that are
3813 * continuation of a previous truncated line begin with "+" instead of " "
3814 * after the offset. The new pointer is returned.
3815 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003816static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3817 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003818{
3819 int end;
3820 unsigned char c;
3821
3822 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003823 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003824 return ptr;
3825
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003826 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003827
Willy Tarreaud426a182010-03-05 14:58:26 +01003828 while (ptr < len && ptr < bsize) {
3829 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003830 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003831 if (out->len > end - 2)
3832 break;
3833 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003834 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003835 if (out->len > end - 3)
3836 break;
3837 out->str[out->len++] = '\\';
3838 switch (c) {
3839 case '\t': c = 't'; break;
3840 case '\n': c = 'n'; break;
3841 case '\r': c = 'r'; break;
3842 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003843 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003844 }
3845 out->str[out->len++] = c;
3846 } else {
3847 if (out->len > end - 5)
3848 break;
3849 out->str[out->len++] = '\\';
3850 out->str[out->len++] = 'x';
3851 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3852 out->str[out->len++] = hextab[c & 0xF];
3853 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003854 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003855 /* we had a line break, let's return now */
3856 out->str[out->len++] = '\n';
3857 *line = ptr;
3858 return ptr;
3859 }
3860 }
3861 /* we have an incomplete line, we return it as-is */
3862 out->str[out->len++] = '\n';
3863 return ptr;
3864}
3865
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003866/* This function dumps all captured errors onto the stream intreface's
3867 * read buffer. The data_ctx must have been zeroed first, and the flags
3868 * properly set. It returns 0 if the output buffer is full and it needs
3869 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003870 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003871static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003872{
3873 extern const char *monthname[12];
3874 struct chunk msg;
3875
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003876 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003877 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003878
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003879 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003880
Willy Tarreau295a8372011-03-10 11:25:07 +01003881 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003882 /* the function had not been called yet, let's prepare the
3883 * buffer for a response.
3884 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003885 struct tm tm;
3886
3887 get_localtime(date.tv_sec, &tm);
3888 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3889 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3890 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3891 error_snapshot_id);
3892
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003893 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003894 /* Socket buffer full. Let's try again later from the same point */
3895 return 0;
3896 }
3897
Willy Tarreau295a8372011-03-10 11:25:07 +01003898 si->applet.ctx.errors.px = proxy;
3899 si->applet.ctx.errors.buf = 0;
3900 si->applet.ctx.errors.bol = 0;
3901 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003902 }
3903
3904 /* we have two inner loops here, one for the proxy, the other one for
3905 * the buffer.
3906 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003907 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003908 struct error_snapshot *es;
3909
Willy Tarreau295a8372011-03-10 11:25:07 +01003910 if (si->applet.ctx.errors.buf == 0)
3911 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003912 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003913 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003914
3915 if (!es->when.tv_sec)
3916 goto next;
3917
Willy Tarreau295a8372011-03-10 11:25:07 +01003918 if (si->applet.ctx.errors.iid >= 0 &&
3919 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3920 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003921 goto next;
3922
Willy Tarreau295a8372011-03-10 11:25:07 +01003923 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003924 /* just print headers now */
3925
3926 char pn[INET6_ADDRSTRLEN];
3927 struct tm tm;
3928
3929 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003930 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003931 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003932 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003933
Willy Tarreau631f01c2011-09-05 00:36:48 +02003934 addr_to_str(&es->src, pn, sizeof(pn));
Willy Tarreau295a8372011-03-10 11:25:07 +01003935 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003936 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003937 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003938 " frontend %s (#%d): invalid request\n"
3939 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003940 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003941 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003942 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003943 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3944 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003945 es->srv ? es->srv->id : "<NONE>",
3946 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003947 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003948 es->len, es->pos);
3949 break;
3950 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003951 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003952 " backend %s (#%d) : invalid response\n"
3953 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003954 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003955 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003956 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003957 pn, es->sid, es->oe->id, es->oe->uuid,
3958 es->srv ? es->srv->id : "<NONE>",
3959 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003960 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003961 es->len, es->pos);
3962 break;
3963 }
3964
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003965 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003966 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003967 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003968 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003969 si->applet.ctx.errors.ptr = 0;
3970 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003971 }
3972
Willy Tarreau295a8372011-03-10 11:25:07 +01003973 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003974 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003975 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003976 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003977 if (bi_putchk(si->ib, &msg) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02003978 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003979 goto next;
3980 }
3981
3982 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003983 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003984 int newptr;
3985 int newline;
3986
Willy Tarreau295a8372011-03-10 11:25:07 +01003987 newline = si->applet.ctx.errors.bol;
3988 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3989 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003990 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003991
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003992 if (bi_putchk(si->ib, &msg) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003993 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003994 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003995 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003996 si->applet.ctx.errors.ptr = newptr;
3997 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003998 };
3999 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01004000 si->applet.ctx.errors.bol = 0;
4001 si->applet.ctx.errors.ptr = -1;
4002 si->applet.ctx.errors.buf++;
4003 if (si->applet.ctx.errors.buf > 1) {
4004 si->applet.ctx.errors.buf = 0;
4005 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004006 }
4007 }
4008
4009 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02004010 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01004011}
4012
Willy Tarreaub24281b2011-02-13 13:16:36 +01004013struct si_applet http_stats_applet = {
4014 .name = "<STATS>", /* used for logging */
4015 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004016 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004017};
4018
Simon Horman9bd2c732011-06-15 15:18:44 +09004019static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01004020 .name = "<CLI>", /* used for logging */
4021 .fct = cli_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07004022 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01004023};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004024
Willy Tarreau10522fd2008-07-09 20:12:41 +02004025static struct cfg_kw_list cfg_kws = {{ },{
4026 { CFG_GLOBAL, "stats", stats_parse_global },
4027 { 0, NULL, NULL },
4028}};
4029
4030__attribute__((constructor))
4031static void __dumpstats_module_init(void)
4032{
4033 cfg_register_keywords(&cfg_kws);
4034}
4035
Willy Tarreau91861262007-10-17 17:06:05 +02004036/*
4037 * Local variables:
4038 * c-indent-level: 8
4039 * c-basic-offset: 8
4040 * End:
4041 */