blob: 5dcd8c4b7285b39a6ce3e21732fc7d14e4ba8968 [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>
Simon Hormancec9a222011-06-15 15:18:51 +090018#include <stdint.h>
Willy Tarreau91861262007-10-17 17:06:05 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020022#include <pwd.h>
23#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020024
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
Willy Tarreau10522fd2008-07-09 20:12:41 +020029#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020030#include <common/compat.h>
31#include <common/config.h>
32#include <common/debug.h>
33#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
40
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
44#include <proto/buffers.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020051#include <proto/protocols.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020052#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020053#include <proto/proxy.h>
Willy Tarreau91861262007-10-17 17:06:05 +020054#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020055#include <proto/server.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010056#include <proto/stream_interface.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020057#include <proto/stream_sock.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010058#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059
Simon Horman9bd2c732011-06-15 15:18:44 +090060static int stats_dump_raw_to_buffer(struct stream_interface *si);
61static int stats_dump_full_sess_to_buffer(struct stream_interface *si);
62static int stats_dump_sess_to_buffer(struct stream_interface *si);
63static int stats_dump_errors_to_buffer(struct stream_interface *si);
Simon Hormanc88b8872011-06-15 15:18:49 +090064static int stats_table_request(struct stream_interface *si, bool show);
Simon Horman9bd2c732011-06-15 15:18:44 +090065static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
66static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri);
67
68static struct si_applet cli_applet;
69
70static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020071 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +020072 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +020073 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +020074 " help : this message\n"
75 " prompt : toggle interactive mode with prompt\n"
76 " quit : disconnect\n"
77 " show info : report information about the running process\n"
78 " show stat : report counters for each proxy and server\n"
79 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +010080 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +020081 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +020082 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +020083 " set weight : change a server's weight\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +010084 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +020085 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +020086 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +020087 " disable : put a server or frontend in maintenance mode\n"
88 " enable : re-enable a server or frontend which is in maintenance mode\n"
89 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +020090 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +020091
Simon Horman9bd2c732011-06-15 15:18:44 +090092static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +020093 "Permission denied\n"
94 "";
95
Willy Tarreau295a8372011-03-10 11:25:07 +010096/* data transmission states for the stats responses */
97enum {
98 STAT_ST_INIT = 0,
99 STAT_ST_HEAD,
100 STAT_ST_INFO,
101 STAT_ST_LIST,
102 STAT_ST_END,
103 STAT_ST_FIN,
104};
105
106/* data transmission states for the stats responses inside a proxy */
107enum {
108 STAT_PX_ST_INIT = 0,
109 STAT_PX_ST_TH,
110 STAT_PX_ST_FE,
111 STAT_PX_ST_LI,
112 STAT_PX_ST_SV,
113 STAT_PX_ST_BE,
114 STAT_PX_ST_END,
115 STAT_PX_ST_FIN,
116};
117
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200118/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaueb472682010-05-28 18:46:57 +0200119 * a new stats socket. It returns a positive value upon success, 0 if the connection
120 * needs to be closed and ignored, or a negative value upon critical failure.
121 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900122static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200123{
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200124 /* we have a dedicated I/O handler for the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100125 stream_int_register_handler(&s->si[1], &cli_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200126 copy_target(&s->target, &s->si[1].target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +0100127 s->si[1].applet.private = s;
128 s->si[1].applet.st1 = 0;
129 s->si[1].applet.st0 = STAT_CLI_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +0200130
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200131 tv_zero(&s->logs.tv_request);
132 s->logs.t_queue = 0;
133 s->logs.t_connect = 0;
134 s->logs.t_data = 0;
135 s->logs.t_close = 0;
136 s->logs.bytes_in = s->logs.bytes_out = 0;
137 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
138 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200139
Willy Tarreaueb472682010-05-28 18:46:57 +0200140 s->req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
141
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200142 if (s->listener->timeout) {
143 s->req->rto = *s->listener->timeout;
144 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200145 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200146 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200147}
148
Willy Tarreau07e9e642010-08-17 21:48:17 +0200149/* allocate a new stats frontend named <name>, and return it
150 * (or NULL in case of lack of memory).
151 */
152static struct proxy *alloc_stats_fe(const char *name)
153{
154 struct proxy *fe;
155
156 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
157 if (!fe)
158 return NULL;
159
Willy Tarreau237250c2011-07-29 01:49:03 +0200160 init_new_proxy(fe);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200161
162 fe->last_change = now.tv_sec;
163 fe->id = strdup("GLOBAL");
164 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200165 fe->maxconn = 10; /* default to 10 concurrent connections */
166 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
167
Willy Tarreau07e9e642010-08-17 21:48:17 +0200168 return fe;
169}
170
Willy Tarreaufbee7132007-10-18 13:53:22 +0200171/* This function parses a "stats" statement in the "global" section. It returns
172 * -1 if there is any error, otherwise zero. If it returns -1, it may write an
173 * error message into ther <err> buffer, for at most <errlen> bytes, trailing
174 * zero included. The trailing '\n' must not be written. The function must be
175 * called with <args> pointing to the first word after "stats".
176 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200177static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
178 struct proxy *defpx, char *err, int errlen)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200179{
Willy Tarreau10522fd2008-07-09 20:12:41 +0200180 args++;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200181 if (!strcmp(args[0], "socket")) {
Andreas Kohn16171e22011-01-19 20:29:32 +0100182 struct sockaddr_un *su;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200183 int cur_arg;
184
185 if (*args[1] == 0) {
186 snprintf(err, errlen, "'stats socket' in global section expects a path to a UNIX socket");
187 return -1;
188 }
189
190 if (global.stats_sock.state != LI_NEW) {
191 snprintf(err, errlen, "'stats socket' already specified in global section");
192 return -1;
193 }
194
Andreas Kohn16171e22011-01-19 20:29:32 +0100195 su = str2sun(args[1]);
196 if (!su) {
197 snprintf(err, errlen, "'stats socket' path would require truncation");
198 return -1;
199 }
200 memcpy(&global.stats_sock.addr, su, sizeof(struct sockaddr_un)); // guaranteed to fit
Willy Tarreaufbee7132007-10-18 13:53:22 +0200201
Willy Tarreau89a63132009-08-16 17:41:45 +0200202 if (!global.stats_fe) {
Willy Tarreau07e9e642010-08-17 21:48:17 +0200203 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
Willy Tarreau89a63132009-08-16 17:41:45 +0200204 snprintf(err, errlen, "out of memory");
205 return -1;
206 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200207 }
208
Willy Tarreaufbee7132007-10-18 13:53:22 +0200209 global.stats_sock.state = LI_INIT;
Willy Tarreau3c63fd82011-09-07 18:00:47 +0200210 global.stats_sock.options = LI_O_UNLIMITED;
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200211 global.stats_sock.accept = session_accept;
212 global.stats_fe->accept = stats_accept;
Willy Tarreau104eb362009-08-16 18:51:29 +0200213 global.stats_sock.handler = process_session;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200214 global.stats_sock.analysers = 0;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200215 global.stats_sock.nice = -64; /* we want to boost priority for local stats */
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 global.stats_sock.frontend = global.stats_fe;
Willy Tarreau6162db22009-10-10 17:13:00 +0200217 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200218 global.stats_sock.maxconn = global.stats_fe->maxconn;
Willy Tarreau89a63132009-08-16 17:41:45 +0200219 global.stats_sock.timeout = &global.stats_fe->timeout.client;
220
221 global.stats_sock.next = global.stats_fe->listen;
222 global.stats_fe->listen = &global.stats_sock;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200223
224 cur_arg = 2;
225 while (*args[cur_arg]) {
226 if (!strcmp(args[cur_arg], "uid")) {
227 global.stats_sock.perm.ux.uid = atol(args[cur_arg + 1]);
228 cur_arg += 2;
229 }
230 else if (!strcmp(args[cur_arg], "gid")) {
231 global.stats_sock.perm.ux.gid = atol(args[cur_arg + 1]);
232 cur_arg += 2;
233 }
234 else if (!strcmp(args[cur_arg], "mode")) {
235 global.stats_sock.perm.ux.mode = strtol(args[cur_arg + 1], NULL, 8);
236 cur_arg += 2;
237 }
238 else if (!strcmp(args[cur_arg], "user")) {
239 struct passwd *user;
240 user = getpwnam(args[cur_arg + 1]);
241 if (!user) {
242 snprintf(err, errlen, "unknown user '%s' in 'global' section ('stats user')",
243 args[cur_arg + 1]);
244 return -1;
245 }
246 global.stats_sock.perm.ux.uid = user->pw_uid;
247 cur_arg += 2;
248 }
249 else if (!strcmp(args[cur_arg], "group")) {
250 struct group *group;
251 group = getgrnam(args[cur_arg + 1]);
252 if (!group) {
253 snprintf(err, errlen, "unknown group '%s' in 'global' section ('stats group')",
254 args[cur_arg + 1]);
255 return -1;
256 }
257 global.stats_sock.perm.ux.gid = group->gr_gid;
258 cur_arg += 2;
259 }
Willy Tarreau6162db22009-10-10 17:13:00 +0200260 else if (!strcmp(args[cur_arg], "level")) {
261 if (!strcmp(args[cur_arg+1], "user"))
262 global.stats_sock.perm.ux.level = ACCESS_LVL_USER;
263 else if (!strcmp(args[cur_arg+1], "operator"))
264 global.stats_sock.perm.ux.level = ACCESS_LVL_OPER;
265 else if (!strcmp(args[cur_arg+1], "admin"))
266 global.stats_sock.perm.ux.level = ACCESS_LVL_ADMIN;
267 else {
268 snprintf(err, errlen, "'stats socket level' only supports 'user', 'operator', and 'admin'");
269 return -1;
270 }
271 cur_arg += 2;
272 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273 else {
Willy Tarreau6162db22009-10-10 17:13:00 +0200274 snprintf(err, errlen, "'stats socket' only supports 'user', 'uid', 'group', 'gid', 'level', and 'mode'");
Willy Tarreaufbee7132007-10-18 13:53:22 +0200275 return -1;
276 }
277 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100278
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 uxst_add_listener(&global.stats_sock);
280 global.maxsock++;
281 }
282 else if (!strcmp(args[0], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100283 unsigned timeout;
284 const char *res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
285
286 if (res) {
287 snprintf(err, errlen, "unexpected character '%c' in 'stats timeout' in 'global' section", *res);
288 return -1;
289 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200290
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100291 if (!timeout) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292 snprintf(err, errlen, "a positive value is expected for 'stats timeout' in 'global section'");
293 return -1;
294 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200295 if (!global.stats_fe) {
296 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
297 snprintf(err, errlen, "out of memory");
298 return -1;
299 }
300 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200301 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 }
303 else if (!strcmp(args[0], "maxconn")) {
304 int maxconn = atol(args[1]);
305
306 if (maxconn <= 0) {
307 snprintf(err, errlen, "a positive value is expected for 'stats maxconn' in 'global section'");
308 return -1;
309 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200310
311 if (!global.stats_fe) {
312 if ((global.stats_fe = alloc_stats_fe("GLOBAL")) == NULL) {
313 snprintf(err, errlen, "out of memory");
314 return -1;
315 }
316 }
317 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200318 }
319 else {
320 snprintf(err, errlen, "'stats' only supports 'socket', 'maxconn' and 'timeout' in 'global' section");
321 return -1;
322 }
323 return 0;
324}
325
Simon Horman9bd2c732011-06-15 15:18:44 +0900326static int print_csv_header(struct chunk *msg)
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100327{
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200328 return chunk_printf(msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100329 "# pxname,svname,"
330 "qcur,qmax,"
331 "scur,smax,slim,stot,"
332 "bin,bout,"
333 "dreq,dresp,"
334 "ereq,econ,eresp,"
335 "wretr,wredis,"
336 "status,weight,act,bck,"
337 "chkfail,chkdown,lastchg,downtime,qlimit,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +0200338 "pid,iid,sid,throttle,lbtot,tracked,type,"
339 "rate,rate_lim,rate_max,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +0200340 "check_status,check_code,check_duration,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100341 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
Willy Tarreauae526782010-03-04 20:34:23 +0100342 "req_rate,req_rate_max,req_tot,"
343 "cli_abrt,srv_abrt,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100344 "\n");
345}
346
Simon Hormand9366582011-06-15 15:18:45 +0900347/* print a string of text buffer to <out>. The format is :
348 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
349 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
350 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
351 */
352static int dump_text(struct chunk *out, const char *buf, int bsize)
353{
354 unsigned char c;
355 int ptr = 0;
356
357 while (buf[ptr] && ptr < bsize) {
358 c = buf[ptr];
359 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
360 if (out->len > out->size - 1)
361 break;
362 out->str[out->len++] = c;
363 }
364 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
365 if (out->len > out->size - 2)
366 break;
367 out->str[out->len++] = '\\';
368 switch (c) {
369 case ' ': c = ' '; break;
370 case '\t': c = 't'; break;
371 case '\n': c = 'n'; break;
372 case '\r': c = 'r'; break;
373 case '\e': c = 'e'; break;
374 case '\\': c = '\\'; break;
375 }
376 out->str[out->len++] = c;
377 }
378 else {
379 if (out->len > out->size - 4)
380 break;
381 out->str[out->len++] = '\\';
382 out->str[out->len++] = 'x';
383 out->str[out->len++] = hextab[(c >> 4) & 0xF];
384 out->str[out->len++] = hextab[c & 0xF];
385 }
386 ptr++;
387 }
388
389 return ptr;
390}
391
392/* print a buffer in hexa.
393 * Print stopped if <bsize> is reached, or if no more place in the chunk.
394 */
395static int dump_binary(struct chunk *out, const char *buf, int bsize)
396{
397 unsigned char c;
398 int ptr = 0;
399
400 while (ptr < bsize) {
401 c = buf[ptr];
402
403 if (out->len > out->size - 2)
404 break;
405 out->str[out->len++] = hextab[(c >> 4) & 0xF];
406 out->str[out->len++] = hextab[c & 0xF];
407
408 ptr++;
409 }
410 return ptr;
411}
412
413/* Dump the status of a table to a stream interface's
414 * read buffer. It returns 0 if the output buffer is full
415 * and needs to be called again, otherwise non-zero.
416 */
417static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
418 struct proxy *proxy, struct proxy *target)
419{
420 struct session *s = si->applet.private;
421
422 chunk_printf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
423 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
424
425 /* any other information should be dumped here */
426
427 if (target && s->listener->perm.ux.level < ACCESS_LVL_OPER)
428 chunk_printf(msg, "# contents not dumped due to insufficient privileges\n");
429
430 if (buffer_feed_chunk(si->ib, msg) >= 0)
431 return 0;
432
433 return 1;
434}
435
436/* Dump the a table entry to a stream interface's
437 * read buffer. It returns 0 if the output buffer is full
438 * and needs to be called again, otherwise non-zero.
439 */
440static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
441 struct proxy *proxy, struct stksess *entry)
442{
443 int dt;
444
445 chunk_printf(msg, "%p:", entry);
446
447 if (proxy->table.type == STKTABLE_TYPE_IP) {
448 char addr[INET_ADDRSTRLEN];
449 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
450 chunk_printf(msg, " key=%s", addr);
451 }
452 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
453 char addr[INET6_ADDRSTRLEN];
454 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
455 chunk_printf(msg, " key=%s", addr);
456 }
457 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
458 chunk_printf(msg, " key=%u", *(unsigned int *)entry->key.key);
459 }
460 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
461 chunk_printf(msg, " key=");
462 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
463 }
464 else {
465 chunk_printf(msg, " key=");
466 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
467 }
468
469 chunk_printf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
470
471 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
472 void *ptr;
473
474 if (proxy->table.data_ofs[dt] == 0)
475 continue;
476 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
477 chunk_printf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
478 else
479 chunk_printf(msg, " %s=", stktable_data_types[dt].name);
480
481 ptr = stktable_data_ptr(&proxy->table, entry, dt);
482 switch (stktable_data_types[dt].std_type) {
483 case STD_T_SINT:
484 chunk_printf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
485 break;
486 case STD_T_UINT:
487 chunk_printf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
488 break;
489 case STD_T_ULL:
490 chunk_printf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
491 break;
492 case STD_T_FRQP:
493 chunk_printf(msg, "%d",
494 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
495 proxy->table.data_arg[dt].u));
496 break;
497 }
498 }
499 chunk_printf(msg, "\n");
500
501 if (buffer_feed_chunk(si->ib, msg) >= 0)
502 return 0;
503
504 return 1;
505}
506
Simon Horman17bce342011-06-15 15:18:47 +0900507static void stats_sock_table_key_request(struct stream_interface *si, char **args, bool show)
Simon Horman121f3052011-06-15 15:18:46 +0900508{
509 struct session *s = si->applet.private;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900510 struct proxy *px = si->applet.ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900511 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900512 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900513 unsigned char ip6_key[sizeof(struct in6_addr)];
Simon Horman121f3052011-06-15 15:18:46 +0900514
Simon Hormand5b9fd92011-06-15 15:18:48 +0900515 si->applet.st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900516
517 if (!*args[4]) {
518 si->applet.ctx.cli.msg = "Key value expected\n";
519 si->applet.st0 = STAT_CLI_PRINT;
520 return;
521 }
522
Simon Hormanc5b89f62011-06-15 15:18:50 +0900523 switch (px->table.type) {
524 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900525 uint32_key = htonl(inetaddr_host(args[4]));
526 static_table_key.key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900527 break;
528 case STKTABLE_TYPE_IPV6:
529 inet_pton(AF_INET6, args[4], ip6_key);
530 static_table_key.key = &ip6_key;
531 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900532 case STKTABLE_TYPE_INTEGER:
533 {
534 char *endptr;
535 unsigned long val;
536 errno = 0;
537 val = strtoul(args[4], &endptr, 10);
538 if ((errno == ERANGE && val == ULONG_MAX) ||
539 (errno != 0 && val == 0) || endptr == args[4] ||
540 val > 0xffffffff) {
541 si->applet.ctx.cli.msg = "Invalid key\n";
542 si->applet.st0 = STAT_CLI_PRINT;
543 return;
544 }
545 uint32_key = (uint32_t) val;
546 static_table_key.key = &uint32_key;
547 break;
548 }
549 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900550 case STKTABLE_TYPE_STRING:
551 static_table_key.key = args[4];
552 static_table_key.key_len = strlen(args[4]);
553 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900554 default:
Simon Horman17bce342011-06-15 15:18:47 +0900555 if (show)
Simon Hormancec9a222011-06-15 15:18:51 +0900556 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 +0900557 else
Simon Hormancec9a222011-06-15 15:18:51 +0900558 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 +0900559 si->applet.st0 = STAT_CLI_PRINT;
560 return;
561 }
562
563 /* check permissions */
564 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
565 si->applet.ctx.cli.msg = stats_permission_denied_msg;
566 si->applet.st0 = STAT_CLI_PRINT;
567 return;
568 }
569
570 ts = stktable_lookup_key(&px->table, &static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900571 if (!ts)
572 return;
573
574 if (show) {
575 struct chunk msg;
576 chunk_init(&msg, trash, sizeof(trash));
577 if (!stats_dump_table_head_to_buffer(&msg, si, px, px))
578 return;
579 stats_dump_table_entry_to_buffer(&msg, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900580 return;
581 }
Simon Horman17bce342011-06-15 15:18:47 +0900582
583 if (ts->ref_cnt) {
Simon Horman121f3052011-06-15 15:18:46 +0900584 /* don't delete an entry which is currently referenced */
585 si->applet.ctx.cli.msg = "Entry currently in use, cannot remove\n";
586 si->applet.st0 = STAT_CLI_PRINT;
587 return;
588 }
589
590 stksess_kill(&px->table, ts);
591}
592
Simon Hormand5b9fd92011-06-15 15:18:48 +0900593static void stats_sock_table_data_request(struct stream_interface *si, char **args)
594{
595 /* condition on stored data value */
596 si->applet.ctx.table.data_type = stktable_get_data_type(args[3] + 5);
597 if (si->applet.ctx.table.data_type < 0) {
598 si->applet.ctx.cli.msg = "Unknown data type\n";
599 si->applet.st0 = STAT_CLI_PRINT;
600 return;
601 }
602
603 if (!((struct proxy *)si->applet.ctx.table.target)->table.data_ofs[si->applet.ctx.table.data_type]) {
604 si->applet.ctx.cli.msg = "Data type not stored in this table\n";
605 si->applet.st0 = STAT_CLI_PRINT;
606 return;
607 }
608
609 si->applet.ctx.table.data_op = get_std_op(args[4]);
610 if (si->applet.ctx.table.data_op < 0) {
611 si->applet.ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
612 si->applet.st0 = STAT_CLI_PRINT;
613 return;
614 }
615
616 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &si->applet.ctx.table.value) != 0) {
617 si->applet.ctx.cli.msg = "Require a valid integer value to compare against\n";
618 si->applet.st0 = STAT_CLI_PRINT;
619 return;
620 }
621}
622
623static void stats_sock_table_request(struct stream_interface *si, char **args, bool show)
624{
625 si->applet.ctx.table.data_type = -1;
626 si->applet.state = STAT_ST_INIT;
Willy Tarreaue17a8d02011-08-24 08:23:34 +0200627 si->applet.ctx.table.target = NULL;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900628 si->applet.ctx.table.proxy = NULL;
629 si->applet.ctx.table.entry = NULL;
Simon Hormanc88b8872011-06-15 15:18:49 +0900630 if (show)
631 si->applet.st0 = STAT_CLI_O_TAB;
632 else
633 si->applet.st0 = STAT_CLI_O_CLR;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900634
635 if (*args[2]) {
636 si->applet.ctx.table.target = find_stktable(args[2]);
637 if (!si->applet.ctx.table.target) {
638 si->applet.ctx.cli.msg = "No such table\n";
639 si->applet.st0 = STAT_CLI_PRINT;
640 return;
641 }
642 }
643 else {
644 if (!show)
645 goto err_args;
646 return;
647 }
648
649 if (strcmp(args[3], "key") == 0)
650 stats_sock_table_key_request(si, args, show);
Simon Hormanc88b8872011-06-15 15:18:49 +0900651 else if (strncmp(args[3], "data.", 5) == 0)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900652 stats_sock_table_data_request(si, args);
Simon Hormanc88b8872011-06-15 15:18:49 +0900653 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900654 goto err_args;
655
656 return;
657
658err_args:
659 if (show)
660 si->applet.ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
661 else
Simon Hormanc88b8872011-06-15 15:18:49 +0900662 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 +0900663 si->applet.st0 = STAT_CLI_PRINT;
664}
665
Willy Tarreau532a4502011-09-07 22:37:44 +0200666/* Expects to find a frontend named <arg> and returns it, otherwise displays various
667 * adequate error messages and returns NULL. This function also expects the session
668 * level to be admin.
669 */
670static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
671{
672 struct proxy *px;
673
674 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
675 si->applet.ctx.cli.msg = stats_permission_denied_msg;
676 si->applet.st0 = STAT_CLI_PRINT;
677 return NULL;
678 }
679
680 if (!*arg) {
681 si->applet.ctx.cli.msg = "A frontend name is expected.\n";
682 si->applet.st0 = STAT_CLI_PRINT;
683 return NULL;
684 }
685
686 px = findproxy(arg, PR_CAP_FE);
687 if (!px) {
688 si->applet.ctx.cli.msg = "No such frontend.\n";
689 si->applet.st0 = STAT_CLI_PRINT;
690 return NULL;
691 }
692 return px;
693}
694
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200695/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
696 * and returns the pointer to the server. Otherwise, display adequate error messages
697 * and returns NULL. This function also expects the session level to be admin. Note:
698 * the <arg> is modified to remove the '/'.
699 */
700static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
701{
702 struct proxy *px;
703 struct server *sv;
704 char *line;
705
706 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
707 si->applet.ctx.cli.msg = stats_permission_denied_msg;
708 si->applet.st0 = STAT_CLI_PRINT;
709 return NULL;
710 }
711
712 /* split "backend/server" and make <line> point to server */
713 for (line = arg; *line; line++)
714 if (*line == '/') {
715 *line++ = '\0';
716 break;
717 }
718
719 if (!*line || !*arg) {
720 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
721 si->applet.st0 = STAT_CLI_PRINT;
722 return NULL;
723 }
724
725 if (!get_backend_server(arg, line, &px, &sv)) {
726 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
727 si->applet.st0 = STAT_CLI_PRINT;
728 return NULL;
729 }
730
731 if (px->state == PR_STSTOPPED) {
732 si->applet.ctx.cli.msg = "Proxy is disabled.\n";
733 si->applet.st0 = STAT_CLI_PRINT;
734 return NULL;
735 }
736
737 return sv;
738}
739
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200740/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200741 * called from an applet running in a stream interface. The function returns 1
Willy Tarreaubc4af052011-02-13 13:25:14 +0100742 * if the request was understood, otherwise zero. It sets si->applet.st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +0200743 * designating the function which will have to process the request, which can
744 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200745 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900746static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200747{
Willy Tarreaubc4af052011-02-13 13:25:14 +0100748 struct session *s = si->applet.private;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200749 char *args[MAX_STATS_ARGS + 1];
750 int arg;
751
752 while (isspace((unsigned char)*line))
753 line++;
754
755 arg = 0;
756 args[arg] = line;
757
758 while (*line && arg < MAX_STATS_ARGS) {
759 if (isspace((unsigned char)*line)) {
760 *line++ = '\0';
761
762 while (isspace((unsigned char)*line))
763 line++;
764
765 args[++arg] = line;
766 continue;
767 }
768
769 line++;
770 }
771
772 while (++arg <= MAX_STATS_ARGS)
773 args[arg] = line;
774
Willy Tarreau295a8372011-03-10 11:25:07 +0100775 si->applet.ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200776 if (strcmp(args[0], "show") == 0) {
777 if (strcmp(args[1], "stat") == 0) {
778 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100779 si->applet.ctx.stats.flags |= STAT_BOUND;
780 si->applet.ctx.stats.iid = atoi(args[2]);
781 si->applet.ctx.stats.type = atoi(args[3]);
782 si->applet.ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200783 }
784
Willy Tarreau295a8372011-03-10 11:25:07 +0100785 si->applet.ctx.stats.flags |= STAT_SHOW_STAT;
786 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
787 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100788 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200789 }
790 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100791 si->applet.ctx.stats.flags |= STAT_SHOW_INFO;
792 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
793 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100794 si->applet.st0 = STAT_CLI_O_INFO; // stats_dump_raw_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200795 }
796 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100797 si->applet.state = STAT_ST_INIT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200798 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100799 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100800 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200801 return 1;
802 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100803 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100804 si->applet.ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100805 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100806 si->applet.ctx.sess.target = NULL;
807 si->applet.ctx.sess.section = 0; /* start with session status */
808 si->applet.ctx.sess.pos = 0;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100809 si->applet.st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200810 }
811 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau6162db22009-10-10 17:13:00 +0200812 if (s->listener->perm.ux.level < ACCESS_LVL_OPER) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100813 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100814 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200815 return 1;
816 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200817 if (*args[2])
Willy Tarreau295a8372011-03-10 11:25:07 +0100818 si->applet.ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200819 else
Willy Tarreau295a8372011-03-10 11:25:07 +0100820 si->applet.ctx.errors.iid = -1;
821 si->applet.ctx.errors.px = NULL;
822 si->applet.state = STAT_ST_INIT;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100823 si->applet.st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200824 }
Willy Tarreau69f58c82010-07-12 17:55:33 +0200825 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900826 stats_sock_table_request(si, args, true);
Willy Tarreau69f58c82010-07-12 17:55:33 +0200827 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200828 else { /* neither "stat" nor "info" nor "sess" nor "errors" no "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200829 return 0;
830 }
831 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200832 else if (strcmp(args[0], "clear") == 0) {
833 if (strcmp(args[1], "counters") == 0) {
834 struct proxy *px;
835 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200836 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200837 int clrall = 0;
838
839 if (strcmp(args[2], "all") == 0)
840 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200841
Willy Tarreau6162db22009-10-10 17:13:00 +0200842 /* check permissions */
843 if (s->listener->perm.ux.level < ACCESS_LVL_OPER ||
844 (clrall && s->listener->perm.ux.level < ACCESS_LVL_ADMIN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100845 si->applet.ctx.cli.msg = stats_permission_denied_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +0100846 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +0200847 return 1;
848 }
849
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200850 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100851 if (clrall) {
852 memset(&px->be_counters, 0, sizeof(px->be_counters));
853 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
854 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200855 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100856 px->be_counters.conn_max = 0;
857 px->be_counters.p.http.rps_max = 0;
858 px->be_counters.sps_max = 0;
859 px->be_counters.cps_max = 0;
860 px->be_counters.nbpend_max = 0;
861
862 px->fe_counters.conn_max = 0;
863 px->fe_counters.p.http.rps_max = 0;
864 px->fe_counters.sps_max = 0;
865 px->fe_counters.cps_max = 0;
866 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200867 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200868
869 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200870 if (clrall)
871 memset(&sv->counters, 0, sizeof(sv->counters));
872 else {
873 sv->counters.cur_sess_max = 0;
874 sv->counters.nbpend_max = 0;
875 sv->counters.sps_max = 0;
876 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200877
878 for (li = px->listen; li; li = li->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200879 if (li->counters) {
880 if (clrall)
881 memset(li->counters, 0, sizeof(*li->counters));
882 else
883 li->counters->conn_max = 0;
884 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200885 }
886
Willy Tarreau81c25d02011-09-07 15:17:21 +0200887 global.cps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200888 return 1;
889 }
Willy Tarreau88ee3972010-07-13 13:48:00 +0200890 else if (strcmp(args[1], "table") == 0) {
Simon Hormand5b9fd92011-06-15 15:18:48 +0900891 stats_sock_table_request(si, args, false);
Willy Tarreau88ee3972010-07-13 13:48:00 +0200892 /* end of processing */
893 return 1;
894 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200895 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +0200896 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200897 return 0;
898 }
899 }
Willy Tarreau38338fa2009-10-10 18:37:29 +0200900 else if (strcmp(args[0], "get") == 0) {
901 if (strcmp(args[1], "weight") == 0) {
902 struct proxy *px;
903 struct server *sv;
904
905 /* split "backend/server" and make <line> point to server */
906 for (line = args[2]; *line; line++)
907 if (*line == '/') {
908 *line++ = '\0';
909 break;
910 }
911
912 if (!*line) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100913 si->applet.ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100914 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200915 return 1;
916 }
917
918 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100919 si->applet.ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100920 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +0200921 return 1;
922 }
923
924 /* return server's effective weight at the moment */
925 snprintf(trash, sizeof(trash), "%d (initial %d)\n", sv->uweight, sv->iweight);
926 buffer_feed(si->ib, trash);
927 return 1;
928 }
929 else { /* not "get weight" */
930 return 0;
931 }
932 }
Willy Tarreau4483d432009-10-10 19:30:08 +0200933 else if (strcmp(args[0], "set") == 0) {
934 if (strcmp(args[1], "weight") == 0) {
935 struct proxy *px;
936 struct server *sv;
937 int w;
938
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200939 sv = expect_server_admin(s, si, args[2]);
940 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +0200941 return 1;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942 px = sv->proxy;
Willy Tarreau4483d432009-10-10 19:30:08 +0200943
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200944 /* if the weight is terminated with '%', it is set relative to
945 * the initial weight, otherwise it is absolute.
946 */
947 if (!*args[3]) {
948 si->applet.ctx.cli.msg = "Require <weight> or <weight%>.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100949 si->applet.st0 = STAT_CLI_PRINT;
Cyril Bonté613f0df2011-03-03 20:49:04 +0100950 return 1;
951 }
952
Willy Tarreau4483d432009-10-10 19:30:08 +0200953 w = atoi(args[3]);
954 if (strchr(args[3], '%') != NULL) {
955 if (w < 0 || w > 100) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100956 si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100957 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200958 return 1;
959 }
960 w = sv->iweight * w / 100;
961 }
962 else {
963 if (w < 0 || w > 256) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100964 si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +0100965 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200966 return 1;
967 }
968 }
969
970 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
Willy Tarreau295a8372011-03-10 11:25:07 +0100971 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 +0100972 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +0200973 return 1;
974 }
975
976 sv->uweight = w;
977
978 if (px->lbprm.algo & BE_LB_PROP_DYN) {
979 /* we must take care of not pushing the server to full throttle during slow starts */
980 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
981 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
982 else
983 sv->eweight = BE_WEIGHT_SCALE;
984 sv->eweight *= sv->uweight;
985 } else {
986 sv->eweight = sv->uweight;
987 }
988
989 /* static LB algorithms are a bit harder to update */
990 if (px->lbprm.update_server_eweight)
991 px->lbprm.update_server_eweight(sv);
992 else if (sv->eweight)
993 px->lbprm.set_server_status_up(sv);
994 else
995 px->lbprm.set_server_status_down(sv);
996
997 return 1;
998 }
Willy Tarreau7aabd112010-01-26 10:59:06 +0100999 else if (strcmp(args[1], "timeout") == 0) {
1000 if (strcmp(args[2], "cli") == 0) {
1001 unsigned timeout;
1002 const char *res;
1003
1004 if (!*args[3]) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001005 si->applet.ctx.cli.msg = "Expects an integer value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001006 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001007 return 1;
1008 }
1009
1010 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1011 if (res || timeout < 1) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001012 si->applet.ctx.cli.msg = "Invalid timeout value.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001013 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001014 return 1;
1015 }
1016
1017 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1018 return 1;
1019 }
1020 else {
Willy Tarreau295a8372011-03-10 11:25:07 +01001021 si->applet.ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
Willy Tarreaubc4af052011-02-13 13:25:14 +01001022 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001023 return 1;
1024 }
1025 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001026 else if (strcmp(args[1], "maxconn") == 0) {
1027 if (strcmp(args[2], "frontend") == 0) {
1028 struct proxy *px;
1029 struct listener *l;
1030 int v;
1031
Willy Tarreau532a4502011-09-07 22:37:44 +02001032 px = expect_frontend_admin(s, si, args[3]);
1033 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001034 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001035
1036 if (!*args[4]) {
1037 si->applet.ctx.cli.msg = "Integer value expected.\n";
1038 si->applet.st0 = STAT_CLI_PRINT;
1039 return 1;
1040 }
1041
1042 v = atoi(args[4]);
1043 /* check for unlimited values, we restore default setting (cfg_maxpconn) */
1044 if (v < 1) {
1045 si->applet.ctx.cli.msg = "Value out of range.\n";
1046 si->applet.st0 = STAT_CLI_PRINT;
1047 return 1;
1048 }
1049
1050 /* OK, the value is fine, so we assign it to the proxy and to all of
1051 * its listeners. The blocked ones will be dequeued.
1052 */
1053 px->maxconn = v;
1054 for (l = px->listen; l != NULL; l = l->next) {
1055 l->maxconn = v;
1056 if (l->state == LI_FULL)
1057 resume_listener(l);
1058 }
1059
1060 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1061 dequeue_all_listeners(&s->fe->listener_queue);
1062
1063 return 1;
1064 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001065 else if (strcmp(args[2], "global") == 0) {
1066 int v;
1067
1068 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1069 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1070 si->applet.st0 = STAT_CLI_PRINT;
1071 return 1;
1072 }
1073
1074 if (!*args[3]) {
1075 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1076 si->applet.st0 = STAT_CLI_PRINT;
1077 return 1;
1078 }
1079
1080 v = atoi(args[3]);
1081 if (v > global.hardmaxconn) {
1082 si->applet.ctx.cli.msg = "Value out of range.\n";
1083 si->applet.st0 = STAT_CLI_PRINT;
1084 return 1;
1085 }
1086
1087 /* check for unlimited values */
1088 if (v <= 0)
1089 v = global.hardmaxconn;
1090
1091 global.maxconn = v;
1092
1093 /* Dequeues all of the listeners waiting for a resource */
1094 if (!LIST_ISEMPTY(&global_listener_queue))
1095 dequeue_all_listeners(&global_listener_queue);
1096
1097 return 1;
1098 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001099 else {
Willy Tarreau91886b62011-09-07 14:38:31 +02001100 si->applet.ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001101 si->applet.st0 = STAT_CLI_PRINT;
1102 return 1;
1103 }
1104 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001105 else if (strcmp(args[1], "rate-limit") == 0) {
1106 if (strcmp(args[2], "connections") == 0) {
1107 if (strcmp(args[3], "global") == 0) {
1108 int v;
1109
1110 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1111 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1112 si->applet.st0 = STAT_CLI_PRINT;
1113 return 1;
1114 }
1115
1116 if (!*args[4]) {
1117 si->applet.ctx.cli.msg = "Expects an integer value.\n";
1118 si->applet.st0 = STAT_CLI_PRINT;
1119 return 1;
1120 }
1121
1122 v = atoi(args[4]);
1123 if (v < 0) {
1124 si->applet.ctx.cli.msg = "Value out of range.\n";
1125 si->applet.st0 = STAT_CLI_PRINT;
1126 return 1;
1127 }
1128
1129 global.cps_lim = v;
1130
1131 /* Dequeues all of the listeners waiting for a resource */
1132 if (!LIST_ISEMPTY(&global_listener_queue))
1133 dequeue_all_listeners(&global_listener_queue);
1134
1135 return 1;
1136 }
1137 else {
1138 si->applet.ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1139 si->applet.st0 = STAT_CLI_PRINT;
1140 return 1;
1141 }
1142 }
1143 else {
1144 si->applet.ctx.cli.msg = "'set rate-limit' only supports 'connections'.\n";
1145 si->applet.st0 = STAT_CLI_PRINT;
1146 return 1;
1147 }
1148 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001149 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001150 return 0;
1151 }
1152 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001153 else if (strcmp(args[0], "enable") == 0) {
1154 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001155 struct server *sv;
1156
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001157 sv = expect_server_admin(s, si, args[2]);
1158 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001159 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001160
Cyril Bontécd19e512010-01-31 22:34:03 +01001161 if (sv->state & SRV_MAINTAIN) {
1162 /* The server is really in maintenance, we can change the server state */
1163 if (sv->tracked) {
1164 /* If this server tracks the status of another one,
1165 * we must restore the good status.
1166 */
1167 if (sv->tracked->state & SRV_RUNNING) {
1168 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001169 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001170 } else {
1171 sv->state &= ~SRV_MAINTAIN;
1172 set_server_down(sv);
1173 }
1174 } else {
1175 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02001176 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001177 }
1178 }
1179
Willy Tarreau532a4502011-09-07 22:37:44 +02001180 return 1;
1181 }
1182 else if (strcmp(args[1], "frontend") == 0) {
1183 struct proxy *px;
1184
1185 px = expect_frontend_admin(s, si, args[2]);
1186 if (!px)
1187 return 1;
1188
1189 if (px->state == PR_STSTOPPED) {
1190 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1191 si->applet.st0 = STAT_CLI_PRINT;
1192 return 1;
1193 }
1194
1195 if (px->state != PR_STPAUSED) {
1196 si->applet.ctx.cli.msg = "Frontend is already enabled.\n";
1197 si->applet.st0 = STAT_CLI_PRINT;
1198 return 1;
1199 }
1200
1201 if (!resume_proxy(px)) {
1202 si->applet.ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1203 si->applet.st0 = STAT_CLI_PRINT;
1204 return 1;
1205 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001206 return 1;
1207 }
1208 else { /* unknown "enable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001209 si->applet.ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1210 si->applet.st0 = STAT_CLI_PRINT;
1211 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001212 }
1213 }
1214 else if (strcmp(args[0], "disable") == 0) {
1215 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001216 struct server *sv;
1217
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001218 sv = expect_server_admin(s, si, args[2]);
1219 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001220 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001221
Cyril Bontécd19e512010-01-31 22:34:03 +01001222 if (! (sv->state & SRV_MAINTAIN)) {
1223 /* Not already in maintenance, we can change the server state */
1224 sv->state |= SRV_MAINTAIN;
1225 set_server_down(sv);
1226 }
1227
Willy Tarreau532a4502011-09-07 22:37:44 +02001228 return 1;
1229 }
1230 else if (strcmp(args[1], "frontend") == 0) {
1231 struct proxy *px;
1232
1233 px = expect_frontend_admin(s, si, args[2]);
1234 if (!px)
1235 return 1;
1236
1237 if (px->state == PR_STSTOPPED) {
1238 si->applet.ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1239 si->applet.st0 = STAT_CLI_PRINT;
1240 return 1;
1241 }
1242
1243 if (px->state == PR_STPAUSED) {
1244 si->applet.ctx.cli.msg = "Frontend is already disabled.\n";
1245 si->applet.st0 = STAT_CLI_PRINT;
1246 return 1;
1247 }
1248
1249 if (!pause_proxy(px)) {
1250 si->applet.ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1251 si->applet.st0 = STAT_CLI_PRINT;
1252 return 1;
1253 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001254 return 1;
1255 }
1256 else { /* unknown "disable" parameter */
Willy Tarreau532a4502011-09-07 22:37:44 +02001257 si->applet.ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1258 si->applet.st0 = STAT_CLI_PRINT;
1259 return 1;
1260 }
1261 }
1262 else if (strcmp(args[0], "shutdown") == 0) {
1263 if (strcmp(args[1], "frontend") == 0) {
1264 struct proxy *px;
1265
1266 px = expect_frontend_admin(s, si, args[2]);
1267 if (!px)
1268 return 1;
1269
1270 if (px->state == PR_STSTOPPED) {
1271 si->applet.ctx.cli.msg = "Frontend was already shut down.\n";
1272 si->applet.st0 = STAT_CLI_PRINT;
1273 return 1;
1274 }
1275
1276 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1277 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1278 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1279 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1280 stop_proxy(px);
1281 return 1;
1282 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001283 else if (strcmp(args[1], "session") == 0) {
1284 struct session *sess, *ptr;
1285
1286 if (s->listener->perm.ux.level < ACCESS_LVL_ADMIN) {
1287 si->applet.ctx.cli.msg = stats_permission_denied_msg;
1288 si->applet.st0 = STAT_CLI_PRINT;
1289 return 1;
1290 }
1291
1292 if (!*args[2]) {
1293 si->applet.ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1294 si->applet.st0 = STAT_CLI_PRINT;
1295 return 1;
1296 }
1297
1298 ptr = (void *)strtoul(args[2], NULL, 0);
1299
1300 /* first, look for the requested session in the session table */
1301 list_for_each_entry(sess, &sessions, list) {
1302 if (sess == ptr)
1303 break;
1304 }
1305
1306 /* do we have the session ? */
1307 if (sess != ptr) {
1308 si->applet.ctx.cli.msg = "No such session (use 'show sess').\n";
1309 si->applet.st0 = STAT_CLI_PRINT;
1310 return 1;
1311 }
1312
1313 session_shutdown(sess, SN_ERR_KILLED);
1314 return 1;
1315 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001316 else if (strcmp(args[1], "sessions") == 0) {
1317 if (strcmp(args[2], "server") == 0) {
1318 struct server *sv;
1319 struct session *sess, *sess_bck;
1320
1321 sv = expect_server_admin(s, si, args[3]);
1322 if (!sv)
1323 return 1;
1324
1325 /* kill all the session that are on this server */
1326 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1327 if (sess->srv_conn == sv)
1328 session_shutdown(sess, SN_ERR_KILLED);
1329
1330 return 1;
1331 }
1332 else {
1333 si->applet.ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1334 si->applet.st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001338 else { /* unknown "disable" parameter */
Willy Tarreau52b2d222011-09-07 23:48:48 +02001339 si->applet.ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
Willy Tarreau532a4502011-09-07 22:37:44 +02001340 si->applet.st0 = STAT_CLI_PRINT;
1341 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001342 }
1343 }
1344 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001345 return 0;
1346 }
1347 return 1;
1348}
1349
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001350/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001351 * used to processes I/O from/to the stats unix socket. The system relies on a
1352 * state machine handling requests and various responses. We read a request,
1353 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001354 * Then we can read again. The state is stored in si->applet.st0 and is one of the
1355 * STAT_CLI_* constants. si->applet.st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001356 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001357 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001358static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001359{
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001360 struct buffer *req = si->ob;
1361 struct buffer *res = si->ib;
1362 int reql;
1363 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001364
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001365 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1366 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001367
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001368 while (1) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001369 if (si->applet.st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001370 /* Stats output not initialized yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001371 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreaubc4af052011-02-13 13:25:14 +01001372 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001373 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001374 else if (si->applet.st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001375 /* Let's close for real now. We just close the request
1376 * side, the conditions below will complete if needed.
1377 */
1378 si->shutw(si);
1379 break;
1380 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001381 else if (si->applet.st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02001382 /* ensure we have some output room left in the event we
1383 * would want to return some info right after parsing.
1384 */
1385 if (buffer_almost_full(si->ib))
1386 break;
1387
Willy Tarreau74b08c92010-09-08 17:04:31 +02001388 reql = buffer_get_line(si->ob, trash, sizeof(trash));
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001389 if (reql <= 0) { /* closed or EOL not found */
1390 if (reql == 0)
1391 break;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001392 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001393 continue;
1394 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001395
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001396 /* seek for a possible semi-colon. If we find one, we
1397 * replace it with an LF and skip only this part.
1398 */
1399 for (len = 0; len < reql; len++)
1400 if (trash[len] == ';') {
1401 trash[len] = '\n';
1402 reql = len + 1;
1403 break;
1404 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001405
Willy Tarreau816fc222009-10-04 07:36:58 +02001406 /* now it is time to check that we have a full line,
1407 * remove the trailing \n and possibly \r, then cut the
1408 * line.
1409 */
1410 len = reql - 1;
1411 if (trash[len] != '\n') {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001412 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001413 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001414 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001415
Willy Tarreau816fc222009-10-04 07:36:58 +02001416 if (len && trash[len-1] == '\r')
1417 len--;
1418
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001419 trash[len] = '\0';
1420
Willy Tarreaubc4af052011-02-13 13:25:14 +01001421 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001422 if (len) {
1423 if (strcmp(trash, "quit") == 0) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001424 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001425 continue;
1426 }
1427 else if (strcmp(trash, "prompt") == 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001428 si->applet.st1 = !si->applet.st1;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001429 else if (strcmp(trash, "help") == 0 ||
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001430 !stats_sock_parse_request(si, trash)) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001431 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001432 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001433 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001434 /* NB: stats_sock_parse_request() may have put
Willy Tarreaubc4af052011-02-13 13:25:14 +01001435 * another STAT_CLI_O_* into si->applet.st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001436 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001437 }
Willy Tarreaubc4af052011-02-13 13:25:14 +01001438 else if (!si->applet.st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001439 /* if prompt is disabled, print help on empty lines,
1440 * so that the user at least knows how to enable
1441 * prompt and find help.
1442 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001443 si->applet.ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreaubc4af052011-02-13 13:25:14 +01001444 si->applet.st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001445 }
1446
1447 /* re-adjust req buffer */
1448 buffer_skip(si->ob, reql);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001449 req->flags |= BF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001450 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001451 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001452 if (res->flags & (BF_SHUTR_NOW|BF_SHUTR)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001453 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001454 continue;
1455 }
1456
Willy Tarreaubc4af052011-02-13 13:25:14 +01001457 switch (si->applet.st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001458 case STAT_CLI_PRINT:
Willy Tarreau295a8372011-03-10 11:25:07 +01001459 if (buffer_feed(si->ib, si->applet.ctx.cli.msg) < 0)
Willy Tarreaubc4af052011-02-13 13:25:14 +01001460 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001461 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001462 case STAT_CLI_O_INFO:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001463 if (stats_dump_raw_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001464 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001465 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001466 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001467 if (stats_dump_sess_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001468 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001469 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001470 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001471 if (stats_dump_errors_to_buffer(si))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001472 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001473 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001474 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09001475 if (stats_table_request(si, true))
1476 si->applet.st0 = STAT_CLI_PROMPT;
1477 break;
1478 case STAT_CLI_O_CLR:
1479 if (stats_table_request(si, false))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001480 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02001481 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001482 default: /* abnormal state */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001483 si->applet.st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001484 break;
1485 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001486
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001487 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001488 if (si->applet.st0 == STAT_CLI_PROMPT) {
1489 if (buffer_feed(si->ib, si->applet.st1 ? "\n> " : "\n") < 0)
1490 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001491 }
1492
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001493 /* If the output functions are still there, it means they require more room. */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001494 if (si->applet.st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001495 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001496
1497 /* Now we close the output if one of the writers did so,
1498 * or if we're not in interactive mode and the request
1499 * buffer is empty. This still allows pipelined requests
1500 * to be sent in non-interactive mode.
1501 */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001502 if ((res->flags & (BF_SHUTW|BF_SHUTW_NOW)) || (!si->applet.st1 && !req->send_max)) {
1503 si->applet.st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001504 continue;
1505 }
1506
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001507 /* switch state back to GETREQ to read next requests */
Willy Tarreaubc4af052011-02-13 13:25:14 +01001508 si->applet.st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001509 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001510 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001511
Willy Tarreaubc4af052011-02-13 13:25:14 +01001512 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST) && (si->applet.st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001513 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1514 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1515 /* Other size has closed, let's abort if we have no more processing to do
1516 * and nothing more to consume. This is comparable to a broken pipe, so
1517 * we forward the close to the request side so that it flows upstream to
1518 * the client.
1519 */
1520 si->shutw(si);
1521 }
1522
Willy Tarreaubc4af052011-02-13 13:25:14 +01001523 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && (si->applet.st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001524 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1525 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1526 /* We have no more processing to do, and nothing more to send, and
1527 * the client side has closed. So we'll forward this state downstream
1528 * on the response buffer.
1529 */
1530 si->shutr(si);
1531 res->flags |= BF_READ_NULL;
1532 }
1533
1534 /* update all other flags and resync with the other side */
1535 si->update(si);
1536
1537 /* we don't want to expire timeouts while we're processing requests */
1538 si->ib->rex = TICK_ETERNITY;
1539 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001540
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001541 out:
1542 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rql=%d, rqs=%d, rl=%d, rs=%d\n",
1543 __FUNCTION__, __LINE__,
1544 si->state, req->flags, res->flags, req->l, req->send_max, res->l, res->send_max);
1545
1546 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1547 /* check that we have released everything then unregister */
1548 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001549 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001550}
1551
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001552/* This function dumps statistics onto the stream interface's read buffer.
1553 * The data_ctx must have been zeroed first, and the flags properly set.
Willy Tarreau24955a12009-10-04 11:54:04 +02001554 * It returns 0 as long as it does not complete, non-zero upon completion.
1555 * Some states are not used but it makes the code more similar to other
1556 * functions which handle stats too.
Willy Tarreau3e76e722007-10-17 18:57:38 +02001557 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001558static int stats_dump_raw_to_buffer(struct stream_interface *si)
Willy Tarreau3e76e722007-10-17 18:57:38 +02001559{
Willy Tarreau3e76e722007-10-17 18:57:38 +02001560 struct proxy *px;
1561 struct chunk msg;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001562 unsigned int up;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001563
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001564 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3e76e722007-10-17 18:57:38 +02001565
Willy Tarreau295a8372011-03-10 11:25:07 +01001566 switch (si->applet.state) {
1567 case STAT_ST_INIT:
Willy Tarreau24955a12009-10-04 11:54:04 +02001568 /* the function had not been called yet */
Willy Tarreau295a8372011-03-10 11:25:07 +01001569 si->applet.state = STAT_ST_HEAD;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001570 /* fall through */
1571
Willy Tarreau295a8372011-03-10 11:25:07 +01001572 case STAT_ST_HEAD:
1573 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001574 print_csv_header(&msg);
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001575 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001576 return 0;
1577 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001578
Willy Tarreau295a8372011-03-10 11:25:07 +01001579 si->applet.state = STAT_ST_INFO;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001580 /* fall through */
1581
Willy Tarreau295a8372011-03-10 11:25:07 +01001582 case STAT_ST_INFO:
Willy Tarreaua8efd362008-01-03 10:19:15 +01001583 up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau295a8372011-03-10 11:25:07 +01001584 if (si->applet.ctx.stats.flags & STAT_SHOW_INFO) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001585 chunk_printf(&msg,
Willy Tarreaua8efd362008-01-03 10:19:15 +01001586 "Name: " PRODUCT_NAME "\n"
1587 "Version: " HAPROXY_VERSION "\n"
1588 "Release_date: " HAPROXY_DATE "\n"
1589 "Nbproc: %d\n"
1590 "Process_num: %d\n"
1591 "Pid: %d\n"
1592 "Uptime: %dd %dh%02dm%02ds\n"
1593 "Uptime_sec: %d\n"
1594 "Memmax_MB: %d\n"
1595 "Ulimit-n: %d\n"
1596 "Maxsock: %d\n"
1597 "Maxconn: %d\n"
Willy Tarreau91886b62011-09-07 14:38:31 +02001598 "Hard_maxconn: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001599 "Maxpipes: %d\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001600 "CurrConns: %d\n"
Willy Tarreaua206fa92009-01-25 14:02:00 +01001601 "PipesUsed: %d\n"
1602 "PipesFree: %d\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001603 "ConnRate: %d\n"
1604 "ConnRateLimit: %d\n"
1605 "MaxConnRate: %d\n"
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001606 "Tasks: %d\n"
1607 "Run_queue: %d\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001608 "node: %s\n"
1609 "description: %s\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001610 "",
1611 global.nbproc,
1612 relative_pid,
1613 pid,
1614 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
1615 up,
1616 global.rlimit_memmax,
1617 global.rlimit_nofile,
Willy Tarreau91886b62011-09-07 14:38:31 +02001618 global.maxsock, global.maxconn, global.hardmaxconn, global.maxpipes,
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001619 actconn, pipes_used, pipes_free,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001620 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001621 nb_tasks_cur, run_queue_cur,
1622 global.node, global.desc?global.desc:""
Willy Tarreaua8efd362008-01-03 10:19:15 +01001623 );
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001624 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001625 return 0;
1626 }
1627
Willy Tarreau295a8372011-03-10 11:25:07 +01001628 si->applet.ctx.stats.px = proxy;
1629 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
1630 si->applet.ctx.stats.sv = NULL;
1631 si->applet.state = STAT_ST_LIST;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001632 /* fall through */
1633
Willy Tarreau295a8372011-03-10 11:25:07 +01001634 case STAT_ST_LIST:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001635 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01001636 if (si->applet.ctx.stats.flags & STAT_SHOW_STAT) {
1637 while (si->applet.ctx.stats.px) {
1638 px = si->applet.ctx.stats.px;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001639 /* skip the disabled proxies and non-networked ones */
1640 if (px->state != PR_STSTOPPED &&
Willy Tarreau24955a12009-10-04 11:54:04 +02001641 (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001642 if (stats_dump_proxy(si, px, NULL) == 0)
Willy Tarreaua8efd362008-01-03 10:19:15 +01001643 return 0;
Willy Tarreau24955a12009-10-04 11:54:04 +02001644 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001645
Willy Tarreau295a8372011-03-10 11:25:07 +01001646 si->applet.ctx.stats.px = px->next;
1647 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaua8efd362008-01-03 10:19:15 +01001648 }
1649 /* here, we just have reached the last proxy */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001650 }
Willy Tarreau3e76e722007-10-17 18:57:38 +02001651
Willy Tarreau295a8372011-03-10 11:25:07 +01001652 si->applet.state = STAT_ST_END;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001653 /* fall through */
1654
Willy Tarreau295a8372011-03-10 11:25:07 +01001655 case STAT_ST_END:
1656 si->applet.state = STAT_ST_FIN;
Willy Tarreau0a464892008-12-07 18:30:00 +01001657 /* fall through */
Willy Tarreau3e76e722007-10-17 18:57:38 +02001658
Willy Tarreau295a8372011-03-10 11:25:07 +01001659 case STAT_ST_FIN:
Willy Tarreau3e76e722007-10-17 18:57:38 +02001660 return 1;
1661
1662 default:
1663 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01001664 si->applet.state = STAT_ST_FIN;
Willy Tarreau24955a12009-10-04 11:54:04 +02001665 return 1;
Willy Tarreau3e76e722007-10-17 18:57:38 +02001666 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001667}
1668
1669
Cyril Bonté70be45d2010-10-12 00:14:35 +02001670/* We don't want to land on the posted stats page because a refresh will
1671 * repost the data. We don't want this to happen on accident so we redirect
1672 * the browse to the stats page with a GET.
1673 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001674static int stats_http_redir(struct stream_interface *si, struct uri_auth *uri)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001675{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001676 struct session *s = si->applet.private;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001677 struct chunk msg;
1678
1679 chunk_init(&msg, trash, sizeof(trash));
1680
Willy Tarreau295a8372011-03-10 11:25:07 +01001681 switch (si->applet.state) {
1682 case STAT_ST_INIT:
Cyril Bonté70be45d2010-10-12 00:14:35 +02001683 chunk_printf(&msg,
1684 "HTTP/1.0 303 See Other\r\n"
1685 "Cache-Control: no-cache\r\n"
1686 "Content-Type: text/plain\r\n"
1687 "Connection: close\r\n"
1688 "Location: %s;st=%s",
Willy Tarreau295a8372011-03-10 11:25:07 +01001689 uri->uri_prefix, si->applet.ctx.stats.st_code);
Cyril Bonté70be45d2010-10-12 00:14:35 +02001690 chunk_printf(&msg, "\r\n\r\n");
1691
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001692 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Cyril Bonté70be45d2010-10-12 00:14:35 +02001693 return 0;
1694
1695 s->txn.status = 303;
1696
1697 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1698 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1699 if (!(s->flags & SN_FINST_MASK))
1700 s->flags |= SN_FINST_R;
1701
Willy Tarreau295a8372011-03-10 11:25:07 +01001702 si->applet.state = STAT_ST_FIN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001703 return 1;
1704 }
1705 return 1;
1706}
1707
1708
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001709/* This I/O handler runs as an applet embedded in a stream interface. It is
1710 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreaubc4af052011-02-13 13:25:14 +01001711 * si->applet.st0 becomes non-zero once the transfer is finished. The handler
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001712 * automatically unregisters itself once transfer is complete.
1713 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001714static void http_stats_io_handler(struct stream_interface *si)
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001715{
Willy Tarreaubc4af052011-02-13 13:25:14 +01001716 struct session *s = si->applet.private;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001717 struct buffer *req = si->ob;
1718 struct buffer *res = si->ib;
1719
1720 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1721 goto out;
1722
1723 /* check that the output is not closed */
1724 if (res->flags & (BF_SHUTW|BF_SHUTW_NOW))
Willy Tarreaubc4af052011-02-13 13:25:14 +01001725 si->applet.st0 = 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001726
Willy Tarreaubc4af052011-02-13 13:25:14 +01001727 if (!si->applet.st0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02001728 if (s->txn.meth == HTTP_METH_POST) {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001729 if (stats_http_redir(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001730 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001731 si->shutw(si);
1732 }
1733 } else {
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001734 if (stats_dump_http(si, s->be->uri_auth)) {
Willy Tarreaubc4af052011-02-13 13:25:14 +01001735 si->applet.st0 = 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02001736 si->shutw(si);
1737 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001738 }
1739 }
1740
1741 if ((res->flags & BF_SHUTR) && (si->state == SI_ST_EST))
1742 si->shutw(si);
1743
Willy Tarreaubc4af052011-02-13 13:25:14 +01001744 if ((req->flags & BF_SHUTW) && (si->state == SI_ST_EST) && si->applet.st0) {
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001745 si->shutr(si);
1746 res->flags |= BF_READ_NULL;
1747 }
1748
1749 /* update all other flags and resync with the other side */
1750 si->update(si);
1751
1752 /* we don't want to expire timeouts while we're processing requests */
1753 si->ib->rex = TICK_ETERNITY;
1754 si->ob->wex = TICK_ETERNITY;
1755
1756 out:
1757 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
1758 /* check that we have released everything then unregister */
1759 stream_int_unregister_handler(si);
1760 }
1761}
1762
1763
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001764/* This function dumps statistics in HTTP format onto the stream interface's
1765 * read buffer. The data_ctx must have been zeroed first, and the flags
1766 * properly set. It returns 0 if it had to stop writing data and an I/O is
1767 * needed, 1 if the dump is finished and the session must be closed, or -1
1768 * in case of any error.
Willy Tarreau91861262007-10-17 17:06:05 +02001769 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001770static int stats_dump_http(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02001771{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01001772 struct session *s = si->applet.private;
1773 struct buffer *rep = si->ib;
Willy Tarreau91861262007-10-17 17:06:05 +02001774 struct proxy *px;
1775 struct chunk msg;
1776 unsigned int up;
1777
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001778 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02001779
Willy Tarreau295a8372011-03-10 11:25:07 +01001780 switch (si->applet.state) {
1781 case STAT_ST_INIT:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001782 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001783 "HTTP/1.0 200 OK\r\n"
1784 "Cache-Control: no-cache\r\n"
1785 "Connection: close\r\n"
Willy Tarreau55bb8452007-10-17 18:44:57 +02001786 "Content-Type: %s\r\n",
Willy Tarreau295a8372011-03-10 11:25:07 +01001787 (si->applet.ctx.stats.flags & STAT_FMT_CSV) ? "text/plain" : "text/html");
Willy Tarreau91861262007-10-17 17:06:05 +02001788
Willy Tarreau295a8372011-03-10 11:25:07 +01001789 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001790 chunk_printf(&msg, "Refresh: %d\r\n",
Willy Tarreau91861262007-10-17 17:06:05 +02001791 uri->refresh);
1792
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001793 chunk_printf(&msg, "\r\n");
Willy Tarreau91861262007-10-17 17:06:05 +02001794
1795 s->txn.status = 200;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001796 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau56a560a2009-09-22 19:27:35 +02001797 return 0;
1798
Willy Tarreau91861262007-10-17 17:06:05 +02001799 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
1800 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
1801 if (!(s->flags & SN_FINST_MASK))
1802 s->flags |= SN_FINST_R;
1803
1804 if (s->txn.meth == HTTP_METH_HEAD) {
1805 /* that's all we return in case of HEAD request */
Willy Tarreau295a8372011-03-10 11:25:07 +01001806 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02001807 return 1;
1808 }
1809
Willy Tarreau295a8372011-03-10 11:25:07 +01001810 si->applet.state = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreau91861262007-10-17 17:06:05 +02001811 /* fall through */
1812
Willy Tarreau295a8372011-03-10 11:25:07 +01001813 case STAT_ST_HEAD:
1814 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01001815 /* WARNING! This must fit in the first buffer !!! */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001816 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001817 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
1818 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001819 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001820 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
1821 "<style type=\"text/css\"><!--\n"
1822 "body {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001823 " font-family: arial, helvetica, sans-serif;"
Willy Tarreau91861262007-10-17 17:06:05 +02001824 " font-size: 12px;"
1825 " font-weight: normal;"
1826 " color: black;"
1827 " background: white;"
1828 "}\n"
1829 "th,td {"
Willy Tarreaua94f2d22009-05-10 20:08:10 +02001830 " font-size: 10px;"
Willy Tarreau91861262007-10-17 17:06:05 +02001831 "}\n"
1832 "h1 {"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001833 " font-size: x-large;"
Willy Tarreau91861262007-10-17 17:06:05 +02001834 " margin-bottom: 0.5em;"
1835 "}\n"
1836 "h2 {"
1837 " font-family: helvetica, arial;"
1838 " font-size: x-large;"
1839 " font-weight: bold;"
1840 " font-style: italic;"
1841 " color: #6020a0;"
1842 " margin-top: 0em;"
1843 " margin-bottom: 0em;"
1844 "}\n"
1845 "h3 {"
1846 " font-family: helvetica, arial;"
1847 " font-size: 16px;"
1848 " font-weight: bold;"
1849 " color: #b00040;"
1850 " background: #e8e8d0;"
1851 " margin-top: 0em;"
1852 " margin-bottom: 0em;"
1853 "}\n"
1854 "li {"
1855 " margin-top: 0.25em;"
1856 " margin-right: 2em;"
1857 "}\n"
1858 ".hr {margin-top: 0.25em;"
1859 " border-color: black;"
1860 " border-bottom-style: solid;"
1861 "}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001862 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001863 ".total {background: #20D0D0;color: #ffff80;}\n"
1864 ".frontend {background: #e8e8d0;}\n"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001865 ".socket {background: #d0d0d0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001866 ".backend {background: #e8e8d0;}\n"
1867 ".active0 {background: #ff9090;}\n"
1868 ".active1 {background: #ffd020;}\n"
1869 ".active2 {background: #ffffa0;}\n"
1870 ".active3 {background: #c0ffc0;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001871 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
1872 ".active5 {background: #a0e0a0;}\n" /* NOLB state shows darker than up */
1873 ".active6 {background: #e0e0e0;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001874 ".backup0 {background: #ff9090;}\n"
1875 ".backup1 {background: #ff80ff;}\n"
1876 ".backup2 {background: #c060ff;}\n"
1877 ".backup3 {background: #b0d0ff;}\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001878 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
1879 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
1880 ".backup6 {background: #e0e0e0;}\n"
Cyril Bontécd19e512010-01-31 22:34:03 +01001881 ".maintain {background: #c07820;}\n"
Willy Tarreauda6721b2009-07-15 10:07:05 +02001882 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001883 "\n"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02001884 "a.px:link {color: #ffff40; text-decoration: none;}"
1885 "a.px:visited {color: #ffff40; text-decoration: none;}"
1886 "a.px:hover {color: #ffffff; text-decoration: none;}"
1887 "a.lfsb:link {color: #000000; text-decoration: none;}"
1888 "a.lfsb:visited {color: #000000; text-decoration: none;}"
1889 "a.lfsb:hover {color: #505050; text-decoration: none;}"
1890 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001891 "table.tbl { border-collapse: collapse; border-style: none;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001892 "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"
1893 "table.tbl td.ac { text-align: center;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001894 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001895 "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 +02001896 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001897 "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 +02001898 "\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001899 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
1900 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
1901 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreaue0454092010-02-26 12:29:07 +01001902 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001903 "-->\n"
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001904 "</style></head>\n",
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001905 (uri->flags&ST_SHNODE) ? " on " : "",
1906 (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
Willy Tarreau1d45b7c2009-08-16 10:29:18 +02001907 );
Willy Tarreau55bb8452007-10-17 18:44:57 +02001908 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001909 print_csv_header(&msg);
Willy Tarreau55bb8452007-10-17 18:44:57 +02001910 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02001911 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02001912 return 0;
1913
Willy Tarreau295a8372011-03-10 11:25:07 +01001914 si->applet.state = STAT_ST_INFO;
Willy Tarreau91861262007-10-17 17:06:05 +02001915 /* fall through */
1916
Willy Tarreau295a8372011-03-10 11:25:07 +01001917 case STAT_ST_INFO:
Willy Tarreau91861262007-10-17 17:06:05 +02001918 up = (now.tv_sec - start_date.tv_sec);
1919
1920 /* WARNING! this has to fit the first packet too.
1921 * We are around 3.5 kB, add adding entries will
1922 * become tricky if we want to support 4kB buffers !
1923 */
Willy Tarreau295a8372011-03-10 11:25:07 +01001924 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001925 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001926 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
1927 PRODUCT_NAME "%s</a></h1>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001928 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001929 "<hr width=\"100%%\" class=\"hr\">\n"
1930 "<h3>&gt; General process information</h3>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02001931 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Willy Tarreaua8efd362008-01-03 10:19:15 +01001932 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001933 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001934 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
1935 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001936 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001937 "Running tasks: %d/%d<br>\n"
Willy Tarreau91861262007-10-17 17:06:05 +02001938 "</td><td align=\"center\" nowrap>\n"
1939 "<table class=\"lgd\"><tr>\n"
1940 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
1941 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
1942 "</tr><tr>\n"
1943 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
1944 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
1945 "</tr><tr>\n"
1946 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
1947 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
1948 "</tr><tr>\n"
1949 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001950 "<td class=\"active6\"></td><td class=\"noborder\">not checked </td>"
Cyril Bontécd19e512010-01-31 22:34:03 +01001951 "</tr><tr>\n"
1952 "<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 +02001953 "</tr></table>\n"
Willy Tarreau2ea81932007-11-30 12:04:38 +01001954 "Note: UP with load-balancing disabled is reported as \"NOLB\"."
Willy Tarreau91861262007-10-17 17:06:05 +02001955 "</td>"
1956 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
1957 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
1958 "",
1959 (uri->flags&ST_HIDEVER)?"":(STATS_VERSION_STRING),
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02001960 pid, (uri->flags&ST_SHNODE) ? " on " : "", (uri->flags&ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
1961 (uri->flags&ST_SHDESC)? ": " : "", (uri->flags&ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
1962 pid, relative_pid, global.nbproc,
Willy Tarreau91861262007-10-17 17:06:05 +02001963 up / 86400, (up % 86400) / 3600,
1964 (up % 3600) / 60, (up % 60),
1965 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
1966 global.rlimit_memmax ? " MB" : "",
1967 global.rlimit_nofile,
Willy Tarreaua206fa92009-01-25 14:02:00 +01001968 global.maxsock, global.maxconn, global.maxpipes,
Willy Tarreau9cd552d2011-09-07 15:26:48 +02001969 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreauc7bdf092009-03-21 18:33:52 +01001970 run_queue_cur, nb_tasks_cur
Willy Tarreau91861262007-10-17 17:06:05 +02001971 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01001972
Willy Tarreau295a8372011-03-10 11:25:07 +01001973 if (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001974 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001975 "<li><a href=\"%s%s%s\">Show all servers</a><br>\n",
1976 uri->uri_prefix,
1977 "",
Willy Tarreau295a8372011-03-10 11:25:07 +01001978 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001979 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001980 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001981 "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n",
1982 uri->uri_prefix,
1983 ";up",
Willy Tarreau295a8372011-03-10 11:25:07 +01001984 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02001985
Willy Tarreau55bb8452007-10-17 18:44:57 +02001986 if (uri->refresh > 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01001987 if (si->applet.ctx.stats.flags & STAT_NO_REFRESH)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001988 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001989 "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n",
1990 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001991 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001992 "");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001993 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02001994 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02001995 "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n",
1996 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01001997 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
Willy Tarreau91861262007-10-17 17:06:05 +02001998 ";norefresh");
Willy Tarreau55bb8452007-10-17 18:44:57 +02001999 }
Willy Tarreau91861262007-10-17 17:06:05 +02002000
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\">Refresh now</a><br>\n",
2003 uri->uri_prefix,
Willy Tarreau295a8372011-03-10 11:25:07 +01002004 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2005 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "");
Willy Tarreau91861262007-10-17 17:06:05 +02002006
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002007 chunk_printf(&msg,
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002008 "<li><a href=\"%s;csv%s\">CSV export</a><br>\n",
2009 uri->uri_prefix,
2010 (uri->refresh > 0) ? ";norefresh" : "");
2011
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002012 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002013 "</ul></td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002014 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2015 "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n"
2016 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2017 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2018 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2019 "</ul>"
2020 "</td>"
2021 "</tr></table>\n"
2022 ""
2023 );
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002024
Willy Tarreau295a8372011-03-10 11:25:07 +01002025 if (si->applet.ctx.stats.st_code) {
2026 if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002027 chunk_printf(&msg,
2028 "<p><div class=active3>"
2029 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2030 "Action processed successfully."
2031 "</div>\n", uri->uri_prefix);
2032 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002033 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_NONE) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002034 chunk_printf(&msg,
2035 "<p><div class=active2>"
2036 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2037 "Nothing has changed."
2038 "</div>\n", uri->uri_prefix);
2039 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002040 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_EXCD) == 0) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002041 chunk_printf(&msg,
2042 "<p><div class=active0>"
2043 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2044 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2045 "You should retry with less servers at a time.</b>"
2046 "</div>\n", uri->uri_prefix);
2047 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002048 else if (strcmp(si->applet.ctx.stats.st_code, STAT_STATUS_DENY) == 0) {
Cyril Bonté474be412010-10-12 00:14:36 +02002049 chunk_printf(&msg,
2050 "<p><div class=active0>"
2051 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2052 "<b>Action denied.</b>"
2053 "</div>\n", uri->uri_prefix);
2054 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002055 else {
2056 chunk_printf(&msg,
2057 "<p><div class=active6>"
2058 "<a class=lfsb href=\"%s\" title=\"Remove this message\">[X]</a> "
2059 "Unexpected result."
2060 "</div>\n", uri->uri_prefix);
2061 }
2062 chunk_printf(&msg,"<p>\n");
2063 }
2064
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002065 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002066 return 0;
2067 }
Willy Tarreau91861262007-10-17 17:06:05 +02002068
Willy Tarreau295a8372011-03-10 11:25:07 +01002069 si->applet.ctx.stats.px = proxy;
2070 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
2071 si->applet.state = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02002072 /* fall through */
2073
Willy Tarreau295a8372011-03-10 11:25:07 +01002074 case STAT_ST_LIST:
Willy Tarreau91861262007-10-17 17:06:05 +02002075 /* dump proxies */
Willy Tarreau295a8372011-03-10 11:25:07 +01002076 while (si->applet.ctx.stats.px) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002077 if (buffer_almost_full(rep))
2078 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01002079 px = si->applet.ctx.stats.px;
Willy Tarreau91861262007-10-17 17:06:05 +02002080 /* skip the disabled proxies and non-networked ones */
2081 if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002082 if (stats_dump_proxy(si, px, uri) == 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002083 return 0;
2084
Willy Tarreau295a8372011-03-10 11:25:07 +01002085 si->applet.ctx.stats.px = px->next;
2086 si->applet.ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreau91861262007-10-17 17:06:05 +02002087 }
2088 /* here, we just have reached the last proxy */
2089
Willy Tarreau295a8372011-03-10 11:25:07 +01002090 si->applet.state = STAT_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02002091 /* fall through */
2092
Willy Tarreau295a8372011-03-10 11:25:07 +01002093 case STAT_ST_END:
2094 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002095 chunk_printf(&msg, "</body></html>\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002096 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002097 return 0;
2098 }
Willy Tarreau91861262007-10-17 17:06:05 +02002099
Willy Tarreau295a8372011-03-10 11:25:07 +01002100 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002101 /* fall through */
2102
Willy Tarreau295a8372011-03-10 11:25:07 +01002103 case STAT_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02002104 return 1;
2105
2106 default:
2107 /* unknown state ! */
Willy Tarreau295a8372011-03-10 11:25:07 +01002108 si->applet.state = STAT_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02002109 return -1;
2110 }
2111}
2112
2113
2114/*
2115 * Dumps statistics for a proxy.
2116 * Returns 0 if it had to stop dumping data because of lack of buffer space,
2117 * ot non-zero if everything completed.
2118 */
Simon Horman9bd2c732011-06-15 15:18:44 +09002119static int stats_dump_proxy(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02002120{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002121 struct session *s = si->applet.private;
2122 struct buffer *rep = si->ib;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002123 struct server *sv, *svs; /* server and server-state, server-state=server or server->tracked */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002124 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02002125 struct chunk msg;
2126
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002127 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau91861262007-10-17 17:06:05 +02002128
Willy Tarreau295a8372011-03-10 11:25:07 +01002129 switch (si->applet.ctx.stats.px_st) {
2130 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02002131 /* we are on a new proxy */
2132
2133 if (uri && uri->scope) {
2134 /* we have a limited scope, we have to check the proxy name */
2135 struct stat_scope *scope;
2136 int len;
2137
2138 len = strlen(px->id);
2139 scope = uri->scope;
2140
2141 while (scope) {
2142 /* match exact proxy name */
2143 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2144 break;
2145
2146 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02002147 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02002148 break;
2149 scope = scope->next;
2150 }
2151
2152 /* proxy name not found : don't dump anything */
2153 if (scope == NULL)
2154 return 1;
2155 }
2156
Willy Tarreau295a8372011-03-10 11:25:07 +01002157 if ((si->applet.ctx.stats.flags & STAT_BOUND) && (si->applet.ctx.stats.iid != -1) &&
2158 (px->uuid != si->applet.ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002159 return 1;
2160
Willy Tarreau295a8372011-03-10 11:25:07 +01002161 si->applet.ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02002162 /* fall through */
2163
Willy Tarreau295a8372011-03-10 11:25:07 +01002164 case STAT_PX_ST_TH:
2165 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
2166 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002167 /* A form to enable/disable this proxy servers */
2168 chunk_printf(&msg,
2169 "<form action=\"%s\" method=\"post\">",
2170 uri->uri_prefix);
2171 }
2172
Willy Tarreau55bb8452007-10-17 18:44:57 +02002173 /* print a new table */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002174 chunk_printf(&msg,
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002175 "<table class=\"tbl\" width=\"100%%\">\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002176 "<tr class=\"titre\">"
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002177 "<th class=\"pxname\" width=\"10%%\"");
2178
2179 if (uri->flags&ST_SHLGNDS) {
2180 /* cap, mode, id */
2181 chunk_printf(&msg, " title=\"cap: %s, mode: %s, id: %d",
2182 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2183 px->uuid);
2184
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002185 chunk_printf(&msg, "\"");
2186 }
2187
2188 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002189 ">%s<a name=\"%s\"></a>"
2190 "<a class=px href=\"#%s\">%s</a>%s</th>"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002191 "<th class=\"%s\" width=\"90%%\">%s</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002192 "</tr>\n"
Krzysztof Piotr Oledzki48cb2ae2009-10-02 22:51:14 +02002193 "</table>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002194 "<table class=\"tbl\" width=\"100%%\">\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002195 "<tr class=\"titre\">",
2196 (uri->flags & ST_SHLGNDS)?"<u>":"",
2197 px->id, px->id, px->id,
2198 (uri->flags & ST_SHLGNDS)?"</u>":"",
2199 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2200
Willy Tarreau295a8372011-03-10 11:25:07 +01002201 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002202 /* Column heading for Enable or Disable server */
2203 chunk_printf(&msg, "<th rowspan=2 width=1></th>");
2204 }
2205
2206 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002207 "<th rowspan=2></th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002208 "<th colspan=3>Queue</th>"
2209 "<th colspan=3>Session rate</th><th colspan=5>Sessions</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002210 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002211 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002212 "<th colspan=9>Server</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002213 "</tr>\n"
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002214 "<tr class=\"titre\">"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002215 "<th>Cur</th><th>Max</th><th>Limit</th>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002216 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002217 "<th>Limit</th><th>Total</th><th>LbTot</th><th>In</th><th>Out</th>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002218 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002219 "<th>Resp</th><th>Retr</th><th>Redis</th>"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002220 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002221 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2222 "<th>Thrtle</th>\n"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002223 "</tr>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002224
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002225 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02002226 return 0;
2227 }
Willy Tarreau91861262007-10-17 17:06:05 +02002228
Willy Tarreau295a8372011-03-10 11:25:07 +01002229 si->applet.ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02002230 /* fall through */
2231
Willy Tarreau295a8372011-03-10 11:25:07 +01002232 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02002233 /* print the frontend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002234 if ((px->cap & PR_CAP_FE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002235 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_FE)))) {
2236 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002237 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002238 /* name, queue */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002239 "<tr class=\"frontend\">");
2240
Willy Tarreau295a8372011-03-10 11:25:07 +01002241 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002242 /* Column sub-heading for Enable or Disable server */
2243 chunk_printf(&msg, "<td></td>");
2244 }
2245
2246 chunk_printf(&msg,
2247 "<td class=ac>"
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002248 "<a name=\"%s/Frontend\"></a>"
Cyril Bonté70be45d2010-10-12 00:14:35 +02002249 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2250 "<td colspan=3></td>"
Willy Tarreaub44939a2010-02-26 11:35:39 +01002251 "",
2252 px->id, px->id);
2253
2254 if (px->mode == PR_MODE_HTTP) {
2255 chunk_printf(&msg,
2256 /* sessions rate : current, max, limit */
Willy Tarreaue0454092010-02-26 12:29:07 +01002257 "<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 +01002258 "",
2259 read_freq_ctr(&px->fe_req_per_sec),
2260 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002261 px->fe_counters.p.http.rps_max,
2262 U2H1(px->fe_counters.sps_max),
Willy Tarreaub44939a2010-02-26 11:35:39 +01002263 LIM2A2(px->fe_sps_lim, "-"));
2264 } else {
2265 chunk_printf(&msg,
2266 /* sessions rate : current, max, limit */
2267 "<td>%s</td><td>%s</td><td>%s</td>"
2268 "",
2269 U2H0(read_freq_ctr(&px->fe_sess_per_sec)),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002270 U2H1(px->fe_counters.sps_max), LIM2A2(px->fe_sps_lim, "-"));
Willy Tarreaub44939a2010-02-26 11:35:39 +01002271 }
2272
2273 chunk_printf(&msg,
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002274 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002275 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002276 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002277 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002278 U2H3(px->feconn), U2H4(px->fe_counters.conn_max), U2H5(px->maxconn));
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002279
2280 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2281 if (px->mode == PR_MODE_HTTP) {
2282 int i;
2283
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002284 chunk_printf(&msg, " title=\"%lld requests:", px->fe_counters.p.http.cum_req);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002285
2286 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002287 chunk_printf(&msg, " %dxx=%lld,", i, px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002288
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002289 chunk_printf(&msg, " other=%lld\"", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002290 }
2291
2292 chunk_printf(&msg,
2293 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002294 ">%s%s%s</td><td></td>"
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002295 /* bytes : in, out */
2296 "<td>%s</td><td>%s</td>"
2297 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002298 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002299 U2H6(px->fe_counters.cum_sess),
Willy Tarreaue0454092010-02-26 12:29:07 +01002300 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002301 U2H7(px->fe_counters.bytes_in), U2H8(px->fe_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002302
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002303 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002304 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002305 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002306 /* errors : request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002307 "<td>%s</td><td></td><td></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002308 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002309 "<td></td><td></td>"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002310 /* server status : reflect frontend status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002311 "<td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002312 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002313 "<td class=ac colspan=8></td></tr>"
Willy Tarreau91861262007-10-17 17:06:05 +02002314 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002315 U2H0(px->fe_counters.denied_req), U2H1(px->fe_counters.denied_resp),
2316 U2H2(px->fe_counters.failed_req),
Willy Tarreau562515c2011-07-25 08:11:52 +02002317 px->state == PR_STREADY ? "OPEN" :
2318 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002319 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002320 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002321 /* pxid, name, queue cur, queue max, */
2322 "%s,FRONTEND,,,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002323 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002324 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002325 /* bytes : in, out */
2326 "%lld,%lld,"
2327 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002328 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002329 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002330 "%lld,,,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002331 /* warnings: retries, redispatches */
2332 ",,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002333 /* server status : reflect frontend status */
2334 "%s,"
2335 /* rest of server: nothing */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002336 ",,,,,,,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002337 /* pid, iid, sid, throttle, lbtot, tracked, type */
2338 "%d,%d,0,,,,%d,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002339 /* rate, rate_lim, rate_max */
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002340 "%u,%u,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002341 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002342 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02002343 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002344 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2345 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2346 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2347 px->fe_counters.failed_req,
Willy Tarreau562515c2011-07-25 08:11:52 +02002348 px->state == PR_STREADY ? "OPEN" :
2349 px->state == PR_STFULL ? "FULL" : "STOP",
Willy Tarreau7f062c42009-03-05 18:43:00 +01002350 relative_pid, px->uuid, STATS_TYPE_FE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002351 read_freq_ctr(&px->fe_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002352 px->fe_sps_lim, px->fe_counters.sps_max);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002353
2354 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2355 if (px->mode == PR_MODE_HTTP) {
2356 int i;
2357
2358 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002359 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002360
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002361 chunk_printf(&msg, "%lld,", px->fe_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002362 } else {
2363 chunk_printf(&msg, ",,,,,,");
2364 }
2365
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002366 /* failed health analyses */
2367 chunk_printf(&msg, ",");
2368
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002369 /* requests : req_rate, req_rate_max, req_tot, */
2370 chunk_printf(&msg, "%u,%u,%lld,",
2371 read_freq_ctr(&px->fe_req_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002372 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002373
Willy Tarreauae526782010-03-04 20:34:23 +01002374 /* errors: cli_aborts, srv_aborts */
2375 chunk_printf(&msg, ",,");
2376
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002377 /* finish with EOL */
2378 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002379 }
Willy Tarreau91861262007-10-17 17:06:05 +02002380
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002381 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002382 return 0;
2383 }
2384
Willy Tarreau295a8372011-03-10 11:25:07 +01002385 si->applet.ctx.stats.l = px->listen; /* may be NULL */
2386 si->applet.ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002387 /* fall through */
2388
Willy Tarreau295a8372011-03-10 11:25:07 +01002389 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002390 /* stats.l has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002391 for (; si->applet.ctx.stats.l != NULL; si->applet.ctx.stats.l = l->next) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002392 if (buffer_almost_full(rep))
2393 return 0;
2394
Willy Tarreau295a8372011-03-10 11:25:07 +01002395 l = si->applet.ctx.stats.l;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002396 if (!l->counters)
2397 continue;
2398
Willy Tarreau295a8372011-03-10 11:25:07 +01002399 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2400 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002401 break;
2402
Willy Tarreau295a8372011-03-10 11:25:07 +01002403 if (si->applet.ctx.stats.sid != -1 && l->luid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002404 continue;
2405 }
2406
Willy Tarreau295a8372011-03-10 11:25:07 +01002407 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002408 chunk_printf(&msg, "<tr class=socket>");
Willy Tarreau295a8372011-03-10 11:25:07 +01002409 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002410 /* Column sub-heading for Enable or Disable server */
2411 chunk_printf(&msg, "<td></td>");
2412 }
2413 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002414
2415 if (uri->flags&ST_SHLGNDS) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02002416 char str[INET6_ADDRSTRLEN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002417 int port;
2418
Willy Tarreau631f01c2011-09-05 00:36:48 +02002419 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002420
Willy Tarreau631f01c2011-09-05 00:36:48 +02002421 port = get_host_port(&l->addr);
2422 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2423 case AF_INET:
2424 chunk_printf(&msg, "IPv4: %s:%d, ", str, port);
2425 break;
2426 case AF_INET6:
2427 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, port);
2428 break;
2429 case AF_UNIX:
2430 chunk_printf(&msg, "unix, ");
2431 break;
2432 case -1:
2433 chunk_printf(&msg, "(%s), ", strerror(errno));
2434 break;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002435 }
2436
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002437 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002438 chunk_printf(&msg, "id: %d\"", l->luid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002439 }
2440
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002441 chunk_printf(&msg,
2442 /* name, queue */
Willy Tarreaue0454092010-02-26 12:29:07 +01002443 ">%s<a name=\"%s/+%s\"></a>"
2444 "<a class=lfsb href=\"#%s/+%s\">%s</a></td><td colspan=3>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002445 /* sessions rate: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002446 "<td colspan=3>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002447 /* sessions: current, max, limit, total, lbtot */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002448 "<td>%s</td><td>%s</td><td>%s</td>"
2449 "<td>%s</td><td>&nbsp;</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002450 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002451 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002452 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002453 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki2ec025d2010-01-04 11:33:32 +01002454 px->id, l->name, px->id, l->name, l->name,
Willy Tarreaue0454092010-02-26 12:29:07 +01002455 (uri->flags & ST_SHLGNDS)?"</u>":"",
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002456 U2H3(l->nbconn), U2H4(l->counters->conn_max), U2H5(l->maxconn),
2457 U2H6(l->counters->cum_conn), U2H7(l->counters->bytes_in), U2H8(l->counters->bytes_out));
2458
2459 chunk_printf(&msg,
2460 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002461 "<td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002462 /* errors: request, connect, response */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002463 "<td>%s</td><td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002464 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002465 "<td></td><td></td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002466 /* server status: reflect listener status */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002467 "<td class=ac>%s</td>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002468 /* rest of server: nothing */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002469 "<td class=ac colspan=8></td></tr>"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002470 "",
2471 U2H0(l->counters->denied_req), U2H1(l->counters->denied_resp),
2472 U2H2(l->counters->failed_req),
Willy Tarreaua17c2d92011-07-25 08:16:20 +02002473 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002474 } else {
2475 chunk_printf(&msg,
2476 /* pxid, name, queue cur, queue max, */
2477 "%s,%s,,,"
2478 /* sessions: current, max, limit, total */
2479 "%d,%d,%d,%lld,"
2480 /* bytes: in, out */
2481 "%lld,%lld,"
2482 /* denied: req, resp */
2483 "%lld,%lld,"
2484 /* errors: request, connect, response */
2485 "%lld,,,"
2486 /* warnings: retries, redispatches */
2487 ",,"
2488 /* server status: reflect listener status */
2489 "%s,"
2490 /* rest of server: nothing */
2491 ",,,,,,,,"
2492 /* pid, iid, sid, throttle, lbtot, tracked, type */
2493 "%d,%d,%d,,,,%d,"
2494 /* rate, rate_lim, rate_max */
2495 ",,,"
2496 /* check_status, check_code, check_duration */
2497 ",,,"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002498 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2499 ",,,,,,"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002500 /* failed health analyses */
2501 ","
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002502 /* requests : req_rate, req_rate_max, req_tot, */
2503 ",,,"
Willy Tarreauae526782010-03-04 20:34:23 +01002504 /* errors: cli_aborts, srv_aborts */
2505 ",,"
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002506 "\n",
2507 px->id, l->name,
2508 l->nbconn, l->counters->conn_max,
2509 l->maxconn, l->counters->cum_conn,
2510 l->counters->bytes_in, l->counters->bytes_out,
2511 l->counters->denied_req, l->counters->denied_resp,
2512 l->counters->failed_req,
2513 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2514 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2515 }
2516
2517 if (buffer_feed_chunk(rep, &msg) >= 0)
2518 return 0;
2519 }
2520
Willy Tarreau295a8372011-03-10 11:25:07 +01002521 si->applet.ctx.stats.sv = px->srv; /* may be NULL */
2522 si->applet.ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02002523 /* fall through */
2524
Willy Tarreau295a8372011-03-10 11:25:07 +01002525 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02002526 /* stats.sv has been initialized above */
Willy Tarreau295a8372011-03-10 11:25:07 +01002527 for (; si->applet.ctx.stats.sv != NULL; si->applet.ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002528 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 +02002529
Willy Tarreau4e33d862009-10-11 23:35:10 +02002530 if (buffer_almost_full(rep))
2531 return 0;
2532
Willy Tarreau295a8372011-03-10 11:25:07 +01002533 sv = si->applet.ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02002534
Willy Tarreau295a8372011-03-10 11:25:07 +01002535 if (si->applet.ctx.stats.flags & STAT_BOUND) {
2536 if (!(si->applet.ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002537 break;
2538
Willy Tarreau295a8372011-03-10 11:25:07 +01002539 if (si->applet.ctx.stats.sid != -1 && sv->puid != si->applet.ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002540 continue;
2541 }
2542
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002543 if (sv->tracked)
2544 svs = sv->tracked;
2545 else
2546 svs = sv;
2547
Willy Tarreau91861262007-10-17 17:06:05 +02002548 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002549 if (!(svs->state & SRV_CHECKED))
Willy Tarreau2ea81932007-11-30 12:04:38 +01002550 sv_state = 6;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002551 else if (svs->state & SRV_RUNNING) {
2552 if (svs->health == svs->rise + svs->fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02002553 sv_state = 3; /* UP */
2554 else
2555 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01002556
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002557 if (svs->state & SRV_GOINGDOWN)
Willy Tarreau2ea81932007-11-30 12:04:38 +01002558 sv_state += 2;
2559 }
Willy Tarreau91861262007-10-17 17:06:05 +02002560 else
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002561 if (svs->health)
Willy Tarreau91861262007-10-17 17:06:05 +02002562 sv_state = 1; /* going up */
2563 else
2564 sv_state = 0; /* DOWN */
2565
Willy Tarreau295a8372011-03-10 11:25:07 +01002566 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (si->applet.ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02002567 /* do not report servers which are DOWN */
Willy Tarreau295a8372011-03-10 11:25:07 +01002568 si->applet.ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02002569 continue;
2570 }
2571
Willy Tarreau295a8372011-03-10 11:25:07 +01002572 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002573 static char *srv_hlt_st[7] = { "DOWN", "DN %d/%d &uarr;",
2574 "UP %d/%d &darr;", "UP",
2575 "NOLB %d/%d &darr;", "NOLB",
2576 "<i>no check</i>" };
Cyril Bonté0dae5852010-02-03 00:26:28 +01002577 if ((sv->state & SRV_MAINTAIN) || (svs->state & SRV_MAINTAIN)) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002578 chunk_printf(&msg,
2579 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580 "<tr class=\"maintain\">"
Cyril Bontécd19e512010-01-31 22:34:03 +01002581 );
2582 }
2583 else {
2584 chunk_printf(&msg,
2585 /* name */
Cyril Bonté70be45d2010-10-12 00:14:35 +02002586 "<tr class=\"%s%d\">",
Cyril Bontécd19e512010-01-31 22:34:03 +01002587 (sv->state & SRV_BACKUP) ? "backup" : "active", sv_state);
2588 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002589
Willy Tarreau295a8372011-03-10 11:25:07 +01002590 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté474be412010-10-12 00:14:36 +02002591 chunk_printf(&msg,
2592 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2593 sv->id);
2594 }
2595
2596 chunk_printf(&msg, "<td class=ac");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002597
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002598 if (uri->flags&ST_SHLGNDS) {
2599 char str[INET6_ADDRSTRLEN];
2600
Willy Tarreau631f01c2011-09-05 00:36:48 +02002601 chunk_printf(&msg, " title=\"");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002602
Willy Tarreau631f01c2011-09-05 00:36:48 +02002603 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
David du Colombier6f5ccb12011-03-10 22:26:24 +01002604 case AF_INET:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002605 chunk_printf(&msg, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
David du Colombier6f5ccb12011-03-10 22:26:24 +01002606 break;
2607 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02002608 chunk_printf(&msg, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2609 break;
2610 case AF_UNIX:
2611 chunk_printf(&msg, "unix, ");
2612 break;
2613 case -1:
2614 chunk_printf(&msg, "(%s), ", strerror(errno));
2615 break;
2616 default: /* address family not supported */
David du Colombier6f5ccb12011-03-10 22:26:24 +01002617 break;
2618 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002619
2620 /* id */
Willy Tarreau631f01c2011-09-05 00:36:48 +02002621 chunk_printf(&msg, "id: %d", sv->puid);
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002622
2623 /* cookie */
2624 if (sv->cookie) {
2625 struct chunk src;
2626
2627 chunk_printf(&msg, ", cookie: '");
2628
2629 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2630 chunk_htmlencode(&msg, &src);
2631
2632 chunk_printf(&msg, "'");
2633 }
2634
2635 chunk_printf(&msg, "\"");
2636 }
2637
2638 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002639 ">%s<a name=\"%s/%s\"></a>"
2640 "<a class=lfsb href=\"#%s/%s\">%s</a>%s</td>"
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002641 /* queue : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002642 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002643 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002644 "<td>%s</td><td>%s</td><td></td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002645 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002646 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002647 "<td"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002648 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002649 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002650 px->id, sv->id, px->id, sv->id, sv->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002651 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002652 U2H0(sv->nbpend), U2H1(sv->counters.nbpend_max), LIM2A2(sv->maxqueue, "-"),
2653 U2H3(read_freq_ctr(&sv->sess_per_sec)), U2H4(sv->counters.sps_max),
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002654 U2H5(sv->cur_sess), U2H6(sv->counters.cur_sess_max), LIM2A7(sv->maxconn, "-"));
2655
2656 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2657 if (px->mode == PR_MODE_HTTP) {
2658 int i;
2659
2660 chunk_printf(&msg, " title=\"rsp codes:");
2661
2662 for (i = 1; i < 6; i++)
2663 chunk_printf(&msg, " %dxx=%lld,", i, sv->counters.p.http.rsp[i]);
2664
2665 chunk_printf(&msg, " other=%lld\"", sv->counters.p.http.rsp[0]);
2666 }
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02002667
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002668 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002669 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01002670 ">%s%s%s</td><td>%s</td>",
2671 (px->mode == PR_MODE_HTTP)?"<u>":"",
2672 U2H0(sv->counters.cum_sess),
2673 (px->mode == PR_MODE_HTTP)?"</u>":"",
2674 U2H1(sv->counters.cum_lbconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002675
2676 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002677 /* bytes : in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002678 "<td>%s</td><td>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002679 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002680 "<td></td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01002681 /* errors : request, connect */
2682 "<td></td><td>%s</td>"
2683 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002684 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002685 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002686 "<td>%lld</td><td>%lld</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002687 "",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002688 U2H0(sv->counters.bytes_in), U2H1(sv->counters.bytes_out),
2689 U2H2(sv->counters.failed_secu),
Willy Tarreauae526782010-03-04 20:34:23 +01002690 U2H3(sv->counters.failed_conns),
Willy Tarreau6a8573e2010-03-05 18:15:23 +01002691 sv->counters.cli_aborts,
2692 sv->counters.srv_aborts,
Willy Tarreauae526782010-03-04 20:34:23 +01002693 U2H6(sv->counters.failed_resp),
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002694 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002695
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002696 /* status, lest check */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002697 chunk_printf(&msg, "<td class=ac>");
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002698
Cyril Bontécd19e512010-01-31 22:34:03 +01002699 if (sv->state & SRV_MAINTAIN) {
2700 chunk_printf(&msg, "%s ",
2701 human_time(now.tv_sec - sv->last_change, 1));
2702 chunk_printf(&msg, "MAINT");
2703 }
Cyril Bonté0dae5852010-02-03 00:26:28 +01002704 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2705 chunk_printf(&msg, "%s ",
2706 human_time(now.tv_sec - svs->last_change, 1));
2707 chunk_printf(&msg, "MAINT(via)");
2708 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002709 else if (svs->state & SRV_CHECKED) {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002710 chunk_printf(&msg, "%s ",
Cyril Bonté0dae5852010-02-03 00:26:28 +01002711 human_time(now.tv_sec - svs->last_change, 1));
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002712
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002713 chunk_printf(&msg,
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002714 srv_hlt_st[sv_state],
2715 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2716 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002717 }
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002718
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002719 if (sv->state & SRV_CHECKED) {
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002720 chunk_printf(&msg, "</td><td class=ac title=\"%s",
Krzysztof Piotr Oledzkif7089f52009-10-10 21:06:49 +02002721 get_check_status_description(sv->check_status));
2722
2723 if (*sv->check_desc) {
2724 struct chunk src;
2725
2726 chunk_printf(&msg, ": ");
2727
2728 chunk_initlen(&src, sv->check_desc, 0, strlen(sv->check_desc));
2729 chunk_htmlencode(&msg, &src);
2730 }
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002731
Willy Tarreaue0454092010-02-26 12:29:07 +01002732 chunk_printf(&msg, "\"><u> %s%s",
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002733 tv_iszero(&sv->check_start)?"":"* ",
2734 get_check_status_info(sv->check_status));
2735
2736 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002737 chunk_printf(&msg, "/%d", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002738
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002739 if (sv->check_status >= HCHK_STATUS_CHECKED && sv->check_duration >= 0)
Willy Tarreaue0454092010-02-26 12:29:07 +01002740 chunk_printf(&msg, " in %lums</u>", sv->check_duration);
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002741 } else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002742 chunk_printf(&msg, "</td><td>");
Willy Tarreau91861262007-10-17 17:06:05 +02002743
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002744 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02002745 /* weight */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002746 "</td><td class=ac>%d</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002747 /* act, bck */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002748 "<td class=ac>%s</td><td class=ac>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002749 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002750 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau91861262007-10-17 17:06:05 +02002751 (sv->state & SRV_BACKUP) ? "-" : "Y",
2752 (sv->state & SRV_BACKUP) ? "Y" : "-");
2753
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002754 /* check failures: unique, fatal, down time */
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002755 if (sv->state & SRV_CHECKED) {
Willy Tarreaue0454092010-02-26 12:29:07 +01002756 chunk_printf(&msg, "<td title=\"Failed Health Checks%s\"><u>%lld",
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002757 svs->observe?"/Health Analyses":"", svs->counters.failed_checks);
2758
2759 if (svs->observe)
2760 chunk_printf(&msg, "/%lld", svs->counters.failed_hana);
2761
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002762 chunk_printf(&msg,
Willy Tarreaue0454092010-02-26 12:29:07 +01002763 "</u></td>"
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002764 "<td>%lld</td><td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002765 "",
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002766 svs->counters.down_trans, human_time(srv_downtime(sv), 1));
Krzysztof Piotr Oledzki748196e2010-01-04 00:48:43 +01002767 } else if (sv != svs)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002768 chunk_printf(&msg,
Krzysztof Piotr Oledzki88a14af2010-01-05 18:33:01 +01002769 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2770 svs->proxy->id, svs->id, svs->proxy->id, svs->id);
Willy Tarreau55bb8452007-10-17 18:44:57 +02002771 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002772 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002773 "<td colspan=3></td>");
2774
2775 /* throttle */
2776 if ((sv->state & SRV_WARMINGUP) &&
2777 now.tv_sec < sv->last_change + sv->slowstart &&
2778 now.tv_sec >= sv->last_change) {
2779 unsigned int ratio;
2780 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002781 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002782 "<td class=ac>%d %%</td></tr>\n", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002783 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002784 chunk_printf(&msg,
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002785 "<td class=ac>-</td></tr>\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002786 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02002787 } else {
Willy Tarreau2ea81932007-11-30 12:04:38 +01002788 static char *srv_hlt_st[7] = { "DOWN,", "DOWN %d/%d,",
2789 "UP %d/%d,", "UP,",
2790 "NOLB %d/%d,", "NOLB,",
2791 "no check," };
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002792 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002793 /* pxid, name */
2794 "%s,%s,"
2795 /* queue : current, max */
2796 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002797 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002798 "%d,%d,%s,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002799 /* bytes : in, out */
2800 "%lld,%lld,"
2801 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002802 ",%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002803 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002804 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02002805 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02002806 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02002807 "",
2808 px->id, sv->id,
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002809 sv->nbpend, sv->counters.nbpend_max,
2810 sv->cur_sess, sv->counters.cur_sess_max, LIM2A0(sv->maxconn, ""), sv->counters.cum_sess,
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002811 sv->counters.bytes_in, sv->counters.bytes_out,
2812 sv->counters.failed_secu,
2813 sv->counters.failed_conns, sv->counters.failed_resp,
2814 sv->counters.retries, sv->counters.redispatches);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002815
Willy Tarreau55bb8452007-10-17 18:44:57 +02002816 /* status */
Cyril Bontécd19e512010-01-31 22:34:03 +01002817 if (sv->state & SRV_MAINTAIN) {
2818 chunk_printf(&msg, "MAINT,");
Cyril Bonté0dae5852010-02-03 00:26:28 +01002819 }
2820 else if (svs != sv && svs->state & SRV_MAINTAIN) {
2821 chunk_printf(&msg, "MAINT(via),");
2822 }
2823 else {
Cyril Bontécd19e512010-01-31 22:34:03 +01002824 chunk_printf(&msg,
2825 srv_hlt_st[sv_state],
2826 (svs->state & SRV_RUNNING) ? (svs->health - svs->rise + 1) : (svs->health),
2827 (svs->state & SRV_RUNNING) ? (svs->fall) : (svs->rise));
2828 }
Willy Tarreau91861262007-10-17 17:06:05 +02002829
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002830 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002831 /* weight, active, backup */
2832 "%d,%d,%d,"
2833 "",
Willy Tarreau5542af62007-12-03 02:04:00 +01002834 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau55bb8452007-10-17 18:44:57 +02002835 (sv->state & SRV_BACKUP) ? 0 : 1,
2836 (sv->state & SRV_BACKUP) ? 1 : 0);
2837
Krzysztof Oledzki85130942007-10-22 16:21:10 +02002838 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreau55bb8452007-10-17 18:44:57 +02002839 if (sv->state & SRV_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002840 chunk_printf(&msg,
Willy Tarreau3b88d442009-04-11 20:44:08 +02002841 "%lld,%lld,%d,%d,",
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002842 sv->counters.failed_checks, sv->counters.down_trans,
Willy Tarreau1772ece2009-04-03 14:49:12 +02002843 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002844 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002845 chunk_printf(&msg,
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02002846 ",,,,");
2847
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002848 /* queue limit, pid, iid, sid, */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002849 chunk_printf(&msg,
Willy Tarreaudcd47712007-11-04 23:35:08 +01002850 "%s,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002851 "%d,%d,%d,",
Willy Tarreaudcd47712007-11-04 23:35:08 +01002852 LIM2A0(sv->maxqueue, ""),
2853 relative_pid, px->uuid, sv->puid);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002854
2855 /* throttle */
2856 if ((sv->state & SRV_WARMINGUP) &&
2857 now.tv_sec < sv->last_change + sv->slowstart &&
2858 now.tv_sec >= sv->last_change) {
2859 unsigned int ratio;
2860 ratio = MAX(1, 100 * (now.tv_sec - sv->last_change) / sv->slowstart);
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002861 chunk_printf(&msg, "%d", ratio);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01002862 }
2863
Willy Tarreauddbb82f2007-12-05 10:34:49 +01002864 /* sessions: lbtot */
Krzysztof Piotr Oledzki052d4fd2009-10-04 14:52:57 +02002865 chunk_printf(&msg, ",%lld,", sv->counters.cum_lbconn);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002866
2867 /* tracked */
2868 if (sv->tracked)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002869 chunk_printf(&msg, "%s/%s,",
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002870 sv->tracked->proxy->id, sv->tracked->id);
2871 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002872 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002873
Willy Tarreau7f062c42009-03-05 18:43:00 +01002874 /* type */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002875 chunk_printf(&msg, "%d,", STATS_TYPE_SV);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002876
2877 /* rate */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002878 chunk_printf(&msg, "%u,,%u,",
Willy Tarreau8f208ec2009-05-10 19:01:49 +02002879 read_freq_ctr(&sv->sess_per_sec),
Willy Tarreauac68c5d2009-10-04 23:12:44 +02002880 sv->counters.sps_max);
Willy Tarreau7f062c42009-03-05 18:43:00 +01002881
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002882 if (sv->state & SRV_CHECKED) {
2883 /* check_status */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002884 chunk_printf(&msg, "%s,", get_check_status_info(sv->check_status));
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002885
2886 /* check_code */
2887 if (sv->check_status >= HCHK_STATUS_L57DATA)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002888 chunk_printf(&msg, "%u,", sv->check_code);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002889 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002890 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002891
2892 /* check_duration */
Krzysztof Piotr Oledzki034550b2010-01-05 18:44:44 +01002893 if (sv->check_status >= HCHK_STATUS_CHECKED)
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002894 chunk_printf(&msg, "%lu,", sv->check_duration);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002895 else
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002896 chunk_printf(&msg, ",");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002897
2898 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002899 chunk_printf(&msg, ",,,");
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02002900 }
2901
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002902 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2903 if (px->mode == PR_MODE_HTTP) {
2904 int i;
2905
2906 for (i=1; i<6; i++)
2907 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[i]);
2908
2909 chunk_printf(&msg, "%lld,", sv->counters.p.http.rsp[0]);
2910 } else {
2911 chunk_printf(&msg, ",,,,,,");
2912 }
2913
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01002914 /* failed health analyses */
2915 chunk_printf(&msg, "%lld,", sv->counters.failed_hana);
2916
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002917 /* requests : req_rate, req_rate_max, req_tot, */
2918 chunk_printf(&msg, ",,,");
2919
Willy Tarreauae526782010-03-04 20:34:23 +01002920 /* errors: cli_aborts, srv_aborts */
2921 chunk_printf(&msg, "%lld,%lld,",
2922 sv->counters.cli_aborts, sv->counters.srv_aborts);
2923
Willy Tarreau7f062c42009-03-05 18:43:00 +01002924 /* finish with EOL */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002925 chunk_printf(&msg, "\n");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002926 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002927 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02002928 return 0;
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01002929 } /* for sv */
Willy Tarreau91861262007-10-17 17:06:05 +02002930
Willy Tarreau295a8372011-03-10 11:25:07 +01002931 si->applet.ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreau91861262007-10-17 17:06:05 +02002932 /* fall through */
2933
Willy Tarreau295a8372011-03-10 11:25:07 +01002934 case STAT_PX_ST_BE:
Willy Tarreau91861262007-10-17 17:06:05 +02002935 /* print the backend */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01002936 if ((px->cap & PR_CAP_BE) &&
Willy Tarreau295a8372011-03-10 11:25:07 +01002937 (!(si->applet.ctx.stats.flags & STAT_BOUND) || (si->applet.ctx.stats.type & (1 << STATS_TYPE_BE)))) {
2938 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002939 chunk_printf(&msg, "<tr class=\"backend\">");
Willy Tarreau295a8372011-03-10 11:25:07 +01002940 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002941 /* Column sub-heading for Enable or Disable server */
2942 chunk_printf(&msg, "<td></td>");
2943 }
2944 chunk_printf(&msg, "<td class=ac");
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002945
2946 if (uri->flags&ST_SHLGNDS) {
2947 /* balancing */
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002948 chunk_printf(&msg, " title=\"balancing: %s",
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002949 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Krzysztof Piotr Oledzkic0f0c862010-01-06 15:03:18 +01002950
2951 /* cookie */
2952 if (px->cookie_name) {
2953 struct chunk src;
2954
2955 chunk_printf(&msg, ", cookie: '");
2956
2957 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
2958 chunk_htmlencode(&msg, &src);
2959
2960 chunk_printf(&msg, "'");
2961 }
2962
2963 chunk_printf(&msg, "\"");
2964
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01002965 }
2966
2967 chunk_printf(&msg,
Cyril Bonté70be45d2010-10-12 00:14:35 +02002968 /* name */
Willy Tarreaue0454092010-02-26 12:29:07 +01002969 ">%s<a name=\"%s/Backend\"></a>"
2970 "<a class=lfsb href=\"#%s/Backend\">Backend</a>%s</td>"
Willy Tarreau91861262007-10-17 17:06:05 +02002971 /* queue : current, max */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002972 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002973 /* sessions rate : current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002974 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaua3e49422009-05-10 19:19:41 +02002975 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01002976 (uri->flags & ST_SHLGNDS)?"<u>":"",
Krzysztof Piotr Oledzki1f672852009-10-24 14:24:30 +02002977 px->id, px->id,
Willy Tarreaue0454092010-02-26 12:29:07 +01002978 (uri->flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002979 U2H0(px->nbpend) /* or px->totpend ? */, U2H1(px->be_counters.nbpend_max),
2980 U2H2(read_freq_ctr(&px->be_sess_per_sec)), U2H3(px->be_counters.sps_max));
Willy Tarreaua3e49422009-05-10 19:19:41 +02002981
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02002982 chunk_printf(&msg,
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002983 /* sessions: current, max, limit */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02002984 "<td>%s</td><td>%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002985 "<td"
2986 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002987 U2H2(px->beconn), U2H3(px->be_counters.conn_max), U2H4(px->fullconn));
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002988
2989 /* http response (via td title): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2990 if (px->mode == PR_MODE_HTTP) {
2991 int i;
2992
2993 chunk_printf(&msg, " title=\"rsp codes:");
2994
2995 for (i = 1; i < 6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002996 chunk_printf(&msg, " %dxx=%lld", i, px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002997
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002998 chunk_printf(&msg, " other=%lld\"", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02002999 }
3000
3001 chunk_printf(&msg,
3002 /* sessions: total, lbtot */
Willy Tarreaue0454092010-02-26 12:29:07 +01003003 ">%s%s%s</td><td>%s</td>"
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003004 /* bytes: in, out */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003005 "<td>%s</td><td>%s</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003006 "",
Willy Tarreaue0454092010-02-26 12:29:07 +01003007 (px->mode == PR_MODE_HTTP)?"<u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003008 U2H6(px->be_counters.cum_conn),
Willy Tarreaue0454092010-02-26 12:29:07 +01003009 (px->mode == PR_MODE_HTTP)?"</u>":"",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003010 U2H7(px->be_counters.cum_lbconn),
3011 U2H8(px->be_counters.bytes_in), U2H9(px->be_counters.bytes_out));
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003012
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003013 chunk_printf(&msg,
Willy Tarreau91861262007-10-17 17:06:05 +02003014 /* denied: req, resp */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003015 "<td>%s</td><td>%s</td>"
Willy Tarreauae526782010-03-04 20:34:23 +01003016 /* errors : request, connect */
3017 "<td></td><td>%s</td>"
3018 /* errors : response */
Willy Tarreau6a8573e2010-03-05 18:15:23 +01003019 "<td title=\"Connection resets during transfers: %lld client, %lld server\"><u>%s</u></td>"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003020 /* warnings: retries, redispatches */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003021 "<td>%lld</td><td>%lld</td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003022 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau91861262007-10-17 17:06:05 +02003023 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003024 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau91861262007-10-17 17:06:05 +02003025 * active and backups. */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003026 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3027 "<td class=ac>%d</td><td class=ac>%d</td>"
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02003028 "",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003029 U2H0(px->be_counters.denied_req), U2H1(px->be_counters.denied_resp),
3030 U2H2(px->be_counters.failed_conns),
3031 px->be_counters.cli_aborts,
3032 px->be_counters.srv_aborts,
3033 U2H5(px->be_counters.failed_resp),
3034 px->be_counters.retries, px->be_counters.redispatches,
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003035 human_time(now.tv_sec - px->last_change, 1),
Willy Tarreau2ea81932007-11-30 12:04:38 +01003036 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3037 "<font color=\"red\"><b>DOWN</b></font>",
Willy Tarreau5542af62007-12-03 02:04:00 +01003038 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003039 px->srv_act, px->srv_bck);
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003040
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003041 chunk_printf(&msg,
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003042 /* rest of backend: nothing, down transitions, total downtime, throttle */
Krzysztof Piotr Oledzkif2d2b1d2009-10-12 23:09:08 +02003043 "<td class=ac>&nbsp;</td><td>%d</td>"
3044 "<td>%s</td>"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003045 "<td></td>"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003046 "</tr>",
3047 px->down_trans,
3048 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003049 } else {
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003050 chunk_printf(&msg,
Willy Tarreau55bb8452007-10-17 18:44:57 +02003051 /* pxid, name */
3052 "%s,BACKEND,"
3053 /* queue : current, max */
3054 "%d,%d,"
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003055 /* sessions : current, max, limit, total */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003056 "%d,%d,%d,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003057 /* bytes : in, out */
3058 "%lld,%lld,"
3059 /* denied: req, resp */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003060 "%lld,%lld,"
Willy Tarreau55bb8452007-10-17 18:44:57 +02003061 /* errors : request, connect, response */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003062 ",%lld,%lld,"
Krzysztof Oledzki1cf36ba2007-10-18 19:12:30 +02003063 /* warnings: retries, redispatches */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003064 "%lld,%lld,"
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003065 /* backend status: reflect backend status (up/down): we display UP
Willy Tarreau55bb8452007-10-17 18:44:57 +02003066 * if the backend has known working servers or if it has no server at
Krzysztof Oledzki85130942007-10-22 16:21:10 +02003067 * all (eg: for stats). Then we display the total weight, number of
Willy Tarreau55bb8452007-10-17 18:44:57 +02003068 * active and backups. */
3069 "%s,"
3070 "%d,%d,%d,"
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003071 /* rest of backend: nothing, down transitions, last change, total downtime */
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003072 ",%d,%d,%d,,"
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003073 /* pid, iid, sid, throttle, lbtot, tracked, type */
Willy Tarreau3b88d442009-04-11 20:44:08 +02003074 "%d,%d,0,,%lld,,%d,"
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003075 /* rate, rate_lim, rate_max, */
3076 "%u,,%u,"
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003077 /* check_status, check_code, check_duration */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003078 ",,,",
Willy Tarreau55bb8452007-10-17 18:44:57 +02003079 px->id,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003080 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3081 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3082 px->be_counters.bytes_in, px->be_counters.bytes_out,
3083 px->be_counters.denied_req, px->be_counters.denied_resp,
3084 px->be_counters.failed_conns, px->be_counters.failed_resp,
3085 px->be_counters.retries, px->be_counters.redispatches,
Willy Tarreau20697042007-11-15 23:26:18 +01003086 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
Willy Tarreau5542af62007-12-03 02:04:00 +01003087 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreau20697042007-11-15 23:26:18 +01003088 px->srv_act, px->srv_bck,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003089 px->down_trans, (int)(now.tv_sec - px->last_change),
Willy Tarreau20697042007-11-15 23:26:18 +01003090 px->srv?be_downtime(px):0,
Willy Tarreauddbb82f2007-12-05 10:34:49 +01003091 relative_pid, px->uuid,
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003092 px->be_counters.cum_lbconn, STATS_TYPE_BE,
Willy Tarreau8f208ec2009-05-10 19:01:49 +02003093 read_freq_ctr(&px->be_sess_per_sec),
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003094 px->be_counters.sps_max);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003095
3096 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3097 if (px->mode == PR_MODE_HTTP) {
3098 int i;
3099
3100 for (i=1; i<6; i++)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003101 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[i]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003102
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003103 chunk_printf(&msg, "%lld,", px->be_counters.p.http.rsp[0]);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003104 } else {
3105 chunk_printf(&msg, ",,,,,,");
3106 }
3107
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003108 /* failed health analyses */
3109 chunk_printf(&msg, ",");
3110
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003111 /* requests : req_rate, req_rate_max, req_tot, */
3112 chunk_printf(&msg, ",,,");
3113
Willy Tarreauae526782010-03-04 20:34:23 +01003114 /* errors: cli_aborts, srv_aborts */
3115 chunk_printf(&msg, "%lld,%lld,",
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003116 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
Willy Tarreauae526782010-03-04 20:34:23 +01003117
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003118 /* finish with EOL */
3119 chunk_printf(&msg, "\n");
3120
Willy Tarreau55bb8452007-10-17 18:44:57 +02003121 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003122 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau91861262007-10-17 17:06:05 +02003123 return 0;
3124 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003125
Willy Tarreau295a8372011-03-10 11:25:07 +01003126 si->applet.ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreau91861262007-10-17 17:06:05 +02003127 /* fall through */
3128
Willy Tarreau295a8372011-03-10 11:25:07 +01003129 case STAT_PX_ST_END:
3130 if (!(si->applet.ctx.stats.flags & STAT_FMT_CSV)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003131 chunk_printf(&msg, "</table>");
3132
Willy Tarreau295a8372011-03-10 11:25:07 +01003133 if (px->cap & PR_CAP_BE && px->srv && (si->applet.ctx.stats.flags & STAT_ADMIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02003134 /* close the form used to enable/disable this proxy servers */
3135 chunk_printf(&msg,
3136 "Choose the action to perform on the checked servers : "
3137 "<select name=action>"
3138 "<option value=\"\"></option>"
3139 "<option value=\"disable\">Disable</option>"
3140 "<option value=\"enable\">Enable</option>"
3141 "</select>"
3142 "<input type=\"hidden\" name=\"b\" value=\"%s\">"
3143 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3144 "</form>",
3145 px->id);
3146 }
3147
3148 chunk_printf(&msg, "<p>\n");
Willy Tarreau91861262007-10-17 17:06:05 +02003149
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003150 if (buffer_feed_chunk(rep, &msg) >= 0)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003151 return 0;
3152 }
Willy Tarreau91861262007-10-17 17:06:05 +02003153
Willy Tarreau295a8372011-03-10 11:25:07 +01003154 si->applet.ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreau91861262007-10-17 17:06:05 +02003155 /* fall through */
3156
Willy Tarreau295a8372011-03-10 11:25:07 +01003157 case STAT_PX_ST_FIN:
Willy Tarreau91861262007-10-17 17:06:05 +02003158 return 1;
3159
3160 default:
3161 /* unknown state, we should put an abort() here ! */
3162 return 1;
3163 }
3164}
3165
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003166/* This function dumps a complete session state onto the stream intreface's
3167 * read buffer. The data_ctx must have been zeroed first, and the flags
3168 * properly set. The session has to be set in data_ctx.sess.target. It returns
3169 * 0 if the output buffer is full and it needs to be called again, otherwise
3170 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003171 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003172static int stats_dump_full_sess_to_buffer(struct stream_interface *si)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003173{
3174 struct tm tm;
3175 struct chunk msg;
3176 struct session *sess;
3177 extern const char *monthname[12];
3178 char pn[INET6_ADDRSTRLEN];
3179
3180 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau295a8372011-03-10 11:25:07 +01003181 sess = si->applet.ctx.sess.target;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003182
Willy Tarreau295a8372011-03-10 11:25:07 +01003183 if (si->applet.ctx.sess.section > 0 && si->applet.ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003184 /* session changed, no need to go any further */
3185 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003186 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003187 return 0;
Willy Tarreau295a8372011-03-10 11:25:07 +01003188 si->applet.ctx.sess.target = NULL;
3189 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003190 return 1;
3191 }
3192
Willy Tarreau295a8372011-03-10 11:25:07 +01003193 switch (si->applet.ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003194 case 0: /* main status of the session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003195 si->applet.ctx.sess.uid = sess->uniq_id;
3196 si->applet.ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003197 /* fall through */
3198
3199 case 1:
3200 chunk_printf(&msg,
3201 "%p: id=%u, proto=%s",
3202 sess,
3203 sess->uniq_id,
3204 sess->listener->proto->name);
3205
Willy Tarreau631f01c2011-09-05 00:36:48 +02003206 switch (addr_to_str(&sess->si[0].addr.c.from, pn, sizeof(pn))) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003207 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003208 case AF_INET6:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003209 chunk_printf(&msg, " source=%s:%d\n",
3210 pn, get_host_port(&sess->si[0].addr.c.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003211 break;
3212 case AF_UNIX:
Willy Tarreau631f01c2011-09-05 00:36:48 +02003213 chunk_printf(&msg, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02003214 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003215 default:
3216 /* no more information to print right now */
3217 chunk_printf(&msg, "\n");
3218 break;
3219 }
3220
3221 chunk_printf(&msg,
3222 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02003223 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003224
3225 chunk_printf(&msg,
3226 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)\n",
3227 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
3228 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
3229 sess->listener ? sess->listener->luid : 0);
3230
Willy Tarreau50943332011-09-02 17:33:05 +02003231 if (sess->be->cap & PR_CAP_BE)
3232 chunk_printf(&msg,
3233 " backend=%s (id=%u mode=%s) server=%s (id=%u)\n",
3234 sess->be->id,
3235 sess->be->uuid, sess->be->mode ? "http" : "tcp",
3236 target_srv(&sess->target) ? target_srv(&sess->target)->id : "<none>",
3237 target_srv(&sess->target) ? target_srv(&sess->target)->puid : 0);
3238 else
3239 chunk_printf(&msg, " backend=<NONE> (id=-1 mode=-) server=<NONE> (id=-1)\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003240
3241 chunk_printf(&msg,
3242 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s)\n",
3243 sess->task,
3244 sess->task->state,
3245 sess->task->nice, sess->task->calls,
3246 sess->task->expire ?
3247 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
3248 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
3249 TICKS_TO_MS(1000)) : "<NEVER>",
3250 task_in_rq(sess->task) ? ", running" : "");
3251
3252 get_localtime(sess->logs.accept_date.tv_sec, &tm);
3253 chunk_printf(&msg,
3254 " task created [%02d/%s/%04d:%02d:%02d:%02d.%06d] (age=%s)\n",
3255 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3256 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
3257 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
3258
3259 chunk_printf(&msg,
3260 " si[0]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3261 &sess->si[0],
3262 sess->si[0].state,
3263 sess->si[0].flags,
3264 sess->si[0].fd,
3265 sess->si[0].exp ?
3266 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
3267 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
3268 TICKS_TO_MS(1000)) : "<NEVER>",
3269 sess->si[0].err_type);
3270
3271 chunk_printf(&msg,
3272 " si[1]=%p (state=%d flags=0x%02x fd=%d exp=%s, et=0x%03x)\n",
3273 &sess->si[1],
3274 sess->si[1].state,
3275 sess->si[1].flags,
3276 sess->si[1].fd,
3277 sess->si[1].exp ?
3278 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
3279 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
3280 TICKS_TO_MS(1000)) : "<NEVER>",
3281 sess->si[1].err_type);
3282
3283 chunk_printf(&msg,
3284 " txn=%p (flags=0x%x meth=%d status=%d req.st=%d rsp.st=%d)\n",
3285 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
3286 sess->txn.req.msg_state, sess->txn.rsp.msg_state);
3287
3288
3289 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003290 " req=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003291 " an_exp=%s",
3292 sess->req,
3293 sess->req->flags, sess->req->analysers,
3294 sess->req->l, sess->req->send_max,
3295 sess->req->pipe ? sess->req->pipe->data : 0,
3296 sess->req->to_forward,
3297 sess->req->analyse_exp ?
3298 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
3299 TICKS_TO_MS(1000)) : "<NEVER>");
3300
3301 chunk_printf(&msg,
3302 " rex=%s",
3303 sess->req->rex ?
3304 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
3305 TICKS_TO_MS(1000)) : "<NEVER>");
3306
3307 chunk_printf(&msg,
3308 " wex=%s\n"
3309 " data=%p r=%d w=%d lr=%d total=%lld\n",
3310 sess->req->wex ?
3311 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
3312 TICKS_TO_MS(1000)) : "<NEVER>",
3313 sess->req->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003314 (int)(sess->req->r - sess->req->data),
3315 (int)(sess->req->w - sess->req->data),
3316 (int)(sess->req->lr - sess->req->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003317 sess->req->total);
3318
3319 chunk_printf(&msg,
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02003320 " res=%p (f=0x%06x an=0x%x l=%d sndmx=%d pipe=%d fwd=%d)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003321 " an_exp=%s",
3322 sess->rep,
3323 sess->rep->flags, sess->rep->analysers,
3324 sess->rep->l, sess->rep->send_max,
3325 sess->rep->pipe ? sess->rep->pipe->data : 0,
3326 sess->rep->to_forward,
3327 sess->rep->analyse_exp ?
3328 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
3329 TICKS_TO_MS(1000)) : "<NEVER>");
3330
3331 chunk_printf(&msg,
3332 " rex=%s",
3333 sess->rep->rex ?
3334 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
3335 TICKS_TO_MS(1000)) : "<NEVER>");
3336
3337 chunk_printf(&msg,
3338 " wex=%s\n"
3339 " data=%p r=%d w=%d lr=%d total=%lld\n",
3340 sess->rep->wex ?
3341 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
3342 TICKS_TO_MS(1000)) : "<NEVER>",
3343 sess->rep->data,
Willy Tarreau4e1554c2010-03-21 23:21:00 +01003344 (int)(sess->rep->r - sess->rep->data),
3345 (int)(sess->rep->w - sess->rep->data),
3346 (int)(sess->rep->lr - sess->rep->data),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003347 sess->rep->total);
3348
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003349 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003350 return 0;
3351
3352 /* use other states to dump the contents */
3353 }
3354 /* end of dump */
Willy Tarreau295a8372011-03-10 11:25:07 +01003355 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003356 return 1;
3357}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003358
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003359/* This function dumps all sessions' states onto the stream intreface's
3360 * read buffer. The data_ctx must have been zeroed first, and the flags
3361 * properly set. It returns 0 if the output buffer is full and it needs
3362 * to be called again, otherwise non-zero. It is designed to be called
3363 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003364 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003365static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003366{
3367 struct chunk msg;
3368
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003369 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003370 /* If we're forced to shut down, we might have to remove our
3371 * reference to the last session being dumped.
3372 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003373 if (si->applet.state == STAT_ST_LIST) {
3374 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3375 LIST_DEL(&si->applet.ctx.sess.bref.users);
3376 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003377 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003378 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003379 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003380 }
3381
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003382 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003383
Willy Tarreau295a8372011-03-10 11:25:07 +01003384 switch (si->applet.state) {
3385 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003386 /* the function had not been called yet, let's prepare the
3387 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003388 * pointer to the first in the global list. When a target
3389 * session is being destroyed, it is responsible for updating
3390 * this pointer. We know we have reached the end when this
3391 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003392 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003393 LIST_INIT(&si->applet.ctx.sess.bref.users);
3394 si->applet.ctx.sess.bref.ref = sessions.n;
3395 si->applet.state = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003396 /* fall through */
3397
Willy Tarreau295a8372011-03-10 11:25:07 +01003398 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003399 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau295a8372011-03-10 11:25:07 +01003400 if (!LIST_ISEMPTY(&si->applet.ctx.sess.bref.users)) {
3401 LIST_DEL(&si->applet.ctx.sess.bref.users);
3402 LIST_INIT(&si->applet.ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003403 }
3404
3405 /* and start from where we stopped */
Willy Tarreau295a8372011-03-10 11:25:07 +01003406 while (si->applet.ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01003407 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003408 struct session *curr_sess;
3409
Willy Tarreau295a8372011-03-10 11:25:07 +01003410 curr_sess = LIST_ELEM(si->applet.ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003411
Willy Tarreau295a8372011-03-10 11:25:07 +01003412 if (si->applet.ctx.sess.target) {
3413 if (si->applet.ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003414 goto next_sess;
3415
Willy Tarreau295a8372011-03-10 11:25:07 +01003416 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003417 /* call the proper dump() function and return if we're missing space */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003418 if (!stats_dump_full_sess_to_buffer(si))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003419 return 0;
3420
3421 /* session dump complete */
Willy Tarreau295a8372011-03-10 11:25:07 +01003422 LIST_DEL(&si->applet.ctx.sess.bref.users);
3423 LIST_INIT(&si->applet.ctx.sess.bref.users);
3424 si->applet.ctx.sess.target = NULL;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003425 break;
3426 }
3427
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003428 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003429 "%p: proto=%s",
3430 curr_sess,
3431 curr_sess->listener->proto->name);
3432
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003433
Willy Tarreau631f01c2011-09-05 00:36:48 +02003434 switch (addr_to_str(&curr_sess->si[0].addr.c.from, pn, sizeof(pn))) {
3435 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003436 case AF_INET6:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003437 chunk_printf(&msg,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003438 " src=%s:%d fe=%s be=%s srv=%s",
3439 pn,
Willy Tarreau631f01c2011-09-05 00:36:48 +02003440 get_host_port(&curr_sess->si[0].addr.c.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003441 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003442 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003443 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003444 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003445 break;
3446 case AF_UNIX:
Emeric Brun837ca522010-10-22 16:19:01 +02003447 chunk_printf(&msg,
3448 " src=unix:%d fe=%s be=%s srv=%s",
3449 curr_sess->listener->luid,
3450 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02003451 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau827aee92011-03-10 16:55:02 +01003452 target_srv(&curr_sess->target) ? target_srv(&curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02003453 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003454 break;
3455 }
3456
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003457 chunk_printf(&msg,
Willy Tarreau65671ab2009-10-04 14:24:59 +02003458 " ts=%02x age=%s calls=%d",
3459 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01003460 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
3461 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003462
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003463 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003464 " rq[f=%06xh,l=%d,an=%02xh,rx=%s",
3465 curr_sess->req->flags,
3466 curr_sess->req->l,
3467 curr_sess->req->analysers,
3468 curr_sess->req->rex ?
3469 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
3470 TICKS_TO_MS(1000)) : "");
3471
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003472 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003473 ",wx=%s",
3474 curr_sess->req->wex ?
3475 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
3476 TICKS_TO_MS(1000)) : "");
3477
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003478 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003479 ",ax=%s]",
3480 curr_sess->req->analyse_exp ?
3481 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
3482 TICKS_TO_MS(1000)) : "");
3483
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003484 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003485 " rp[f=%06xh,l=%d,an=%02xh,rx=%s",
3486 curr_sess->rep->flags,
3487 curr_sess->rep->l,
3488 curr_sess->rep->analysers,
3489 curr_sess->rep->rex ?
3490 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
3491 TICKS_TO_MS(1000)) : "");
3492
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003493 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003494 ",wx=%s",
3495 curr_sess->rep->wex ?
3496 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
3497 TICKS_TO_MS(1000)) : "");
3498
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003499 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003500 ",ax=%s]",
3501 curr_sess->rep->analyse_exp ?
3502 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
3503 TICKS_TO_MS(1000)) : "");
3504
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003505 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003506 " s0=[%d,%1xh,fd=%d,ex=%s]",
3507 curr_sess->si[0].state,
3508 curr_sess->si[0].flags,
3509 curr_sess->si[0].fd,
3510 curr_sess->si[0].exp ?
3511 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
3512 TICKS_TO_MS(1000)) : "");
3513
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003514 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003515 " s1=[%d,%1xh,fd=%d,ex=%s]",
3516 curr_sess->si[1].state,
3517 curr_sess->si[1].flags,
3518 curr_sess->si[1].fd,
3519 curr_sess->si[1].exp ?
3520 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
3521 TICKS_TO_MS(1000)) : "");
3522
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003523 chunk_printf(&msg,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003524 " exp=%s",
3525 curr_sess->task->expire ?
3526 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
3527 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01003528 if (task_in_rq(curr_sess->task))
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003529 chunk_printf(&msg, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01003530
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003531 chunk_printf(&msg, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003532
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003533 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01003534 /* let's try again later from this session. We add ourselves into
3535 * this session's users so that it can remove us upon termination.
3536 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003537 LIST_ADDQ(&curr_sess->back_refs, &si->applet.ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003538 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003539 }
3540
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003541 next_sess:
Willy Tarreau295a8372011-03-10 11:25:07 +01003542 si->applet.ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003543 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003544
Willy Tarreau295a8372011-03-10 11:25:07 +01003545 if (si->applet.ctx.sess.target) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003546 /* specified session not found */
Willy Tarreau295a8372011-03-10 11:25:07 +01003547 if (si->applet.ctx.sess.section > 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003548 chunk_printf(&msg, " *** session terminated while we were watching it ***\n");
3549 else
3550 chunk_printf(&msg, "Session not found.\n");
3551
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003552 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003553 return 0;
3554
Willy Tarreau295a8372011-03-10 11:25:07 +01003555 si->applet.ctx.sess.target = NULL;
3556 si->applet.ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01003557 return 1;
3558 }
3559
Willy Tarreau295a8372011-03-10 11:25:07 +01003560 si->applet.state = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003561 /* fall through */
3562
3563 default:
Willy Tarreau295a8372011-03-10 11:25:07 +01003564 si->applet.state = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02003565 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003566 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02003567}
3568
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003569/* This function dumps all tables' states onto the stream intreface's
3570 * read buffer. The data_ctx must have been zeroed first, and the flags
3571 * properly set. It returns 0 if the output buffer is full and it needs
3572 * to be called again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02003573 */
Simon Hormanc88b8872011-06-15 15:18:49 +09003574static int stats_table_request(struct stream_interface *si, bool show)
Willy Tarreau69f58c82010-07-12 17:55:33 +02003575{
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003576 struct session *s = si->applet.private;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003577 struct chunk msg;
3578 struct ebmb_node *eb;
3579 int dt;
Simon Hormanc88b8872011-06-15 15:18:49 +09003580 bool skip_entry;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003581
3582 /*
Willy Tarreau295a8372011-03-10 11:25:07 +01003583 * We have 3 possible states in si->applet.state :
3584 * - STAT_ST_INIT : the first call
3585 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02003586 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003587 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02003588 * and the entry pointer points to the next entry to be dumped,
3589 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01003590 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02003591 * data though.
3592 */
3593
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003594 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02003595 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau295a8372011-03-10 11:25:07 +01003596 if (si->applet.state == STAT_ST_LIST) {
3597 si->applet.ctx.table.entry->ref_cnt--;
3598 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02003599 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02003600 return 1;
3601 }
3602
3603 chunk_init(&msg, trash, sizeof(trash));
3604
Willy Tarreau295a8372011-03-10 11:25:07 +01003605 while (si->applet.state != STAT_ST_FIN) {
3606 switch (si->applet.state) {
3607 case STAT_ST_INIT:
3608 si->applet.ctx.table.proxy = si->applet.ctx.table.target;
3609 if (!si->applet.ctx.table.proxy)
3610 si->applet.ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003611
Willy Tarreau295a8372011-03-10 11:25:07 +01003612 si->applet.ctx.table.entry = NULL;
3613 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003614 break;
3615
Willy Tarreau295a8372011-03-10 11:25:07 +01003616 case STAT_ST_INFO:
3617 if (!si->applet.ctx.table.proxy ||
3618 (si->applet.ctx.table.target &&
3619 si->applet.ctx.table.proxy != si->applet.ctx.table.target)) {
3620 si->applet.state = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003621 break;
3622 }
3623
Willy Tarreau295a8372011-03-10 11:25:07 +01003624 if (si->applet.ctx.table.proxy->table.size) {
Simon Hormanc88b8872011-06-15 15:18:49 +09003625 if (show && !stats_dump_table_head_to_buffer(&msg, si, si->applet.ctx.table.proxy,
3626 si->applet.ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02003627 return 0;
3628
Willy Tarreau295a8372011-03-10 11:25:07 +01003629 if (si->applet.ctx.table.target &&
Willy Tarreau69f58c82010-07-12 17:55:33 +02003630 s->listener->perm.ux.level >= ACCESS_LVL_OPER) {
3631 /* dump entries only if table explicitly requested */
Willy Tarreau295a8372011-03-10 11:25:07 +01003632 eb = ebmb_first(&si->applet.ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003633 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003634 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3635 si->applet.ctx.table.entry->ref_cnt++;
3636 si->applet.state = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003637 break;
3638 }
3639 }
3640 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003641 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003642 break;
3643
Willy Tarreau295a8372011-03-10 11:25:07 +01003644 case STAT_ST_LIST:
Simon Hormanc88b8872011-06-15 15:18:49 +09003645 skip_entry = false;
3646
Willy Tarreau295a8372011-03-10 11:25:07 +01003647 if (si->applet.ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003648 /* we're filtering on some data contents */
3649 void *ptr;
3650 long long data;
3651
Willy Tarreau295a8372011-03-10 11:25:07 +01003652 dt = si->applet.ctx.table.data_type;
3653 ptr = stktable_data_ptr(&si->applet.ctx.table.proxy->table,
3654 si->applet.ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003655 dt);
3656
3657 data = 0;
3658 switch (stktable_data_types[dt].std_type) {
3659 case STD_T_SINT:
3660 data = stktable_data_cast(ptr, std_t_sint);
3661 break;
3662 case STD_T_UINT:
3663 data = stktable_data_cast(ptr, std_t_uint);
3664 break;
3665 case STD_T_ULL:
3666 data = stktable_data_cast(ptr, std_t_ull);
3667 break;
3668 case STD_T_FRQP:
3669 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau295a8372011-03-10 11:25:07 +01003670 si->applet.ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003671 break;
3672 }
3673
3674 /* skip the entry if the data does not match the test and the value */
Willy Tarreau295a8372011-03-10 11:25:07 +01003675 if ((data < si->applet.ctx.table.value &&
3676 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3677 si->applet.ctx.table.data_op == STD_OP_GT ||
3678 si->applet.ctx.table.data_op == STD_OP_GE)) ||
3679 (data == si->applet.ctx.table.value &&
3680 (si->applet.ctx.table.data_op == STD_OP_NE ||
3681 si->applet.ctx.table.data_op == STD_OP_GT ||
3682 si->applet.ctx.table.data_op == STD_OP_LT)) ||
3683 (data > si->applet.ctx.table.value &&
3684 (si->applet.ctx.table.data_op == STD_OP_EQ ||
3685 si->applet.ctx.table.data_op == STD_OP_LT ||
3686 si->applet.ctx.table.data_op == STD_OP_LE)))
Simon Hormanc88b8872011-06-15 15:18:49 +09003687 skip_entry = true;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02003688 }
3689
Simon Hormanc88b8872011-06-15 15:18:49 +09003690 if (show && !skip_entry &&
3691 !stats_dump_table_entry_to_buffer(&msg, si, si->applet.ctx.table.proxy,
Simon Hormand9366582011-06-15 15:18:45 +09003692 si->applet.ctx.table.entry))
3693 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003694
Willy Tarreau295a8372011-03-10 11:25:07 +01003695 si->applet.ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003696
Willy Tarreau295a8372011-03-10 11:25:07 +01003697 eb = ebmb_next(&si->applet.ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02003698 if (eb) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003699 struct stksess *old = si->applet.ctx.table.entry;
3700 si->applet.ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3701 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, old);
3702 si->applet.ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003703 break;
3704 }
3705
Simon Hormanc88b8872011-06-15 15:18:49 +09003706
3707 if (show)
3708 stksess_kill_if_expired(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3709 else if (!skip_entry && !si->applet.ctx.table.entry->ref_cnt)
3710 stksess_kill(&si->applet.ctx.table.proxy->table, si->applet.ctx.table.entry);
3711
Willy Tarreau295a8372011-03-10 11:25:07 +01003712 si->applet.ctx.table.proxy = si->applet.ctx.table.proxy->next;
3713 si->applet.state = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003714 break;
3715
Willy Tarreau295a8372011-03-10 11:25:07 +01003716 case STAT_ST_END:
3717 si->applet.state = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02003718 break;
3719 }
3720 }
3721 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003722}
3723
Willy Tarreaud426a182010-03-05 14:58:26 +01003724/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01003725 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3726 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3727 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3728 * lines are respected within the limit of 70 output chars. Lines that are
3729 * continuation of a previous truncated line begin with "+" instead of " "
3730 * after the offset. The new pointer is returned.
3731 */
Willy Tarreaud426a182010-03-05 14:58:26 +01003732static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3733 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003734{
3735 int end;
3736 unsigned char c;
3737
3738 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003739 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003740 return ptr;
3741
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003742 chunk_printf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01003743
Willy Tarreaud426a182010-03-05 14:58:26 +01003744 while (ptr < len && ptr < bsize) {
3745 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01003746 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003747 if (out->len > end - 2)
3748 break;
3749 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003750 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003751 if (out->len > end - 3)
3752 break;
3753 out->str[out->len++] = '\\';
3754 switch (c) {
3755 case '\t': c = 't'; break;
3756 case '\n': c = 'n'; break;
3757 case '\r': c = 'r'; break;
3758 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01003759 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003760 }
3761 out->str[out->len++] = c;
3762 } else {
3763 if (out->len > end - 5)
3764 break;
3765 out->str[out->len++] = '\\';
3766 out->str[out->len++] = 'x';
3767 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3768 out->str[out->len++] = hextab[c & 0xF];
3769 }
Willy Tarreaud426a182010-03-05 14:58:26 +01003770 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003771 /* we had a line break, let's return now */
3772 out->str[out->len++] = '\n';
3773 *line = ptr;
3774 return ptr;
3775 }
3776 }
3777 /* we have an incomplete line, we return it as-is */
3778 out->str[out->len++] = '\n';
3779 return ptr;
3780}
3781
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003782/* This function dumps all captured errors onto the stream intreface's
3783 * read buffer. The data_ctx must have been zeroed first, and the flags
3784 * properly set. It returns 0 if the output buffer is full and it needs
3785 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01003786 */
Simon Horman9bd2c732011-06-15 15:18:44 +09003787static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003788{
3789 extern const char *monthname[12];
3790 struct chunk msg;
3791
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003792 if (unlikely(si->ib->flags & (BF_WRITE_ERROR|BF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02003793 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003794
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003795 chunk_init(&msg, trash, sizeof(trash));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003796
Willy Tarreau295a8372011-03-10 11:25:07 +01003797 if (!si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003798 /* the function had not been called yet, let's prepare the
3799 * buffer for a response.
3800 */
Willy Tarreau10479e42010-12-12 14:00:34 +01003801 struct tm tm;
3802
3803 get_localtime(date.tv_sec, &tm);
3804 chunk_printf(&msg, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3805 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3806 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3807 error_snapshot_id);
3808
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003809 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau10479e42010-12-12 14:00:34 +01003810 /* Socket buffer full. Let's try again later from the same point */
3811 return 0;
3812 }
3813
Willy Tarreau295a8372011-03-10 11:25:07 +01003814 si->applet.ctx.errors.px = proxy;
3815 si->applet.ctx.errors.buf = 0;
3816 si->applet.ctx.errors.bol = 0;
3817 si->applet.ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003818 }
3819
3820 /* we have two inner loops here, one for the proxy, the other one for
3821 * the buffer.
3822 */
Willy Tarreau295a8372011-03-10 11:25:07 +01003823 while (si->applet.ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003824 struct error_snapshot *es;
3825
Willy Tarreau295a8372011-03-10 11:25:07 +01003826 if (si->applet.ctx.errors.buf == 0)
3827 es = &si->applet.ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003828 else
Willy Tarreau295a8372011-03-10 11:25:07 +01003829 es = &si->applet.ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003830
3831 if (!es->when.tv_sec)
3832 goto next;
3833
Willy Tarreau295a8372011-03-10 11:25:07 +01003834 if (si->applet.ctx.errors.iid >= 0 &&
3835 si->applet.ctx.errors.px->uuid != si->applet.ctx.errors.iid &&
3836 es->oe->uuid != si->applet.ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01003837 goto next;
3838
Willy Tarreau295a8372011-03-10 11:25:07 +01003839 if (si->applet.ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003840 /* just print headers now */
3841
3842 char pn[INET6_ADDRSTRLEN];
3843 struct tm tm;
3844
3845 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003846 chunk_printf(&msg, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01003847 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02003848 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01003849
Willy Tarreau631f01c2011-09-05 00:36:48 +02003850 addr_to_str(&es->src, pn, sizeof(pn));
Willy Tarreau295a8372011-03-10 11:25:07 +01003851 switch (si->applet.ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003852 case 0:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003853 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003854 " frontend %s (#%d): invalid request\n"
3855 " src %s, session #%d, backend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003856 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003857 " request length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003858 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau3fd6cec2010-03-25 06:45:07 +01003859 pn, es->sid, (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3860 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003861 es->srv ? es->srv->id : "<NONE>",
3862 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003863 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003864 es->len, es->pos);
3865 break;
3866 case 1:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003867 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003868 " backend %s (#%d) : invalid response\n"
3869 " src %s, session #%d, frontend %s (#%d), server %s (#%d)\n"
Willy Tarreau10479e42010-12-12 14:00:34 +01003870 " HTTP internal state %d, buffer flags 0x%08x, event #%u\n"
Willy Tarreau8811f8e2010-03-05 17:42:58 +01003871 " response length %d bytes, error at position %d:\n \n",
Willy Tarreau295a8372011-03-10 11:25:07 +01003872 si->applet.ctx.errors.px->id, si->applet.ctx.errors.px->uuid,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003873 pn, es->sid, es->oe->id, es->oe->uuid,
3874 es->srv ? es->srv->id : "<NONE>",
3875 es->srv ? es->srv->puid : -1,
Willy Tarreau10479e42010-12-12 14:00:34 +01003876 es->state, es->flags, es->ev_id,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003877 es->len, es->pos);
3878 break;
3879 }
3880
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003881 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003882 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003883 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003884 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003885 si->applet.ctx.errors.ptr = 0;
3886 si->applet.ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003887 }
3888
Willy Tarreau295a8372011-03-10 11:25:07 +01003889 if (si->applet.ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003890 /* the snapshot changed while we were dumping it */
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003891 chunk_printf(&msg,
Willy Tarreau74808cb2009-03-04 15:53:18 +01003892 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003893 if (buffer_feed_chunk(si->ib, &msg) >= 0)
Willy Tarreau61b34732009-10-03 23:49:35 +02003894 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003895 goto next;
3896 }
3897
3898 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau295a8372011-03-10 11:25:07 +01003899 while (si->applet.ctx.errors.ptr < es->len && si->applet.ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003900 int newptr;
3901 int newline;
3902
Willy Tarreau295a8372011-03-10 11:25:07 +01003903 newline = si->applet.ctx.errors.bol;
3904 newptr = dump_text_line(&msg, es->buf, sizeof(es->buf), es->len, &newline, si->applet.ctx.errors.ptr);
3905 if (newptr == si->applet.ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02003906 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003907
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01003908 if (buffer_feed_chunk(si->ib, &msg) >= 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01003909 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02003910 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003911 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003912 si->applet.ctx.errors.ptr = newptr;
3913 si->applet.ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003914 };
3915 next:
Willy Tarreau295a8372011-03-10 11:25:07 +01003916 si->applet.ctx.errors.bol = 0;
3917 si->applet.ctx.errors.ptr = -1;
3918 si->applet.ctx.errors.buf++;
3919 if (si->applet.ctx.errors.buf > 1) {
3920 si->applet.ctx.errors.buf = 0;
3921 si->applet.ctx.errors.px = si->applet.ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003922 }
3923 }
3924
3925 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02003926 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01003927}
3928
Willy Tarreaub24281b2011-02-13 13:16:36 +01003929struct si_applet http_stats_applet = {
3930 .name = "<STATS>", /* used for logging */
3931 .fct = http_stats_io_handler,
3932};
3933
Simon Horman9bd2c732011-06-15 15:18:44 +09003934static struct si_applet cli_applet = {
Willy Tarreaub24281b2011-02-13 13:16:36 +01003935 .name = "<CLI>", /* used for logging */
3936 .fct = cli_io_handler,
3937};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01003938
Willy Tarreau10522fd2008-07-09 20:12:41 +02003939static struct cfg_kw_list cfg_kws = {{ },{
3940 { CFG_GLOBAL, "stats", stats_parse_global },
3941 { 0, NULL, NULL },
3942}};
3943
3944__attribute__((constructor))
3945static void __dumpstats_module_init(void)
3946{
3947 cfg_register_keywords(&cfg_kws);
3948}
3949
Willy Tarreau91861262007-10-17 17:06:05 +02003950/*
3951 * Local variables:
3952 * c-indent-level: 8
3953 * c-basic-offset: 8
3954 * End:
3955 */